blib/lib/RackMan/Tasks.pm

filter: Statementstmt 13.0% 22 / 169 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 46.6% 7 / 15 Podpod 100% 6 / 6 totaltotal 10.8% 35 / 322 CC 62 SCAR 50.2
CC
62
Cov
8%
task_list
306.0
task_info
240.0
task_write
132.0
CRAP 151.1
« bin/cisco-status ↑ Summary blib/lib/RackMan/Device/PDU/APC_RackPDU.pm »
1 package RackMan::Tasks;
2  
3 1 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use RackMan;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use RackMan::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util qw< looks_like_number >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Term::ANSIColor qw< BOLD RED >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 #
13 # task_list()
14 # ---------
15 0 ▶sub task_list {
Subroutine task_list: not called CC=17 SCAR=57.2
Pod documented
16 0 my ($class, $opts) = @_;
17  
18 print STDERR "usage: $::COMMAND list <pdu | server | switch | all>\n"
19 0 ▶ and return if $opts->{stdout} and not defined $opts->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"stdout"} and not defined $opts->{"type"} 0 0 0
Condition0%0/8
$opts->{'stdout'} && !defined($opts->{'type'}) && (print(STDERR "usage: $main::COMMAND list <pdu | server | switch | all>\n") && (return))
!ll&&!rl&&r
000
$opts->{"stdout"} and not defined $opts->{"type"}
!ll&&!rl&&r
000
print STDERR "usage: $main::COMMAND list <pdu | server | switch | all>\n" and return
!ll
00
MC/DC0%0/3
$opts->{'stdout'} && !defined($opts->{'type'}) && (print(STDERR "usage: $main::COMMAND list <pdu | server | switch | all>\n") && (return))
$opts->{"stdout"} not defined $opts->{"type"} print STDERR "usage: $main::COMMAND list <pdu | server | switch | all>\n"
Truth table0%0/4
A and not(B) && C
A= $opts->{"stdout"}B= not defined $opts->{"type"}C= print STDERR "usage: $main::COMMAND list <pdu | server | switch | all>\n"
ABCresult
0XX0
10X0
1100
1111
20  
21 # the object connected to the RackTables database
22 0 my $rackman = $opts->{rackman};
23 0 my $racktables = $rackman->racktables;
24  
25 0 my $req_type = lc $opts->{type};
26 0 my %conds;
27  
28 0 ▶ if ($req_type ne "all") {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_type ne "all") 0 0 0
29 # determine the type of objects to look
30 0 $req_type = RackMan::Types->to_racktables($req_type);
31  
32 # construct the additional WHERE conditions
33 0 %conds = ( dict_value => $req_type );
34 }
35  
36 # create the resultset
37 0 my $list = $racktables->resultset("RackObject")->search(
38 { chapter_id => 1, %conds },
39 { join => "dictionary", order_by => { -asc => "name" } },
40 );
41  
42 0 my $r = 1;
43 0 my @objects;
44 0 ▶ $opts->{as} ||= "simple";
Condition0%0/2
$opts->{"as"} ||= "simple"
l!l
00
MC/DC0%0/1
$opts->{"as"} ||= "simple"
$opts->{"as"}
Truth table0%0/2
A
A= $opts->{"as"}
Aresult
11
00
45  
46 # fetch and print the corresponding objects
47 0 ▶ while (my $rackobject = $list->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rackobject = $list->next) 0 0 0
48 0 my $id = $rackobject->id;
49 0 my $name = $rackobject->name;
50 0 my $type = $rackobject->dictionary->dict_value;
51 0 ▶ not defined and $_ = "(null)" for $name, $type;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or $_ = "(null)" 0 0 0
52  
53 # direct output on screen
54 0 ▶ if ($opts->{stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'stdout'}) { } 0 0 0
55 0 my $rackdev = $rackman->device_by_id($id);
56  
57 0 ▶ if ($opts->{as} eq "ldif") {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'as'} eq 'ldif') { } 0 0 0
58 0 require RackMan::Format::LDAP;
59 0 my $ldif = eval { RackMan::Format::LDAP::make_ldif($rackdev) };
60 0 ▶ $r &&= print $ldif, $/ if $ldif;
Branch: 1 branch
BranchTrueFalseTotal
if $ldif 0 0 0
Condition0%0/3
$r &&= print($ldif, $/)
!ll&&!rl&&r
000
MC/DC0%0/2
$r &&= print($ldif, $/)
$r print($ldif, $/)
Truth table0%0/3
A &&= B
A= $rB= print($ldif, $/)
ABresult
0X0
100
111
61 }
62 else {
63 $r &&= printf "%s (%d), %s, use:%s\n", $name, $id, $type,
64 0 ▶ eval { $rackdev->attributes->{Use} } || "(null)";
Condition0%0/5
$r &&= printf("%s (%d), %s, use:%s\n", $name, $id, $type, eval { do { $rackdev->attributes->{"Use"} } } || "(null)")
!ll&&!rl&&r
000
eval { do { $rackdev->attributes->{'Use'} } } || '(null)'
l!l
00
MC/DC0%0/2
$r &&= printf("%s (%d), %s, use:%s\n", $name, $id, $type, eval { do { $rackdev->attributes->{"Use"} } } || "(null)")
$r printf("%s (%d), %s, use:%s\n", $name, $id, $type, eval { do { $rackdev->attributes->{"Use"} } } || "(null)")
MC/DC0%0/1
eval { do { $rackdev->attributes->{'Use'} } } || '(null)'
eval { do { $rackdev->attributes->{'Use'} } }
Truth table0%0/3
A &&= B
A= $rB= printf("%s (%d), %s, use:%s\n", $name, $id, $type, eval { do { $rackdev->attributes->{"Use"} } } || "(null)")
ABresult
0X0
100
111
Truth table0%0/2
A
A= eval { do { $rackdev->attributes->{'Use'} } }
Aresult
11
00
65 }
66 }
67 # don't print, return the data
68 else {
69 0 push @objects, { id => $id, name => $name, type => $type };
70 }
71 }
72  
73 0 ▶ if ($opts->{stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'stdout'}) { } 0 0 0
74 0 return $r
75 }
76 else {
77 0 ▶ return wantarray ? @objects : \@objects
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
78 }
79 }
80  
81  
82 #
83 # task_info()
84 # ---------
85 0 ▶sub task_info {
Subroutine task_info: not called CC=15 SCAR=54.8
Pod documented
86 0 my ($self, $opts) = @_;
87  
88 0 print "Name: ", $self->object_name, " (", $self->object_id, ")", $/,
89 "Type: ", $self->object_type, $/;
90 0 ▶ print "Asset tag: ", $self->object_asset_no, $/ if $self->object_asset_no;
Branch: 1 branch
BranchTrueFalseTotal
if $self->object_asset_no 0 0 0
91 0 print $/;
92  
93 0 print "Attributes\n----------\n";
94 0 my $attr = $self->attributes;
95 0 print " $_: $attr->{$_}\n" for sort keys %$attr;
96 0 print $/;
97  
98 0 print "Tags\n----\n";
99 0 print " explicit tags: ", join(", ", @{ $self->explicit_tags }), $/;
100 0 print " implicit tags: ", join(", ", @{ $self->implicit_tags }), $/;
101 0 print $/;
102  
103 0 print "Location\n--------\n";
104  
105 0 ▶ if (my ($parent_id) = @{ $self->parents }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($parent_id) = @{$self->parents;}) 0 0 0
106 0 my $parent = $self->rackman->device_by_id($parent_id);
107 0 print " host: ", $parent->object_name, $/;
108 }
109  
110 print " rack: ", $self->rack->{name}, $/,
111 0 " site: ", $self->rack->{row_name}, $/;
112 0 print $/;
113  
114 0 ▶ print "Comment\n-------\n", $self->object_comment, $/, $/
Branch: 1 branch
BranchTrueFalseTotal
if $self->object_comment 0 0 0
115 if $self->object_comment;
116  
117 # choose the appropriate sort function for the ports
118 0 ▶ if ($self->object_type eq "Switch") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->object_type eq 'Switch') { } 0 0 0
119 0 ▶ *by_port_name = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
120 0 my @a = ( (split "/", $a->{name}), 0, 0);
121 0 my @b = ( (split "/", $b->{name}), 0, 0);
122 0 ▶ lc $a[0] cmp lc $b[0] || $a[1] <=> $b[1] || $a[2] <=> $b[2]
Branch: 1 branch
BranchTrueFalseTotal
lc $a[0] cmp lc $b[0] or $a[1] <=> $b[1] or $a[2] <=> $b[2] 0 0 0
Condition0%0/3
lc $a[0] cmp lc $b[0] or $a[1] <=> $b[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
lc $a[0] cmp lc $b[0] or $a[1] <=> $b[1]
lc $a[0] cmp lc $b[0] $a[1] <=> $b[1]
Truth table0%0/3
A or B
A= lc $a[0] cmp lc $b[0]B= $a[1] <=> $b[1]
ABresult
1X1
011
000
123 }
124 0 } else {
125 0 ▶ *by_port_name = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
126 my @a = looks_like_number($a->{name})
127 0 ▶ ? ("", $a->{name}) : ($a->{name}, "");
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($RackMan::Tasks::a->{'name'}) ? : 0 0 0
128 my @b = looks_like_number($b->{name})
129 0 ▶ ? ("", $b->{name}) : ($b->{name}, "");
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($RackMan::Tasks::b->{'name'}) ? : 0 0 0
130 0 ▶ lc $a[0] cmp lc $b[0] || $a[1] <=> $b[1]
Branch: 1 branch
BranchTrueFalseTotal
lc $a[0] cmp lc $b[0] or $a[1] <=> $b[1] 0 0 0
131 }
132 0 }
133  
134 0 ▶ if (@{ $self->ports }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->ports;}) 0 0 0
135 0 print "Ports\n-----\n";
136 printf " [%s] --(%s)--> %s [%s]\n",
137 $_->{name}, $_->{oif_name},
138 $_->{peer_object_name}||"", $_->{peer_port_name}||""
139 0 ▶ for sort by_port_name @{ $self->ports };
Condition0%0/4
$_->{'peer_object_name'} || ''
l!l
00
$_->{'peer_port_name'} || ''
l!l
00
MC/DC0%0/1
$_->{'peer_object_name'} || ''
$_->{'peer_object_name'}
MC/DC0%0/1
$_->{'peer_port_name'} || ''
$_->{'peer_port_name'}
Truth table0%0/2
A
A= $_->{'peer_object_name'}
Aresult
11
00
Truth table0%0/2
A
A= $_->{'peer_port_name'}
Aresult
11
00
140 0 print $/;
141 }
142  
143 0 ▶ if ($self->has_ipv4addrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_ipv4addrs) 0 0 0
144 0 print "IPv4 addresses\n--------------\n";
145  
146 0 for my $addr (@{ $self->ipv4addrs }) {
147 0 print " $addr->{type} | $addr->{iface} | $addr->{addr}\n"
148 }
149  
150 0 my $gateway = $self->default_ipv4_gateway->{addr};
151 0 ▶ print " * default gateway: $gateway\n" if defined $gateway;
Branch: 1 branch
BranchTrueFalseTotal
if defined $gateway 0 0 0
152 }
153  
154 0 ▶ if ($self->has_ipv6addrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_ipv6addrs) 0 0 0
155 0 print "IPv6 addresses\n--------------\n";
156  
157 0 for my $addr (@{ $self->ipv6addrs }) {
158 0 print " $addr->{type} | $addr->{iface} | $addr->{addr}\n"
159 }
160 }
161  
162 0 print $/;
163 }
164  
165  
166 #
167 # task_dump()
168 # ---------
169 0 ▶sub task_dump {
Subroutine task_dump: not called CC=9 SCAR=45.0
Pod documented
170 0 my ($self, $opts) = @_;
171  
172 # extract the meaningful parts of the object
173 0 my %struct;
174 0 my @attrs = grep { !/^rack/ } map { $_->name }
175 $self->meta->get_all_attributes;
176 0 push @attrs, qw<
177 object_id object_name object_asset_no
178 object_comment object_has_problem rack
179 >;
180  
181 # .. and copy over its attributes
182 0 for my $attr (@attrs) {
183 0 $struct{$attr} = $self->$attr;
184 }
185  
186 0 ▶ $opts->{as} ||= "yaml";
Condition0%0/2
$opts->{"as"} ||= "yaml"
l!l
00
MC/DC0%0/1
$opts->{"as"} ||= "yaml"
$opts->{"as"}
Truth table0%0/2
A
A= $opts->{"as"}
Aresult
11
00
187 0 my $dump;
188  
189 0 ▶ if ($opts->{as} eq "ldif") {
Branch: 3 branches
BranchTrueFalseTotal
if ($opts->{'as'} eq 'ldif') { } 0 0 0
elsif ($opts->{'as'} eq 'json') { } 0 0 0
elsif ($opts->{'as'} eq 'perl') { } 0 0 0
190 0 require RackMan::Format::LDAP;
191 0 $dump = RackMan::Format::LDAP::make_ldif($self);
192 }
193 elsif ($opts->{as} eq "json") {
194 0 require JSON;
195 0 $dump = JSON::encode_json(\%struct);
196 }
197 elsif ($opts->{as} eq "perl") {
198 0 $dump = \%struct
199 }
200 else {
201 0 require YAML;
202 0 $dump = YAML::Dump(\%struct);
203 }
204  
205 0 ▶ if ($opts->{stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'stdout'}) { } 0 0 0
206 # stringify Perl structure
207 0 ▶ if (ref $dump) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dump) 0 0 0
208 0 require Data::Dumper;
209 0 my $dumper = Data::Dumper->new([$dump]);
210 0 $dumper->Indent(1)->Sortkeys(1)->Terse(1);
211 0 $dump = $dumper->Dump;
212 }
213  
214 # add a newline if output is missing one (typically for JSON)
215 0 ▶ $dump .= "\n" unless $dump =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dump =~ /\n$/ 0 0 0
216  
217 0 return print $dump
218 }
219 else {
220 0 return $dump
221 }
222 }
223  
224  
225 #
226 # task_write()
227 # ----------
228 0 ▶sub task_write {
Subroutine task_write: not called CC=11 SCAR=48.8
Pod documented
229 0 my ($self, $opts) = @_;
230  
231 0 my $verbose = $opts->{rackman}->options->{verbose};
232  
233 0 ▶ if ($self->class and not $opts->{rackman}->options->{no_write_dev_cfg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class and not $opts->{"rackman"}->options->{"no_write_dev_cfg"}) 0 0 0
Condition0%0/3
$self->class and not $opts->{"rackman"}->options->{"no_write_dev_cfg"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->class and not $opts->{"rackman"}->options->{"no_write_dev_cfg"}
$self->class not $opts->{"rackman"}->options->{"no_write_dev_cfg"}
Truth table0%0/3
A and not(B)
A= $self->classB= not $opts->{"rackman"}->options->{"no_write_dev_cfg"}
ABresult
0X0
100
111
234 # invoke the write_config() method, which should be provided by
235 # the appropriate device role (RackMan::Device::*)
236 0 my ($type) = $self->class =~ /::(\w+)$/;
237 0 ▶ print "- ", BOLD($type), "\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
238 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->write_config({"rackman", $opts->{"rackman"}, "verbose", $verbose}); 1 } } or warn $@ 0 0 0
239 $self->write_config({
240 rackman => $opts->{rackman},
241 0 verbose => $verbose,
242 0 }); 1
243 } or warn $@;
244 }
245  
246 # invoke the write() method from each format that the device
247 # role has declared to be relevent
248 0 for my $format ($self->formats) {
249 0 ▶ print "- ", BOLD($format) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
250  
251 # load the module
252 0 my $module = "RackMan::Format::$format";
253 0 ▶ if (not eval { Module::Runtime::require_module($module) }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { &Module::Runtime::require_module($module) } }) 0 0 0
254 0 ▶ print BOLD(RED(" -- failed")), "\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
255 0 (my $error = $@) =~ s/INC \(.+$/INC/sm;
256 0 RackMan->warning("can't load $module: $error");
257 next
258 0 }
259  
260 0 ▶ print $/ if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
261  
262 # execute its write() method
263 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $module->write({"rackdev", $self, "rackman", $opts->{"rackman"}, "verbose", $verbose}); 1 } } or warn $@ 0 0 0
264 $module->write({
265 rackdev => $self,
266 rackman => $opts->{rackman},
267 0 verbose => $verbose,
268 0 }); 1
269 } or warn $@;
270 }
271 }
272  
273  
274 #
275 # task_diff()
276 # ---------
277 0 ▶sub task_diff {
Subroutine task_diff: not called CC=5 SCAR=34.0
Pod documented
278 0 my ($self, $opts) = @_;
279  
280 0 my $verbose = $opts->{verbose} = $opts->{rackman}->options->{verbose};
281  
282 0 ▶ RackMan->error("object '", $self->object_name, "' has no hardware trait")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->class 0 0 0
283 unless $self->class;
284 0 ▶ RackMan->error("hardware trait '", $self->class, "' has no support for ",
Branch: 1 branch
BranchTrueFalseTotal
unless $self->class->can("diff_config") 0 0 0
285 "the 'diff' task") unless $self->class->can("diff_config");
286  
287 0 my ($type) = $self->class =~ /::(\w+)$/;
288 0 ▶ print "- ", BOLD($type), "\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
289 0 ▶ eval { $self->diff_config($opts); 1 } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->diff_config($opts); 1 } } or warn $@ 0 0 0
290 }
291  
292  
293 #
294 # task_push()
295 # ---------
296 0 ▶sub task_push {
Subroutine task_push: not called CC=5 SCAR=34.0
Pod documented
297 0 my ($self, $opts) = @_;
298  
299 0 my $verbose = $opts->{verbose} = $opts->{rackman}->options->{verbose};
300  
301 0 ▶ RackMan->error("object '", $self->object_name, "' has no hardware trait")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->class 0 0 0
302 unless $self->class;
303 0 ▶ RackMan->error("hardware trait '", $self->class, "' has no support for ",
Branch: 1 branch
BranchTrueFalseTotal
unless $self->class->can("diff_config") 0 0 0
304 "the 'diff' task") unless $self->class->can("diff_config");
305  
306 0 my ($type) = $self->class =~ /::(\w+)$/;
307 0 ▶ print "- ", BOLD($type), "\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
308 0 ▶ eval { $self->push_config($opts); 1 } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->push_config($opts); 1 } } or warn $@ 0 0 0
309 }
310  
311  
312 1 __PACKAGE__
313  
314 __END__
« bin/cisco-status ↑ Summary blib/lib/RackMan/Device/PDU/APC_RackPDU.pm »