From f47458cb2213662e7830e2db4b9fd543a9a70e3c Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 3 Dec 2012 22:15:44 +0000 Subject: [PATCH] implement contact set by factoring out location set --- Netdisco/lib/Netdisco/PortControl.pm | 39 ++++++++++++++++++---------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/Netdisco/lib/Netdisco/PortControl.pm b/Netdisco/lib/Netdisco/PortControl.pm index bacc55b1..328452b5 100644 --- a/Netdisco/lib/Netdisco/PortControl.pm +++ b/Netdisco/lib/Netdisco/PortControl.pm @@ -5,44 +5,55 @@ use Try::Tiny; sub set_location { my ($self, $job) = @_; + return $self->_set_generic($job->device, 'location', $job->subaction); +} + +sub set_contact { + my ($self, $job) = @_; + return $self->_set_generic($job->device, 'contact', $job->subaction); +} + +sub _set_generic { + my ($self, $ip, $slot, $data) = @_; + $data ||= ''; try { # snmp connect using rw community - my $info = snmp_connect($job->device) + my $info = snmp_connect($ip) or return (); - my $location = ($job->subaction || ''); - my $rv = $info->set_location($location); + my $method = 'set_'. $slot; + my $rv = $info->$method($data); if (!defined $rv) { - my $log = sprintf 'Failed to set location on [%s]: %s', - $job->device, ($info->error || ''); + my $log = sprintf 'Failed to set %s on [%s]: %s', + $slot, $ip, ($info->error || ''); return ('error', $log); } # double check $info->clear_cache; - my $new_location = ($info->location || ''); - if ($new_location ne $location) { - my $log = sprintf 'Failed to update location on [%s] to [%s]', - $job->device, ($location); + my $new_data = ($info->$slot || ''); + if ($new_data ne $data) { + my $log = sprintf 'Failed to update %s on [%s] to [%s]', + $slot, $ip, $data; return ('error', $log); } # get device details from db - my $device = get_device($job->device) + my $device = get_device($ip) or return (); # update netdisco DB - $device->update({location => $location}); + $device->update({$slot => $data}); - my $log = sprintf 'Updated location on [%s] to [%s]', - $job->device, $location; + my $log = sprintf 'Updated %s on [%s] to [%s]', + $slot, $ip, $data; return ('done', $log); } catch { return( 'error', - (sprintf 'Failed to update location on [%s]: %s', $job->device, $_) + (sprintf 'Failed to update %s on [%s]: %s', $slot, $ip, $_) ); }; }