blib/lib/App/Netdisco/Worker/Plugin/Discover/Properties/Tags.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 148 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::Discover::Properties::Tags;
2  
3 use Dancer ':syntax';
4 use App::Netdisco::Worker::Plugin;
5 use aliased 'App::Netdisco::Worker::Status';
6  
7 use Dancer::Plugin::DBIC 'schema';
8 use App::Netdisco::Util::Web 'sort_port';
9 use App::Netdisco::Util::Permission 'acl_matches';
10  
11 register_worker({ phase => 'main', title => 'device tags' }, sub {
12 my ($job, $workerconf) = @_;
13 my $device = $job->device;
14 return unless $device->in_storage;
15  
16 return unless setting('tags')
17 and ref {} eq ref setting('tags')
18 and exists setting('tags')->{'device'}
19 and ref {} eq ref setting('tags')->{'device'};
20  
21 my $tags = setting('tags')->{'device'};
22 my @tags_to_set = ();
23  
24 foreach my $tag (sort keys %$tags) {
25 # lhs is tag, rhs matches device
26 next unless acl_matches($device, $tags->{$tag});
27 push @tags_to_set, $tag;
28 }
29  
30 return unless scalar @tags_to_set;
31 $device->update({ tags => \@tags_to_set });
32 debug sprintf ' [%s] properties - set %s tag%s',
33 $device->ip, scalar @tags_to_set, (scalar @tags_to_set > 1);
34 });
35  
36 register_worker({ phase => 'main', title => 'device port tags' }, sub {
37 my ($job, $workerconf) = @_;
38 my $device = $job->device;
39 return unless $device->in_storage;
40  
41 # no ports list means the ports worker did not finish (no uptime, no SNMP),
42 # so which ports match is unknown and stored tags must stand
43 my $ports = vars->{'hook_data'}->{'ports'};
44 return unless ref [] eq ref $ports;
45  
46 # a missing or malformed setting matches nothing, which clears stale tags
47 my $tags = (setting('tags')
48 and ref {} eq ref setting('tags')
49 and ref {} eq ref setting('tags')->{'device_port'})
50 ? setting('tags')->{'device_port'} : {};
51  
52 my %tags_to_set = ();
53 my $port_map = {};
54  
55 # hook data appears after the Properties workers which are in early phase
56 map { push @{ $port_map->{ $_->{port} } }, $_ }
57 @$ports,
58 grep { defined $_->{port} } @{ vars->{'hook_data'}->{'device_ips'} || [] };
59  
60 foreach my $tag (sort keys %$tags) {
61 # lhs is tag, rhs is acl map
62 my $maps = (ref {} eq ref $tags->{$tag}) ? [ $tags->{$tag} ]
63 : ( $tags->{$tag} || [] );
64  
65 foreach my $map (@$maps) {
66 foreach my $key (sort keys %$map) {
67 # lhs matches device, rhs matches port
68 next unless $key and $map->{$key};
69 next unless acl_matches($device, $key);
70  
71 foreach my $port (sort { sort_port($a, $b) } keys %$port_map) {
72 next unless acl_matches($port_map->{$port}, $map->{$key});
73  
74 push @{ $tags_to_set{$port} }, $tag;
75 }
76 }
77 }
78 }
79  
80 foreach my $port (sort keys %tags_to_set) {
81 schema('netdisco')->resultset('DevicePort')
82 ->search({
83 ip => $device->ip, port => $port,
84 # a bare arrayref would be read as a list of alternatives
85 tags => { '!=' => \[ '?', [
86 { dbic_colname => 'tags', sqlt_datatype => 'text[]' } => $tags_to_set{$port} ] ] },
87 }, {for => 'update'})
88 ->update({ tags => ( $tags_to_set{$port} || [] ) });
89  
90 debug sprintf ' [%s] properties - set %s tag%s on port %s',
91 $device->ip, scalar @{ $tags_to_set{$port} },
92 (scalar @{ $tags_to_set{$port} } > 1), $port;
93 }
94  
95 # discover no longer recreates the port rows, so stale tags are cleared here
96 my $cleared = schema('netdisco')->resultset('DevicePort')->search({
97 ip => $device->ip,
98 port => { -not_in => [ sort keys %tags_to_set ] },
99 tags => { '!=' => '{}' },
100 })->update({ tags => '{}' });
101  
102 debug sprintf ' [%s] properties - cleared tags on %d port%s',
103 $device->ip, $cleared, ($cleared != 1) if $cleared and $cleared > 0;
104 });
105  
106 true;