implement port name setting
This commit is contained in:
@@ -8,16 +8,16 @@ use Moo::Role;
|
|||||||
|
|
||||||
sub set_location {
|
sub set_location {
|
||||||
my ($self, $job) = @_;
|
my ($self, $job) = @_;
|
||||||
return $self->_set_device_generic($job->device, 'location', $job->subaction);
|
return _set_device_generic($job->device, 'location', $job->subaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub set_contact {
|
sub set_contact {
|
||||||
my ($self, $job) = @_;
|
my ($self, $job) = @_;
|
||||||
return $self->_set_device_generic($job->device, 'contact', $job->subaction);
|
return _set_device_generic($job->device, 'contact', $job->subaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _set_device_generic {
|
sub _set_device_generic {
|
||||||
my ($self, $ip, $slot, $data) = @_;
|
my ($ip, $slot, $data) = @_;
|
||||||
$data ||= '';
|
$data ||= '';
|
||||||
|
|
||||||
# snmp connect using rw community
|
# snmp connect using rw community
|
||||||
|
|||||||
@@ -1,26 +1,60 @@
|
|||||||
package Netdisco::Daemon::Actions::Port;
|
package Netdisco::Daemon::Actions::Port;
|
||||||
|
|
||||||
use Netdisco::Util::Connect ':all';
|
use Netdisco::Util::Connect ':all';
|
||||||
use Netdisco::Util::Permissions 'port_reconfig_check';
|
use Netdisco::Util::Permissions ':all';
|
||||||
use Netdisco::Daemon::Actions::Util ':all';
|
use Netdisco::Daemon::Actions::Util ':all';
|
||||||
|
|
||||||
use namespace::clean;
|
use namespace::clean;
|
||||||
use Moo::Role;
|
use Moo::Role;
|
||||||
|
|
||||||
|
sub set_portname {
|
||||||
|
my ($self, $job) = @_;
|
||||||
|
return _set_port_generic($job, 'alias', 'name');
|
||||||
|
}
|
||||||
|
|
||||||
sub set_portcontrol {
|
sub set_portcontrol {
|
||||||
my ($self, $job) = @_;
|
my ($self, $job) = @_;
|
||||||
|
|
||||||
my $ip = $job->device;
|
my $port = get_port($job->device, $job->port)
|
||||||
my $pn = $job->port;
|
or return error(sprintf "Unknown port name [%s] on device [%s]",
|
||||||
(my $dir = $job->subaction) =~ s/-\w+//;
|
$job->port, $job->device);
|
||||||
|
|
||||||
my $port = get_port($ip, $pn)
|
|
||||||
or return error("Unknown port name [$pn] on device [$ip]");
|
|
||||||
|
|
||||||
my $reconfig_check = port_reconfig_check($port);
|
my $reconfig_check = port_reconfig_check($port);
|
||||||
return error("Cannot alter port: $reconfig_check")
|
return error("Cannot alter port: $reconfig_check")
|
||||||
if length $reconfig_check;
|
if length $reconfig_check;
|
||||||
|
|
||||||
|
return _set_port_generic($job, 'up_admin');
|
||||||
|
}
|
||||||
|
|
||||||
|
sub set_vlan {
|
||||||
|
my ($self, $job) = @_;
|
||||||
|
|
||||||
|
my $port = get_port($job->device, $job->port)
|
||||||
|
or return error(sprintf "Unknown port name [%s] on device [%s]",
|
||||||
|
$job->port, $job->device);
|
||||||
|
|
||||||
|
my $port_reconfig_check = port_reconfig_check($port);
|
||||||
|
return error("Cannot alter port: $port_reconfig_check")
|
||||||
|
if length $port_reconfig_check;
|
||||||
|
|
||||||
|
my $vlan_reconfig_check = vlan_reconfig_check($port);
|
||||||
|
return error("Cannot alter vlan: $vlan_reconfig_check")
|
||||||
|
if length $vlan_reconfig_check;
|
||||||
|
|
||||||
|
return _set_port_generic($job, 'vlan');
|
||||||
|
}
|
||||||
|
|
||||||
|
sub _set_port_generic {
|
||||||
|
my ($job, $slot, $column) = @_;
|
||||||
|
$column ||= $slot;
|
||||||
|
|
||||||
|
my $ip = $job->device;
|
||||||
|
my $pn = $job->port;
|
||||||
|
(my $data = $job->subaction) =~ s/-\w+//;
|
||||||
|
|
||||||
|
my $port = get_port($ip, $pn)
|
||||||
|
or return error("Unknown port name [$pn] on device [$ip]");
|
||||||
|
|
||||||
# snmp connect using rw community
|
# snmp connect using rw community
|
||||||
my $info = snmp_connect($ip)
|
my $info = snmp_connect($ip)
|
||||||
or return error("Failed to connect to device [$ip] to control port");
|
or return error("Failed to connect to device [$ip] to control port");
|
||||||
@@ -28,22 +62,26 @@ sub set_portcontrol {
|
|||||||
my $iid = get_iid($info, $port)
|
my $iid = get_iid($info, $port)
|
||||||
or return error("Failed to get port ID for [$pn] from [$ip]");
|
or return error("Failed to get port ID for [$pn] from [$ip]");
|
||||||
|
|
||||||
my $rv = $info->set_i_up_admin(lc($dir), $iid);
|
my $method = 'set_i_'. $slot;
|
||||||
|
my $rv = $info->$method($data, $iid);
|
||||||
|
|
||||||
return error("Failed to set [$pn] port status to [$dir] on [$ip]")
|
if (!defined $rv) {
|
||||||
if !defined $rv;
|
return error(sprintf 'Failed to set [%s] %s to [%s] on [%s]: %s',
|
||||||
|
$pn, $slot, $data, $ip, ($info->error || ''));
|
||||||
|
}
|
||||||
|
|
||||||
# confirm the set happened
|
# confirm the set happened
|
||||||
$info->clear_cache;
|
$info->clear_cache;
|
||||||
my $state = ($info->i_up_admin($iid) || '');
|
my $check_method = 'i_'. $slot;
|
||||||
if (ref {} ne ref $state or $state->{$iid} ne $dir) {
|
my $state = ($info->$check_method($iid) || '');
|
||||||
return error("Verify of [$pn] port status failed on [$ip]");
|
if (ref {} ne ref $state or $state->{$iid} ne $data) {
|
||||||
|
return error("Verify of [$pn] $slot failed on [$ip]");
|
||||||
}
|
}
|
||||||
|
|
||||||
# update netdisco DB
|
# update netdisco DB
|
||||||
$port->update({up_admin => $dir});
|
$port->update({$column => $data});
|
||||||
|
|
||||||
return done("Updated [$pn] port status on [$ip] to [$dir]");
|
return done("Updated [$pn] $slot status on [$ip] to [$data]");
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|||||||
Reference in New Issue
Block a user