allow api params to be generated from DBIC schema spec
This commit is contained in:
@@ -13,19 +13,24 @@ use base 'DBIx::Class::Core';
|
|||||||
__PACKAGE__->table("node_ip");
|
__PACKAGE__->table("node_ip");
|
||||||
__PACKAGE__->add_columns(
|
__PACKAGE__->add_columns(
|
||||||
"mac",
|
"mac",
|
||||||
{ data_type => "macaddr", is_nullable => 0 },
|
{ data_type => "macaddr", is_nullable => 0,
|
||||||
|
extra => { descr => 'MAC address' } },
|
||||||
"ip",
|
"ip",
|
||||||
{ data_type => "inet", is_nullable => 0 },
|
{ data_type => "inet", is_nullable => 0,
|
||||||
|
extra => { descr => 'IP address' } },
|
||||||
"dns",
|
"dns",
|
||||||
{ data_type => "text", is_nullable => 1 },
|
{ data_type => "text", is_nullable => 1,
|
||||||
|
extra => { descr => 'FQDN of the node' } },
|
||||||
"active",
|
"active",
|
||||||
{ data_type => "boolean", is_nullable => 1 },
|
{ data_type => "boolean", is_nullable => 1,
|
||||||
|
extra => { descr => 'Whether the entry is still "fresh"' } },
|
||||||
"time_first",
|
"time_first",
|
||||||
{
|
{
|
||||||
data_type => "timestamp",
|
data_type => "timestamp",
|
||||||
default_value => \"current_timestamp",
|
default_value => \"current_timestamp",
|
||||||
is_nullable => 1,
|
is_nullable => 1,
|
||||||
original => { default_value => \"now()" },
|
original => { default_value => \"now()" },
|
||||||
|
extra => { hide_from_api => 1 },
|
||||||
},
|
},
|
||||||
"time_last",
|
"time_last",
|
||||||
{
|
{
|
||||||
@@ -33,6 +38,7 @@ __PACKAGE__->add_columns(
|
|||||||
default_value => \"current_timestamp",
|
default_value => \"current_timestamp",
|
||||||
is_nullable => 1,
|
is_nullable => 1,
|
||||||
original => { default_value => \"now()" },
|
original => { default_value => \"now()" },
|
||||||
|
extra => { hide_from_api => 1 },
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
__PACKAGE__->set_primary_key("mac", "ip");
|
__PACKAGE__->set_primary_key("mac", "ip");
|
||||||
|
|||||||
@@ -1,13 +1,44 @@
|
|||||||
package App::Netdisco::Util::API;
|
package App::Netdisco::Util::API;
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
use Dancer ':syntax';
|
use Dancer ':syntax';
|
||||||
|
use Dancer::Plugin::DBIC 'schema';
|
||||||
|
|
||||||
|
use List::MoreUtils 'singleton';
|
||||||
|
|
||||||
use base 'Exporter';
|
use base 'Exporter';
|
||||||
our @EXPORT = qw/
|
our @EXPORT = ();
|
||||||
|
our @EXPORT_OK = qw/
|
||||||
|
resultsource_to_openapi_params
|
||||||
parse_search_params
|
parse_search_params
|
||||||
format_data
|
format_data
|
||||||
format_error
|
format_error
|
||||||
/;
|
/;
|
||||||
|
our %EXPORT_TAGS = (all => \@EXPORT_OK);
|
||||||
|
|
||||||
|
sub resultsource_to_openapi_params {
|
||||||
|
my $sourcename = shift or return ();
|
||||||
|
my @params = ();
|
||||||
|
|
||||||
|
my $rs = schema('netdisco')->source($sourcename) or return ();
|
||||||
|
my $columns = $rs->columns_info;
|
||||||
|
|
||||||
|
foreach my $col ($rs->primary_columns,
|
||||||
|
(singleton ($rs->primary_columns, keys %{ $columns }))) {
|
||||||
|
my $data = $columns->{$col};
|
||||||
|
next if $data->{extra}->{hide_from_api};
|
||||||
|
push @params, (
|
||||||
|
$col => {
|
||||||
|
description => $data->{extra}->{descr},
|
||||||
|
type => ($data->{data_type} =~ m/int/ ? 'integer'
|
||||||
|
: $data->{data_type} eq 'boolean' ? 'boolean' : 'string'),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return @params;
|
||||||
|
}
|
||||||
|
|
||||||
sub parse_search_params {
|
sub parse_search_params {
|
||||||
my $params = shift;
|
my $params = shift;
|
||||||
|
|||||||
@@ -3,27 +3,21 @@ package App::Netdisco::Web::Plugin::API::NodeIP;
|
|||||||
use Dancer ':syntax';
|
use Dancer ':syntax';
|
||||||
use Dancer::Plugin::DBIC 'schema';
|
use Dancer::Plugin::DBIC 'schema';
|
||||||
use Dancer::Plugin::Auth::Extensible;
|
use Dancer::Plugin::Auth::Extensible;
|
||||||
use Dancer::Exception qw(:all);
|
|
||||||
use Dancer::Plugin::Swagger;
|
use Dancer::Plugin::Swagger;
|
||||||
|
|
||||||
use App::Netdisco::Web::Plugin;
|
use App::Netdisco::Web::Plugin;
|
||||||
use App::Netdisco::Util::API;
|
use App::Netdisco::Util::API ':all';
|
||||||
|
|
||||||
use NetAddr::IP::Lite;
|
use NetAddr::IP::Lite;
|
||||||
|
|
||||||
swagger_path {
|
swagger_path {
|
||||||
description => 'Search for a Node to IP mapping (ARP entry)',
|
description => 'Search for a Node to IP mapping (v4 ARP or v6 Neighbor entry)',
|
||||||
tags => ['NodeIPs'],
|
tags => ['NodeIPs'],
|
||||||
parameters => [
|
parameters => [
|
||||||
mac => 'MAC address',
|
resultsource_to_openapi_params('NodeIp'),
|
||||||
ip => 'IP address',
|
|
||||||
dns => 'FQDN of the node',
|
|
||||||
active => { type => 'boolean', description => 'Whether the entry is still fresh', },
|
|
||||||
time_first => 'When first seen',
|
|
||||||
time_last => 'When last seen',
|
|
||||||
partial => {
|
partial => {
|
||||||
type => 'boolean',
|
type => 'boolean',
|
||||||
description => 'All parameters will be searched for case insensitively in their values',
|
description => 'Parameters can match anywhere in the value, ignoring case',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
responses => {
|
responses => {
|
||||||
@@ -40,6 +34,8 @@ get '/api/nodeip/search' => require_role api => sub {
|
|||||||
return format_data($ips);
|
return format_data($ips);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# FIXME does not work as you can have more than one IP entry in that table
|
||||||
|
# will issue a warning from DBIC
|
||||||
get '/api/nodeip/:node' => sub {
|
get '/api/nodeip/:node' => sub {
|
||||||
my $node = params->{node};
|
my $node = params->{node};
|
||||||
if (defined NetAddr::IP::Lite->new($node)){
|
if (defined NetAddr::IP::Lite->new($node)){
|
||||||
@@ -55,6 +51,8 @@ get '/api/nodeip/:node' => sub {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# FIXME does not work as you can have more than one IP entry in that table
|
||||||
|
# will issue a warning from DBIC
|
||||||
get '/api/nodeip/:node/:method' => sub {
|
get '/api/nodeip/:node/:method' => sub {
|
||||||
my $node = params->{node};
|
my $node = params->{node};
|
||||||
my $method = params->{method};
|
my $method = params->{method};
|
||||||
|
|||||||
Reference in New Issue
Block a user