Oterica/Hub.pm

filter: Statementstmt 71.2% 62 / 87 Branchbran 40.9% 9 / 22 Conditioncond 16.6% 3 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 73.6% 14 / 19 Podpod 80.0% 8 / 10 totaltotal 57.1% 96 / 168 CC 23 SCAR 19.5
CC
23
Cov
58%
host
20.0
nodes
12.0
add_network
10.4
CRAP 7.0
« Oterica/Node.pm ↑ Summary Oterica/NodeFamily.pm »
1 package DNS::Oterica::Hub;
2 # ABSTRACT: the center of control for a DNS::Oterica system
3 1 $DNS::Oterica::Hub::VERSION = '0.313';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 with 'DNS::Oterica::Role::RecordMaker';
6  
7 # use MooseX::AttributeHelpers;
8  
9 1 ▶use DNS::Oterica::Network;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DNS::Oterica::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DNS::Oterica::Node::Domain;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use DNS::Oterica::Node::Host;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use DNS::Oterica::NodeFamily;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #pod =head1 OVERVIEW
16 #pod
17 #pod The hub is the central collector of DNS::Oterica data. All new entries are
18 #pod given to the hub to collect. The hub takes care of preventing duplicates and
19 #pod keeping data synchronized.
20 #pod
21 #pod =cut
22  
23 has [ qw(_domain_registry _net_registry _node_family_registry) ] => (
24 is => 'ro',
25 isa => 'HashRef',
26 init_arg => undef,
27 3 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 );
29  
30 #pod =attr ns_family
31 #pod
32 #pod This is the name of the family whose hosts will be used for NS records for
33 #pod hosts and in SOA lines.
34 #pod
35 #pod =cut
36  
37 1 has ns_family => (
38 is => 'ro',
39 isa => 'Str',
40 required => 1,
41 );
42  
43 #pod =attr hostmaster
44 #pod
45 #pod This is the email address to be used as the contact point in SOA lines.
46 #pod
47 #pod =cut
48  
49 1 has hostmaster => (
50 is => 'ro',
51 isa => 'Str',
52 required => 1,
53 );
54  
55 sub soa_rname {
56 0 ▶ my ($self) = @_;
Subroutine soa_rname: not called CC=1 SCAR=6.9
Pod undocumented
57 0 my $addr = $self->hostmaster;
58 0 $addr =~ s/@/./;
59 0 return $addr;
60 }
61  
62 use Module::Pluggable
63 search_path => [ qw(DNS::Oterica::NodeFamily) ],
64 require => 1,
65 $ENV{DNSO_TESTING_PLUGINS}
66 1 ▶ ? (only => [ split /,/, $ENV{DNSO_TESTING_PLUGINS} ])
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DNSO_TESTING_PLUGINS'} ? : 1 0 1
67 1 ▶ : ();
Subroutine BEGIN: called CC=2 SCAR=6.9
68  
69 1 has fallback_network_name => (is => 'ro', isa => 'Str', default => 'FALLBACK');
70 1 has all_network_name => (is => 'ro', isa => 'Str', default => 'ALL');
71  
72 sub BUILD {
73 1 ▶ my ($self) = @_;
Subroutine BUILD: called CC=3 SCAR=14.2
Pod undocumented
74  
75 1 for my $plugin ($self->plugins) {
76 confess "tried to register " . $plugin->name . " twice" if exists
77 0 ▶ $self->_node_family_registry->{$plugin->name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_node_family_registry->{$plugin->name} 0 0 0
78 0 $self->_node_family_registry->{ $plugin->name }
79 = $plugin->new({ hub => $self });
80 }
81  
82 $self->add_network({
83 1 name => $self->fallback_network_name,
84 code => 'FB', # should it be configurable? eh.
85 subnets => [ '0.0.0.0/0' ],
86 });
87  
88 1 $self->add_network({
89 name => $self->all_network_name,
90 code => '',
91 subnets => [ '0.0.0.0/32' ],
92 });
93 }
94  
95 #pod =method domain
96 #pod
97 #pod my $new_domain = $hub->domain($name => \%arg);
98 #pod
99 #pod my $domain = $hub->domain($name);
100 #pod
101 #pod This method will return a domain found by name, or if C<\%arg> is given, will
102 #pod create a new domain.
103 #pod
104 #pod If no domain is found and C<\%arg> is not given, an exception is raised.
105 #pod
106 #pod If C<\%arg> is given for a domain that already exists, an exception is raised.
107 #pod
108 #pod =cut
109  
110 sub domain {
111 1 ▶ my ($self, $name, $arg) = @_;
Subroutine domain: called CC=5 SCAR=18.9
Pod documented
112 1 my $domreg = $self->_domain_registry;
113  
114 1 ▶ confess "tried to create domain $name twice" if $domreg->{$name} and $arg;
Branch: 1 branch
BranchTrueFalseTotal
if $domreg->{$name} and $arg 0 1 1
Condition33%1/3
$domreg->{$name} and $arg
!ll&&!rl&&r
100
MC/DC0%0/2
$domreg->{$name} and $arg
$domreg->{$name} $arg
Truth table33%1/3
A and B
A= $domreg->{$name}B= $arg
ABresult
0X0
100
111
115  
116 # XXX: This should be possible to do. -- rjbs, 2009-09-11
117 # confess "no such domain: $name" if ! defined $arg and ! $domreg->{$name};
118  
119 return $domreg->{$name} ||= DNS::Oterica::Node::Domain->new({
120 domain => $name,
121 1 ▶ %{ $arg || {} },
Branch: 1 branch
BranchTrueFalseTotal
$arg or {} 1 0 1
Condition33%1/3
$domreg->{$name} ||= "DNS::Oterica::Node::Domain"->new({"domain", $name, %{$arg or {};}, "hub", $self})
l!l&&r!l&&!r
010
MC/DC0%0/2
$domreg->{$name} ||= "DNS::Oterica::Node::Domain"->new({"domain", $name, %{$arg or {};}, "hub", $self})
$domreg->{$name} "DNS::Oterica::Node::Domain"->new({"domain", $name, %{$arg or {};}, "hub", $self})
Truth table33%1/3
A ||= B
A= $domreg->{$name}B= "DNS::Oterica::Node::Domain"->new({"domain", $name, %{$arg or {};}, "hub", $self})
ABresult
1X1
011
000
122 hub => $self,
123 });
124 }
125  
126 #pod =method network
127 #pod
128 #pod my $net = $hub->network($name);
129 #pod
130 #pod This method finds the named network and returns it. If no network for the
131 #pod given name is registered, an exception is raised.
132 #pod
133 #pod =cut
134  
135 sub network {
136 0 ▶ my ($self, $name) = @_;
Subroutine network: not called CC=2 SCAR=17.9
Pod documented
137 0 ▶ return $self->_net_registry->{$name} || confess "no such network '$name'";
Condition0%0/3
$self->_net_registry->{$name} || confess("no such network '${name}'")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_net_registry->{$name} || confess("no such network '${name}'")
$self->_net_registry->{$name} confess("no such network '${name}'")
Truth table0%0/3
A || B
A= $self->_net_registry->{$name}B= confess("no such network '${name}'")
ABresult
1X1
011
000
138 }
139  
140 #pod =method networks
141 #pod
142 #pod my @net = $hub->networks;
143 #pod
144 #pod =cut
145  
146 sub networks {
147 4 ▶ my ($self) = @_;
Subroutine networks: called CC=1 SCAR=0.0
Pod documented
148 4 return values %{ $self->_net_registry };
149 }
150  
151 #pod =method add_network
152 #pod
153 #pod my $net = $hub->add_network(\%arg);
154 #pod
155 #pod This registers a new network, raising an exception if one already exists for
156 #pod the given name.
157 #pod
158 #pod =cut
159  
160 sub add_network {
161 4 ▶ my ($self, $arg) = @_;
Subroutine add_network: called CC=9 SCAR=23.4
Pod documented
162  
163 4 my $net = DNS::Oterica::Network->new({ %$arg, hub => $self });
164  
165 4 my $name = $net->name;
166 4 ▶ confess "tried to create $name twice" if $self->_net_registry->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_net_registry->{$name} 0 4 4
167  
168 4 my $code = $net->code;
169  
170 4 my @errors;
171 4 for my $existing ($self->networks) {
172 6 ▶ if ($net->code eq $existing->code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($net->code eq $existing->code) 0 6 6
173 0 push @errors, sprintf "code '%s' conflicts with network %s",
174 $code, $existing->name;
175 }
176  
177 6 ▶ next if $existing->name eq $self->all_network_name;
Branch: 1 branch
BranchTrueFalseTotal
if $existing->name eq $self->all_network_name 2 4 6
178  
179 4 for my $our ($net->subnets) {
180 4 for my $their ($existing->subnets) {
181 4 ▶ if ($our->overlaps($their) == $Net::IP::IP_IDENTICAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($our->overlaps($their) == $Net::IP::IP_IDENTICAL) 0 4 4
182 0 push @errors, sprintf "network '%s' conflicts with network %s (%s)",
183 $our->ip, $existing->name, $their->ip;
184 }
185 }
186 }
187 }
188  
189 4 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 4 4
190 0 confess("errors registering network $name: " . join q{; }, @errors);
191 }
192  
193 4 $self->_net_registry->{$name} = $net;
194 }
195  
196 #pod =method host
197 #pod
198 #pod my $host = $hub->host($domain_name, $hostname);
199 #pod
200 #pod my $new_host = $hub->host($domain_name, $hostname, \%arg);
201 #pod
202 #pod This method will find or create a host, much like the C<L</domain>> method.
203 #pod
204 #pod =cut
205  
206 sub host {
207 0 ▶ my ($self, $domain_name, $name, $arg) = @_;
Subroutine host: not called CC=4 SCAR=30.0
Pod documented
208 0 my $domain = $self->domain($domain_name);
209  
210 confess "tried to create $name . $domain_name twice"
211 0 ▶ if $domain->{$name} and $arg;
Branch: 1 branch
BranchTrueFalseTotal
if $domain->{$name} and $arg 0 0 0
Condition0%0/3
$domain->{$name} and $arg
!ll&&!rl&&r
000
MC/DC0%0/2
$domain->{$name} and $arg
$domain->{$name} $arg
Truth table0%0/3
A and B
A= $domain->{$name}B= $arg
ABresult
0X0
100
111
212  
213 0 ▶ return $domain->{nodes}{$name} ||= DNS::Oterica::Node::Host->new({
Condition0%0/3
$domain->{"nodes"}{$name} ||= "DNS::Oterica::Node::Host"->new({"domain", $domain_name, "hostname", $name, %$arg, "hub", $self})
l!l&&r!l&&!r
000
MC/DC0%0/2
$domain->{"nodes"}{$name} ||= "DNS::Oterica::Node::Host"->new({"domain", $domain_name, "hostname", $name, %$arg, "hub", $self})
$domain->{"nodes"}{$name} "DNS::Oterica::Node::Host"->new({"domain", $domain_name, "hostname", $name, %$arg, "hub", $self})
Truth table0%0/3
A ||= B
A= $domain->{"nodes"}{$name}B= "DNS::Oterica::Node::Host"->new({"domain", $domain_name, "hostname", $name, %$arg, "hub", $self})
ABresult
1X1
011
000
214 domain => $domain_name,
215 hostname => $name,
216 %$arg,
217 hub => $self,
218 });
219 }
220  
221 #pod =method nodes
222 #pod
223 #pod This method will return a list of all nodes registered with the system.
224 #pod
225 #pod B<Warning>: at present this will return only hosts.
226 #pod
227 #pod =cut
228  
229 sub nodes {
230 0 ▶ my ($self) = @_;
Subroutine nodes: not called CC=3 SCAR=24.8
Pod documented
231  
232 0 my @nodes;
233  
234 0 for my $domain (values %{ $self->_domain_registry }) {
235 0 ▶ push @nodes, values %{ $domain->{nodes} || {} };
Branch: 1 branch
BranchTrueFalseTotal
$domain->{"nodes"} or {} 0 0 0
236 }
237  
238 0 return @nodes;
239 }
240  
241 #pod =method node_family
242 #pod
243 #pod my $family = $hub->node_family($family_name);
244 #pod
245 #pod This method will return the named familiy. If no such family exists, an
246 #pod exception will be raised.
247 #pod
248 #pod =cut
249  
250 sub node_family {
251 1 ▶ my ($self, $name) = @_;
Subroutine node_family: called CC=2 SCAR=8.1
Pod documented
252  
253 1 ▶ return $self->_node_family_registry->{$name}
Condition33%1/3
$self->_node_family_registry->{$name} || confess("unknown family $name")
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->_node_family_registry->{$name} || confess("unknown family $name")
$self->_node_family_registry->{$name} confess("unknown family $name")
Truth table33%1/3
A || B
A= $self->_node_family_registry->{$name}B= confess("unknown family $name")
ABresult
1X1
011
000
254 || confess "unknown family $name";
255 }
256  
257 #pod =method node_families
258 #pod
259 #pod my @families = $hub->node_families;
260 #pod
261 #pod This method will return all node families. (These are set up during hub
262 #pod initialization.)
263 #pod
264 #pod =cut
265  
266 sub node_families {
267 0 ▶ my ($self) = @_;
Subroutine node_families: not called CC=1 SCAR=6.9
Pod documented
268 0 return values %{ $self->_node_family_registry };
269 }
270  
271 1 __PACKAGE__->meta->make_immutable;
272 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
273 1 1;
274  
275 __END__
276  
277 =pod
278  
279 =encoding UTF-8
280  
281 =head1 NAME
282  
283 DNS::Oterica::Hub - the center of control for a DNS::Oterica system
284  
285 =head1 VERSION
286  
287 version 0.313
288  
289 =head1 OVERVIEW
290  
291 The hub is the central collector of DNS::Oterica data. All new entries are
292 given to the hub to collect. The hub takes care of preventing duplicates and
293 keeping data synchronized.
294  
295 =head1 PERL VERSION
296  
297 This library should run on perls released even a long time ago. It should work
298 on any version of perl released in the last five years.
299  
300 Although it may work on older versions of perl, no guarantee is made that the
301 minimum required version will not be increased. The version may be increased
302 for any reason, and there is no promise that patches will be accepted to lower
303 the minimum required perl.
304  
305 =head1 ATTRIBUTES
306  
307 =head2 ns_family
308  
309 This is the name of the family whose hosts will be used for NS records for
310 hosts and in SOA lines.
311  
312 =head2 hostmaster
313  
314 This is the email address to be used as the contact point in SOA lines.
315  
316 =head1 METHODS
317  
318 =head2 domain
319  
320 my $new_domain = $hub->domain($name => \%arg);
321  
322 my $domain = $hub->domain($name);
323  
324 This method will return a domain found by name, or if C<\%arg> is given, will
325 create a new domain.
326  
327 If no domain is found and C<\%arg> is not given, an exception is raised.
328  
329 If C<\%arg> is given for a domain that already exists, an exception is raised.
330  
331 =head2 network
332  
333 my $net = $hub->network($name);
334  
335 This method finds the named network and returns it. If no network for the
336 given name is registered, an exception is raised.
337  
338 =head2 networks
339  
340 my @net = $hub->networks;
341  
342 =head2 add_network
343  
344 my $net = $hub->add_network(\%arg);
345  
346 This registers a new network, raising an exception if one already exists for
347 the given name.
348  
349 =head2 host
350  
351 my $host = $hub->host($domain_name, $hostname);
352  
353 my $new_host = $hub->host($domain_name, $hostname, \%arg);
354  
355 This method will find or create a host, much like the C<L</domain>> method.
356  
357 =head2 nodes
358  
359 This method will return a list of all nodes registered with the system.
360  
361 B<Warning>: at present this will return only hosts.
362  
363 =head2 node_family
364  
365 my $family = $hub->node_family($family_name);
366  
367 This method will return the named familiy. If no such family exists, an
368 exception will be raised.
369  
370 =head2 node_families
371  
372 my @families = $hub->node_families;
373  
374 This method will return all node families. (These are set up during hub
375 initialization.)
376  
377 =head1 AUTHOR
378  
379 Ricardo SIGNES <rjbs@semiotic.systems>
380  
381 =head1 COPYRIGHT AND LICENSE
382  
383 This software is copyright (c) 2021 by Ricardo SIGNES.
384  
385 This is free software; you can redistribute it and/or modify it under
386 the same terms as the Perl 5 programming language system itself.
387  
388 =cut
« Oterica/Node.pm ↑ Summary Oterica/NodeFamily.pm »