remove API calls for nodes and devices
This commit is contained in:
		@@ -11,7 +11,7 @@ __PACKAGE__->load_namespaces(
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
our # try to hide from kwalitee
 | 
			
		||||
  $VERSION = 54; # schema version used for upgrades, keep as integer
 | 
			
		||||
  $VERSION = 55; # schema version used for upgrades, keep as integer
 | 
			
		||||
 | 
			
		||||
use Path::Class;
 | 
			
		||||
use File::ShareDir 'dist_dir';
 | 
			
		||||
 
 | 
			
		||||
@@ -1,104 +0,0 @@
 | 
			
		||||
package App::Netdisco::Web::Plugin::API::Device;
 | 
			
		||||
 | 
			
		||||
use Dancer ':syntax';
 | 
			
		||||
 | 
			
		||||
use Dancer::Plugin::Ajax;
 | 
			
		||||
use Dancer::Plugin::Auth::Extensible;
 | 
			
		||||
use Dancer::Plugin::DBIC;
 | 
			
		||||
 | 
			
		||||
use Dancer::Exception qw(:all);
 | 
			
		||||
 | 
			
		||||
use App::Netdisco::Web::Plugin;
 | 
			
		||||
 | 
			
		||||
use App::Netdisco::Web::Plugin::API::Util;
 | 
			
		||||
 | 
			
		||||
get '/api/device/all' => sub {
 | 
			
		||||
    my $devices=schema('netdisco')->resultset('Device')->all;
 | 
			
		||||
    return format_data($devices);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
post '/api/device/discover' => sub {
 | 
			
		||||
    my $devices = from_json( request->body );
 | 
			
		||||
    ## NOT IMPLEMENTED YET
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
get '/api/device/searchports' => sub {
 | 
			
		||||
    my $para = params;
 | 
			
		||||
    my $search = {};
 | 
			
		||||
    $search = parse_search_params($para);
 | 
			
		||||
    my $devices;
 | 
			
		||||
    try {
 | 
			
		||||
       $devices=schema('netdisco')->resultset('DevicePort')->search($search);
 | 
			
		||||
    };
 | 
			
		||||
    return format_data($devices);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
get '/api/device/search' => sub {
 | 
			
		||||
    my $para = params;
 | 
			
		||||
    my $search = parse_search_params($para);
 | 
			
		||||
    my $devices;
 | 
			
		||||
    try {
 | 
			
		||||
       $devices=schema('netdisco')->resultset('Device')->search($search);
 | 
			
		||||
    };
 | 
			
		||||
    return format_data($devices);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
get '/api/device/:device' => sub {
 | 
			
		||||
    my $dev = params->{device};
 | 
			
		||||
    my $device = schema('netdisco')->resultset('Device')
 | 
			
		||||
      ->search_for_device($dev) or send_error('Bad Device', 404);
 | 
			
		||||
    return format_data($device);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
get '/api/device/:device/:method' => sub {
 | 
			
		||||
    my $dev = params->{device};
 | 
			
		||||
    my $method = params->{method};
 | 
			
		||||
    if (! ($method =~ m/[-_a-z]/)) {
 | 
			
		||||
        return format_error(400,"Invalid collection $method.");
 | 
			
		||||
    }
 | 
			
		||||
    try {
 | 
			
		||||
        my $device = schema('netdisco')->resultset('Device')->search_for_device($dev);
 | 
			
		||||
        my $results = $device->$method;
 | 
			
		||||
        return {} if not defined $results;
 | 
			
		||||
        return format_data($results);
 | 
			
		||||
    } catch {
 | 
			
		||||
        my ($exception) = @_;
 | 
			
		||||
        if ($exception =~ m/Can\'t call method "$method" on an undefined value/) {
 | 
			
		||||
            return format_error(404,"Device not found.");
 | 
			
		||||
        }
 | 
			
		||||
        return format_error(400,"Invalid collection $method.");
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
get qr{/api/device/(?<ip>.*)/port/(?<port>.*)/(?<method>[-_a-z]+)$} => sub {
 | 
			
		||||
    my $param =captures;
 | 
			
		||||
    my $method = $$param{method};
 | 
			
		||||
    try {
 | 
			
		||||
        my $port = schema('netdisco')->resultset('DevicePort')->find({ip=>$$param{ip}, port => $$param{port}});
 | 
			
		||||
 | 
			
		||||
        my $results = $port->$method;
 | 
			
		||||
        return {} if not defined $results;
 | 
			
		||||
        return format_data($results);
 | 
			
		||||
    } catch {
 | 
			
		||||
        my ($exception) = @_;
 | 
			
		||||
        if ($exception =~ m/Can\'t call method "$method" on an undefined value/) {
 | 
			
		||||
            return format_error(404,"Port not found.");
 | 
			
		||||
        }
 | 
			
		||||
        return format_error(400, "Invalid collection $method.");
 | 
			
		||||
    };
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
get qr{/api/device/(?<ip>.*)/port/(?<port>.*)} => sub {
 | 
			
		||||
    my $param =captures;
 | 
			
		||||
    my $port;
 | 
			
		||||
    try {
 | 
			
		||||
        $port = schema('netdisco')->resultset('DevicePort')->find({ip=>$$param{ip}, port => $$param{port}});
 | 
			
		||||
        return format_error(404, "Port not found.") if not defined $port;
 | 
			
		||||
        return format_data($port);
 | 
			
		||||
    } catch {
 | 
			
		||||
        return format_error(404, "Port not found.");
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
true;
 | 
			
		||||
@@ -1,76 +0,0 @@
 | 
			
		||||
package App::Netdisco::Web::Plugin::API::Node;
 | 
			
		||||
 | 
			
		||||
use Dancer ':syntax';
 | 
			
		||||
 | 
			
		||||
use Dancer::Plugin::Ajax;
 | 
			
		||||
use Dancer::Plugin::Auth::Extensible;
 | 
			
		||||
use Dancer::Plugin::DBIC;
 | 
			
		||||
 | 
			
		||||
use App::Netdisco::Web::Plugin;
 | 
			
		||||
 | 
			
		||||
sub api_array_json {
 | 
			
		||||
    my $items = shift;
 | 
			
		||||
    my @results;
 | 
			
		||||
    foreach my $item (@{$items}) {
 | 
			
		||||
        my $c = {};
 | 
			
		||||
        my $columns = $item->{_column_data};
 | 
			
		||||
        foreach my $col (keys %{$columns}) {
 | 
			
		||||
            $c->{$col} = $columns->{$col};
 | 
			
		||||
        }
 | 
			
		||||
        push @results, $c;
 | 
			
		||||
    }
 | 
			
		||||
    return (\@results);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
get '/api/node/search' => require_login sub {
 | 
			
		||||
    my $para = params;
 | 
			
		||||
    my $search = {};
 | 
			
		||||
    # Generate a hashref of search terms. 
 | 
			
		||||
    foreach my $param (keys %{$para}) {
 | 
			
		||||
        if ($param ne 'return_url') {
 | 
			
		||||
            $search->{$param} = $para->{$param};
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    my @ips;
 | 
			
		||||
    try {
 | 
			
		||||
       @ips = schema('netdisco')->resultset('Node')->search($search);
 | 
			
		||||
    };
 | 
			
		||||
    return to_json api_array_json(\@ips);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
get '/api/node/:node/:method' => require_login sub {
 | 
			
		||||
    my $node = params->{node};
 | 
			
		||||
    my $method = params->{method};
 | 
			
		||||
    # Make sure $node is actually a mac address in the proper format.
 | 
			
		||||
    # TODO change to NetAddr::MAC
 | 
			
		||||
    if (!($node =~ m/([0-9a-f]{2}:){5}([0-9a-f]{2})/)){
 | 
			
		||||
        status 400;
 | 
			
		||||
        return to_json { error => "Not a MAC Address. Address must follow the format aa:bb:cc:dd:ee:ff." };
 | 
			
		||||
    }
 | 
			
		||||
    try {
 | 
			
		||||
        my @nodesearch = schema('netdisco')->resultset('Node')->search({ mac => $node});
 | 
			
		||||
        # Searching by mac there should be only one result
 | 
			
		||||
        my $node = $nodesearch[0]->$method;
 | 
			
		||||
 | 
			
		||||
        # ResultSets need to be converted to an array of hashes before being returned.
 | 
			
		||||
        # Dancer's JSON serializer doesn't like the objects
 | 
			
		||||
        if (ref($node) =~ m/ResultSet/) {
 | 
			
		||||
            my @nodes = $node->all;
 | 
			
		||||
            return to_json api_array_json(\@nodes);
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            my $nodes = $node;
 | 
			
		||||
            return to_json $nodes->{_column_data};
 | 
			
		||||
        }
 | 
			
		||||
    } catch {
 | 
			
		||||
        my ($exception) = @_;
 | 
			
		||||
        if ($exception =~ m/Can\'t call method "$method" on an undefined value/) {
 | 
			
		||||
            status 404;
 | 
			
		||||
            return to_json { error => "MAC Address not found."};
 | 
			
		||||
        }
 | 
			
		||||
        status 400;
 | 
			
		||||
        return to_json { error => "Invalid collection $method." };
 | 
			
		||||
    };
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
true;
 | 
			
		||||
@@ -1,86 +0,0 @@
 | 
			
		||||
package App::Netdisco::Web::Plugin::API::NodeIP;
 | 
			
		||||
 | 
			
		||||
use Dancer ':syntax';
 | 
			
		||||
use Dancer::Plugin::DBIC 'schema';
 | 
			
		||||
use Dancer::Plugin::Auth::Extensible;
 | 
			
		||||
use Dancer::Plugin::Swagger;
 | 
			
		||||
 | 
			
		||||
use App::Netdisco::Web::Plugin;
 | 
			
		||||
use App::Netdisco::Util::API ':all';
 | 
			
		||||
 | 
			
		||||
use NetAddr::IP::Lite;
 | 
			
		||||
 | 
			
		||||
swagger_path {
 | 
			
		||||
  description => 'Search for a Node to IP mapping (v4 ARP or v6 Neighbor entry)',
 | 
			
		||||
  tags => ['NodeIPs'],
 | 
			
		||||
  parameters => [
 | 
			
		||||
    resultsource_to_openapi_params('NodeIp'),
 | 
			
		||||
    partial => {
 | 
			
		||||
      type => 'boolean',
 | 
			
		||||
      description => 'Parameters can match anywhere in the value, ignoring case',
 | 
			
		||||
    },
 | 
			
		||||
  ],
 | 
			
		||||
  responses => {
 | 
			
		||||
    default => { description => 'A row from the node_ip table' },
 | 
			
		||||
  },
 | 
			
		||||
},
 | 
			
		||||
get '/api/nodeip/search' => require_role api => sub {
 | 
			
		||||
    my $para = params;
 | 
			
		||||
    my $search = parse_search_params($para);
 | 
			
		||||
    my $ips;
 | 
			
		||||
    try {
 | 
			
		||||
       $ips = schema('netdisco')->resultset('NodeIp')->search($search);
 | 
			
		||||
    };
 | 
			
		||||
    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)){
 | 
			
		||||
        try {
 | 
			
		||||
            my $node = schema('netdisco')->resultset('NodeIp')->find({ ip => $node});
 | 
			
		||||
            return format_error(404, "IP address not found.")
 | 
			
		||||
                if not defined $node->{_column_data};
 | 
			
		||||
            return format_data($node);
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        format_error(400,"Not an IP address.");
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
# 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};
 | 
			
		||||
 | 
			
		||||
    if (! ($method =~ m/[-_a-z]/)) {
 | 
			
		||||
        return format_error(400,"Invalid collection $method.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (defined NetAddr::IP::Lite->new($node)){
 | 
			
		||||
        try {
 | 
			
		||||
            my $node = schema('netdisco')->resultset('NodeIp')->find({ ip => $node});
 | 
			
		||||
 | 
			
		||||
            return format_error(404, "IP address not found.")
 | 
			
		||||
                if not defined $node->{_column_data};
 | 
			
		||||
 | 
			
		||||
            my $data = $node->$method;
 | 
			
		||||
 | 
			
		||||
            return format_data($data);
 | 
			
		||||
        } catch {
 | 
			
		||||
            my ($exception) = @_;
 | 
			
		||||
            if ($exception =~ m/Can\'t call method "$method" on an undefined value/) {
 | 
			
		||||
                return format_error(404, "IP address not found.")
 | 
			
		||||
            }
 | 
			
		||||
            format_error(400,"Invalid collection $method.");
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        format_error(400,"Not an IP address.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
@@ -41,9 +41,6 @@ api_token_lifetime: 3600
 | 
			
		||||
path: '/'
 | 
			
		||||
web_plugins:
 | 
			
		||||
  - Inventory
 | 
			
		||||
#  - API::Node
 | 
			
		||||
  - API::NodeIP
 | 
			
		||||
#  - API::Device
 | 
			
		||||
  - Report::PortVLANMismatch
 | 
			
		||||
  - Report::PortAdminDown
 | 
			
		||||
  - Report::PortBlocking
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user