File Coverage

blib/lib/DNS/Oterica/App.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package DNS::Oterica::App;
2             # ABSTRACT: the code behind `dnsoterica`
3             $DNS::Oterica::App::VERSION = '0.205';
4 1     1   1924 use Moose;
  0            
  0            
5             use DNS::Oterica::Hub;
6             use File::Find::Rule;
7             use YAML::XS ();
8              
9             #pod =attr hub
10             #pod
11             #pod This is the L<DNS::Oterica::Hub> into which entries will be loaded.
12             #pod
13             #pod =cut
14              
15             has hub => (
16             is => 'ro',
17             isa => 'DNS::Oterica::Hub',
18             writer => '_set_hub',
19             predicate => '_has_hub',
20             );
21              
22             sub BUILD {
23             my ($self, $arg) = @_;
24              
25             confess "both hub and hub_args provided"
26             if $self->_has_hub and $arg->{hub_args};
27              
28             unless ($self->_has_hub) {
29             my %args = %{$arg->{hub_args}};
30             $self->_set_hub( DNS::Oterica::Hub->new(\%args || {}) );
31             }
32             }
33              
34             #pod =attr root
35             #pod
36             #pod This is a directory in which F<dnsoterica> will look for configuration files.
37             #pod
38             #pod It will look in the subdirectory F<domains> for domain definitions and F<hosts>
39             #pod for hosts.
40             #pod
41             #pod =cut
42              
43             has root => (
44             is => 'ro',
45             required => 1,
46             );
47              
48             sub populate_networks {
49             my ($self) = @_;
50              
51             my $root = $self->root;
52             for my $file (File::Find::Rule->file->in("$root/networks")) {
53             for my $data (YAML::XS::LoadFile($file)) {
54             $self->hub->add_network($data);
55             }
56             }
57             }
58              
59             sub populate_domains {
60             my ($self) = @_;
61             my $root = $self->root;
62             for my $file (File::Find::Rule->file->in("$root/domains")) {
63             for my $data (YAML::XS::LoadFile($file)) {
64             my $node = $self->hub->domain(
65             $data->{domain},
66             );
67              
68             for my $name (@{ $data->{families} }) {
69             my $family = $self->hub->node_family($name);
70              
71             $node->add_to_family($family);
72             }
73             }
74             }
75             }
76              
77             sub populate_hosts {
78             my ($self) = @_;
79             my $root = $self->root;
80             my $hub = $self->hub;
81              
82             for my $file (File::Find::Rule->file->in("$root/hosts")) {
83             for my $data (YAML::XS::LoadFile($file)) {
84             my $interfaces;
85             if (ref $data->{ip}) {
86             $interfaces = [
87             map {;
88             [
89             $data->{ip}{$_} => $hub->network($_) ]
90             } keys %{ $data->{ip}}
91             ];
92             } else {
93             $interfaces = [
94             [ $data->{ip} => $hub->network( $hub->all_network_name ) ]
95             ];
96             }
97              
98             my $node = $hub->host(
99             $data->{domain},
100             $data->{hostname},
101             {
102             interfaces => $interfaces,
103             location => $data->{location},
104             aliases => $data->{aliases} || [],
105             (exists $data->{ttl} ? (ttl => $data->{ttl}) : ()),
106             },
107             );
108              
109             for my $name (@{ $data->{families} }) {
110             my $family = $hub->node_family($name);
111              
112             $node->add_to_family($family);
113             }
114             }
115             }
116             }
117              
118             1;
119              
120             __END__
121              
122             =pod
123              
124             =encoding UTF-8
125              
126             =head1 NAME
127              
128             DNS::Oterica::App - the code behind `dnsoterica`
129              
130             =head1 VERSION
131              
132             version 0.205
133              
134             =head1 ATTRIBUTES
135              
136             =head2 hub
137              
138             This is the L<DNS::Oterica::Hub> into which entries will be loaded.
139              
140             =head2 root
141              
142             This is a directory in which F<dnsoterica> will look for configuration files.
143              
144             It will look in the subdirectory F<domains> for domain definitions and F<hosts>
145             for hosts.
146              
147             =head1 AUTHOR
148              
149             Ricardo SIGNES <rjbs@cpan.org>
150              
151             =head1 COPYRIGHT AND LICENSE
152              
153             This software is copyright (c) 2014 by Ricardo SIGNES.
154              
155             This is free software; you can redistribute it and/or modify it under
156             the same terms as the Perl 5 programming language system itself.
157              
158             =cut