Files
netdisco/lib/App/Netdisco/Web/Search.pm
Oliver Gorwits dff26abc5c API implementation (#712)
* initial v0 creator

* working json api for generic reports

* add require login

* move report swagger into plugin, and set new default layout of noop

* require proper role and also use new util func

* start to tidy authn

* some work on cleaning up web authn

* clean up the authN checks

* fix bug

* fix the auth for api

* fixes to json handling

* set swagger sort order

* enable most reports for api endpoints

* fix doc

* add paramters to reports

* add missed report

* allow api_parameters in reports config

* reorganise api

* add vlan search

* add port search

* make sure to enable layout processing

* add device search

* add v1 to api paths

* add Node Search

* support api_responses

* add device object search; fix spurious ports field in device result class

* handle some plugins just returning undef if search fails

* errors from api seamlessley

* fix error in date range default

* more sensible default for prefix

* change order of endpoints in swagger-ui

* all db row classes can now TO_JSON

* add device_port api endpoint

* add device ports endpoint

* do not expand docs

* add swagger ui json tree formatter

* add all relations from Device table

* add port relations

* add nodes retrieve on device or vlan

* rename to GetAPIKey

* update config for previous commit
2020-04-15 21:15:52 +01:00

102 lines
3.2 KiB
Perl

package App::Netdisco::Web::Search;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Util::Web 'sql_match';
use Regexp::Common 'net';
use NetAddr::MAC ();
hook 'before_template' => sub {
my $tokens = shift;
return unless (request->path eq uri_for('/search')->path
or index(request->path, uri_for('/ajax/content/search')->path) == 0);
# used in the device search sidebar template to set selected items
foreach my $opt (qw/model vendor os os_ver/) {
my $p = (ref [] eq ref param($opt) ? param($opt)
: (param($opt) ? [param($opt)] : []));
$tokens->{"${opt}_lkp"} = { map { $_ => 1 } @$p };
}
};
get '/search' => require_login sub {
my $q = param('q');
my $s = schema('netdisco');
if (not param('tab')) {
if (not $q) {
return redirect uri_for('/')->path;
}
# pick most likely tab for initial results
if ($q =~ m/^\d+$/) {
params->{'tab'} = 'vlan';
}
else {
my $nd = $s->resultset('Device')->search_fuzzy($q);
my ($likeval, $likeclause) = sql_match($q);
my $mac = NetAddr::MAC->new($q);
undef $mac if
($mac and $mac->as_ieee
and (($mac->as_ieee eq '00:00:00:00:00:00')
or ($mac->as_ieee !~ m/$RE{net}{MAC}/)));
if ($nd and $nd->count) {
if ($nd->count == 1) {
# redirect to device details for the one device
return redirect uri_for('/device', {
tab => 'details',
q => $nd->first->ip,
f => '',
})->path_query;
}
# multiple devices
params->{'tab'} = 'device';
}
elsif ($s->resultset('DevicePort')
->search({
-or => [
{name => $likeclause},
((!defined $mac or $mac->errstr)
? \['mac::text ILIKE ?', $likeval]
: {mac => $mac->as_ieee}),
],
})->count) {
params->{'tab'} = 'port';
}
}
# if all else fails
params->{'tab'} ||= 'node';
}
# used in the device search sidebar to populate select inputs
my $model_list = [ grep { defined } $s->resultset('Device')->get_distinct_col('model') ];
my $os_list = [ grep { defined } $s->resultset('Device')->get_distinct_col('os') ];
my $vendor_list = [ grep { defined } $s->resultset('Device')->get_distinct_col('vendor') ];
my %os_vermap = (
map { $_ => (join '', map {sprintf '%05s', $_} split m/(\D)/) }
grep { defined }
$s->resultset('Device')->get_distinct_col('os_ver')
);
my $os_ver_list = [ sort {$os_vermap{$a} cmp $os_vermap{$b}} keys %os_vermap ];
template 'search', {
search => params->{'tab'},
model_list => $model_list,
os_list => $os_list,
os_ver_list => $os_ver_list,
vendor_list => $vendor_list,
}, { layout => 'main' };
};
true;