From 5b1021b3b5cba5db237abfc53b6e4699f4886fc2 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 11 Mar 2019 17:48:09 +0000 Subject: [PATCH] remove API calls for nodes and devices --- lib/App/Netdisco/DB.pm | 2 +- lib/App/Netdisco/Web/Plugin/API/Device.pm | 104 ------------------ lib/App/Netdisco/Web/Plugin/API/Node.pm | 76 ------------- lib/App/Netdisco/Web/Plugin/API/NodeIP.pm | 86 --------------- share/config.yml | 3 - ...l => App-Netdisco-DB-54-55-PostgreSQL.sql} | 0 6 files changed, 1 insertion(+), 270 deletions(-) delete mode 100644 lib/App/Netdisco/Web/Plugin/API/Device.pm delete mode 100644 lib/App/Netdisco/Web/Plugin/API/Node.pm delete mode 100644 lib/App/Netdisco/Web/Plugin/API/NodeIP.pm rename share/schema_versions/{App-Netdisco-DB-53-54-PostgreSQL.sql => App-Netdisco-DB-54-55-PostgreSQL.sql} (100%) diff --git a/lib/App/Netdisco/DB.pm b/lib/App/Netdisco/DB.pm index 5c589386..e5dad2c9 100644 --- a/lib/App/Netdisco/DB.pm +++ b/lib/App/Netdisco/DB.pm @@ -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'; diff --git a/lib/App/Netdisco/Web/Plugin/API/Device.pm b/lib/App/Netdisco/Web/Plugin/API/Device.pm deleted file mode 100644 index a270910d..00000000 --- a/lib/App/Netdisco/Web/Plugin/API/Device.pm +++ /dev/null @@ -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/(?.*)/port/(?.*)/(?[-_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/(?.*)/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; diff --git a/lib/App/Netdisco/Web/Plugin/API/Node.pm b/lib/App/Netdisco/Web/Plugin/API/Node.pm deleted file mode 100644 index 26aa0946..00000000 --- a/lib/App/Netdisco/Web/Plugin/API/Node.pm +++ /dev/null @@ -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; diff --git a/lib/App/Netdisco/Web/Plugin/API/NodeIP.pm b/lib/App/Netdisco/Web/Plugin/API/NodeIP.pm deleted file mode 100644 index 26ae2168..00000000 --- a/lib/App/Netdisco/Web/Plugin/API/NodeIP.pm +++ /dev/null @@ -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."); - } - -}; diff --git a/share/config.yml b/share/config.yml index fb3c2ab2..5996d436 100644 --- a/share/config.yml +++ b/share/config.yml @@ -41,9 +41,6 @@ api_token_lifetime: 3600 path: '/' web_plugins: - Inventory -# - API::Node - - API::NodeIP -# - API::Device - Report::PortVLANMismatch - Report::PortAdminDown - Report::PortBlocking diff --git a/share/schema_versions/App-Netdisco-DB-53-54-PostgreSQL.sql b/share/schema_versions/App-Netdisco-DB-54-55-PostgreSQL.sql similarity index 100% rename from share/schema_versions/App-Netdisco-DB-53-54-PostgreSQL.sql rename to share/schema_versions/App-Netdisco-DB-54-55-PostgreSQL.sql