File Coverage

blib/lib/Net/Cloudflare/DNS.pm
Criterion Covered Total %
statement 20 79 25.3
branch 0 8 0.0
condition n/a
subroutine 7 12 58.3
pod 5 5 100.0
total 32 104 30.7


line stmt bran cond sub pod time code
1             package Net::Cloudflare::DNS;
2              
3 1     1   56875 use 5.006;
  1         4  
4 1     1   4 use strict;
  1         2  
  1         28  
5 1     1   5 use warnings;
  1         2  
  1         29  
6 1     1   609 use JSON;
  1         10507  
  1         5  
7 1     1   638 use URI;
  1         3560  
  1         26  
8 1     1   604 use LWP::UserAgent;
  1         32454  
  1         36  
9 1     1   442 use LWP::Protocol::https;
  1         82906  
  1         569  
10            
11            
12             sub new {
13 0     0 1   my $class = shift;
14 0           my %args = @_;
15 0           bless \%args, $class;
16             }
17            
18             sub get_records {
19 0     0 1   my $self = shift;
20 0           my %args = @_;
21              
22 0           my $email = $self->{email};
23 0           my $api_key = $self->{api_key};
24 0           my $zone_id = $self->{zone_id};
25            
26 0           my $uri = URI->new("https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records");
27 0           $uri->query_form(%args);
28              
29 0           my $ua = LWP::UserAgent->new;
30 0           my %headers = (
31             'Content-Type' => 'application/json',
32             'X-Auth-Key' => $api_key,
33             'X-Auth-Email' => $email,
34             );
35            
36 0           my $res = $ua->get($uri,
37             %headers,
38             );
39              
40 0 0         if ($res->is_success ) {
41 0           return $res->decoded_content;
42             } else {
43 0           print $res->decoded_content;
44 0           die $res->status_line;
45             }
46             }
47              
48              
49             sub create_record {
50 0     0 1   my $self = shift;
51 0           my %args = @_;
52              
53 0           my $data = encode_json(\%args);
54 0           my $email = $self->{email};
55 0           my $api_key = $self->{api_key};
56 0           my $zone_id = $self->{zone_id};
57            
58              
59 0           my $uri = "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records";
60 0           my $ua = LWP::UserAgent->new;
61 0           my %headers = (
62             'Content-Type' => 'application/json',
63             'X-Auth-Key' => $api_key,
64             'X-Auth-Email' => $email,
65             );
66            
67 0           my $res = $ua->post($uri,
68             %headers,
69             Content => $data,
70             );
71              
72 0 0         if ($res->is_success ) {
73 0           return $res->decoded_content;
74             } else {
75 0           print $res->decoded_content;
76 0           die $res->status_line;
77             }
78             }
79              
80             sub update_record {
81 0     0 1   my $self = shift;
82 0           my $record_id = shift;
83 0           my %args = @_;
84              
85 0           my $data = encode_json(\%args);
86 0           my $email = $self->{email};
87 0           my $api_key = $self->{api_key};
88 0           my $zone_id = $self->{zone_id};
89              
90 0           my $uri = "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id";
91 0           my $ua = LWP::UserAgent->new;
92 0           my %headers = (
93             'Content-Type' => 'application/json',
94             'X-Auth-Key' => $api_key,
95             'X-Auth-Email' => $email,
96             );
97            
98 0           my $res = $ua->put($uri,
99             %headers,
100             Content => $data,
101             );
102              
103 0 0         if ($res->is_success ) {
104 0           return $res->decoded_content;
105             } else {
106 0           print $res->decoded_content;
107 0           die $res->status_line;
108             }
109             }
110              
111             sub delete_record {
112 0     0 1   my $self = shift;
113 0           my $record_id = shift;
114              
115 0           my $email = $self->{email};
116 0           my $api_key = $self->{api_key};
117 0           my $zone_id = $self->{zone_id};
118              
119 0           my $uri = "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id";
120 0           my $ua = LWP::UserAgent->new;
121 0           my %headers = (
122             'Content-Type' => 'application/json',
123             'X-Auth-Key' => $api_key,
124             'X-Auth-Email' => $email,
125             );
126            
127 0           my $res = $ua->delete($uri,
128             %headers,
129             );
130              
131 0 0         if ($res->is_success ) {
132 0           return $res->decoded_content;
133             } else {
134 0           print $res->decoded_content;
135 0           die $res->status_line;
136             }
137             }
138              
139             =head1 NAME
140              
141             Net::Cloudflare::DNS - DNS API for Cloudflare API v4
142              
143             =head1 VERSION
144              
145             Version 0.01
146              
147             =cut
148              
149             our $VERSION = '0.01';
150              
151              
152             =head1 SYNOPSIS
153              
154             Cloudflare API v4 has big improvement to the older ones. This API operates against the specific zone based on API v4.
155              
156             I use this module to dyna update my DNS zone everyday. Cloudflare's DNS and its API behave very well in my life.
157              
158             If you have met any issue with the module, please don't hesitate to drop me an email: iwesley [at] pobox.com
159              
160             To use the module, you must have these two perl modules installed in the system:
161              
162             sudo apt install libio-socket-ssl-perl
163             sudo cpanm LWP::Protocol::https
164              
165             My system is Ubuntu, which can use apt to install IO::Socket::SSL. I can't install this module with cpanm tool.
166              
167             use Net::Cloudflare::DNS;
168              
169             # new the object
170             my $dns = Net::Cloudflare::DNS->new(email=>$email, api_key=>$api_key, zone_id=>$zone_id);
171              
172             # create the record
173             my $res = $dns->create_record(type=>"A", name=>"test.myhostnames.com",content=>"1.1.1.1",ttl=>1);
174              
175             # update the record
176             $res = $dns->update_record($record_id, type=>"TXT", name=>"test.myhostnames.com",content=>"bala bala",ttl=>1);
177              
178             # delete the record
179             $res = $dns->delete_record($record_id);
180              
181             # list records by conditions
182             $res = $dns->get_records(name=>"test.myhostnames.com");
183              
184             # for success response, the content is stored in $res. otherwise the method just dies.
185             print $res;
186              
187              
188              
189             =head1 SUBROUTINES/METHODS
190              
191             =head2 new
192              
193             my $dns = Net::Cloudflare::DNS->new(email=>$email, api_key=>$api_key, zone_id=>$zone_id);
194              
195             You have to provide 3 arguments to new() method. One is your registration email on Cloudflare. Another is your API Key, which can be
196             found on Cloudflare's management panel ("Global API Key"). The last is Zone ID, each zone has the unique ID, which can be found
197             on zone's page.
198              
199             Please notice: You must enable zone edit permissions for this API. In management panel, when you click "Create Token", you have the
200             chance to setup permissions for the zone, with which you can edit zone's DNS records.
201            
202              
203              
204             =head2 create_record
205              
206             my $res = $dns->create_record(type=>"A", name=>"test.myhostnames.com",content=>"1.1.1.1",ttl=>1);
207              
208             You can create record in the zone with this method.
209              
210             Required parameters:
211              
212             type: includes "A", "TXT", "MX", "CNAME" ... They are standard DNS record type.
213              
214             name: the hostname you want to create, must be FQDN.
215              
216             content: record's value, for "A" record, it's an IP address.
217              
218             ttl: time to live. You can always set it to 1, which means automatic by Cloudflare. otherwise it's must be larger than 120.
219              
220             Optional parameters:
221              
222             priority: MX's priority, default 0.
223              
224             proxied: whether proxied by cloudflare, default false.
225              
226             Please read the official documentation here:
227              
228             https://api.cloudflare.com/#dns-records-for-a-zone-properties
229              
230              
231              
232             =head2 update_record
233              
234             $res = $dns->update_record($record_id, type=>"TXT", name=>"test.myhostnames.com",content=>"bala bala",ttl=>1);
235              
236             Update the record in the zone.
237              
238             You must provide $record_id as the first argument, the left arguments are almost the same with create_record method.
239              
240             You can get $record_id from get_records method.
241              
242              
243              
244             =head2 delete_record
245              
246             $res = $dns->delete_record($record_id);
247              
248             Delete the record in the zone.
249              
250             You must provide $record_id as the unique argument.
251              
252              
253              
254             =head2 get_records
255              
256             $res = $dns->get_records(conditions...);
257              
258             List the records by conditions. For details you can read the documentation:
259              
260             https://api.cloudflare.com/#dns-records-for-a-zone-properties
261              
262              
263              
264             =head1 AUTHOR
265              
266             Wesley Peng, C<< >>
267              
268             =head1 BUGS
269              
270             Please report any bugs or feature requests to C, or through
271             the web interface at L. I will be notified, and then you'll
272             automatically be notified of progress on your bug as I make changes.
273              
274              
275              
276              
277             =head1 SUPPORT
278              
279             You can find documentation for this module with the perldoc command.
280              
281             perldoc Net::Cloudflare::DNS
282              
283              
284             You can also look for information at:
285              
286             =over 4
287              
288             =item * RT: CPAN's request tracker (report bugs here)
289              
290             L
291              
292             =item * CPAN Ratings
293              
294             L
295              
296             =item * Search CPAN
297              
298             L
299              
300             =back
301              
302              
303             =head1 ACKNOWLEDGEMENTS
304              
305              
306             =head1 LICENSE AND COPYRIGHT
307              
308             This software is Copyright (c) 2020 by Wesley Peng.
309              
310             This is free software, licensed under:
311              
312             The Artistic License 2.0 (GPL Compatible)
313              
314              
315             =cut
316              
317             1; # End of Net::Cloudflare::DNS