diff --git a/Netdisco/lib/Netdisco/DB/ResultSet/DevicePort.pm b/Netdisco/lib/Netdisco/DB/ResultSet/DevicePort.pm index 5b8a028a..deeb3c3e 100644 --- a/Netdisco/lib/Netdisco/DB/ResultSet/DevicePort.pm +++ b/Netdisco/lib/Netdisco/DB/ResultSet/DevicePort.pm @@ -21,5 +21,23 @@ sub by_mac { ); } +# confusingly the "name" field is set using IOS "descrption" +# command but should not be confused with the "descr" field +sub by_name { + my ($set, $name) = @_; + return $set unless $name; + + return $set->search( + { + 'me.name' => { '-ilike' => $name }, + }, + { + order_by => [qw/ me.ip me.port /], + columns => [qw/ ip port descr name vlan device.dns /], + join => 'device', + }, + ); +} + 1; diff --git a/Netdisco/lib/Netdisco/Web.pm b/Netdisco/lib/Netdisco/Web.pm index 434f7c0e..91a04690 100644 --- a/Netdisco/lib/Netdisco/Web.pm +++ b/Netdisco/lib/Netdisco/Web.pm @@ -3,7 +3,6 @@ package Netdisco::Web; use Dancer ':syntax'; use Dancer::Plugin::Ajax; use Dancer::Plugin::DBIC; -use Dancer::Exception ':all'; use Digest::MD5 (); use Socket6 (); # to ensure dependency is met @@ -43,8 +42,9 @@ ajax '/ajax/content/search/node' => sub { return unless $node; content_type('text/html'); - try { - my $mac = Net::MAC->new(mac => $node); + my $mac = Net::MAC->new(mac => $node, 'die' => 0, verbose => 0); + if (eval { $mac->as_IEEE }) { + my $ips = schema('netdisco')->resultset('NodeIp') ->by_mac(param('archived'), $mac->as_IEEE); return unless $ips->count; @@ -61,7 +61,7 @@ ajax '/ajax/content/search/node' => sub { ports => $ports, }, { layout => undef }; } - catch { + else { my $set; if (my $ip = NetAddr::IP::Lite->new($node)) { @@ -80,7 +80,7 @@ ajax '/ajax/content/search/node' => sub { template 'ajax/node_by_ip.tt', { results => $set, }, { layout => undef }; - }; + } }; # devices carrying vlan xxx @@ -97,6 +97,20 @@ ajax '/ajax/content/search/vlan' => sub { }, { layout => undef }; }; +# device ports with a description (er, name) matching +ajax '/ajax/content/search/port' => sub { + my $name = param('q'); + return unless $name; + + my $set = schema('netdisco')->resultset('DevicePort')->by_name($name); + return unless $set->count; + + content_type('text/html'); + template 'ajax/port.tt', { + results => $set, + }, { layout => undef }; +}; + get '/search' => sub { my $q = param('q'); if ($q and not param('tab')) { diff --git a/Netdisco/views/ajax/port.tt b/Netdisco/views/ajax/port.tt new file mode 100644 index 00000000..ecfa0ba7 --- /dev/null +++ b/Netdisco/views/ajax/port.tt @@ -0,0 +1,22 @@ + + + + + + + + + + + [% WHILE (row = results.next) %] + + + + + + + [% END %] + +
NameDescriptionVlanPort
[% row.name %][% row.descr %][% row.vlan %][% row.ip %] [ [% row.port %] ] + [% ' (' _ row.device.dns.remove(settings.domain_suffix) _ ')' IF row.device.dns %] +