From 9ebd1d1b7d43cf1840519df93e38bb592bc886e3 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Tue, 4 Dec 2012 22:50:49 +0000 Subject: [PATCH] refactor actions --- Netdisco/lib/Netdisco/PortControl/Actions.pm | 27 ++++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/Netdisco/lib/Netdisco/PortControl/Actions.pm b/Netdisco/lib/Netdisco/PortControl/Actions.pm index c6e899c9..b391ce3d 100644 --- a/Netdisco/lib/Netdisco/PortControl/Actions.pm +++ b/Netdisco/lib/Netdisco/PortControl/Actions.pm @@ -20,45 +20,38 @@ sub _set_generic { try { # snmp connect using rw community my $info = snmp_connect($ip) - or return ('error', - sprintf 'Failed to connect to device [%s] to update %s', $ip, $slot); + or return _error("Failed to connect to device [$ip] to update $slot"); my $method = 'set_'. $slot; my $rv = $info->$method($data); if (!defined $rv) { - my $log = sprintf 'Failed to set %s on [%s]: %s', - $slot, $ip, ($info->error || ''); - return ('error', $log); + 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) { - my $log = sprintf 'Verify of %s update failed on [%s]', - $slot, $ip, $data; - return ('error', $log); + return _error("Verify of $slot update failed on [$ip]"); } # get device details from db my $device = get_device($ip) - or return ('error', - sprintf 'Updated %s on [%s] to [%s] but failed to update database', - $slot, $ip, $data); + or return _error("Updated $slot on [$ip] to [$data] but failed to update DB"); # update netdisco DB $device->update({$slot => $data}); - my $log = sprintf 'Updated %s on [%s] to [%s]', - $slot, $ip, $data; - return ('done', $log); + return _done("Updated $slot on [$ip] to [$data]"); } catch { - return( 'error', - (sprintf 'Failed to update %s on [%s]: %s', $slot, $ip, $_) - ); + return _error("Failed to update $slot on [$ip]: $_"); }; } +sub _done { return ('done', shift) } +sub _error { return ('error', shift) } + 1;