blib/lib/DNS/PunyDNS.pm

filter: Statementstmt 23.6% 26 / 110 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.0% 6 / 15 Podpod 100% 7 / 7 totaltotal 21.5% 39 / 181 CC 24 SCAR 28.5
CC
24
Cov
17%
add_dns
30.0
get_dns_info
30.0
new
20.0
CRAP 17.3
↑ Summary
1 package DNS::PunyDNS;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 DNS::PunyDNS - Interact with your SAPO dynamic DNS entries
13  
14 =head1 VERSION
15  
16 Version 0.02
17  
18 =cut
19  
20 2 our $VERSION = '0.02';
21  
22 2 Readonly::Scalar my $BASE_URL => 'https://services.sapo.pt/PunyUrl/DNS/';
23  
24 2 Readonly::Scalar my $LISTDNS => 'ListDns';
25 2 Readonly::Scalar my $LISTDNSINFO => 'ListDnsInfo';
26 2 Readonly::Scalar my $GETDNSINFO => 'GetDnsInformation';
27 2 Readonly::Scalar my $ADDDNS => 'AddDns';
28 2 Readonly::Scalar my $REMOVEDNS => 'RemoveDns';
29 2 Readonly::Scalar my $UPDATEDNS => 'UpdateDns';
30  
31 =head1 SYNOPSIS
32  
33 This module allows you to create/remove/update your SAPO dynamic DNS entries (L<http://sl.pt>).
34  
35 use DNS::PunyDNS;
36  
37 my $dns = DNS::PunyDNS->new({'username' => '<sapousername>', password => '<sapopassword>' } );
38  
39 my $added = $dns->add_dns( $domain, $ip, $record_type );
40  
41 if (!$added) {
42 warn $dns->{'error'};
43 }
44 ...
45  
46  
47 =head1 METHODS
48  
49 =head2 new
50  
51 Creates a new DNS::PunyDNS object.
52  
53  
54 DNS::PunyDNS->new( { 'username' => '<sapousername>', 'password' => '<sapopassword>' } )
55  
56 Your SAPO username and password must be provided.
57  
58 =head2 add_dns
59  
60 Adds a dynamic DNS entry.
61  
62 $dns->add_dns( $domain, $ip, $record_type );
63  
64 This function returns false if the operation fails, the cause of the failure is set in C<< $dns->{'error'} >>.
65  
66 =head2 update_dns
67  
68 Updates a dynamic DNS entry.
69  
70 $dns->update_dns( $domain, $ip, $record_type, [$old_record_type]);
71  
72 This function returns false if the operation fails, the cause of the failure is set in C<< $dns->{'error'} >>.
73  
74 =head2 get_dns_info
75  
76 Gets DNS information of a specific entrry.
77  
78 my $info = $dns->get_dns_info( $domain );
79  
80  
81 =head2 list_dns
82  
83 Gets all the dynamic DNS entry names associated with your account.
84  
85 my $list = $dns->list_dns();
86  
87 =head2 list_dns_info
88  
89 Gets all the dynamic DNS entrys information.
90  
91 my $list_info = $dns->list_dns_info();
92  
93 =head2 remove_dns
94  
95 Removes a DNS entry.
96  
97 $removed = $dns->remove_dns( $domain, [$record_type] );
98  
99 This function returns false if the operation fails, the cause of the failure is set in C<< $dns->{'error'} >>.
100  
101  
102 =cut
103  
104 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
105 0 my ( $class, @args ) = @_;
106 0 my $self = {};
107 0 ▶ bless $self, $class || $class;
Condition0%0/3
$class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
$class || $class
$class $class
Truth table0%0/3
A || B
A= $classB= $class
ABresult
1X1
011
000
108 0 ▶ $self->{'username'} = $args[0]->{'username'} || die "No username provided";
Condition0%0/2
$args[0]{'username'} || die('No username provided')
l!l
00
MC/DC0%0/1
$args[0]{'username'} || die('No username provided')
$args[0]{'username'}
Truth table0%0/2
A
A= $args[0]{'username'}
Aresult
11
00
109 0 ▶ $self->{'password'} = $args[0]->{'password'} || die "No password provided";
Condition0%0/2
$args[0]{'password'} || die('No password provided')
l!l
00
MC/DC0%0/1
$args[0]{'password'} || die('No password provided')
$args[0]{'password'}
Truth table0%0/2
A
A= $args[0]{'password'}
Aresult
11
00
110  
111 0 return $self;
112 }
113  
114 0 ▶sub update_dns {
Subroutine update_dns: not called CC=4 SCAR=30.0
Pod documented
115 0 my ( $self, $domain, $ip, $record_type, $old_record_type ) = @_;
116 0 ▶ die "You must provide a domain" if !$domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain 0 0 0
117 0 ▶ die "You must provide an IP address" if !$ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 0 0 0
118  
119 0 my %args = (
120 'domain' => $domain,
121 'ip' => $ip,
122 'record_type' => $record_type,
123 );
124 0 ▶ $args{'old_record_type'} = $old_record_type if $old_record_type;
Branch: 1 branch
BranchTrueFalseTotal
if $old_record_type 0 0 0
125 0 my $response = $self->_get_it( $UPDATEDNS, \%args );
126 0 return $response;
127  
128 }
129  
130 0 ▶sub remove_dns {
Subroutine remove_dns: not called CC=4 SCAR=30.0
Pod documented
131 0 my ( $self, $domain, $record_type ) = @_;
132 0 ▶ die "You must provide a domain" if !$domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain 0 0 0
133 0 my %args = ( 'domain' => $domain );
134 0 ▶ $args{'record_type'} = $record_type if $record_type;
Branch: 1 branch
BranchTrueFalseTotal
if $record_type 0 0 0
135 0 my $response = $self->_get_it( $REMOVEDNS, \%args );
136 0 ▶ return 0 if ( $self->{'error'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
137 0 return 1;
138 }
139  
140 0 ▶sub add_dns {
Subroutine add_dns: not called CC=5 SCAR=34.0
Pod documented
141 0 my ( $self, $domain, $ip, $record_type ) = @_;
142 0 ▶ die "You must provide a domain" if !$domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain 0 0 0
143 0 ▶ die "You must provide an IP address" if !$ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 0 0 0
144 0 ▶ die "You must provide a Record Type" if !$record_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $record_type 0 0 0
145 0 my $response = $self->_get_it(
146 $ADDDNS,
147 {
148 'domain' => $domain,
149 'ip' => $ip,
150 'record_type' => $record_type
151 } );
152 0 ▶ return 0 if ( $self->{'error'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
153 0 return 1;
154 }
155  
156 0 ▶sub get_dns_info {
Subroutine get_dns_info: not called CC=5 SCAR=34.0
Pod documented
157 0 my ( $self, $domain ) = @_;
158 0 ▶ die "You must provide a domain to check" if ( !$domain );
Branch: 1 branch
BranchTrueFalseTotal
unless $domain 0 0 0
159 0 my $response = $self->_get_it( $GETDNSINFO, { 'domain' => $domain } );
160 0 ▶ return if ( $self->{'error'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
161 0 my @domain_info = ();
162 0 my $info = $response->{'info'};
163 0 ▶ if ( ref($info) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info eq 'ARRAY') { } 0 0 0
164 0 for my $dnsinfo ( @{$info} ) {
165 0 my %tmp_info;
166 0 $tmp_info{'domain'} = $response->{'domain'};
167 0 $tmp_info{'ip'} = $dnsinfo->{'ip'};
168 0 $tmp_info{'type'} = $dnsinfo->{'type'};
169 0 push @domain_info, \%tmp_info;
170 }
171 } else {
172 my %tmp_info = (
173 'domain' => $response->{'domain'},
174 'ip' => $info->{'ip'},
175 0 'type' => $info->{'type'},
176 );
177 0 push @domain_info, \%tmp_info;
178 }
179  
180 0 return \@domain_info;
181 } ## end sub get_dns_info
182  
183 0 ▶sub list_dns_info {
Subroutine list_dns_info: not called CC=3 SCAR=24.8
Pod documented
184 0 my ($self) = @_;
185 0 my $response = $self->_get_it($LISTDNSINFO);
186 0 my @domains = ();
187 0 ▶ if ( $response->{'domains'}{'domain'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"domains"}{"domain"}) 0 0 0
188 0 ▶ if ( ref( $response->{'domains'}{'domain'} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response->{'domains'}{'domain'} eq 'ARRAY') { } 0 0 0
189 0 push @domains, @{ $response->{'domains'}{'domain'} };
190 } else {
191 0 push @domains, $response->{'domains'}{'domain'};
192 }
193 }
194 0 return \@domains;
195 }
196  
197 0 ▶sub list_dns {
Subroutine list_dns: not called CC=3 SCAR=24.8
Pod documented
198 0 my ($self) = @_;
199 0 my $response = $self->_get_it($LISTDNS);
200 0 my @domains = ();
201 0 ▶ if ( $response->{'domain'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"domain"}) 0 0 0
202 0 ▶ if ( ref( $response->{'domains'} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response->{'domains'} eq 'ARRAY') { } 0 0 0
203 0 push @domains, @{ $response->{'domain'} };
204 } else {
205 0 push @domains, $response->{'domain'};
206 }
207 }
208 0 return \@domains;
209 }
210  
211 0 ▶sub _build_request {
Subroutine _build_request: not called CC=1 SCAR=6.9
212 0 my ( $self, $endpoint, $args ) = @_;
213 0 $args->{'ESBUsername'} = $self->{'username'};
214 0 $args->{'ESBPassword'} = $self->{'password'};
215 0 $args->{'lang'} = 'en';
216 0 my @keys = keys %{$args};
217  
218 0 my $url = $BASE_URL . $endpoint . '?' . join( '&', map { $_ . '=' . $args->{$_} } @keys );
219  
220 0 return $url;
221 }
222  
223 0 ▶sub _get_it {
Subroutine _get_it: not called CC=3 SCAR=24.8
224 0 my ( $self, $endpoint, $args ) = @_;
225  
226 0 my $url = $self->_build_request( $endpoint, $args );
227 0 delete $self->{'error'};
228  
229 0 my $ua = new LWP::UserAgent();
230 0 my $req = new HTTP::Request( 'GET', $url );
231 0 my $response = $ua->request($req);
232  
233 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
234 0 my $content = $response->content;
235 0 my $decoded_content = XMLin( \$content, KeyAttr => 'domain' );
236 0 ▶ if ( $decoded_content->{'error'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoded_content->{"error"}) 0 0 0
237 0 $self->{'error'} = $decoded_content->{'error'};
238 }
239 0 return $decoded_content;
240 } else {
241 0 die "There was a problem with the request\n" . $response->status_line;
242 }
243  
244 } ## end sub _get_it
245  
246 =head1 AUTHOR
247  
248 Bruno Martins, C<< <bruno-martins at telecom.pt> >>
249  
250 =head1 NOTES
251  
252 =head2 Handling Errors
253  
254 There are several ways that the operations can fail, for example, you can try to add a dns entry that already exists or you can try to update a dns entry that is not under your account, etc.
255  
256 Each time an operation is executed and raises an error C<< $dns->{'error'} >> is set with the error reason.
257  
258 =head2 SAPO dynamic DNS API authentication
259  
260 SAPO dynamic DNS API is only available over https, so your username and password are not sent I<clean>
261  
262 =head2 Record Types
263  
264 SAPO dynamic DNS API only allows A and AAAA record types
265  
266 =head2 Domain names
267  
268 At this time, SAPO dynamic DNS only allows .sl.pt domains
269  
270 =head1 SUPPORT
271  
272 You can find documentation for this module with the perldoc command.
273  
274 perldoc DNS::PunyDNS
275  
276  
277 You can also look for information at:
278  
279 =over 4
280  
281 =item * Search CPAN
282  
283 L<http://search.cpan.org/dist/DNS::PunyDNS/>
284  
285 =back
286  
287  
288 =head1 LICENSE AND COPYRIGHT
289  
290 Copyright 2011 Bruno Martins C<< <bruno-martins at telecom.pt> >> and SAPO L<http://www.sapo.pt>, all rights reserved.
291  
292  
293 This program is free software; you can redistribute it and/or modify it
294 under the terms of either: the GNU General Public License as published
295 by the Free Software Foundation; or the Artistic License.
296  
297  
298 See http://dev.perl.org/licenses/ for more information.
299  
300 =cut
301  
302 2 1; # End of DNS::PunyDNS
↑ Summary