blib/lib/App/Netdisco/Worker/Plugin/PortName.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 87 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package App::Netdisco::Worker::Plugin::PortName;
2  
3 use Dancer ':syntax';
4 use App::Netdisco::Worker::Plugin;
5 use aliased 'App::Netdisco::Worker::Status';
6  
7 use App::Netdisco::Transport::SNMP;
8 use App::Netdisco::Util::Port ':all';
9  
10 register_worker({ phase => 'check' }, sub {
11 my ($job, $workerconf) = @_;
12 my ($device, $port, $data) = map {$job->$_} qw/device port extra/;
13  
14 return Status->error('PortName failed: unable to interpret device param')
15 unless defined $device;
16  
17 return Status->error("PortName skipped: $device not yet discovered")
18 unless $device->in_storage;
19  
20 return Status->error('Missing port (-p).') unless defined $job->port;
21  
22 vars->{'port'} = get_port($device, $port)
23 or return Status->error("Unknown port name [$port] on device $device");
24  
25 return Status->error('Missing name (-e).') unless defined $job->subaction;
26  
27 # the same check the device ports view makes before it offers the name for
28 # editing. the Configuration wiki has portctl_by_role as the only setting
29 # which can refuse a name change: portctl_no and portctl_only always allow
30 # it, and portctl_nameonly and the uplink, phone and WAP guards are about
31 # changing what a port does, not its name
32 return Status->error("Permission denied to change port name")
33 unless port_acl_name(vars->{'port'}, $device, $job->username);
34  
35 return Status->done('PortName is able to run');
36 });
37  
38 register_worker({ phase => 'main', driver => 'snmp' }, sub {
39 my ($job, $workerconf) = @_;
40 my ($device, $pn, $data) = map {$job->$_} qw/device port extra/;
41  
42 # update pseudo devices directly in database
43 unless ($device->is_pseudo()) {
44 # snmp connect using rw community
45 my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
46 or return Status->defer("failed to connect to $device to update alias");
47  
48 my $iid = get_iid($snmp, vars->{'port'})
49 or return Status->error("Failed to get port ID for [$pn] from $device");
50  
51 my $rv = $snmp->set_i_alias($data, $iid);
52  
53 if (!defined $rv) {
54 return Status->error(sprintf 'Failed to set [%s] alias to [%s] on $device: %s',
55 $pn, $data, ($snmp->error || ''));
56 }
57  
58 # confirm the set happened
59 $snmp->clear_cache;
60 my $state = ($snmp->i_alias($iid) || '');
61 if (ref {} ne ref $state or $state->{$iid} ne $data) {
62 return Status->error("Verify of [$pn] alias failed on $device");
63 }
64 }
65  
66 # update netdisco DB
67 vars->{'port'}->update({name => $data});
68  
69 return Status->done("Updated [$pn] alias on [$device] to [$data]");
70 });
71  
72 true;