blib/lib/Net/DNS/Create/Route53.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 SCAR 0.0
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Build ↑ Summary
1 # Copyright (c) 2013-2014 David Caldwell, All Rights Reserved.
2  
3 package Net::DNS::Create::Route53;
4 1 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Net::DNS::Create qw(internal full_host email interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::Amazon::Route53;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 our %config;
12 sub import {
13 my $package = shift;
14 my %c = @_;
15 $config{$_} = $c{$_} for keys %c;
16 }
17  
18 my $r53;
19 sub r53() {
20 $r53 //= Net::Amazon::Route53->new(id => $config{amazon_id},
21 key => $config{amazon_key});
22 }
23  
24 my $zones;
25 sub hosted_zone($) {
26 # The eval works around a bug in Net::Amazon::Route53 where it dies if there are no zones at all.
27 $zones = eval { [r53->get_hosted_zones()] } || [] unless defined $zones;
28 (grep { $_->name eq $_[0] } @$zones)[0] // undef;
29 }
30  
31 sub txt(@) {
32 map { s/[^-a-zA-Z0-9._='():;* ]/$& eq '"' ? '\\"' : sprintf("\\%03o", ord($&))/ge;
33 "\"$_\"" } @_;
34 }
35  
36 sub group_by_type_and_name($$) {
37 my ($re, $entries) = @_;
38 my %set;
39 for my $r (grep { lc($_->type) =~ $re } @$entries) {
40 push @{$set{$r->type .'_'. $r->name}}, $r;
41 }
42 map { $set{$_} } keys %set;
43 }
44  
45 my @domain;
46 sub _domain() { @domain } # Hook for testing
47 sub domain($$) {
48 my ($package, $domain, $entries) = @_;
49  
50 my @entries = map { ;
51 my $rr = lc $_->type;
52  
53 $rr eq 'soa' ? () : # Amazon manages its own SOA stuff. Just ignore things we might have.
54 $rr eq 'rp' ? (warn("Amazon doesn't support RP records :-(") && ()) :
55  
56 $rr eq 'mx' || $rr eq 'ns' || $rr eq 'srv' || $rr eq 'txt' ? () : # Handled specially, below
57  
58 +{
59 action => 'create',
60 name => $_->name.'.',
61 ttl => $_->ttl,
62 type => uc $rr,
63 $rr eq 'a' ? (value => $_->address) :
64 $rr eq 'cname' ? (value => $_->cname.'.') :
65 (err => warn "Don't know how to handle \"$rr\" RRs yet.")
66  
67 }
68 } @$entries;
69  
70 # Amazon wants all NS,MX,TXT and SRV entries for a particular name in one of their entries. We get them in as
71 # separate entries so first we have to group them together.
72 push @entries, map { my @set = @$_;
73 my $rr = lc $set[0]->type;
74 $rr eq 'ns' && $set[0]->name.'.' eq $domain ? () : # Amazon manages its own NS stuff. Just ignore things we might have.
75 +{
76 action => 'create',
77 name => $set[0]->name.'.',
78 ttl => $set[0]->ttl,
79 type => uc $rr,
80 $rr eq 'mx' ? (records => [map { $_->preference." ".$_->exchange.'.' } @set]) :
81 $rr eq 'ns' ? (records => [map { $_->nsdname.'.' } @set] ) :
82 $rr eq 'srv' ? (records => [map { $_->priority ." ".$_->weight ." ".$_->port ." ".$_->target.'.' } @set]) :
83 $rr eq 'txt' ? (records => [map { join ' ', txt($_->char_str_list) } @set]) :
84 (err => die uc($rr)." can't happen here!")
85 }
86 } group_by_type_and_name(qr/^(?:mx|ns|srv|txt)$/, $entries);
87  
88 push @domain, { name => $domain,
89 entries => \@entries };
90 }
91  
92 my $counter = rand(1000);
93 sub master() {
94 my ($package) = @_;
95 local $|=1;
96  
97 for my $domain (@domain) {
98 my $zone = hosted_zone(full_host($domain->{name}));
99 if (!$zone && scalar @{$domain->{entries}}) {
100 my $hostedzone = Net::Amazon::Route53::HostedZone->new(route53 => r53,
101 name => $domain->{name},
102 comment=>(getpwuid($<))[0].'/'.__PACKAGE__,
103 callerreference=>__PACKAGE__."-".localtime."-".($counter++));
104 print "New Zone: $domain->{name}...";
105 $hostedzone->create();
106 $zone = $hostedzone;
107 print "Created. Nameservers:\n".join('', map { " $_\n" } @{$zone->nameservers});
108 }
109  
110 if ($zone) {
111 my $current = [ grep { $_->type ne 'SOA' && ($_->type ne 'NS' || $_->name ne $domain->{name}) } @{$zone->resource_record_sets} ];
112 my $new = [ map { Net::Amazon::Route53::ResourceRecordSet->new(%{$_},
113 values => [$_->{value} // @{$_->{records}}],
114 route53 => r53,
115 hostedzone => $zone) } @{$domain->{entries}} ];
116 printf "%s: %d -> %d\n", $domain->{name}, scalar @$current, scalar @$new;
117 my $change = scalar @$current > 0 ? r53->atomic_update($current,$new) :
118 scalar @$new > 0 ? r53->batch_create($new) :
119 undef;
120  
121 unless (scalar @{$domain->{entries}}) {
122 print "Deleting $domain->{name}\n";
123 $zone->delete;
124 }
125 }
126 }
127 }
128  
129 sub domain_list($@) {
130 my $zone = hosted_zone(full_host($_[0]));
131 printf "%-30s %-30s %s\n", $zone ? $zone->id : '', $_[0], !$zone ? '' : ' ['.join(" ",@{$zone->nameservers}).']';
132 }
133  
134 sub master_list($$) {
135 # This doesn't really make sense in the route53 context
136 }
137  
138 1;
139 __END__
« Build ↑ Summary