« blib/lib/App/Netdisco/Web/Plugin/Report/NodeVendor.pm
↑ Summary
blib/lib/App/Netdisco/Web/Plugin/Report/PortAdminDown.pm »
| 1 | package App::Netdisco::Web::Plugin::Report::NodesDiscovered; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | use Dancer::Plugin::Auth::Extensible; | ||
| 6 | |||
| 7 | use App::Netdisco::Web::Plugin; | ||
| 8 | use App::Netdisco::Util::Web 'sql_match'; | ||
| 9 | |||
| 10 | register_report( | ||
| 11 | { category => 'Node', | ||
| 12 | tag => 'nodesdiscovered', | ||
| 13 | label => 'Nodes discovered through LLDP/CDP', | ||
| 14 | provides_csv => 1, | ||
| 15 | api_endpoint => 1, | ||
| 16 | api_parameters => [ | ||
| 17 | remote_id => { | ||
| 18 | description => 'Host Name reported', | ||
| 19 | }, | ||
| 20 | remote_type => { | ||
| 21 | description => 'Platform reported', | ||
| 22 | }, | ||
| 23 | aps => { | ||
| 24 | description => 'Include Wireless APs in the report', | ||
| 25 | type => 'boolean', | ||
| 26 | default => 'false', | ||
| 27 | }, | ||
| 28 | phones => { | ||
| 29 | description => 'Include IP Phones in the report', | ||
| 30 | type => 'boolean', | ||
| 31 | default => 'false', | ||
| 32 | }, | ||
| 33 | matchall => { | ||
| 34 | description => 'Match all parameters (true) or any (false)', | ||
| 35 | type => 'boolean', | ||
| 36 | default => 'false', | ||
| 37 | }, | ||
| 38 | ], | ||
| 39 | } | ||
| 40 | ); | ||
| 41 | |||
| 42 | get '/ajax/content/report/nodesdiscovered' => require_login sub { | ||
| 43 | my $op = param('matchall') ? '-and' : '-or'; | ||
| 44 | |||
| 45 | my @results = schema(vars->{'tenant'})->resultset('Virtual::NodesDiscovered') | ||
| 46 | ->search({ | ||
| 47 | $op => [ | ||
| 48 | (param('aps') ? | ||
| 49 | ('me.remote_type' => { -ilike => 'AP:%' }) : ()), | ||
| 50 | (param('phones') ? | ||
| 51 | ('me.remote_type' => { -ilike => '%ip_phone%' }) : ()), | ||
| 52 | (param('remote_id') ? | ||
| 53 | ('me.remote_id' => { -ilike => scalar sql_match(param('remote_id')) }) : ()), | ||
| 54 | (param('remote_type') ? ('-or' => [ | ||
| 55 | map {( 'me.remote_type' => { -ilike => scalar sql_match($_) } )} | ||
| 56 | grep { $_ } | ||
| 57 | (ref param('remote_type') ? @{param('remote_type')} : param('remote_type')) | ||
| 58 | ]) : ()), | ||
| 59 | ], | ||
| 60 | }) | ||
| 61 | ->hri->all; | ||
| 62 | |||
| 63 | return unless scalar @results; | ||
| 64 | |||
| 65 | if ( request->is_ajax ) { | ||
| 66 | my $json = to_json( \@results ); | ||
| 67 | template 'ajax/report/nodesdiscovered.tt', { results => $json }, { layout => 'noop' }; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | header( 'Content-Type' => 'text/comma-separated-values' ); | ||
| 71 | template 'ajax/report/nodesdiscovered_csv.tt', | ||
| 72 | { results => \@results }, { layout => 'noop' }; | ||
| 73 | } | ||
| 74 | }; | ||
| 75 | |||
| 76 | 1; |