eradicate checks for dns column in node_ip table - now assumed to be there

This commit is contained in:
Oliver Gorwits
2012-12-01 22:42:17 +00:00
parent 8bf63007dc
commit d14f8db355
6 changed files with 36 additions and 118 deletions

View File

@@ -14,6 +14,8 @@ __PACKAGE__->add_columns(
{ data_type => "macaddr", is_nullable => 0 },
"ip",
{ data_type => "inet", is_nullable => 0 },
"dns",
{ data_type => "text", is_nullable => 1 },
"active",
{ data_type => "boolean", is_nullable => 1 },
"time_first",
@@ -37,43 +39,6 @@ __PACKAGE__->set_primary_key("mac", "ip");
# Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:9+CuvuVWH88WxAf6IBij8g
=head1 GENERAL NOTES
This Result Class for the C<node_ip> table supports sites that have customized
their table to include a C<dns> column, containing a cached DNS record for the
Node at the time of discovery.
Calling the C<dns()> accessor will either return the content of that field if
the field is configured and installed, or else perform a live DNS lookup on
the IP field within the record (returning the first PTR, or undef).
To enable this feature, set the C<HAVE_NODEIP_DNS_COL> environment variable to
a true value. In the Netdisco web app you can activate the column using the
C<have_nodeip_dns_col> application setting, instead.
=cut
__PACKAGE__->add_column("dns" =>
{ data_type => "text", is_nullable => 1, accessor => undef });
# some customize their node_ip table to have a dns column which
# is the cached record at the time of discovery
sub dns {
my $row = shift;
return $row->get_column('dns')
if $row->result_source->has_column('dns');
use Net::DNS ();
my $q = Net::DNS::Resolver->new->query($row->ip);
if ($q) {
foreach my $rr ($q->answer) {
next unless $rr->type eq 'PTR';
return $rr->ptrdname;
}
}
return undef;
}
=head1 RELATIONSHIPS
=head2 oui
@@ -167,8 +132,6 @@ sub ip_aliases {
my ($row, $cond, $attrs) = @_;
my $rs = $row->node_ips({ip => { '!=' => $row->ip }});
$rs = $rs->search_rs({}, {'+columns' => 'dns'})
if $rs->has_dns_col;
return $rs
->search_rs({}, $search_attr)

View File

@@ -4,24 +4,6 @@ use base 'DBIx::Class::ResultSet';
use strict;
use warnings FATAL => 'all';
# some customize their node_ip table to have a dns column which
# is the cached record at the time of discovery
=head1 has_dns_col
Some sites customize their C<node_ip> table to include a C<dns> field which is
the cached record at the time of node discovery.
This method returns True if the C<node_ip> table is configured with a C<dns>
column.
=cut
sub has_dns_col {
my $rs = shift;
return $rs->result_source->has_column('dns');
}
my $search_attr = {
order_by => {'-desc' => 'time_last'},
'+columns' => [
@@ -82,9 +64,6 @@ sub search_by_ip {
}
$cond->{ip} = { $op => $ip };
$rs = $rs->search_rs({}, {'+columns' => 'dns'})
if $rs->has_dns_col;
return $rs
->search_rs({}, $search_attr)
->search($cond, $attrs);
@@ -131,16 +110,12 @@ To limit results only to active IPs, set C<< {active => 1} >> in C<cond>.
sub search_by_dns {
my ($rs, $cond, $attrs) = @_;
die "search_by_dns requires a dns col on the node_ip table.\n"
if not $rs->has_dns_col;
die "dns field required for search_by_dns\n"
if ref {} ne ref $cond or !exists $cond->{dns};
$cond->{dns} = { '-ilike' => delete $cond->{dns} };
return $rs
->search_rs({}, {'+columns' => 'dns'})
->search_rs({}, $search_attr)
->search($cond, $attrs);
}
@@ -184,9 +159,6 @@ sub search_by_mac {
die "mac address required for search_by_mac\n"
if ref {} ne ref $cond or !exists $cond->{mac};
$rs = $rs->search_rs({}, {'+columns' => 'dns'})
if $rs->has_dns_col;
return $rs
->search_rs({}, $search_attr)
->search($cond, $attrs);

View File

@@ -118,33 +118,15 @@ ajax '/ajax/content/search/node' => sub {
->search_by_ip({ip => $ip, @active});
}
else {
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')
->search_by_dns({dns => $node, @active});
if (param('partial')) {
$node = "\%$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')
->search_by_ip({ip => $node, @active});
}
$set = schema('netdisco')->resultset('NodeIp')
->search_by_dns({dns => $node, @active});
}
return unless $set and $set->count;
@@ -263,9 +245,6 @@ get '/search' => sub {
{ id => 'port', label => 'Port' },
]);
var('node_ip_has_dns_col' =>
schema('netdisco')->resultset('NodeIp')->has_dns_col);
template 'search';
};