File Coverage

lib/Provision/Unix/DNS.pm
Criterion Covered Total %
statement 38 72 52.7
branch 6 26 23.0
condition n/a
subroutine 9 16 56.2
pod 3 9 33.3
total 56 123 45.5


line stmt bran cond sub pod time code
1             package Provision::Unix::DNS;
2             # ABSTRACT: generic class for common DNS tasks
3              
4 2     2   1844 use strict;
  2         4  
  2         75  
5 2     2   12 use warnings;
  2         4  
  2         91  
6              
7             our $VERSION = '0.25';
8              
9 2     2   11 use English qw( -no_match_vars );
  2         3  
  2         16  
10 2     2   1141 use Params::Validate qw(:all);
  2         5  
  2         358  
11              
12 2     2   14 use lib 'lib';
  2         4  
  2         15  
13 2     2   1005 use Provision::Unix::Utility;
  2         7  
  2         1648  
14             my $util;
15              
16             sub new {
17 1     1 0 42 my $class = shift;
18 1         68 my %p = validate(
19             @_,
20             { prov => { type => OBJECT },
21             debug => { type => BOOLEAN, optional => 1, default => 1 },
22             fatal => { type => BOOLEAN, optional => 1, default => 1 },
23             }
24             );
25              
26 1         13 my $self = {
27             prov => $p{prov},
28             debug => $p{debug},
29             fatal => $p{fatal},
30             };
31 1         3 bless( $self, $class );
32              
33 1         30 $util = Provision::Unix::Utility->new( log => $p{prov}, debug=>$p{debug},fatal=>$p{fatal} );
34 1 50       8 $self->{server} = $self->_get_server() or return;
35 1         5 $self->{prov}->audit("loaded DNS");
36              
37 1         20 return $self;
38             }
39              
40             sub connect {
41 0     0 0 0 my $self = shift;
42 0         0 my %args = @_;
43 0 0       0 foreach ( keys %args ) { delete $args{$_} if ! defined $args{$_}; };
  0         0  
44 0         0 $self->{server}->connect(%args);
45             }
46              
47             sub create_zone {
48              
49             ############################################
50             # Usage : $dns->create_zone({ zone=>'example.com' });
51             # Purpose : Create a new zone
52             # Returns : failure: undef
53             # : success: zone_id for nictool, 1 for others methods
54             # Parameters
55             # Required : S - zone - the fully qualified zone name
56             # Optional : S - contact - the email address of the hostmaster
57             # : I - ttl, refresh, retry, expire, minimum
58             # : S - template - the name of a template to use
59             # : S - ip - an IP address for the template
60             # : S - mailip - an IP address for the zones MX
61             # Throws : no exceptions
62              
63 0     0 1 0 my $self = shift;
64 0         0 my %args = @_;
65 0 0       0 foreach ( keys %args ) { delete $args{$_} if ! defined $args{$_}; };
  0         0  
66 0         0 $self->{server}->create_zone(%args);
67             }
68              
69             sub create_zone_record {
70              
71             ############################################
72             # Usage : $dns->create_zone_record();
73             # Purpose : Create a new zone record
74             # Returns : failure: undef, success: 1
75             # Parameters
76             # Required : S - zone - the fully qualified zone name
77             # : S - name - the zone record name
78             # : S - type - A, MX, CNAME, NS, SRV, TXT
79             # : S - address - an IP address
80             # : S - port - SRV records only
81             # : S - priority - SRV records only
82             # Optional : S - ttl - TTL
83             # : S - zone_id - zone id
84             # : S - weight (mx & srv records only)
85              
86 0     0 1 0 my $self = shift;
87 0         0 $self->{server}->create_zone_record(@_);
88             }
89              
90             sub get_zone {
91              
92             ############################################
93             # Usage : $dns->get_zone( zone=>'example.com');
94             # Purpose : Find a zone
95             # Returns : depends on $dns backend
96             # Parameters
97             # Required : S - zone - the fully qualified zone name
98              
99 0     0 1 0 my $self = shift;
100 0         0 return $self->{server}->get_zone(@_);
101             }
102              
103 0     0 0 0 sub modify_zone {
104             }
105              
106             sub delete_zone {
107              
108 0     0 0 0 my $self = shift;
109 0         0 return $self->{server}->delete_zone(@_);
110             }
111              
112             sub delete_zone_record {
113              
114 0     0 0 0 my $self = shift;
115 0         0 return $self->{server}->delete_zone_record(@_);
116             }
117              
118             sub qualify {
119              
120             # this is server dependent. BIND and NicTool support shortcuts like @. Others
121             # need to be fully qualified (like tinydns).
122              
123 3     3 0 288 my $self = shift;
124 3         16 return $self->{server}->qualify(@_);
125             }
126              
127             sub _get_server {
128              
129 1     1   3 my $self = shift;
130 1         6 my $prov = $self->{prov};
131 1         6 my $debug = $self->{debug};
132 1         3 my $fatal = $self->{fatal};
133              
134 1 50       6 my $chosen_server = $prov->{config}{DNS}{server}
135             or $prov->error( 'missing [DNS] server setting in provision.conf',
136             fatal => $fatal,
137             debug => $debug,
138             );
139              
140             # try to autodetect the server
141 1 50       6 if ( ! $chosen_server ) {
142 0 0       0 if ( $util->find_bin( 'tinydns', debug=>0,fatal => 0 ) ) {
    0          
143 0         0 $chosen_server = 'tinydns';
144             }
145             elsif ( $util->find_bin( 'named', debug=>0,fatal => 0) ) {
146 0         0 $chosen_server = 'bind';
147             };
148             };
149              
150 1 50       13 if ( ! $chosen_server ) {
151 0         0 return $prov->error( "No DNS server selected and I could not find one installed. Giving up.",
152             fatal => $fatal,
153             debug => $debug,
154             );
155             };
156              
157 1 50       7 if ( $chosen_server eq 'nictool' ) {
    50          
    0          
158 0         0 eval { require Provision::Unix::DNS::NicTool; };
  0         0  
159 0 0       0 if ($EVAL_ERROR) {
160 0         0 return $prov->error ( $EVAL_ERROR, fatal => $fatal, debug => $debug );
161             };
162 0         0 my $r = Provision::Unix::DNS::NicTool->new(
163             prov => $prov,
164             fatal => $fatal,
165             debug => $debug,
166             );
167             #warn Data::Dumper::Dumper($r);
168 0 0       0 if ( ! $r ) {
169 0         0 return $prov->error( $prov->get_last_error(),
170             debug => $debug,
171             fatal => $fatal,
172             );
173             }
174 0         0 return $r;
175             }
176             elsif ( $chosen_server eq 'tinydns' ) {
177 1         1039 require Provision::Unix::DNS::tinydns;
178 1         14 return Provision::Unix::DNS::tinydns->new( prov => $prov );
179             }
180             elsif ( $chosen_server eq 'bind' ) {
181 0           require Provision::Unix::DNS::BIND;
182 0           return Provision::Unix::DNS::BIND->new( prov => $prov );
183             }
184             else {
185 0           return $prov->error( "no support for $chosen_server yet",
186             fatal => $fatal,
187             debug => $debug,
188             );
189             }
190             }
191              
192             1;
193              
194              
195             =pod
196              
197             =head1 NAME
198              
199             Provision::Unix::DNS - generic class for common DNS tasks
200              
201             =head1 VERSION
202              
203             version 1.06
204              
205             =head1 SYNOPSIS
206              
207             The Provision::Unix::DNS provides a consistent API for managing DNS zones and records regardless of the underlying DNS server. Applications make calls to Provision::Unix::DNS such as create_zone, create_zone_record, modify_zone, etc.
208              
209             use Provision::Unix::DNS;
210              
211             my $dns = Provision::Unix::DNS->new();
212             $dns->zone_create( zone=>'example.com' );
213              
214             $dns->zone_modify( zone=>'example.com', hostmaster=>'dnsadmin@admin-zone.com' );
215              
216             =head1 DESCRIPTION
217              
218             Rather than write code to generate BIND zone files, tinydns data files, or API calls to various servers, write your application to use Provision::Unix::DNS instead. The higher level DNS class contains methods for each type of DNS task as well as error handling, rollback support, and logging. Based on the settings in your provision.conf file, your request will be dispatched to your DNS Server of choice. Subclasses are created for each type of DNS server.
219              
220             Support is included for NicTool via its native API and tinydns. I will leave it to others (or myself in the unplanned future) to write modules to interface with other DNS servers. Good candidates for modules are BIND and PowerDNS.
221              
222             =head1 FUNCTIONS
223              
224             =head2 create_zone
225              
226             =head2 create_zone_record
227              
228             =head2 get_zone
229              
230             =head1 BUGS
231              
232             Please report any bugs or feature requests to C<bug-unix-provision-dns at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Provision-Unix>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
233              
234             =head1 SUPPORT
235              
236             You can find documentation for this module with the perldoc command.
237              
238             perldoc Provision::Unix::DNS
239              
240             You can also look for information at:
241              
242             =over 4
243              
244             =item * RT: CPAN's request tracker
245              
246             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Provision-Unix>
247              
248             =item * AnnoCPAN: Annotated CPAN documentation
249              
250             L<http://annocpan.org/dist/Provision-Unix>
251              
252             =item * CPAN Ratings
253              
254             L<http://cpanratings.perl.org/d/Provision-Unix>
255              
256             =item * Search CPAN
257              
258             L<http://search.cpan.org/dist/Provision-Unix>
259              
260             =back
261              
262             =head1 AUTHOR
263              
264             Matt Simerson <msimerson@cpan.org>
265              
266             =head1 COPYRIGHT AND LICENSE
267              
268             This software is copyright (c) 2013 by The Network People, Inc..
269              
270             This is free software; you can redistribute it and/or modify it under
271             the same terms as the Perl 5 programming language system itself.
272              
273             =cut
274              
275              
276             __END__
277              
278              
279