refactor actions
This commit is contained in:
@@ -20,45 +20,38 @@ sub _set_generic {
|
|||||||
try {
|
try {
|
||||||
# snmp connect using rw community
|
# snmp connect using rw community
|
||||||
my $info = snmp_connect($ip)
|
my $info = snmp_connect($ip)
|
||||||
or return ('error',
|
or return _error("Failed to connect to device [$ip] to update $slot");
|
||||||
sprintf 'Failed to connect to device [%s] to update %s', $ip, $slot);
|
|
||||||
|
|
||||||
my $method = 'set_'. $slot;
|
my $method = 'set_'. $slot;
|
||||||
my $rv = $info->$method($data);
|
my $rv = $info->$method($data);
|
||||||
|
|
||||||
if (!defined $rv) {
|
if (!defined $rv) {
|
||||||
my $log = sprintf 'Failed to set %s on [%s]: %s',
|
return _error(sprintf 'Failed to set %s on [%s]: %s',
|
||||||
$slot, $ip, ($info->error || '');
|
$slot, $ip, ($info->error || ''));
|
||||||
return ('error', $log);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# confirm the set happened
|
# confirm the set happened
|
||||||
$info->clear_cache;
|
$info->clear_cache;
|
||||||
my $new_data = ($info->$slot || '');
|
my $new_data = ($info->$slot || '');
|
||||||
if ($new_data ne $data) {
|
if ($new_data ne $data) {
|
||||||
my $log = sprintf 'Verify of %s update failed on [%s]',
|
return _error("Verify of $slot update failed on [$ip]");
|
||||||
$slot, $ip, $data;
|
|
||||||
return ('error', $log);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# get device details from db
|
# get device details from db
|
||||||
my $device = get_device($ip)
|
my $device = get_device($ip)
|
||||||
or return ('error',
|
or return _error("Updated $slot on [$ip] to [$data] but failed to update DB");
|
||||||
sprintf 'Updated %s on [%s] to [%s] but failed to update database',
|
|
||||||
$slot, $ip, $data);
|
|
||||||
|
|
||||||
# update netdisco DB
|
# update netdisco DB
|
||||||
$device->update({$slot => $data});
|
$device->update({$slot => $data});
|
||||||
|
|
||||||
my $log = sprintf 'Updated %s on [%s] to [%s]',
|
return _done("Updated $slot on [$ip] to [$data]");
|
||||||
$slot, $ip, $data;
|
|
||||||
return ('done', $log);
|
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
return( 'error',
|
return _error("Failed to update $slot on [$ip]: $_");
|
||||||
(sprintf 'Failed to update %s on [%s]: %s', $slot, $ip, $_)
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _done { return ('done', shift) }
|
||||||
|
sub _error { return ('error', shift) }
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|||||||
Reference in New Issue
Block a user