blib/lib/App/Netdisco/Web/Plugin/Search/Port.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 142 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package App::Netdisco::Web::Plugin::Search::Port;
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::Port 'to_speed';
9 use App::Netdisco::Util::Web 'sql_match';
10  
11 use Regexp::Common 'net';
12 use NetAddr::MAC ();
13  
14 register_search_tab({
15 tag => 'port',
16 label => 'Port',
17 provides_csv => 1,
18 api_endpoint => 1,
19 api_parameters => [
20 q => {
21 description => 'Port name, VLAN, or MAC address',
22 required => 1,
23 },
24 partial => {
25 description => 'Search for a partial match on parameter "q"',
26 type => 'boolean',
27 default => 'true',
28 },
29 uplink => {
30 description => 'Include uplinks in results',
31 type => 'boolean',
32 default => 'false',
33 },
34 descr => {
35 description => 'Search in the Port Description field',
36 type => 'boolean',
37 default => 'false',
38 },
39 ethernet => {
40 description => 'Only Ethernet type interfaces in results',
41 type => 'boolean',
42 default => 'true',
43 },
44 ],
45 });
46  
47 # device ports with a description (er, name) matching
48 get '/ajax/content/search/port' => require_login sub {
49 my $q = param('q');
50 send_error( 'Missing query', 400 ) unless $q;
51 my $rs;
52  
53 if ($q =~ m/^[0-9]+$/ and $q < 4096) {
54 $rs = schema(vars->{'tenant'})->resultset('DevicePort')
55 ->columns( [qw/ ip port name up up_admin speed /] )->search({
56 "port_vlans.vlan" => $q,
57 ( param('uplink') ? () : (-or => [
58 {-not_bool => "properties.remote_is_discoverable"},
59 {-or => [
60 {-not_bool => "me.is_uplink"},
61 {"me.is_uplink" => undef},
62 ]}
63 ]) ),
64 ( param('ethernet') ? ("me.type" => 'ethernetCsmacd') : () ),
65 },{ '+columns' => [qw/ device.dns device.name port_vlans.vlan /],
66 join => [qw/ properties port_vlans device /]
67 }
68 )->with_times;
69 }
70 else {
71 my ( $likeval, $likeclause ) = sql_match($q);
72 my $mac = NetAddr::MAC->new(mac => ($q || ''));
73  
74 undef $mac if
75 ($mac and $mac->as_ieee
76 and (($mac->as_ieee eq '00:00:00:00:00:00')
77 or ($mac->as_ieee !~ m/^$RE{net}{MAC}$/i)));
78  
79 $rs = schema(vars->{'tenant'})->resultset('DevicePort')
80 ->columns( [qw/ ip port name up up_admin speed properties.remote_dns /] )
81 ->search({
82 -and => [
83 -or => [
84 { "me.name" => ( param('partial') ? $likeclause : $q ) },
85 ( param('descr') ? (
86 { "me.descr" => ( param('partial') ? $likeclause : $q ) },
87 ) : () ),
88 ( ((!defined $mac) or $mac->errstr)
89 ? \[ 'me.mac::text ILIKE ?', $likeval ]
90 : { 'me.mac' => $mac->as_ieee }
91 ),
92 { "properties.remote_dns" => $likeclause },
93 ( param('uplink') ? (
94 { "me.remote_id" => $likeclause },
95 { "me.remote_type" => $likeclause },
96 ) : () ),
97 ],
98 ( param('uplink') ? () : (-or => [
99 { "properties.remote_dns" => $likeclause },
100 {-not_bool => "properties.remote_is_discoverable"},
101 {-or => [
102 {-not_bool => "me.is_uplink"},
103 {"me.is_uplink" => undef},
104 ]}
105 ]) ),
106 ( param('ethernet') ? ("me.type" => 'ethernetCsmacd') : () ),
107 ]
108 },
109 { '+columns' => [qw/ device.dns device.name /, {vlan_agg => q{array_to_string(array_agg(port_vlans.vlan), ', ')}} ],
110 join => [qw/ properties port_vlans device /],
111 group_by => [qw/me.ip me.port me.name me.up me.up_admin me.speed device.dns device.name device.last_discover device.uptime properties.remote_dns/],
112 }
113 )->with_times;
114 }
115  
116 my @results = $rs->hri->all;
117 return unless scalar @results;
118 map { $_->{speed} = to_speed( $_->{speed} ) } @results;
119  
120 if ( request->is_ajax ) {
121 my $json = to_json( \@results );
122 template 'ajax/search/port.tt', { results => $json }, { layout => 'noop' };
123 }
124 else {
125 header( 'Content-Type' => 'text/comma-separated-values' );
126 template 'ajax/search/port_csv.tt', { results => \@results }, { layout => 'noop' };
127 }
128 };
129  
130 1;