implement contact set by factoring out location set

This commit is contained in:
Oliver Gorwits
2012-12-03 22:15:44 +00:00
parent 2801d390bd
commit f47458cb22

View File

@@ -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, $_)
);
};
}