blib/lib/Net/Cloudflare/DNS.pm

filter: Statementstmt 30.1% 22 / 73 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 58.3% 7 / 12 Podpod 100% 5 / 5 totaltotal 33.3% 34 / 102 CC 7 SCAR 16.2
CC
7
Cov
26%
new
12.0
get_records
6.0
create_record
6.0
CRAP 5.1
↑ Summary
1 package Net::Cloudflare::DNS;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11
12 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
13 0 my $class = shift;
14 0 my %args = @_;
15  
16 0 my $ua = LWP::UserAgent->new;
17 0 my %headers;
18  
19 0 ▶ if (defined $args{api_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'api_token'}) { } 0 0 0
20 0 %headers = (
21 'Content-Type' => 'application/json',
22 'Authorization' => "Bearer $args{api_token}",
23 );
24 } else {
25 %headers = (
26 'Content-Type' => 'application/json',
27 'X-Auth-Key' => $args{api_key},
28 'X-Auth-Email' => $args{email},
29 0 );
30 }
31  
32 0 my $zone_id = $args{zone_id};
33 0 ▶ die "no zone_id provided" unless defined $zone_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $zone_id 0 0 0
34  
35 0 my $base_url = "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records";
36  
37 0 bless { ua => $ua,
38 headers => \%headers,
39 base_url => $base_url,
40 }, $class;
41 }
42
43 0 ▶sub get_records {
Subroutine get_records: not called CC=2 SCAR=17.9
Pod documented
44 0 my $self = shift;
45 0 my %args = @_;
46  
47 0 my $uri = URI->new($self->{base_url});
48 0 $uri->query_form(%args);
49 0 my $ua = $self->{ua};
50  
51 my $res = $ua->get($uri,
52 0 %{$self->{headers}},
53 );
54  
55 0 ▶ if ($res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
56 0 return decode_json($res->decoded_content);
57 } else {
58 0 die $res->status_line, $res->decoded_content;
59 }
60 }
61  
62 0 ▶sub create_record {
Subroutine create_record: not called CC=2 SCAR=17.9
Pod documented
63 0 my $self = shift;
64 0 my %args = @_;
65  
66 0 my $data = encode_json(\%args);
67 0 my $uri = URI->new($self->{base_url});
68 0 my $ua = $self->{ua};
69  
70 my $res = $ua->post($uri,
71 0 %{$self->{headers}},
72 Content => $data,
73 );
74  
75 0 ▶ if ($res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
76 0 return decode_json($res->decoded_content);
77 } else {
78 0 die $res->status_line, $res->decoded_content;
79 }
80 }
81  
82 0 ▶sub update_record {
Subroutine update_record: not called CC=2 SCAR=17.9
Pod documented
83 0 my $self = shift;
84 0 my $record_id = shift;
85 0 my %args = @_;
86  
87 0 my $data = encode_json(\%args);
88 0 my $uri = URI->new($self->{base_url} . "/$record_id");
89 0 my $ua = $self->{ua};
90  
91 my $res = $ua->put($uri,
92 0 %{$self->{headers}},
93 Content => $data,
94 );
95  
96 0 ▶ if ($res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
97 0 return decode_json($res->decoded_content);
98 } else {
99 0 die $res->status_line, $res->decoded_content;
100 }
101 }
102  
103 0 ▶sub delete_record {
Subroutine delete_record: not called CC=2 SCAR=17.9
Pod documented
104 0 my $self = shift;
105 0 my $record_id = shift;
106  
107 0 my $uri = URI->new($self->{base_url} . "/$record_id");
108 0 my $ua = $self->{ua};
109
110 my $res = $ua->delete($uri,
111 0 %{$self->{headers}},
112 );
113  
114 0 ▶ if ($res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
115 0 return decode_json($res->decoded_content);
116 } else {
117 0 die $res->status_line, $res->decoded_content;
118 }
119 }
120  
121 =head1 NAME
122  
123 Net::Cloudflare::DNS - DNS API for Cloudflare API v4
124  
125 =head1 VERSION
126  
127 Version 0.22
128  
129 =cut
130  
131 1 our $VERSION = '0.22';
132  
133  
134 =head1 SYNOPSIS
135  
136 This perl module is working for Cloudflare DNS API v4.
137  
138 My system is Ubuntu, to use the module, I have the following perl modules pre-installed in the system.
139  
140 sudo apt install libio-socket-ssl-perl
141 sudo cpanm LWP::Protocol::https
142  
143 After then, you can run "cpanm Net::Cloudflare::DNS" to install this module.
144  
145 use Net::Cloudflare::DNS;
146 use Data::Dumper;
147  
148 # zone_id for your domain
149 my $zone_id = " ";
150  
151 # new the object with email and api_key
152 my $email = " ";
153 my $api_key = " ";
154 my $dns = Net::Cloudflare::DNS->new(email=>$email, api_key=>$api_key, zone_id=>$zone_id);
155  
156 # or, new the object with bearer token
157 my $api_token = " ";
158 my $dns = Net::Cloudflare::DNS->new(api_token=>$api_token, zone_id=>$zone_id);
159  
160 # create record
161 $dns->create_record(type=>"A", name=>"www.sample.com", content=>"74.81.81.81", proxied=>\1, ttl=>60);
162  
163 # get records
164 my $res = $dns->get_records(name=>"www.sample.com");
165  
166 # parse record id
167 my $rid = $res->{result}->[0]->{id};
168  
169 # update record
170 $dns->update_record($rid, type=>"A", name=>"www.sample.com",content=>"52.1.14.22", proxied=>\0, ttl=>60);
171  
172 # delete record
173 $dns->delete_record($rid);
174  
175 If the instance method succeeds, cloudflare's response is resturned as a reference to you.
176 Otherwise it just dies, you could catch the error in the code.
177  
178 $res = $dns->create_record(...);
179 print Dumper $res;
180  
181  
182 =head1 SUBROUTINES/METHODS
183  
184 =head2 new
185  
186 my $dns = Net::Cloudflare::DNS->new(email=>$email, api_key=>$api_key, zone_id=>$zone_id);
187 # or,
188 my $dns = Net::Cloudflare::DNS->new(api_token=>$api_token, zone_id=>$zone_id);
189  
190 You have to provide either email+api_key or api_token along with zone_id to new() method.
191 All those values can be found on cloudflare's management panel.
192  
193 Please note: You must enable zone edit permissions for this API. In management panel, when you click "Create Token",
194 you have the chance to setup permissions for the zone, with which you can edit zone's DNS records.
195
196  
197 =head2 create_record
198  
199 $dns->create_record(type=>"A", name=>"www.sample.com",content=>"1.2.3.4",proxied=>\1,ttl=>1);
200  
201 Create record in the zone.
202  
203 You have to provide the following parameters.
204  
205 type: includes "A", "TXT", "MX", "CNAME" ... They are standard DNS record types.
206  
207 name: the hostname you want to create, such as www.example.com.
208  
209 content: record value, for "A" record, it's an IP address.
210  
211 ttl: time to live. You can set it to 1, which means to be automated by Cloudflare.
212  
213 Optional parameters:
214  
215 priority: MX priority, default 0.
216  
217 proxied: whether proxied by cloudflare, it's either \1 (true) or \0 (false).
218  
219 Please read their official documentation below.
220  
221 https://developers.cloudflare.com/api/#dns-records-for-a-zone-properties
222  
223  
224 =head2 update_record
225  
226 $dns->update_record($record_id, type=>"TXT", name=>"www.sample.com",content=>"bala bala",ttl=>1);
227  
228 Update record in the zone.
229  
230 Please provide $record_id as the first argument, the rest are almost the same as create_record.
231  
232 You can get $record_id from get_records method.
233  
234  
235 =head2 delete_record
236  
237 $dns->delete_record($record_id);
238  
239 Delete record from the zone.
240  
241 Provide $record_id as the unique argument.
242  
243  
244 =head2 get_records
245  
246 my $res = $dns->get_records(conditions...);
247  
248 List records by conditions. For details please read the following documentation.
249  
250 https://developers.cloudflare.com/api/#dns-records-for-a-zone-properties
251  
252  
253  
254 =head1 AUTHOR
255  
256 Wesley Peng, C<< <wesley at cpan.org> >>
257  
258 =head1 BUGS
259  
260 Please report any bugs or feature requests to C<bug-net-cloudflare-dns at rt.cpan.org>, or through
261 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Cloudflare-DNS>. I will be notified, and then you'll
262 automatically be notified of progress on your bug as I make changes.
263  
264  
265 =head1 THANKS
266  
267 1. Thibault, C<< <thibault.duponchelle at gmail.com> >> for porting the bearer token authentication method.
268  
269  
270 =head1 SUPPORT
271  
272 You can find documentation for this module with the perldoc command.
273  
274 perldoc Net::Cloudflare::DNS
275  
276  
277 You can also look for information at:
278  
279 =over 4
280  
281 =item * RT: CPAN's request tracker (report bugs here)
282  
283 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Cloudflare-DNS>
284  
285 =item * CPAN Ratings
286  
287 L<https://cpanratings.perl.org/d/Net-Cloudflare-DNS>
288  
289 =item * Search CPAN
290  
291 L<https://metacpan.org/release/Net-Cloudflare-DNS>
292  
293 =back
294  
295  
296 =head1 ACKNOWLEDGEMENTS
297  
298  
299 =head1 LICENSE AND COPYRIGHT
300  
301 This software is Copyright (c) 2020 by Wesley Peng.
302  
303 This is free software, licensed under:
304  
305 The Artistic License 2.0 (GPL Compatible)
306  
307  
308 =cut
309  
310 1 1; # End of Net::Cloudflare::DNS
↑ Summary