blib/lib/App/Netdisco/Worker/Plugin/Discover/CanonicalIP.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 133 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package App::Netdisco::Worker::Plugin::Discover::CanonicalIP;
2  
3 use Dancer ':syntax';
4 use App::Netdisco::Worker::Plugin;
5 use aliased 'App::Netdisco::Worker::Status';
6  
7 use App::Netdisco::Transport::SNMP ();
8 use App::Netdisco::Util::Permission 'acl_matches';
9 use App::Netdisco::Util::DNS 'ipv4_from_hostname';
10 use App::Netdisco::Util::Device 'is_discoverable';
11 use Dancer::Plugin::DBIC 'schema';
12  
13 register_worker({ phase => 'main', driver => 'snmp' }, sub {
14 my ($job, $workerconf) = @_;
15  
16 my $device = $job->device;
17 return unless $device->in_storage;
18 my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
19 or return Status->defer("discover failed: could not SNMP connect to $device");
20  
21 my $old_ip = $device->ip;
22 my $new_ip = $old_ip;
23 my $revofname = ipv4_from_hostname($snmp->name);
24  
25 if (setting('reverse_sysname') and $revofname) {
26 if (App::Netdisco::Transport::SNMP->test_connection( $new_ip )) {
27 $new_ip = $revofname;
28 }
29 else {
30 debug sprintf ' [%s] device - cannot renumber to %s - SNMP connect failed',
31 $old_ip, $revofname;
32 }
33 }
34  
35 if (scalar @{ setting('device_identity') }) {
36 my @idmaps = @{ setting('device_identity') };
37 my @devips = $device->device_ips->search({ vrf => '' })->order_by('alias')->all;
38  
39 # using ALIASMAP break so that we stop after first successful renumber
40  
41 ALIASMAP: foreach my $map (@idmaps) {
42 next unless ref {} eq ref $map;
43  
44 foreach my $key (sort keys %$map) {
45 # lhs matches device, rhs matches device_ip
46 next unless $key and $map->{$key};
47 next unless acl_matches($device, $key);
48  
49 foreach my $alias (@devips) {
50 next if $alias->alias eq $old_ip;
51 next unless acl_matches($alias, $map->{$key});
52  
53 if (not is_discoverable( $alias->alias )) {
54 debug sprintf ' [%s] device - cannot renumber to %s - not discoverable',
55 $old_ip, $alias->alias;
56 next;
57 }
58  
59 if (App::Netdisco::Transport::SNMP->test_connection( $alias->alias )) {
60 $new_ip = $alias->alias;
61 last ALIASMAP;
62 }
63 else {
64 debug sprintf ' [%s] device - cannot renumber to %s - SNMP connect failed',
65 $old_ip, $alias->alias;
66 }
67 }
68 }
69 }
70 }
71  
72 return if $new_ip eq $old_ip;
73  
74 schema('netdisco')->txn_do(sub {
75 my $existing = schema('netdisco')->resultset('Device')->search({
76 ip => $new_ip, vendor => $device->vendor, serial => $device->serial,
77 });
78  
79 if (vars->{'new_device'} and $existing->count) {
80 $device->delete;
81 return $job->cancel(
82 " [$old_ip] device - cancelling fresh discover: already known as $new_ip");
83 }
84  
85 # discover existing device but change IP, need to remove existing device
86 $existing->delete;
87  
88 # if target device exists then this will die
89 $device->renumber($new_ip)
90 or die "cannot renumber to: $new_ip"; # rollback
91  
92 # is not done in renumber, but required, otherwise confusing at job end!
93 schema('netdisco')->resultset('Admin')
94 ->search({job => $job->id})->update({device => $new_ip}) if $job->id;
95  
96 return Status->info(sprintf ' [%s] device - changed IP to %s (%s)',
97 $old_ip, $device->ip, ($device->dns || ''));
98 });
99 });
100  
101 true;