remove need for instance() call

This commit is contained in:
Oliver Gorwits
2017-07-31 17:55:10 +01:00
parent c6d0f1c035
commit 3faee1cf16
6 changed files with 20 additions and 21 deletions

View File

@@ -138,7 +138,7 @@ unless ($action) {
$extra = $class; $extra = $class;
undef $class; undef $class;
} }
my $i = App::Netdisco::Core::Transport::SNMP->instance->reader_for($device, $class); my $i = App::Netdisco::Core::Transport::SNMP->reader_for($device, $class);
Data::Printer::p($i->$extra); Data::Printer::p($i->$extra);
return ('done', sprintf "Showed %s response from %s.", $extra, $device->ip); return ('done', sprintf "Showed %s response from %s.", $extra, $device->ip);
} }

View File

@@ -22,7 +22,7 @@ sub _set_device_generic {
$data ||= ''; $data ||= '';
# snmp connect using rw community # snmp connect using rw community
my $info = App::Netdisco::Core::Transport::SNMP->instance->writer_for($ip) my $info = App::Netdisco::Core::Transport::SNMP->writer_for($ip)
or return job_defer("Failed to connect to device [$ip] to update $slot"); or return job_defer("Failed to connect to device [$ip] to update $slot");
my $method = 'set_'. $slot; my $method = 'set_'. $slot;

View File

@@ -74,7 +74,7 @@ sub _set_port_generic {
if ($device->vendor ne 'netdisco') { if ($device->vendor ne 'netdisco') {
# snmp connect using rw community # snmp connect using rw community
my $info = App::Netdisco::Core::Transport::SNMP->instance->writer_for($ip) my $info = App::Netdisco::Core::Transport::SNMP->writer_for($ip)
or return job_defer("Failed to connect to device [$ip] to control port"); or return job_defer("Failed to connect to device [$ip] to control port");
my $iid = get_iid($info, $port) my $iid = get_iid($info, $port)
@@ -127,7 +127,7 @@ sub power {
$data = 'false' if $data =~ m/^(off|no|down)$/; $data = 'false' if $data =~ m/^(off|no|down)$/;
# snmp connect using rw community # snmp connect using rw community
my $info = App::Netdisco::Core::Transport::SNMP->instance->writer_for($ip) my $info = App::Netdisco::Core::Transport::SNMP->writer_for($ip)
or return job_defer("Failed to connect to device [$ip] to control power"); or return job_defer("Failed to connect to device [$ip] to control power");
my $powerid = get_powerid($info, $port) my $powerid = get_powerid($info, $port)

View File

@@ -63,7 +63,7 @@ sub _single_body {
return job_defer("$job_type deferred: $host is not ${job_type}able"); return job_defer("$job_type deferred: $host is not ${job_type}able");
} }
my $snmp = App::Netdisco::Core::Transport::SNMP->instance->reader_for($device); my $snmp = App::Netdisco::Core::Transport::SNMP->reader_for($device);
if (!defined $snmp) { if (!defined $snmp) {
return job_defer("$job_type failed: could not SNMP connect to $host"); return job_defer("$job_type failed: could not SNMP connect to $host");
} }

View File

@@ -59,7 +59,7 @@ sub discover {
return job_defer("discover deferred: $host is not discoverable"); return job_defer("discover deferred: $host is not discoverable");
} }
my $snmp = App::Netdisco::Core::Transport::SNMP->instance->reader_for($device); my $snmp = App::Netdisco::Core::Transport::SNMP->reader_for($device);
if (!defined $snmp) { if (!defined $snmp) {
return job_defer("discover failed: could not SNMP connect to $host"); return job_defer("discover failed: could not SNMP connect to $host");
} }

View File

@@ -19,9 +19,9 @@ App::Netdisco::Core::Transport::SNMP
=head1 DESCRIPTION =head1 DESCRIPTION
Singleton for SNMP connections. Returns cached L<SNMP::Info> instance for a Singleton for SNMP connections. Returns cached L<SNMP::Info> instance for a
given device IP, or else undef. Prefix calls to this class with: given device IP, or else undef. All methods are class methods, for example:
App::Netdisco::Core::Transport::SNMP->instance() App::Netdisco::Core::Transport::SNMP->reader_for( ... );
=cut =cut
@@ -41,8 +41,8 @@ connected to that device. The IP can be any on the device, and the management
interface will be connected to. interface will be connected to.
If the device is known to Netdisco and there is a cached SNMP community If the device is known to Netdisco and there is a cached SNMP community
string, this will be tried first, and then other community string(s) from the string, that community will be tried first, and then other community strings
application configuration will be tried. from the application configuration will be tried.
If C<$useclass> is provided, it will be used as the L<SNMP::Info> device If C<$useclass> is provided, it will be used as the L<SNMP::Info> device
class instead of the class in the Netdisco database. class instead of the class in the Netdisco database.
@@ -52,18 +52,18 @@ Returns C<undef> if the connection fails.
=cut =cut
sub reader_for { sub reader_for {
my ($self, $ip, $useclass) = @_; my ($class, $ip, $useclass) = @_;
my $device = get_device($ip) or return undef; my $device = get_device($ip) or return undef;
return $self->readers->{$device->ip} my $readers = $class->instance->readers or return undef;
if exists $self->readers->{$device->ip}; return $readers->{$device->ip} if exists $readers->{$device->ip};
debug sprintf 'snmp reader cache warm: [%s]', $device->ip; debug sprintf 'snmp reader cache warm: [%s]', $device->ip;
return ($self->readers->{$device->ip} return ($readers->{$device->ip}
= _snmp_connect_generic('read', $device, $useclass)); = _snmp_connect_generic('read', $device, $useclass));
} }
=head2 writer_for( $ip, $useclass? ) =head1 writer_for( $ip, $useclass? )
Same as C<reader_for> but uses the read-write community string(s) from the Same as C<reader_for> but uses the read-write community strings from the
application configuration file. application configuration file.
Returns C<undef> if the connection fails. Returns C<undef> if the connection fails.
@@ -71,16 +71,15 @@ Returns C<undef> if the connection fails.
=cut =cut
sub writer_for { sub writer_for {
my ($self, $ip, $useclass) = @_; my ($class, $ip, $useclass) = @_;
my $device = get_device($ip) or return undef; my $device = get_device($ip) or return undef;
return $self->writers->{$device->ip} my $writers = $class->instance->writers or return undef;
if exists $self->writers->{$device->ip}; return $writers->{$device->ip} if exists $writers->{$device->ip};
debug sprintf 'snmp writer cache warm: [%s]', $device->ip; debug sprintf 'snmp writer cache warm: [%s]', $device->ip;
return ($self->writers->{$device->ip} return ($writers->{$device->ip}
= _snmp_connect_generic('write', $device, $useclass)); = _snmp_connect_generic('write', $device, $useclass));
} }
sub _snmp_connect_generic { sub _snmp_connect_generic {
my ($mode, $device, $useclass) = @_; my ($mode, $device, $useclass) = @_;
$mode ||= 'read'; $mode ||= 'read';