From db7e68702aab766f6058b59016851c5b602eeb31 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sun, 8 Jan 2012 23:01:06 +0000 Subject: [PATCH] allow vlan search by name --- Netdisco/lib/Netdisco/DB/ResultSet/Device.pm | 17 ++++++++ Netdisco/lib/Netdisco/Web.pm | 46 +++++++++++--------- Netdisco/views/ajax/device.tt | 2 +- Netdisco/views/ajax/node_by_ip.tt | 2 +- Netdisco/views/ajax/node_by_mac.tt | 4 +- Netdisco/views/ajax/vlan.tt | 13 +++--- 6 files changed, 54 insertions(+), 30 deletions(-) diff --git a/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm b/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm index 6dc34a53..fefb7efb 100644 --- a/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm +++ b/Netdisco/lib/Netdisco/DB/ResultSet/Device.pm @@ -110,4 +110,21 @@ sub carrying_vlan { ); } +sub carrying_vlan_name { + my ($set, $name) = @_; + return $set unless $name; + $name = "\%$name\%" if $name !~ m/\%/; + + return $set->search( + { + 'vlans.description' => { '-ilike' => $name }, + }, + { + order_by => [qw/ me.dns me.ip /], + columns => [qw/ me.ip me.dns me.model me.os me.vendor /], + prefetch => 'vlans', + }, + ); +} + 1; diff --git a/Netdisco/lib/Netdisco/Web.pm b/Netdisco/lib/Netdisco/Web.pm index fdc5519a..fced281b 100644 --- a/Netdisco/lib/Netdisco/Web.pm +++ b/Netdisco/lib/Netdisco/Web.pm @@ -30,17 +30,6 @@ hook 'before' => sub { var('query_defaults' => { map { ($_ => "tab=$_") } qw/node/ }); var('query_defaults')->{node} .= "\&$_=". (param($_) || '') for qw/stamps vendor archived partial/; - - # set up property lists for device search - var('model_list' => [ - schema('netdisco')->resultset('Device')->get_distinct('model') - ]); - var('os_ver_list' => [ - schema('netdisco')->resultset('Device')->get_distinct('os_ver') - ]); - var('vendor_list' => [ - schema('netdisco')->resultset('Device')->get_distinct('vendor') - ]); }; # device with various properties or a default match-all @@ -50,16 +39,15 @@ ajax '/ajax/content/search/device' => sub { my $set; if ($has_opt) { - $set = schema('netdisco')->resultset('Device')->by_field(scalar params); - return unless $set->count; + $set = schema('netdisco')->resultset('Device')->by_field(scalar params); } else { - my $q = param('q'); - return unless $q; + my $q = param('q'); + return unless $q; - $set = schema('netdisco')->resultset('Device')->by_any($q); - return unless $set->count; + $set = schema('netdisco')->resultset('Device')->by_any($q); } + return unless $set->count; content_type('text/html'); template 'ajax/device.tt', { @@ -99,14 +87,13 @@ ajax '/ajax/content/search/node' => sub { # by_ip() will extract cidr notation if necessary $set = schema('netdisco')->resultset('NodeIp') ->by_ip(param('archived'), $ip); - return unless $set->count; } else { $node = "\%$node\%" if param('partial'); $set = schema('netdisco')->resultset('NodeIp') ->by_name(param('archived'), $node); - return unless $set->count; } + return unless $set->count; template 'ajax/node_by_ip.tt', { results => $set, @@ -117,9 +104,15 @@ ajax '/ajax/content/search/node' => sub { # devices carrying vlan xxx ajax '/ajax/content/search/vlan' => sub { my $vlan = param('q'); - return unless $vlan and $vlan =~ m/^\d+$/; + return unless $vlan; + my $set; - my $set = schema('netdisco')->resultset('Device')->carrying_vlan($vlan); + if ($vlan =~ m/^\d+$/) { + $set = schema('netdisco')->resultset('Device')->carrying_vlan($vlan); + } + else { + $set = schema('netdisco')->resultset('Device')->carrying_vlan_name($vlan); + } return unless $set->count; content_type('text/html'); @@ -147,6 +140,17 @@ get '/' => sub { }; get '/search' => sub { + # set up property lists for device search + var('model_list' => [ + schema('netdisco')->resultset('Device')->get_distinct('model') + ]); + var('os_ver_list' => [ + schema('netdisco')->resultset('Device')->get_distinct('os_ver') + ]); + var('vendor_list' => [ + schema('netdisco')->resultset('Device')->get_distinct('vendor') + ]); + my $q = param('q'); if ($q and not param('tab')) { # pick most likely tab for initial results diff --git a/Netdisco/views/ajax/device.tt b/Netdisco/views/ajax/device.tt index 7b31c6d0..28b3f3b3 100644 --- a/Netdisco/views/ajax/device.tt +++ b/Netdisco/views/ajax/device.tt @@ -14,7 +14,7 @@ [% WHILE (row = results.next) %] - [% row.dns.remove(settings.domain_suffix) %] + [% row.dns.remove(settings.domain_suffix) %] [% row.contact %] [% row.location %] [% row.name %] diff --git a/Netdisco/views/ajax/node_by_ip.tt b/Netdisco/views/ajax/node_by_ip.tt index a19a5c64..8d4cb7a7 100644 --- a/Netdisco/views/ajax/node_by_ip.tt +++ b/Netdisco/views/ajax/node_by_ip.tt @@ -38,7 +38,7 @@   [% END %] Switch Port - [% node.switch %] [ [% node.port %] ] + [% node.switch %] [ [% node.port %] ] [% ' (' _ node.device.dns.remove(settings.domain_suffix) _ ')' IF node.device.dns %] [% ' a' IF NOT node.active %] diff --git a/Netdisco/views/ajax/node_by_mac.tt b/Netdisco/views/ajax/node_by_mac.tt index 72e0e96e..818d381a 100644 --- a/Netdisco/views/ajax/node_by_mac.tt +++ b/Netdisco/views/ajax/node_by_mac.tt @@ -53,7 +53,7 @@   [% END %] Switch Port - [% node.switch %] [ [% node.port %] ] + [% node.switch %] [ [% node.port %] ] [% ' (' _ node.device.dns.remove(settings.domain_suffix) _ ')' IF node.device.dns %] [% ' a' IF NOT node.active %] @@ -70,7 +70,7 @@   [% END %] Switch Port - [% port.ip %] [ [% port.port %] ] + [% port.ip %] [ [% port.port %] ] [% ' (' _ port.device.dns.remove(settings.domain_suffix) _ ')' IF port.device.dns %] [% IF params.stamps %] diff --git a/Netdisco/views/ajax/vlan.tt b/Netdisco/views/ajax/vlan.tt index 870bfbdb..0a57eea3 100644 --- a/Netdisco/views/ajax/vlan.tt +++ b/Netdisco/views/ajax/vlan.tt @@ -1,6 +1,7 @@ + @@ -11,16 +12,18 @@ [% WHILE (row = results.next) %] + + href="/device?ip=[% row.ip %]&vlan=[% row.vlan.vlan %]">[% row.dns %] + href="/device?ip=[% row.ip %]&vlan=[% row.vlan.vlan %]">[% row.vlan.description %] + href="/device?ip=[% row.ip %]&vlan=[% row.vlan.vlan %]">[% row.model %] + href="/device?ip=[% row.ip %]&vlan=[% row.vlan.vlan %]">[% row.os %] + href="/device?ip=[% row.ip %]&vlan=[% row.vlan.vlan %]">[% row.vendor %] [% END %]
Vlan Device Description Model
[% row.vlan.vlan %] [% row.dns %] [% row.vlan.description %] [% row.model %] [% row.os %] [% row.vendor %]