implement contact set by factoring out location set
This commit is contained in:
@@ -5,44 +5,55 @@ use Try::Tiny;
|
|||||||
|
|
||||||
sub set_location {
|
sub set_location {
|
||||||
my ($self, $job) = @_;
|
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 {
|
try {
|
||||||
# snmp connect using rw community
|
# snmp connect using rw community
|
||||||
my $info = snmp_connect($job->device)
|
my $info = snmp_connect($ip)
|
||||||
or return ();
|
or return ();
|
||||||
|
|
||||||
my $location = ($job->subaction || '');
|
my $method = 'set_'. $slot;
|
||||||
my $rv = $info->set_location($location);
|
my $rv = $info->$method($data);
|
||||||
|
|
||||||
if (!defined $rv) {
|
if (!defined $rv) {
|
||||||
my $log = sprintf 'Failed to set location on [%s]: %s',
|
my $log = sprintf 'Failed to set %s on [%s]: %s',
|
||||||
$job->device, ($info->error || '');
|
$slot, $ip, ($info->error || '');
|
||||||
return ('error', $log);
|
return ('error', $log);
|
||||||
}
|
}
|
||||||
|
|
||||||
# double check
|
# double check
|
||||||
$info->clear_cache;
|
$info->clear_cache;
|
||||||
my $new_location = ($info->location || '');
|
my $new_data = ($info->$slot || '');
|
||||||
if ($new_location ne $location) {
|
if ($new_data ne $data) {
|
||||||
my $log = sprintf 'Failed to update location on [%s] to [%s]',
|
my $log = sprintf 'Failed to update %s on [%s] to [%s]',
|
||||||
$job->device, ($location);
|
$slot, $ip, $data;
|
||||||
return ('error', $log);
|
return ('error', $log);
|
||||||
}
|
}
|
||||||
|
|
||||||
# get device details from db
|
# get device details from db
|
||||||
my $device = get_device($job->device)
|
my $device = get_device($ip)
|
||||||
or return ();
|
or return ();
|
||||||
|
|
||||||
# update netdisco DB
|
# update netdisco DB
|
||||||
$device->update({location => $location});
|
$device->update({$slot => $data});
|
||||||
|
|
||||||
my $log = sprintf 'Updated location on [%s] to [%s]',
|
my $log = sprintf 'Updated %s on [%s] to [%s]',
|
||||||
$job->device, $location;
|
$slot, $ip, $data;
|
||||||
return ('done', $log);
|
return ('done', $log);
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
return( 'error',
|
return( 'error',
|
||||||
(sprintf 'Failed to update location on [%s]: %s', $job->device, $_)
|
(sprintf 'Failed to update %s on [%s]: %s', $slot, $ip, $_)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user