1package App::Netdisco::Web::Plugin::Search::Device; 2 3use Dancer ':syntax'; 4use Dancer::Plugin::DBIC; 5use Dancer::Plugin::Auth::Extensible; 6 7use List::MoreUtils (); 8 9use App::Netdisco::Web::Plugin; 10 11register_search_tab({ 12 tag => 'device', 13 label => 'Device', 14 provides_csv => 1, 15 api_endpoint => 1, 16 api_parameters => [ 17 q => { 18 description => 'Partial match of Device contact, serial, module serials, location, name, description, dns, or any IP alias', 19 }, 20 name => { 21 description => 'Partial match of the Device name', 22 }, 23 location => { 24 description => 'Partial match of the Device location', 25 }, 26 dns => { 27 description => 'Partial match of any of the Device IP aliases', 28 }, 29 ip => { 30 description => 'IP or IP Prefix within which the Device must have an interface address', 31 }, 32 description => { 33 description => 'Partial match of the Device description', 34 }, 35 mac => { 36 description => 'MAC Address of the Device or any of its Interfaces', 37 }, 38 model => { 39 description => 'Exact match of the Device model', 40 }, 41 os => { 42 description => 'Exact match of the Device operating system', 43 }, 44 os_ver => { 45 description => 'Exact match of the Device operating system version', 46 }, 47 vendor => { 48 description => 'Exact match of the Device vendor', 49 }, 50 layers => { 51 description => 'OSI Layer which the device must support', 52 }, 53 matchall => { 54 description => 'If true, all fields (except "q") must match the Device', 55 type => 'boolean', 56 default => 'false', 57 }, 58 ], 59}); 60 61# device with various properties or a default match-all 62get '/ajax/content/search/device' => require_login sub { 63 my $has_opt = List::MoreUtils::any { param($_) } 64 qw/name location dns ip description model os os_ver vendor layers mac/; 65 my $rs; 66 67 if ($has_opt) { 68 $rs = schema('netdisco')->resultset('Device')->columns( 69 [ "ip", "dns", "name", 70 "location", "model", "os_ver", "serial" 71 ] 72 )->with_times->search_by_field( scalar params ); 73 } 74 else { 75 my $q = param('q'); 76 send_error( 'Missing query', 400 ) unless $q; 77 78 $rs = schema('netdisco')->resultset('Device')->columns( 79 [ "ip", "dns", "name", 80 "location", "model", "os_ver", "serial" 81 ] 82 )->with_times->search_fuzzy($q); 83 } 84 85 my @results = $rs->hri->all; 86 return unless scalar @results; 87 88 if ( request->is_ajax ) { 89 my $json = to_json( \@results ); 90 template 'ajax/search/device.tt', { results => $json }; 91 } 92 else { 93 header( 'Content-Type' => 'text/comma-separated-values' ); 94 template 'ajax/search/device_csv.tt', { results => \@results, }; 95 } 96}; 97 981; 99