line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Cloudflare::Record; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Mojo::Cloudflare::Record - Represent a Cloudflare DNS record |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 DESCRIPTION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
L represents a DNS record in the |
10
|
|
|
|
|
|
|
L module. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
This module inherit from L. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
3
|
|
|
3
|
|
13
|
use Mojo::Base 'Mojo::JSON::Pointer'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
19
|
|
17
|
3
|
|
|
3
|
|
510
|
use Mojo::JSON::Pointer; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
13
|
|
18
|
3
|
|
|
3
|
|
99
|
use Mojo::UserAgent; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
49
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
require Mojo::Cloudflare; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head2 content |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$str = $self->content; |
27
|
|
|
|
|
|
|
$self = $self->content($str); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
The content of the DNS record, will depend on the the type of record being |
30
|
|
|
|
|
|
|
added. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
This attribute is required to do anything useful with this object. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 id |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$str = $self->id; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
The identifier from Cloudflare. Can only be set in constructor. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 name |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$str = $self->name; |
43
|
|
|
|
|
|
|
$self = $self->name($str); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Name of the DNS record. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
This attribute is required to do anything useful with this object. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head2 priority |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$int = $self->priority; |
52
|
|
|
|
|
|
|
$self = $self->priority($int); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
MX record priority. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 ttl |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$int = $self->ttl; |
59
|
|
|
|
|
|
|
$self = $self->ttl($int); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
TTL of record in seconds. 1 (default) = Automatic, otherwise, value must in |
62
|
|
|
|
|
|
|
between 120 and 86400 seconds. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 service_mode |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$int = $self->service_mode; |
67
|
|
|
|
|
|
|
$self = $self->service_mode($int); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Status of CloudFlare Proxy: |
70
|
|
|
|
|
|
|
1 = orange cloud (active), |
71
|
|
|
|
|
|
|
0 = grey cloud (deactive). |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head2 type |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
$str = $self->type; |
76
|
|
|
|
|
|
|
$self = $self->type($str); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Type of the DNS record: A, CNAME, MX, TXT, SPF, AAAA, NS, SRV, or LOC. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
This attribute is required to do anything useful with this object. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
for my $attr (qw( content name priority type )) { |
85
|
|
|
|
|
|
|
has $attr => sub { $_[0]->get("/$attr") || $_[0]->get("/obj/$attr") || "" }; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
0
|
0
|
1
|
0
|
sub id { $_[0]->{id} ||= $_[0]->get("/obj/rec_id") || "" }; |
|
|
|
0
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
has service_mode => ''; |
91
|
|
|
|
|
|
|
has ttl => sub { shift->data->{ttl} || 1 }; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Will be public once I know what to call the attribute |
94
|
|
|
|
|
|
|
has _cf => sub { Mojo::Cloudflare->new }; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 METHODS |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 delete |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
$self = $self->delete(sub { my($self, $err) = @_; ... }); |
101
|
|
|
|
|
|
|
$self = $self->delete; # die $err on failure |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Used to save delete record from Cloudflare. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub delete { |
108
|
0
|
|
|
0
|
1
|
0
|
my($self, $cb) = @_; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
0
|
$self->_cf->_post( |
111
|
|
|
|
|
|
|
{ a => 'rec_delete', id => $self->id, _class => $self }, |
112
|
|
|
|
|
|
|
$cb, |
113
|
|
|
|
|
|
|
); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 save |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
$self = $self->save(sub { my($self, $err) = @_; ... }); |
119
|
|
|
|
|
|
|
$self = $self->save; # die $err on failure |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Used to save record to Cloudflare. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=cut |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub save { |
126
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
127
|
0
|
0
|
|
|
|
0
|
return $self->id ? $self->_rec_edit(@_) : $self->_rec_new(@_); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub _new_from_tx { |
131
|
2
|
|
|
2
|
|
7
|
my($class, $tx) = @_; |
132
|
2
|
|
|
|
|
9
|
my $err = $tx->error; |
133
|
2
|
|
50
|
|
|
240
|
my $json = $tx->res->json || {}; |
134
|
|
|
|
|
|
|
|
135
|
2
|
|
50
|
|
|
2163
|
$json->{result} //= ''; |
136
|
2
|
50
|
0
|
|
|
15
|
$err ||= $json->{msg} || $json->{result} || 'Unknown error.' if $json->{result} ne 'success'; |
|
|
|
0
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
2
|
50
|
|
|
|
9
|
if(ref $class) { # object instead of class |
139
|
0
|
|
0
|
|
|
0
|
my $obj = $json->{response}{rec}{obj} || {}; |
140
|
0
|
|
|
|
|
0
|
for my $k (keys %$obj) { |
141
|
0
|
|
|
|
|
0
|
$class->data->{obj}{$k} = $obj->{$k}; |
142
|
0
|
0
|
|
|
|
0
|
$class->$k($obj->{$k}) if $class->can($k); |
143
|
|
|
|
|
|
|
} |
144
|
0
|
|
|
|
|
0
|
return $err, $class; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
else { |
147
|
2
|
|
100
|
|
|
35
|
return $err, $class->new($json->{response}{rec} || {}); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub _rec_new { |
152
|
0
|
|
|
0
|
|
|
my($self, $cb) = @_; |
153
|
0
|
|
|
|
|
|
my %args = map { ($_, $self->$_) } qw( content name ttl type ); |
|
0
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
$args{_class} = $self; |
156
|
0
|
|
|
|
|
|
$args{a} = 'rec_new'; |
157
|
0
|
0
|
|
|
|
|
$args{prio} = $self->priority if length $self->priority; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
return $self->_cf->_post(\%args, $cb); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub _rec_edit { |
163
|
0
|
|
|
0
|
|
|
my($self, $cb) = @_; |
164
|
0
|
|
|
|
|
|
my %args = map { ($_, $self->$_) } qw( content name ttl type ); |
|
0
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
$args{_class} = $self; |
167
|
0
|
|
|
|
|
|
$args{a} = 'rec_edit'; |
168
|
0
|
0
|
|
|
|
|
$args{id} = $self->id or die "Cannot update record ($self->{name}) without 'id'"; |
169
|
0
|
0
|
|
|
|
|
$args{prio} = $self->priority if length $self->priority; |
170
|
0
|
0
|
|
|
|
|
$args{service_mode} = $self->service_mode ? 1 : 0 if length $self->service_mode; |
|
|
0
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
return $self->_cf->_post(\%args, $cb); |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Copyright (C) 2014, Jan Henning Thorsen |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under |
180
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 AUTHOR |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Jan Henning Thorsen - C |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
1; |