do renumber hostname lookup inside core

This commit is contained in:
Oliver Gorwits
2015-04-27 16:48:11 +01:00
parent 39174c7e39
commit 469d8ed819
3 changed files with 31 additions and 25 deletions

View File

@@ -85,9 +85,6 @@ sub set_canonical_ip {
$device->renumber($new_ip) $device->renumber($new_ip)
or die "cannot renumber to: $new_ip"; # rollback or die "cannot renumber to: $new_ip"; # rollback
my $hostname = hostname_from_ip($device->ip);
$device->update({dns => $hostname});
debug sprintf ' [%s] device - changed IP to %s (%s)', debug sprintf ' [%s] device - changed IP to %s (%s)',
$old_ip, $device->ip, ($device->dns || ''); $old_ip, $device->ip, ($device->dns || '');
}); });

View File

@@ -6,7 +6,9 @@ package App::Netdisco::DB::Result::Device;
use strict; use strict;
use warnings; use warnings;
use NetAddr::IP::Lite ':lower'; use NetAddr::IP::Lite ':lower';
use App::Netdisco::Util::DNS 'hostname_from_ip';
use base 'DBIx::Class::Core'; use base 'DBIx::Class::Core';
__PACKAGE__->table("device"); __PACKAGE__->table("device");
@@ -193,7 +195,7 @@ __PACKAGE__->might_have(
Will update this device and all related database records to use the new IP Will update this device and all related database records to use the new IP
C<$new_ip>. Returns C<undef> if $new_ip seems invalid, otherwise returns the C<$new_ip>. Returns C<undef> if $new_ip seems invalid, otherwise returns the
Device row object. Does NOT update the device C<dns> field. Device row object.
=cut =cut
@@ -201,9 +203,16 @@ sub renumber {
my ($device, $ip) = @_; my ($device, $ip) = @_;
my $schema = $device->result_source->schema; my $schema = $device->result_source->schema;
my $new_ip = NetAddr::IP::Lite->new($ip) my $new_addr = NetAddr::IP::Lite->new($ip)
or return; or return;
my $old_ip = $device->ip;
my $new_ip = $new_addr->addr;
return
if $new_ip eq '0.0.0.0'
or $new_ip eq '127.0.0.1';
foreach my $set (qw/ foreach my $set (qw/
DeviceIp DeviceIp
DeviceModule DeviceModule
@@ -218,34 +227,38 @@ sub renumber {
Community Community
/) { /) {
$schema->resultset($set) $schema->resultset($set)
->search({ip => $device->ip}) ->search({ip => $old_ip})
->update({ip => $new_ip->addr}); ->update({ip => $new_ip});
} }
$schema->resultset('DevicePort') $schema->resultset('DevicePort')
->search({remote_ip => $device->ip}) ->search({remote_ip => $old_ip})
->update({remote_ip => $new_ip->addr}); ->update({remote_ip => $new_ip});
$schema->resultset('DeviceIp') $schema->resultset('DeviceIp')
->search({alias => $device->ip}) ->search({alias => $old_ip})
->update({alias => $new_ip->addr}); ->update({alias => $new_ip});
$schema->resultset('Admin') $schema->resultset('Admin')
->search({device => $device->ip}) ->search({device => $old_ip})
->update({device => $new_ip->addr}); ->update({device => $new_ip});
$schema->resultset('Node') $schema->resultset('Node')
->search({switch => $device->ip}) ->search({switch => $old_ip})
->update({switch => $new_ip->addr}); ->update({switch => $new_ip});
$schema->resultset('Topology') $schema->resultset('Topology')
->search({dev1 => $device->ip}) ->search({dev1 => $old_ip})
->update({dev1 => $new_ip->addr}); ->update({dev1 => $new_ip});
$schema->resultset('Topology')
->search({dev2 => $device->ip})
->update({dev2 => $new_ip->addr});
$device->update({ip => $new_ip->addr}); $schema->resultset('Topology')
->search({dev2 => $old_ip})
->update({dev2 => $new_ip});
$device->update({
ip => $new_ip,
dns => hostname_from_ip($new_ip),
});
return $device; return $device;
} }

View File

@@ -3,7 +3,6 @@ package App::Netdisco::Util::Device;
use Dancer qw/:syntax :script/; use Dancer qw/:syntax :script/;
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
use App::Netdisco::Util::Permission 'check_acl'; use App::Netdisco::Util::Permission 'check_acl';
use App::Netdisco::Util::DNS 'hostname_from_ip';
use base 'Exporter'; use base 'Exporter';
our @EXPORT = (); our @EXPORT = ();
@@ -126,9 +125,6 @@ sub renumber_device {
event => "Renumbered device from $ip to $new_ip", event => "Renumbered device from $ip to $new_ip",
}); });
my $hostname = hostname_from_ip($device->ip);
$device->update({dns => $hostname});
$happy = 1; $happy = 1;
}); });