From d3afddbb8f2ba6cf75ac0e43806fa955982b5415 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sun, 9 Dec 2012 12:48:07 +0000 Subject: [PATCH] delete duplicated code in new libs --- .../lib/Netdisco/Daemon/Actions/Device.pm | 60 ++----------------- Netdisco/lib/Netdisco/Daemon/Actions/Port.pm | 54 +---------------- Netdisco/lib/Netdisco/Util.pm | 7 +++ 3 files changed, 12 insertions(+), 109 deletions(-) diff --git a/Netdisco/lib/Netdisco/Daemon/Actions/Device.pm b/Netdisco/lib/Netdisco/Daemon/Actions/Device.pm index 3324dbd1..e9939dcf 100644 --- a/Netdisco/lib/Netdisco/Daemon/Actions/Device.pm +++ b/Netdisco/lib/Netdisco/Daemon/Actions/Device.pm @@ -1,68 +1,19 @@ -package Netdisco::Daemon::PortControl::Actions; +package Netdisco::Daemon::Actions::Device; use Netdisco::Util ':port_control'; use Try::Tiny; -sub portcontrol { - my ($self, $job) = @_; - - my $ip = $job->device; - my $pn = $job->port; - (my $dir = $job->subaction) =~ s/-\w+//; - - try { - my $port = get_port($ip, $pn) - or return _error("Unknown port name [$pn] on device [$ip]"); - - my $reconfig_check = port_reconfig_check($port); - return _error("Cannot alter port: $reconfig_check") - if length $reconfig_check; - - # snmp connect using rw community - my $info = snmp_connect($ip) - or return _error("Failed to connect to device [$ip] to control port"); - - my $iid = get_iid($port) - or return _error("Failed to get port ID for [$pn] from [$ip]"); - - my $rv = $info->set_i_up_admin(lc($dir), $iid); - - return _error("Failed to set [$pn] port status to [$dir] on [$ip]") - if !defined $rv; - - # confirm the set happened - $info->clear_cache; - my $state = ($info->i_up_admin($iid) || ''); - if ($state ne $dir) { - return _error("Verify of [$pn] port status failed on [$ip]: $state"); - } - - # get device details from db - my $device = $port->device - or return _error("Updated [$pn] port status on [$ip] but failed to update DB"); - - # update netdisco DB - $device->update({up_admin => $state}); - - return _done("Updated [$pn] port status on [$ip] to [$state]"); - } - catch { - return _error("Failed to update [$pn] port status on [$ip]: $_"); - }; -} - - sub set_location { my ($self, $job) = @_; - return $self->_set_generic($job->device, 'location', $job->subaction); + return $self->_set_device_generic($job->device, 'location', $job->subaction); } sub set_contact { my ($self, $job) = @_; - return $self->_set_generic($job->device, 'contact', $job->subaction); + return $self->_set_device_generic($job->device, 'contact', $job->subaction); } -sub _set_generic { +sub _set_device_generic { my ($self, $ip, $slot, $data) = @_; $data ||= ''; @@ -100,7 +51,4 @@ sub _set_generic { }; } -sub _done { return ('done', shift) } -sub _error { return ('error', shift) } - 1; diff --git a/Netdisco/lib/Netdisco/Daemon/Actions/Port.pm b/Netdisco/lib/Netdisco/Daemon/Actions/Port.pm index 3324dbd1..1585ffe0 100644 --- a/Netdisco/lib/Netdisco/Daemon/Actions/Port.pm +++ b/Netdisco/lib/Netdisco/Daemon/Actions/Port.pm @@ -1,4 +1,4 @@ -package Netdisco::Daemon::PortControl::Actions; +package Netdisco::Daemon::Actions::Port; use Netdisco::Util ':port_control'; use Try::Tiny; @@ -51,56 +51,4 @@ sub portcontrol { }; } - -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($ip) - or return _error("Failed to connect to device [$ip] to update $slot"); - - my $method = 'set_'. $slot; - my $rv = $info->$method($data); - - if (!defined $rv) { - return _error(sprintf 'Failed to set %s on [%s]: %s', - $slot, $ip, ($info->error || '')); - } - - # confirm the set happened - $info->clear_cache; - my $new_data = ($info->$slot || ''); - if ($new_data ne $data) { - return _error("Verify of $slot update failed on [$ip]: $new_data"); - } - - # get device details from db - my $device = get_device($ip) - or return _error("Updated $slot on [$ip] to [$data] but failed to update DB"); - - # update netdisco DB - $device->update({$slot => $data}); - - return _done("Updated $slot on [$ip] to [$data]"); - } - catch { - return _error("Failed to update $slot on [$ip]: $_"); - }; -} - -sub _done { return ('done', shift) } -sub _error { return ('error', shift) } - 1; diff --git a/Netdisco/lib/Netdisco/Util.pm b/Netdisco/lib/Netdisco/Util.pm index a0c3cf2e..cf5cdfe0 100644 --- a/Netdisco/lib/Netdisco/Util.pm +++ b/Netdisco/lib/Netdisco/Util.pm @@ -19,10 +19,12 @@ our @EXPORT_OK = qw/ vlan_reconfig_check port_reconfig_check snmp_connect sort_port + _done _error /; our %EXPORT_TAGS = (port_control => [qw/ get_device get_port snmp_connect port_reconfig_check + _done _error /]); =head1 Netdisco::Util @@ -32,6 +34,11 @@ A set of helper subroutines to support parts of the Netdisco application. There are no default exports, however the C<:port_control> tag will export the C and C subroutines. +=cut + +sub _done { return ('done', shift) } +sub _error { return ('error', shift) } + =head2 is_discoverable( $ip ) Given an IP address, returns C if Netdisco on this host is permitted to