diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Device.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Device.pm index 9c9e498c..58aef8f8 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Device.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Device.pm @@ -11,21 +11,37 @@ use App::Netdisco::Web::Plugin; register_search_tab({ tag => 'device', label => 'Device' }); +my $headers = ['Device','Contact','Location','System Name','Model','OS Version','Management IP','Serial']; + # device with various properties or a default match-all -ajax '/ajax/content/search/device' => require_login sub { - my $has_opt = List::MoreUtils::any {param($_)} - qw/name location dns ip description model os_ver vendor/; +sub get_rs_device { + my $q = shift; + my $has_opt = shift; + my $set; if ($has_opt) { $set = schema('netdisco')->resultset('Device')->search_by_field(scalar params); } else { - my $q = param('q'); send_error('Missing query', 400) unless $q; $set = schema('netdisco')->resultset('Device')->search_fuzzy($q); } + return $set; +} + +ajax '/ajax/content/search/device' => require_login sub { + my $q = param('q'); + my $has_opt = List::MoreUtils::any {param($_)} + qw/name location dns ip description model os_ver vendor/; + + unless ($has_opt || $q) { + send_error('Missing query', 400) + } + + my $set = get_rs_device($q, $has_opt); + return unless $set->count; content_type('text/html'); @@ -34,4 +50,33 @@ ajax '/ajax/content/search/device' => require_login sub { }, { layout => undef }; }; +get '/search/device' => require_login sub { + my $q = param('q'); + my $format = param('format'); + my $has_opt = List::MoreUtils::any {param($_)} + qw/name location dns ip description model os_ver vendor/; + + unless ($has_opt || $q) { + send_error('Missing query', 400) + } + + my $set = get_rs_device($q, $has_opt); + + return unless $set->count; + + if ( $format eq 'csv' ) { + + header( 'Content-Type' => 'text/comma-separated-values' ); + header( 'Content-Disposition' => + "attachment; filename=\"nd-devicesearch.csv\"" ); + template 'ajax/search/device_csv.tt', { + results => $set, + headers => $headers, + }, { layout => undef }; + } + else { + return; + } +}; + true; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Port.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Port.pm index 41792154..f3e07550 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Port.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/Port.pm @@ -9,10 +9,9 @@ use App::Netdisco::Web::Plugin; register_search_tab({ tag => 'port', label => 'Port' }); -# device ports with a description (er, name) matching -ajax '/ajax/content/search/port' => require_login sub { - my $q = param('q'); - send_error('Missing query', 400) unless $q; +sub get_rs_port { + my $q = shift; + my $set; if ($q =~ m/^\d+$/) { @@ -28,6 +27,16 @@ ajax '/ajax/content/search/port' => require_login sub { $set = schema('netdisco')->resultset('DevicePort') ->search({name => $query}); } + return $set; +} + +# device ports with a description (er, name) matching +ajax '/ajax/content/search/port' => require_login sub { + my $q = param('q'); + send_error('Missing query', 400) unless $q; + + my $set = get_rs_port($q); + return unless $set->count; content_type('text/html'); @@ -36,4 +45,27 @@ ajax '/ajax/content/search/port' => require_login sub { }, { layout => undef }; }; +get '/search/port' => require_login sub { + my $q = param('q'); + my $format = param('format'); + send_error('Missing query', 400) unless $q; + + my $set = get_rs_port($q); + + return unless $set->count; + + if ( $format eq 'csv' ) { + + header( 'Content-Type' => 'text/comma-separated-values' ); + header( 'Content-Disposition' => + "attachment; filename=\"nd-portsearch.csv\"" ); + template 'ajax/search/port_csv.tt', { + results => $set, + }, { layout => undef }; + } + else { + return; + } +}; + true; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm index 4ecec8ce..974c4e65 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Search/VLAN.pm @@ -7,26 +7,57 @@ use Dancer::Plugin::Auth::Extensible; use App::Netdisco::Web::Plugin; -register_search_tab({ tag => 'vlan', label => 'VLAN' }); +register_search_tab( { tag => 'vlan', label => 'VLAN' } ); # devices carrying vlan xxx -ajax '/ajax/content/search/vlan' => require_login sub { - my $q = param('q'); - send_error('Missing query', 400) unless $q; +sub get_rs_vlan { + my $q = shift; + my $set; - if ($q =~ m/^\d+$/) { - $set = schema('netdisco')->resultset('Device')->carrying_vlan({vlan => $q}); + if ( $q =~ m/^\d+$/ ) { + $set = schema('netdisco')->resultset('Device') + ->carrying_vlan( { vlan => $q } ); } else { - $set = schema('netdisco')->resultset('Device')->carrying_vlan_name({name => $q}); + $set = schema('netdisco')->resultset('Device') + ->carrying_vlan_name( { name => $q } ); } + return $set; +} + +ajax '/ajax/content/search/vlan' => require_login sub { + my $q = param('q'); + send_error( 'Missing query', 400 ) unless $q; + + my $set = get_rs_vlan($q); + return unless $set->count; content_type('text/html'); - template 'ajax/search/vlan.tt', { - results => $set, - }, { layout => undef }; + template 'ajax/search/vlan.tt', { results => $set, }, { layout => undef }; +}; + +get '/search/vlan' => require_login sub { + my $q = param('q'); + my $format = param('format'); + send_error( 'Missing query', 400 ) unless $q; + + my $set = get_rs_vlan($q); + + return unless $set->count; + + if ( $format eq 'csv' ) { + + header( 'Content-Type' => 'text/comma-separated-values' ); + header( 'Content-Disposition' => + "attachment; filename=\"nd-vlansearch.csv\"" ); + template 'ajax/search/vlan_csv.tt', { results => $set, }, + { layout => undef }; + } + else { + return; + } }; true; diff --git a/Netdisco/share/views/ajax/search/device.tt b/Netdisco/share/views/ajax/search/device.tt index 8ee38d10..bab35b88 100644 --- a/Netdisco/share/views/ajax/search/device.tt +++ b/Netdisco/share/views/ajax/search/device.tt @@ -1,3 +1,4 @@ +[% INCLUDE "download_as.tt" %]