R53.pm

filter: Statementstmt 45.1% 65 / 144 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 48.6% 18 / 37 Podpod 100% 9 / 9 totaltotal 40.1% 92 / 229 CC 18 SCAR 25.9
CC
18
Cov
37%
get_resource_record_sets
56.0
create_hosted_zone
30.0
delete_hosted_zone
20.0
CRAP 13.3
« R53/HostedZone.pm ↑ Summary R53/Role/NewFromRawData.pm »
1 #
2 # This file is part of Net-Amazon-R53
3 #
4 # This software is Copyright (c) 2012 by Campus Explorer, Inc.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Net::Amazon::R53;
11 {
12 4 $Net::Amazon::R53::VERSION = '0.002'; # TRIAL
13 }
14  
15 # ABSTRACT: An interface to Amazon's Route53
16  
17 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use autobox::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use MooseX::AlwaysCoerce;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use MooseX::AttributeShortcuts 0.017;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use MooseX::Types::Common::String ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use MooseX::Types::Path::Class ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 4 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use File::ShareDir::ProjectDistDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use List::AllUtils 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use LWP::UserAgent::Determined;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 4 ▶use aliased 'Net::Amazon::Signature::V3';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # debugging...
40 #use Smart::Comments '###';
41  
42 4 with 'MooseX::RelatedClasses' => {
43 names => [ qw{
44 AtomicChange
45 ChangeInfo
46 HostedZone
47 ResourceRecordSet
48 ResourceRecordSet::Stub
49 ResourceRecordSet::Change
50 } ],
51 };
52  
53  
54 has $_ => (is => 'ro', required => 1, isa => NonEmptySimpleStr)
55 4 for qw{ id key };
56  
57  
58 has signer => (
59 is => 'lazy',
60 isa => 'Net::Amazon::Signature::V3',
61 0 ▶ builder => sub { V3->new(id => $_[0]->id, key => $_[0]->key) },
Subroutine _build_signer: not called CC=1 SCAR=6.9
62 4 );
63  
64 has ua => (
65 is => 'lazy',
66 isa => 'LWP::UserAgent::Determined',
67 0 ▶ builder => sub { LWP::UserAgent::Determined->new },
Subroutine _build_ua: not called CC=1 SCAR=6.9
68 4 );
69  
70 has endpoint_base => (
71 is => 'lazy',
72 isa => NonEmptySimpleStr,
73 0 ▶ builder => sub { 'https://route53.amazonaws.com/2012-02-29/' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _build_endpoint_base: not called CC=1 SCAR=6.9
74 4 );
75  
76  
77 4 has hosted_zones_hash => (
78 traits => ['Hash'],
79 is => 'lazy',
80 isa => 'HashRef[Net::Amazon::R53::HostedZone]',
81 clearer => 1,
82 predicate => 'has_fetched_hosted_zones',
83 handles => {
84 # predicate not for the attribute, but content
85 has_hosted_zones => 'count',
86 has_hosted_zone => 'exists',
87 hosted_zone_ids => 'keys',
88 hosted_zones => 'values',
89 hosted_zone => 'get',
90 hosted_zone_by_id => 'get',
91 hosted_zones_count => 'count',
92 _add_hosted_zone => 'set',
93 _delete_hosted_zone => 'delete',
94 },
95 );
96  
97 0 ▶sub _build_hosted_zones_hash {
Subroutine _build_hosted_zones_hash: not called CC=1 SCAR=6.9
98 0 my $self = shift @_;
99  
100 0 my $part = 'hostedzone?maxitems=100';
101 0 my $resp = $self->get_request($part, undef);
102  
103 ### parsing content: $resp->content
104 0 my $zones = XMLin(
105 $resp->content,
106 ForceArray => [ qw{ HostedZone Config } ],
107 GroupTags => { HostedZones => 'HostedZone' },
108 );
109  
110 ### $zones
111 0 my $hz_class = $self->hosted_zone_class;
112 my @zones =
113 0 map { $hz_class->new_from_raw_data($self, $_) }
114 $zones->{HostedZones}->flatten
115 0 ;
116  
117 0 return { map { $_->plain_id => $_ } @zones };
118 }
119  
120  
121 0 ▶sub hosted_zone_by_caller_reference {
Subroutine hosted_zone_by_caller_reference: not called CC=1 SCAR=6.9
Pod documented
122 0 my ($self, $caller_ref) = @_;
123  
124 # implementation note: caller references are guaranteed unique by Amazon,
125 # so using first() here isn't going to have any weird side-effects.
126  
127 ### searching for: $caller_ref
128 my $hz =
129 0 ▶ first { $_->caller_reference eq $caller_ref }
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 $self->hosted_zones
131 ;
132  
133 0 return $hz;
134 }
135  
136  
137 # TODO move into HZ class?
138 0 ▶sub get_resource_record_sets {
Subroutine get_resource_record_sets: not called CC=7 SCAR=40.3
Pod documented
139 0 my ($self, $hz_id) = @_;
140  
141 ### fetching records for hostedzone: $hz_id
142 0 my $base_part = my $part = "hostedzone/$hz_id/rrset?maxitems=100";
143 0 my @unparsed_rrs;
144  
145 0 while (1) {
146  
147 ### getting rrs via: $part
148 0 my $resp = $self->get_request($part, undef);
149  
150 ### parsing content: $resp->content
151 0 my $rrs_set = XMLin(
152 $resp->content,
153 ForceArray => [ qw{ ResourceRecordSet ResourceRecord } ],
154 GroupTags => {
155 ResourceRecordSets => 'ResourceRecordSet',
156 ResourceRecords => 'ResourceRecord',
157 },
158 );
159  
160 # I can't _quite_ figure out how to get XML::Simple to do this for me
161 0 ▶ $rrs_set->{ResourceRecordSets} ||= [];
Condition0%0/2
$rrs_set->{"ResourceRecordSets"} ||= []
l!l
00
MC/DC0%0/1
$rrs_set->{"ResourceRecordSets"} ||= []
$rrs_set->{"ResourceRecordSets"}
Truth table0%0/2
A
A= $rrs_set->{"ResourceRecordSets"}
Aresult
11
00
162 0 for my $rrs ($rrs_set->{ResourceRecordSets}->flatten) {
163  
164 0 ▶ next unless $rrs->{ResourceRecords};
Branch: 1 branch
BranchTrueFalseTotal
unless $rrs->{"ResourceRecords"} 0 0 0
165 my @values =
166 0 map { $_->{Value} }
167 0 $rrs->{ResourceRecords}->flatten;
168 0 $rrs->{ResourceRecords} = \@values;
169 }
170  
171 0 push @unparsed_rrs, $rrs_set->{ResourceRecordSets}->flatten;
172  
173 0 ▶ last unless $rrs_set->{IsTruncated} eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
unless $rrs_set->{"IsTruncated"} eq "true" 0 0 0
174  
175 # prep and create our next part...
176 0 my $query = q{};
177 0 ▶ my $_val = sub { $rrs_set->{'NextRecord' . $_[0]->ucfirst} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
178  
179 0 ▶ do { $query .= "&$_=" . $_val->($_) if $_val->($_) }
Branch: 1 branch
BranchTrueFalseTotal
if &$_val($_) 0 0 0
180 0 for qw{ name type identifier };
181  
182 0 $part = $base_part . $query;
183 }
184  
185 my @rrs =
186 0 map { $self->resource_record_set_class->new_from_raw_data($self, $_) }
187 @unparsed_rrs
188 ;
189  
190 0 return \@rrs;
191 }
192  
193  
194 0 ▶sub create_hosted_zone {
Subroutine create_hosted_zone: not called CC=5 SCAR=34.0
Pod documented
195 0 my $self = shift @_;
196  
197 0 my %opts = validated_hash(\@_,
198 name => { isa => 'Str' },
199 comment => { isa => 'Str', optional => 1 },
200 caller_reference => { isa => 'Str', optional => 1 },
201 );
202  
203 # MX::Params::Validate doesn't deal with dynamic defaults very well yet
204  
205 0 ▶ $opts{comment} //= 'Created ' . localtime;
Condition0%0/2
$opts{"comment"} //= 'Created ' . (localtime)
l!l
00
MC/DC0%0/1
$opts{"comment"} //= 'Created ' . (localtime)
$opts{"comment"}
Truth table0%0/2
A
A= $opts{"comment"}
Aresult
11
00
206 0 ▶ $opts{caller_reference} //= Data::UUID->new->create_str;
Condition0%0/3
$opts{"caller_reference"} //= "Data::UUID"->new->create_str
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"caller_reference"} //= "Data::UUID"->new->create_str
$opts{"caller_reference"} "Data::UUID"->new->create_str
Truth table0%0/3
A //= B
A= $opts{"caller_reference"}B= "Data::UUID"->new->create_str
ABresult
1X1
011
000
207  
208 0 my $part = 'hostedzone';
209 0 my $tmpl = 'create_hosted_zone.tt';
210  
211 0 $self->tt->process('create_hosted_zone.tt', { %opts }, \(my $req_content));
212  
213 ### $req_content
214 0 my $resp = $self->post_request($part, $req_content);
215  
216 ### response: $resp->content
217 0 my $info = XMLin($resp->content, ForceArray => [ qw{ Config } ]);
218 ### $info
219 0 my $change = $self->change_info_class->new_from_raw_data($self, $info->{ChangeInfo});
220 0 my $hz = $self->hosted_zone_class->new_from_raw_data($self, $info->{HostedZone});
221 # TODO delegtion info
222  
223 0 ▶ $self->_add_hosted_zone($hz->plain_id => $hz)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_fetched_hosted_zones 0 0 0
224 if $self->has_fetched_hosted_zones;
225  
226 0 ▶ return wantarray ? ($hz, $change) : $hz;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
227 }
228  
229  
230 0 ▶sub copy_hosted_zone {
Subroutine copy_hosted_zone: not called CC=3 SCAR=24.8
Pod documented
231 0 my ($self, $hz) = @_;
232  
233 #my $self = shift @_;
234 #my ($hz) = validated_list
235  
236 0 ▶ confess '$hz is not a HostedZone instance!'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($hz) and $hz->isa("Net::Amazon::R53::HostedZone") 0 0 0
Condition0%0/3
builtin::blessed($hz) and $hz->isa("Net::Amazon::R53::HostedZone")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($hz) and $hz->isa("Net::Amazon::R53::HostedZone")
builtin::blessed($hz) $hz->isa("Net::Amazon::R53::HostedZone")
Truth table0%0/3
A and B
A= builtin::blessed($hz)B= $hz->isa("Net::Amazon::R53::HostedZone")
ABresult
0X0
100
111
237 unless blessed $hz && $hz->isa('Net::Amazon::R53::HostedZone');
238  
239 0 my $comment = 'Creating copy of zone ' . $hz->plain_id;
240  
241 0 my ($new_hz, $change) = $self->create_hosted_zone(
242 name => $hz->name,
243 comment => $comment,
244 );
245  
246 # if we haven't died yet, then the zone creation was queued/executed successfully
247  
248 my $copy_change = $new_hz->submit_resource_records_change_request(
249 comment => $comment,
250 multi_batch_ok => 1,
251 changes => [
252 0 map { { action => 'CREATE', record => $_ } }
253 0 grep { $_->type !~ /^(NS|SOA)$/ }
254 $hz->resource_record_sets->flatten
255 ],
256 );
257  
258 0 return $new_hz;
259 }
260  
261  
262 0 ▶sub delete_hosted_zone {
Subroutine delete_hosted_zone: not called CC=4 SCAR=30.0
Pod documented
263 0 my ($self, $hz_thing) = @_;
264  
265 0 ▶ my $path
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($hz_thing) ? : 0 0 0
$hz_thing =~ m[^/hostedzone/] ? : 0 0 0
266 = blessed $hz_thing ? $hz_thing->id
267 : $hz_thing =~ m!^/hostedzone/! ? $hz_thing
268 : "/hostedzone/$hz_thing"
269 ;
270  
271 0 my $resp = $self->delete_request($path, undef);
272  
273 # OK if we make it here w/o dying
274 0 ▶ $self->_delete_hosted_zone($path->split(qr!/!)->pop)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_fetched_hosted_zones 0 0 0
275 if $self->has_fetched_hosted_zones;
276  
277 # so here we do something a little different. We use the ChangeInfo data
278 # to construct our change object; this is the raw data that gets passed in
279 # via new_from_raw_data(), but we also pass 'raw_data => $x', where $x is
280 # the full set of returned data from Amazon.
281 #
282 # I could be easily convinced that some other approach is better.
283  
284 ### response: $resp->content
285 0 my $info = XMLin($resp->content, KeepRoot => 1);
286 return $self->change_info_class->new_from_raw_data(
287 $self,
288 $info->{DeleteHostedZoneResponse}->{ChangeInfo},
289 0 raw_data => $info,
290 );
291 }
292  
293  
294 has template_dir => (
295 is => 'lazy',
296 isa => Dir,
297 0 ▶ builder => sub { dist_dir 'Net-Amazon-R53' },
Subroutine _build_template_dir: not called CC=1 SCAR=6.9
298 4 );
299  
300 has tt => (
301 is => 'lazy',
302 isa_class => 'Template',
303 0 ▶ builder => sub { Template->new(DEBUG => 1, INCLUDE_PATH => shift->template_dir) },
Subroutine _build_tt: not called CC=1 SCAR=6.9
304 4 );
305  
306 # build a full url
307 0 ▶sub _full_endpoint { shift->endpoint_base . shift }
Subroutine _full_endpoint: not called CC=1 SCAR=6.9
308  
309  
310 0 ▶sub get_request { shift->request(GET => @_) }
Subroutine get_request: not called CC=1 SCAR=6.9
Pod documented
311 0 ▶sub post_request { shift->request(POST => @_) }
Subroutine post_request: not called CC=1 SCAR=6.9
Pod documented
312 0 ▶sub delete_request { shift->request(DELETE => @_) }
Subroutine delete_request: not called CC=1 SCAR=6.9
Pod documented
313  
314 0 ▶sub request {
Subroutine request: not called CC=3 SCAR=24.8
Pod documented
315 0 my ($self, $method, $part, $content) = @_;
316  
317 # wtf lwp?
318 0 ▶ $content ||= undef;
Condition0%0/2
$content ||= undef
l!l
00
MC/DC0%0/1
$content ||= undef
$content
Truth table0%0/2
A
A= $content
Aresult
11
00
319  
320 0 my $req = HTTP::Request->new(
321 $method,
322 $self->_full_endpoint($part),
323 [
324 $self->signer->signed_headers,
325 Host => 'route53.amazonaws.com',
326 ],
327 $content,
328 );
329  
330 ### request: $req->as_string
331 0 my $resp = $self->ua->request($req);
332  
333 ### request status: $resp->status_line
334 0 ▶ confess "Fail! " . $resp->content
Branch: 1 branch
BranchTrueFalseTotal
if $resp->is_error 0 0 0
335 if $resp->is_error;
336  
337 0 return $resp;
338 }
339  
340 4 __PACKAGE__->meta->make_immutable;
341 4 !!42;
342  
343 __END__
« R53/HostedZone.pm ↑ Summary R53/Role/NewFromRawData.pm »