From ac17c57d8101bb0b87156268c205f94f0adf2aa7 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sun, 15 Jan 2012 14:20:20 +0000 Subject: [PATCH] gracefully handle lack of dns col on node_ip --- Netdisco/lib/Netdisco/DB/Result/NodeIp.pm | 2 +- Netdisco/lib/Netdisco/DB/ResultSet/NodeIp.pm | 4 ++- Netdisco/lib/Netdisco/Web/Search.pm | 34 ++++++++++++++++++-- Netdisco/views/inc/search/node.tt | 2 ++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Netdisco/lib/Netdisco/DB/Result/NodeIp.pm b/Netdisco/lib/Netdisco/DB/Result/NodeIp.pm index fc49c5e4..24291132 100644 --- a/Netdisco/lib/Netdisco/DB/Result/NodeIp.pm +++ b/Netdisco/lib/Netdisco/DB/Result/NodeIp.pm @@ -50,7 +50,7 @@ sub dns { return $row->get_column('dns') if $row->result_source->has_column('dns'); - use Net::DNS; + use Net::DNS (); my $q = Net::DNS::Resolver->new->query($row->ip); if ($q) { foreach my $rr ($q->answer) { diff --git a/Netdisco/lib/Netdisco/DB/ResultSet/NodeIp.pm b/Netdisco/lib/Netdisco/DB/ResultSet/NodeIp.pm index df1f15a3..c3027966 100644 --- a/Netdisco/lib/Netdisco/DB/ResultSet/NodeIp.pm +++ b/Netdisco/lib/Netdisco/DB/ResultSet/NodeIp.pm @@ -47,6 +47,8 @@ sub by_ip { sub by_name { my ($set, $archive, $name) = @_; return $set unless $name; + die "do not call by_name unless you have a dns col on the node_ip table." + if not $set->has_dns_col; return $set->search( { @@ -55,7 +57,7 @@ sub by_name { }, { %$search_attr, - ( $set->has_dns_col ? ('+columns' => 'dns') : () ), + '+columns' => 'dns', } ); } diff --git a/Netdisco/lib/Netdisco/Web/Search.pm b/Netdisco/lib/Netdisco/Web/Search.pm index 8500c3c3..dea5c075 100644 --- a/Netdisco/lib/Netdisco/Web/Search.pm +++ b/Netdisco/lib/Netdisco/Web/Search.pm @@ -7,6 +7,7 @@ use Dancer::Plugin::DBIC; use NetAddr::IP::Lite ':lower'; use Net::MAC (); use List::MoreUtils (); +use Net::DNS (); hook 'before' => sub { # make hash lookups of query lists @@ -94,9 +95,33 @@ ajax '/ajax/content/search/node' => sub { ->by_ip(param('archived'), $ip); } else { - $node = "\%$node\%" if param('partial'); - $set = schema('netdisco')->resultset('NodeIp') - ->by_name(param('archived'), $node); + if (schema('netdisco')->resultset('NodeIp')->has_dns_col) { + if (param('partial')) { + $node = "\%$node\%"; + } + elsif (setting('domain_suffix')) { + $node .= setting('domain_suffix') + if index($node, setting('domain_suffix')) == -1; + } + $set = schema('netdisco')->resultset('NodeIp') + ->by_name(param('archived'), $node); + } + elsif (setting('domain_suffix')) { + $node .= setting('domain_suffix') + if index($node, setting('domain_suffix')) == -1; + my $q = Net::DNS::Resolver->new->query($node); + if ($q) { + foreach my $rr ($q->answer) { + next unless $rr->type eq 'A'; + $node = $rr->address; + } + } + else { + return; + } + $set = schema('netdisco')->resultset('NodeIp') + ->by_ip(param('archived'), $node); + } } return unless $set->count; @@ -205,6 +230,9 @@ get '/search' => sub { { id => 'port', label => 'Port' }, ]); + var('node_ip_has_dns_col' => + schema('netdisco')->resultset('NodeIp')->has_dns_col); + template 'search'; }; diff --git a/Netdisco/views/inc/search/node.tt b/Netdisco/views/inc/search/node.tt index 0b938118..dcb6d857 100644 --- a/Netdisco/views/inc/search/node.tt +++ b/Netdisco/views/inc/search/node.tt @@ -24,6 +24,7 @@ Archived Data a + [% IF vars.node_ip_has_dns_col %]
+ [% END %]