« blib/lib/App/Netdisco/SSHCollector/Platform/EOS.pm
↑ Summary
blib/lib/App/Netdisco/Util/L3Domain.pm »
| 1 | package App::Netdisco::Web::Plugin::Search::Device; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | use Dancer::Plugin::Auth::Extensible; | ||
| 6 | |||
| 7 | use List::MoreUtils (); | ||
| 8 | |||
| 9 | use App::Netdisco::Web::Plugin; | ||
| 10 | |||
| 11 | my @DEFAULT_FIELDS = qw/ip dns name location model os_ver serial chassis_id/; | ||
| 12 | |||
| 13 | # the last known good SNMP read community is a credential, so it is never | ||
| 14 | # offered to a caller, whatever they ask for | ||
| 15 | my %SECRET_FIELDS = map {$_ => 1} qw/snmp_comm/; | ||
| 16 | |||
| 17 | # not a Device column: it is selected from the joined community table | ||
| 18 | my $AUTH_TAG_FIELD = 'device_auth_tag'; | ||
| 19 | |||
| 20 | # Postgres folds an unquoted identifier to lower case over ASCII alone, and | ||
| 21 | # its lexer takes only ASCII whitespace around one, so a name is matched the | ||
| 22 | # way the database would have read it and no more widely | ||
| 23 | sub _folded_name { | ||
| 24 | my $name = shift; | ||
| 25 | $name =~ s/\A[ \t\n\r\f\x0B]+//; | ||
| 26 | $name =~ s/[ \t\n\r\f\x0B]+\z//; | ||
| 27 | $name =~ tr/A-Z/a-z/; | ||
| 28 | return $name; | ||
| 29 | } | ||
| 30 | |||
| 31 | # resolve the caller's "fields" into a SELECT list, or nothing at all if it | ||
| 32 | # names anything which is not a selectable Device column | ||
| 33 | sub _requested_fields { | ||
| 34 | my ($result_source, $fields) = @_; | ||
| 35 | |||
| 36 | my @selectable = grep { ! $SECRET_FIELDS{$_} } $result_source->columns; | ||
| 37 | my %canonical = map {(_folded_name($_) => $_)} @selectable, $AUTH_TAG_FIELD; | ||
| 38 | |||
| 39 | my @asked = $fields eq 'all' ? @selectable | ||
| 40 | : $fields ? split(/\s*,\s*/, $fields) | ||
| 41 | : @DEFAULT_FIELDS; | ||
| 42 | |||
| 43 | my $want_tag = List::MoreUtils::any { $_ eq $AUTH_TAG_FIELD } @asked; | ||
| 44 | my @labels = grep { $_ ne $AUTH_TAG_FIELD } @asked; | ||
| 45 | |||
| 46 | return if List::MoreUtils::any { ! $canonical{ _folded_name($_) } } @labels; | ||
| 47 | |||
| 48 | # each field keeps for its key the name the caller sent, which the label | ||
| 49 | # form holds on the perl side, while the database is only ever asked for | ||
| 50 | # the column that name matched | ||
| 51 | my @cols = map { | ||
| 52 | my $column = $canonical{ _folded_name($_) }; | ||
| 53 | ($_ eq $column) ? $column : { $_ => "me.$column" } | ||
| 54 | } @labels; | ||
| 55 | |||
| 56 | # a "fields" which is given but names no column of its own selected every | ||
| 57 | # column before this list existed, so it still selects every one it may | ||
| 58 | return { | ||
| 59 | columns => (@cols ? \@cols : $fields ? \@selectable : [@DEFAULT_FIELDS]), | ||
| 60 | want_tag => $want_tag, | ||
| 61 | }; | ||
| 62 | } | ||
| 63 | |||
| 64 | register_search_tab({ | ||
| 65 | tag => 'device', | ||
| 66 | label => 'Device', | ||
| 67 | provides_csv => 1, | ||
| 68 | api_endpoint => 1, | ||
| 69 | api_parameters => [ | ||
| 70 | q => { | ||
| 71 | description => 'Partial match of Device contact, serial, chassis ID, module serials, location, name, description, dns, or any IP alias. Optional if "limit" and "offset" are both given, to return all devices.', | ||
| 72 | }, | ||
| 73 | name => { | ||
| 74 | description => 'Partial match of the Device name', | ||
| 75 | }, | ||
| 76 | location => { | ||
| 77 | description => 'Partial match of the Device location', | ||
| 78 | }, | ||
| 79 | dns => { | ||
| 80 | description => 'Partial match of any of the Device IP aliases', | ||
| 81 | }, | ||
| 82 | ip => { | ||
| 83 | description => 'IP or IP Prefix within which the Device must have an interface address', | ||
| 84 | }, | ||
| 85 | description => { | ||
| 86 | description => 'Partial match of the Device description', | ||
| 87 | }, | ||
| 88 | mac => { | ||
| 89 | description => 'MAC Address of the Device or any of its Interfaces', | ||
| 90 | }, | ||
| 91 | model => { | ||
| 92 | description => 'Exact match of the Device model', | ||
| 93 | }, | ||
| 94 | os => { | ||
| 95 | description => 'Exact match of the Device operating system', | ||
| 96 | }, | ||
| 97 | os_ver => { | ||
| 98 | description => 'Exact match of the Device operating system version', | ||
| 99 | }, | ||
| 100 | vendor => { | ||
| 101 | description => 'Exact match of the Device vendor', | ||
| 102 | }, | ||
| 103 | layers => { | ||
| 104 | description => 'OSI Layer which the device must support', | ||
| 105 | }, | ||
| 106 | matchall => { | ||
| 107 | description => 'If true, all fields (except "q") must match the Device', | ||
| 108 | type => 'boolean', | ||
| 109 | default => 'false', | ||
| 110 | }, | ||
| 111 | fields => { | ||
| 112 | description => 'Comma-separated list of fields to return. Default: ip,dns,name,location,model,os_ver,serial,chassis_id. Any Device table column except snmp_comm is valid (e.g. vendor,os,layers,last_discover,last_macsuck,last_arpnip), and any other name is an error. Each field is returned under the name you gave it. Use "all" for every column except snmp_comm, which is never returned. Extra join: device_auth_tag.', | ||
| 113 | }, | ||
| 114 | seeallcolumns => { | ||
| 115 | description => 'Deprecated, use fields=all instead. If true and "fields" is not given, all columns of the Device will be shown.', | ||
| 116 | type => 'boolean', | ||
| 117 | default => 'false', | ||
| 118 | }, | ||
| 119 | limit => { | ||
| 120 | description => 'Maximum number of devices to return. Required, along with "offset", if neither "q" nor any filter parameter is given.', | ||
| 121 | }, | ||
| 122 | offset => { | ||
| 123 | description => 'Number of devices to skip (for paging). Default: 0.', | ||
| 124 | }, | ||
| 125 | ], | ||
| 126 | }); | ||
| 127 | |||
| 128 | # device with various properties or a default match-all | ||
| 129 | get '/ajax/content/search/device' => require_login sub { | ||
| 130 | my $has_opt = List::MoreUtils::any { param($_) } | ||
| 131 | qw/name location dns ip description model os os_ver vendor layers mac/; | ||
| 132 | |||
| 133 | my $fields = param('fields') || (param('seeallcolumns') ? 'all' : ''); | ||
| 134 | |||
| 135 | my $rs_columns = schema(vars->{'tenant'})->resultset('Device'); | ||
| 136 | my $select = _requested_fields( $rs_columns->result_source, $fields ); | ||
| 137 | send_error( 'Unknown field requested in "fields" - only Device table' | ||
| 138 | .' columns, except snmp_comm, and device_auth_tag may be asked for', 400 ) | ||
| 139 | if ! $select; | ||
| 140 | |||
| 141 | $rs_columns = $rs_columns->columns( $select->{columns} ); | ||
| 142 | $rs_columns = $rs_columns->search(undef, { | ||
| 143 | join => 'community', | ||
| 144 | '+columns' => [{ device_auth_tag => 'community.snmp_auth_tag_read' }], | ||
| 145 | }) if $select->{want_tag}; | ||
| 146 | |||
| 147 | my $rs; | ||
| 148 | if ($has_opt) { | ||
| 149 | $rs = $rs_columns->search_by_field( scalar params ); | ||
| 150 | } | ||
| 151 | elsif (param('q')) { | ||
| 152 | $rs = $rs_columns->search_fuzzy( param('q') ); | ||
| 153 | } | ||
| 154 | elsif (defined param('limit') and defined param('offset')) { | ||
| 155 | $rs = $rs_columns->search(undef, { order_by => [qw/me.dns me.ip/] }); | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | send_error( 'Missing query - provide "q", a filter parameter,' | ||
| 159 | .' or both "limit" and "offset"', 400 ); | ||
| 160 | } | ||
| 161 | |||
| 162 | my $limit = param('limit'); | ||
| 163 | my $offset = param('offset'); | ||
| 164 | |||
| 165 | if ($limit or $offset) { | ||
| 166 | # paginate on device ip before with_times/with_module_serials: combining | ||
| 167 | # LIMIT with the module_serials has_many join forces DBIC to wrap the | ||
| 168 | # query in a subquery, which cannot re-express with_times' raw-SQL | ||
| 169 | # computed columns (eg. me.creation) in the outer SELECT, and crashes | ||
| 170 | my @page_ips = $rs->search(undef, { | ||
| 171 | columns => ['ip'], | ||
| 172 | ($limit ? (rows => int($limit)) : ()), | ||
| 173 | ($offset ? (offset => int($offset)) : ()), | ||
| 174 | })->get_column('ip')->all; | ||
| 175 | return unless @page_ips; | ||
| 176 | |||
| 177 | $rs = $rs_columns->search({ 'me.ip' => { -in => \@page_ips } }, | ||
| 178 | { order_by => [qw/me.dns me.ip/] }); | ||
| 179 | } | ||
| 180 | |||
| 181 | my @results = $rs->with_times->with_module_serials # must come after search_fuzzy | ||
| 182 | ->hri->all; | ||
| 183 | return unless scalar @results; | ||
| 184 | |||
| 185 | # deduplicate the results as no longer distinct after with_module_serials | ||
| 186 | my %seen = (); | ||
| 187 | @results = grep { ! $seen{$_->{ip}}++ } @results; | ||
| 188 | |||
| 189 | # flatten device serial, device chassis_id, and module serial(s), and deduplicate | ||
| 190 | map {$_->{module_serials} = [ List::MoreUtils::uniq | ||
| 191 | sort | ||
| 192 | grep {length} | ||
| 193 | grep {defined} ( | ||
| 194 | $_->{serial}, | ||
| 195 | $_->{chassis_id}, | ||
| 196 | ( map { $_->{serial} } | ||
| 197 | @{ $_->{module_serials} } ) | ||
| 198 | ) | ||
| 199 | ]} @results; | ||
| 200 | |||
| 201 | if ( request->is_ajax ) { | ||
| 202 | my $json = to_json( \@results ); | ||
| 203 | template 'ajax/search/device.tt', { results => $json }, { layout => 'noop' };; | ||
| 204 | } | ||
| 205 | else { | ||
| 206 | header( 'Content-Type' => 'text/comma-separated-values' ); | ||
| 207 | template 'ajax/search/device_csv.tt', { results => \@results, }, { layout => 'noop' }; | ||
| 208 | } | ||
| 209 | }; | ||
| 210 | |||
| 211 | 1; |