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__->add_columns(
 | 
			
		||||
  "mac",
 | 
			
		||||
  { data_type => "macaddr", is_nullable => 0 },
 | 
			
		||||
  { data_type => "macaddr", is_nullable => 0,
 | 
			
		||||
    extra => { descr => 'MAC address' } },
 | 
			
		||||
  "ip",
 | 
			
		||||
  { data_type => "inet", is_nullable => 0 },
 | 
			
		||||
  { data_type => "inet", is_nullable => 0,
 | 
			
		||||
    extra => { descr => 'IP address' } },
 | 
			
		||||
  "dns",
 | 
			
		||||
  { data_type => "text", is_nullable => 1 },
 | 
			
		||||
  { data_type => "text", is_nullable => 1,
 | 
			
		||||
    extra => { descr => 'FQDN of the node' } },
 | 
			
		||||
  "active",
 | 
			
		||||
  { data_type => "boolean", is_nullable => 1 },
 | 
			
		||||
  { data_type => "boolean", is_nullable => 1,
 | 
			
		||||
    extra => { descr => 'Whether the entry is still "fresh"' } },
 | 
			
		||||
  "time_first",
 | 
			
		||||
  {
 | 
			
		||||
    data_type     => "timestamp",
 | 
			
		||||
    default_value => \"current_timestamp",
 | 
			
		||||
    is_nullable   => 1,
 | 
			
		||||
    original      => { default_value => \"now()" },
 | 
			
		||||
    extra => { hide_from_api => 1 },
 | 
			
		||||
  },
 | 
			
		||||
  "time_last",
 | 
			
		||||
  {
 | 
			
		||||
@@ -33,6 +38,7 @@ __PACKAGE__->add_columns(
 | 
			
		||||
    default_value => \"current_timestamp",
 | 
			
		||||
    is_nullable   => 1,
 | 
			
		||||
    original      => { default_value => \"now()" },
 | 
			
		||||
    extra => { hide_from_api => 1 },
 | 
			
		||||
  },
 | 
			
		||||
);
 | 
			
		||||
__PACKAGE__->set_primary_key("mac", "ip");
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,44 @@
 | 
			
		||||
package App::Netdisco::Util::API;
 | 
			
		||||
 | 
			
		||||
use strict;
 | 
			
		||||
use warnings;
 | 
			
		||||
use Dancer ':syntax';
 | 
			
		||||
use Dancer::Plugin::DBIC 'schema';
 | 
			
		||||
 | 
			
		||||
use List::MoreUtils 'singleton';
 | 
			
		||||
 | 
			
		||||
use base 'Exporter';
 | 
			
		||||
our @EXPORT = qw/
 | 
			
		||||
our @EXPORT = ();
 | 
			
		||||
our @EXPORT_OK = qw/
 | 
			
		||||
  resultsource_to_openapi_params
 | 
			
		||||
  parse_search_params
 | 
			
		||||
  format_data
 | 
			
		||||
  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 {
 | 
			
		||||
    my $params = shift;
 | 
			
		||||
 
 | 
			
		||||
@@ -3,27 +3,21 @@ package App::Netdisco::Web::Plugin::API::NodeIP;
 | 
			
		||||
use Dancer ':syntax';
 | 
			
		||||
use Dancer::Plugin::DBIC 'schema';
 | 
			
		||||
use Dancer::Plugin::Auth::Extensible;
 | 
			
		||||
use Dancer::Exception qw(:all);
 | 
			
		||||
use Dancer::Plugin::Swagger;
 | 
			
		||||
 | 
			
		||||
use App::Netdisco::Web::Plugin;
 | 
			
		||||
use App::Netdisco::Util::API;
 | 
			
		||||
use App::Netdisco::Util::API ':all';
 | 
			
		||||
 | 
			
		||||
use NetAddr::IP::Lite;
 | 
			
		||||
 | 
			
		||||
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'],
 | 
			
		||||
  parameters => [
 | 
			
		||||
    mac => 'MAC address',
 | 
			
		||||
    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',
 | 
			
		||||
    resultsource_to_openapi_params('NodeIp'),
 | 
			
		||||
    partial => {
 | 
			
		||||
      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 => {
 | 
			
		||||
@@ -40,6 +34,8 @@ get '/api/nodeip/search' => require_role api => sub {
 | 
			
		||||
    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 {
 | 
			
		||||
    my $node = params->{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 {
 | 
			
		||||
    my $node = params->{node};
 | 
			
		||||
    my $method = params->{method};
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user