From 3ec136aea3cba96e8b0985929e6757c79f161da0 Mon Sep 17 00:00:00 2001 From: nick n <39005454+inphobia@users.noreply.github.com> Date: Thu, 2 May 2019 14:19:46 +0200 Subject: [PATCH] allow setting some speudo device info (#572) * allow port name to be changed on pseudo devices, was once added in commit 1f020e07ee0da9bb3a6deacbf690f0ec515d0e49 * also allow this for location and contact --- lib/App/Netdisco/Worker/Plugin/Contact.pm | 29 ++++++++++--------- lib/App/Netdisco/Worker/Plugin/Location.pm | 29 ++++++++++--------- lib/App/Netdisco/Worker/Plugin/PortName.pm | 33 ++++++++++++---------- 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/lib/App/Netdisco/Worker/Plugin/Contact.pm b/lib/App/Netdisco/Worker/Plugin/Contact.pm index 6e059cbf..566aa0c4 100644 --- a/lib/App/Netdisco/Worker/Plugin/Contact.pm +++ b/lib/App/Netdisco/Worker/Plugin/Contact.pm @@ -16,22 +16,25 @@ register_worker({ phase => 'main', driver => 'snmp' }, sub { my ($job, $workerconf) = @_; my ($device, $data) = map {$job->$_} qw/device extra/; - # snmp connect using rw community - my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) - or return Status->defer("failed to connect to $device to update contact"); + # update pseudo devices directly in database + unless ($device->is_pseudo()) { + # snmp connect using rw community + my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) + or return Status->defer("failed to connect to $device to update contact"); - my $rv = $snmp->set_contact($data); + my $rv = $snmp->set_contact($data); - if (!defined $rv) { - return Status->error( - "failed to set contact on $device: ". ($snmp->error || '')); - } + if (!defined $rv) { + return Status->error( + "failed to set contact on $device: ". ($snmp->error || '')); + } - # confirm the set happened - $snmp->clear_cache; - my $new_data = ($snmp->contact || ''); - if ($new_data ne $data) { - return Status->error("verify of contact failed on $device: $new_data"); + # confirm the set happened + $snmp->clear_cache; + my $new_data = ($snmp->contact || ''); + if ($new_data ne $data) { + return Status->error("verify of contact failed on $device: $new_data"); + } } # update netdisco DB diff --git a/lib/App/Netdisco/Worker/Plugin/Location.pm b/lib/App/Netdisco/Worker/Plugin/Location.pm index fbcf3c08..d4882e33 100644 --- a/lib/App/Netdisco/Worker/Plugin/Location.pm +++ b/lib/App/Netdisco/Worker/Plugin/Location.pm @@ -16,22 +16,25 @@ register_worker({ phase => 'main', driver => 'snmp' }, sub { my ($job, $workerconf) = @_; my ($device, $data) = map {$job->$_} qw/device extra/; - # snmp connect using rw community - my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) - or return Status->defer("failed to connect to $device to update location"); + # update pseudo devices directly in database + unless ($device->is_pseudo()) { + # snmp connect using rw community + my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) + or return Status->defer("failed to connect to $device to update location"); - my $rv = $snmp->set_location($data); + my $rv = $snmp->set_location($data); - if (!defined $rv) { - return Status->error( - "failed to set location on $device: ". ($snmp->error || '')); - } + if (!defined $rv) { + return Status->error( + "failed to set location on $device: ". ($snmp->error || '')); + } - # confirm the set happened - $snmp->clear_cache; - my $new_data = ($snmp->location || ''); - if ($new_data ne $data) { - return Status->error("verify of location failed on $device: $new_data"); + # confirm the set happened + $snmp->clear_cache; + my $new_data = ($snmp->location || ''); + if ($new_data ne $data) { + return Status->error("verify of location failed on $device: $new_data"); + } } # update netdisco DB diff --git a/lib/App/Netdisco/Worker/Plugin/PortName.pm b/lib/App/Netdisco/Worker/Plugin/PortName.pm index f5bfdb2a..1b8ae52e 100644 --- a/lib/App/Netdisco/Worker/Plugin/PortName.pm +++ b/lib/App/Netdisco/Worker/Plugin/PortName.pm @@ -24,25 +24,28 @@ register_worker({ phase => 'main', driver => 'snmp' }, sub { my ($job, $workerconf) = @_; my ($device, $pn, $data) = map {$job->$_} qw/device port extra/; - # snmp connect using rw community - my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) - or return Status->defer("failed to connect to $device to update alias"); + # update pseudo devices directly in database + unless ($device->is_pseudo()) { + # snmp connect using rw community + my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) + or return Status->defer("failed to connect to $device to update alias"); - my $iid = get_iid($snmp, vars->{'port'}) - or return Status->error("Failed to get port ID for [$pn] from $device"); + my $iid = get_iid($snmp, vars->{'port'}) + or return Status->error("Failed to get port ID for [$pn] from $device"); - my $rv = $snmp->set_i_alias($data, $iid); + my $rv = $snmp->set_i_alias($data, $iid); - if (!defined $rv) { - return Status->error(sprintf 'Failed to set [%s] alias to [%s] on $device: %s', - $pn, $data, ($snmp->error || '')); - } + if (!defined $rv) { + return Status->error(sprintf 'Failed to set [%s] alias to [%s] on $device: %s', + $pn, $data, ($snmp->error || '')); + } - # confirm the set happened - $snmp->clear_cache; - my $state = ($snmp->i_alias($iid) || ''); - if (ref {} ne ref $state or $state->{$iid} ne $data) { - return Status->error("Verify of [$pn] alias failed on $device"); + # confirm the set happened + $snmp->clear_cache; + my $state = ($snmp->i_alias($iid) || ''); + if (ref {} ne ref $state or $state->{$iid} ne $data) { + return Status->error("Verify of [$pn] alias failed on $device"); + } } # update netdisco DB