« blib/lib/App/Netdisco/DB/ResultSet/DeviceIp.pm
↑ Summary
blib/lib/App/Netdisco/Web/Plugin/AdminTask/Topology.pm »
| 1 | package App::Netdisco::Web::Search; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | use Dancer::Plugin::Auth::Extensible; | ||
| 6 | |||
| 7 | use App::Netdisco::Util::Web 'sql_match'; | ||
| 8 | use Regexp::Common 'net'; | ||
| 9 | use NetAddr::MAC (); | ||
| 10 | |||
| 11 | hook 'before_template' => sub { | ||
| 12 | my $tokens = shift; | ||
| 13 | |||
| 14 | return unless (request->path eq uri_for('/search')->path | ||
| 15 | or index(request->path, uri_for('/ajax/content/search')->path) == 0); | ||
| 16 | |||
| 17 | # used in the device search sidebar template to set selected items | ||
| 18 | foreach my $opt (qw/model vendor os os_ver/) { | ||
| 19 | my $p = (ref [] eq ref param($opt) ? param($opt) | ||
| 20 | : (param($opt) ? [param($opt)] : [])); | ||
| 21 | $tokens->{"${opt}_lkp"} = { map { $_ => 1 } @$p }; | ||
| 22 | } | ||
| 23 | }; | ||
| 24 | |||
| 25 | # Ports with this MAC. Joined to device, as Port search is, so port rows left | ||
| 26 | # behind by a deleted device do not count. | ||
| 27 | sub _mac_port_rs { | ||
| 28 | my ($schema, $mac) = @_; | ||
| 29 | return $schema->resultset('DevicePort') | ||
| 30 | ->search({ 'me.mac' => $mac }, | ||
| 31 | { join => 'device', | ||
| 32 | columns => [qw/me.ip me.port me.type me.is_uplink/] }); | ||
| 33 | } | ||
| 34 | |||
| 35 | # Where a MAC address matching device ports should go. Kept free of param() | ||
| 36 | # and the schema so xt can pin the decision without a session or a database. | ||
| 37 | # | ||
| 38 | # One port goes to the Ports tab filtered to that port. Several ports all on | ||
| 39 | # one device (a shared base MAC) go to the device details, as the MAC then | ||
| 40 | # names the device. Ports on several devices fall back to Port search, which | ||
| 41 | # hides uplinks and non-Ethernet ports unless told otherwise, so those options | ||
| 42 | # are set from the matched ports or the results could come up empty. A port | ||
| 43 | # with no type counts as non-Ethernet, as it does in Port search. | ||
| 44 | # | ||
| 45 | # Returns undef when no port matched, else { path => ..., query => {...} }. | ||
| 46 | sub _mac_search_target { | ||
| 47 | my ($q, $ports) = @_; | ||
| 48 | return undef unless scalar @$ports; | ||
| 49 | |||
| 50 | my %devices = map { $_->ip => 1 } @$ports; | ||
| 51 | |||
| 52 | if (scalar keys %devices == 1) { | ||
| 53 | return { path => '/device', query => ((scalar @$ports == 1) | ||
| 54 | ? { | ||
| 55 | tab => 'ports', | ||
| 56 | q => $ports->[0]->ip, | ||
| 57 | f => $ports->[0]->port, | ||
| 58 | c_mac => 'on', | ||
| 59 | c_nodes => 'on', | ||
| 60 | c_neighbors => 'on', | ||
| 61 | } : { | ||
| 62 | tab => 'details', | ||
| 63 | q => $ports->[0]->ip, | ||
| 64 | f => '', | ||
| 65 | }) }; | ||
| 66 | } | ||
| 67 | |||
| 68 | return { path => '/search', query => { | ||
| 69 | tab => 'port', | ||
| 70 | q => $q, | ||
| 71 | partial => 'on', | ||
| 72 | ((grep { $_->is_uplink } @$ports) ? (uplink => 'on') : ()), | ||
| 73 | ((grep { ($_->type || '') ne 'ethernetCsmacd' } @$ports) | ||
| 74 | ? () : (ethernet => 'on')), | ||
| 75 | } }; | ||
| 76 | } | ||
| 77 | |||
| 78 | get '/search' => require_login sub { | ||
| 79 | my $q = param('q'); | ||
| 80 | my $s = schema(vars->{'tenant'}); | ||
| 81 | |||
| 82 | if (not param('tab')) { | ||
| 83 | if (not $q) { | ||
| 84 | return redirect uri_for('/')->path; | ||
| 85 | } | ||
| 86 | |||
| 87 | # pick most likely tab for initial results | ||
| 88 | if ($q =~ m/^[0-9]+$/ and $q < 4096) { | ||
| 89 | params->{'tab'} = 'vlan'; | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | my $nd = $s->resultset('Device')->search_fuzzy($q); | ||
| 93 | my ($likeval, $likeclause) = sql_match($q); | ||
| 94 | my $mac = NetAddr::MAC->new(mac => ($q || '')); | ||
| 95 | |||
| 96 | undef $mac if | ||
| 97 | ($mac and $mac->as_ieee | ||
| 98 | and (($mac->as_ieee eq '00:00:00:00:00:00') | ||
| 99 | or ($mac->as_ieee !~ m/^$RE{net}{MAC}$/i))); | ||
| 100 | |||
| 101 | # a port's own MAC is not the device's, so device-first matching | ||
| 102 | # lands on the device details page and the MAC is nowhere on it | ||
| 103 | # (#1429). Only for a valid MAC, so names and IPs keep matching | ||
| 104 | # devices first (#1789). | ||
| 105 | my $mac_target = _mac_search_target($q, | ||
| 106 | ((defined $mac and not $mac->errstr) | ||
| 107 | ? [ _mac_port_rs($s, $mac->as_ieee)->all ] : [])); | ||
| 108 | |||
| 109 | if ($mac_target) { | ||
| 110 | return redirect uri_for($mac_target->{path}, | ||
| 111 | $mac_target->{query})->path_query; | ||
| 112 | } | ||
| 113 | elsif ($nd and $nd->count) { | ||
| 114 | if ($nd->count == 1) { | ||
| 115 | # redirect to device details for the one device | ||
| 116 | return redirect uri_for('/device', { | ||
| 117 | tab => 'details', | ||
| 118 | q => $nd->first->ip, | ||
| 119 | f => '', | ||
| 120 | })->path_query; | ||
| 121 | } | ||
| 122 | |||
| 123 | # multiple devices | ||
| 124 | params->{'tab'} = 'device'; | ||
| 125 | } | ||
| 126 | elsif ($s->resultset('DevicePort') | ||
| 127 | ->with_properties | ||
| 128 | ->search({ | ||
| 129 | -or => [ | ||
| 130 | { name => $likeclause }, | ||
| 131 | { 'properties.remote_dns' => $likeclause }, | ||
| 132 | (((!defined $mac) or $mac->errstr) | ||
| 133 | ? \['mac::text ILIKE ?', $likeval] | ||
| 134 | : {mac => $mac->as_ieee}), | ||
| 135 | ], | ||
| 136 | })->count) { | ||
| 137 | |||
| 138 | params->{'tab'} = 'port'; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | # if all else fails | ||
| 143 | params->{'tab'} ||= 'node'; | ||
| 144 | } | ||
| 145 | |||
| 146 | # used in the device search sidebar to populate select inputs | ||
| 147 | my $model_list = [ grep { defined } $s->resultset('Device')->get_distinct_col('model') ]; | ||
| 148 | my $os_list = [ grep { defined } $s->resultset('Device')->get_distinct_col('os') ]; | ||
| 149 | my $vendor_list = [ grep { defined } $s->resultset('Device')->get_distinct_col('vendor') ]; | ||
| 150 | |||
| 151 | my %os_vermap = ( | ||
| 152 | map { $_ => (join '', map {sprintf '%05s', $_} split m/(\D)/) } | ||
| 153 | grep { defined } | ||
| 154 | $s->resultset('Device')->get_distinct_col('os_ver') | ||
| 155 | ); | ||
| 156 | my $os_ver_list = [ sort {$os_vermap{$a} cmp $os_vermap{$b}} keys %os_vermap ]; | ||
| 157 | |||
| 158 | template 'search', { | ||
| 159 | search => params->{'tab'}, | ||
| 160 | model_list => $model_list, | ||
| 161 | os_list => $os_list, | ||
| 162 | os_ver_list => $os_ver_list, | ||
| 163 | vendor_list => $vendor_list, | ||
| 164 | }, { layout => 'main' }; | ||
| 165 | }; | ||
| 166 | |||
| 167 | true; |