blib/lib/RackMan/Template.pm

filter: Statementstmt 52.8% 28 / 53 Branchbran 14.2% 2 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 48.7% 39 / 80 CC 11 SCAR 35.1
CC
11
Cov
43%
populate_from
72.0
BUILDARGS
5.7
BEGIN
1.0
CRAP 33.6
« blib/lib/RackMan/Device.pm ↑ Summary blib/lib/RackMan/Format/Nagios.pm »
1 package RackMan::Template;
2  
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use HTML::Template ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use HTML::Template::Filter::TT2 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use NetAddr::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use RackMan;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 3 has _ht_obj => (
12 is => "ro",
13 isa => "HTML::Template",
14 handles => {
15 param => "param",
16 output => "output",
17 },
18 );
19  
20  
21 #
22 # BUILDARGS()
23 # ---------
24 1 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=4 SCAR=17.4
Pod documented
25 1 my $class = shift;
26 1 my %param;
27  
28 1 ▶ if (@_ % 2 == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 1 0 1
elsif (ref $_[0] eq 'HASH') { } 0 0 0
29 1 %param = @_;
30 }
31 elsif (ref $_[0] eq "HASH") {
32 0 %param = %{$_[0]};
33 }
34 else {
35 0 RackMan->error("invalid argument: ",
36 lc ref $_[0], "ref instead of hashref");
37 }
38  
39 1 my $tmpl = eval { HTML::Template->new(
40 %param,
41 filter => \&HTML::Template::Filter::TT2::ht_tt2_filter,
42 die_on_bad_params => 0,
43 ) };
44 1 ▶ if (not $tmpl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmpl) 0 1 1
45 0 (my $error = $@) =~ s/ at .*//sm;
46 0 RackMan->error($error);
47 }
48  
49 1 return { _ht_obj => $tmpl }
50 }
51  
52  
53 #
54 # populate_from()
55 # -------------
56 0 ▶sub populate_from {
Subroutine populate_from: not called CC=8 SCAR=42.8
Pod documented
57 0 my ($self, $rackdev, $rackman) = @_;
58  
59 # fetch the common name and FQDN of the host
60 0 my $name = $rackdev->object_name;
61 0 my $fqdn = $rackdev->attributes->{FQDN};
62 0 ▶ RackMan->error("RackObject '$name' lacks a FQDN") unless $fqdn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fqdn 0 0 0
63  
64 # fetch the list of regular MAC addresses
65 0 my @mac_addrs = $rackdev->regular_mac_addrs;
66 0 ▶ RackMan->error("RackObject '$name' lacks a MAC address") unless @mac_addrs;
Branch: 1 branch
BranchTrueFalseTotal
unless @mac_addrs 0 0 0
67  
68 # fetch the list of regular IPv4 addresses
69 0 my @ip_addrs = $rackdev->regular_ipv4addrs;
70 0 ▶ RackMan->error("RackObject '$name' lacks an IPv4 address") unless @ip_addrs;
Branch: 1 branch
BranchTrueFalseTotal
unless @ip_addrs 0 0 0
71  
72 # Host IPv4 network parameters
73 0 my $host_gateway = $rackdev->default_ipv4_gateway;
74  
75 # determine the network mask
76 0 my $haddr = NetAddr::IP->new($ip_addrs[0]{addr}, $host_gateway->{masklen});
77 0 my $host_netmask = $haddr->mask;
78  
79 # fetch template parameters specific to the current device
80 0 ▶ my %devparam = $rackdev->tmpl_params if $rackdev->can("tmpl_params");
Branch: 1 branch
BranchTrueFalseTotal
if $rackdev->can("tmpl_params") 0 0 0
81  
82 # fetch the list of attributes, and convert their names to be
83 # valid identifiers
84 0 my %attr = %{ $rackdev->attributes };
85 0 for my $name (keys %attr) {
86 0 my $param = lc $name;
87 0 s/,.*$//, s:[./]::g, s/\W+/_/g for $param;
88 0 $attr{$param} = delete $attr{$name};
89 }
90  
91 # fetch the list of DNS servers
92 my @dns_servers
93 0 = split / +/, $rackman->config->val(general => "dns_servers");
94  
95 # populate the template
96 $self->param(
97 type => $rackdev->object_type,
98 name => $name,
99 fqdn => $fqdn,
100 if0_mac => $mac_addrs[0]{l2address_text},
101 if0_ip => $ip_addrs[0]{addr},
102 if0_name => $ip_addrs[0]{iface},
103 gateway => $host_gateway->{addr} || "0.0.0.0",
104 network => $host_gateway->{network},
105 0 ▶ netmask => $host_netmask,
Condition0%0/2
$host_gateway->{'addr'} || '0.0.0.0'
l!l
00
MC/DC0%0/1
$host_gateway->{'addr'} || '0.0.0.0'
$host_gateway->{'addr'}
Truth table0%0/2
A
A= $host_gateway->{'addr'}
Aresult
11
00
106 dns_server_1 => $dns_servers[0],
107 dns_server_2 => $dns_servers[1],
108 dns_server_3 => $dns_servers[2],
109 %devparam,
110 %attr,
111 );
112 }
113  
114  
115 3 __PACKAGE__->meta->make_immutable
116  
117 __END__
« blib/lib/RackMan/Device.pm ↑ Summary blib/lib/RackMan/Format/Nagios.pm »