File Coverage

blib/lib/AWS/CloudFront.pm
Criterion Covered Total %
statement 30 83 36.1
branch 0 24 0.0
condition 0 30 0.0
subroutine 10 16 62.5
pod 0 6 0.0
total 40 159 25.1


line stmt bran cond sub pod time code
1              
2             package AWS::CloudFront;
3              
4 1     1   13954 use VSO;
  1         13683  
  1         9  
5 1     1   4877 use LWP::UserAgent;
  1         119294  
  1         40  
6 1     1   26 use Carp 'confess';
  1         1  
  1         95  
7 1     1   7 use HTTP::Response;
  1         1  
  1         23  
8 1     1   3192 use IO::Socket::INET;
  1         37407  
  1         11  
9 1     1   1670 use Class::Load 'load_class';
  1         95682  
  1         65  
10              
11 1     1   648 use AWS::CloudFront::Distribution;
  1         5  
  1         8  
12 1     1   951 use AWS::CloudFront::S3Origin;
  1         4  
  1         10  
13 1     1   668 use AWS::CloudFront::CustomOrigin;
  1         3  
  1         38  
14 1     1   13 use AWS::CloudFront::OriginAccessIdentity;
  1         2  
  1         153  
15              
16             our $VERSION = '0.003';
17              
18             has 'access_key_id' => (
19             is => 'ro',
20             isa => 'Str',
21             required => 1,
22             );
23              
24             has 'secret_access_key' => (
25             is => 'ro',
26             isa => 'Str',
27             required => 1,
28             );
29              
30             has 'ua' => (
31             is => 'ro',
32             isa => 'LWP::UserAgent',
33             lazy => 1,
34             required => 0,
35             default => sub { LWP::UserAgent->new( agent => 'foo/bar v1.2' ) }
36             );
37              
38              
39             sub request
40             {
41 0     0 0   my ($s, $type, %args) = @_;
42            
43 0           my $class = "AWS::CloudFront::Request::$type";
44 0           load_class($class);
45 0           return $class->new( %args, cf => $s, type => $type );
46             }# end request()
47              
48              
49             sub distribution
50             {
51 0     0 0   my ($s, %args) = @_;
52            
53 0           my $type = 'GetDistribution';
54 0           my $response = $s->request( $type, %args )->request();
55 0           my $xpc = $response->xpc;
56            
57 0 0         if( my ($node) = $xpc->findnodes('.//cf:Distribution') )
58             {
59 0           my $origin;
60 0 0         if( my ($origin_s3) = $xpc->findnodes('.//cf:S3Origin', $node) )
    0          
61             {
62 0           $origin = AWS::CloudFront::S3Origin->new(
63             OriginAccessIdentity => $xpc->findvalue('.//cf:OriginAccessIdentity', $origin_s3),
64             DNSName => $xpc->findvalue('.//cf:DNSName', $origin_s3),
65             );
66             }
67             elsif( my $origin_custom = $xpc->findnode('.//cf:CustomOrigin', $node) )
68             {
69            
70             }# end if()
71 0 0 0       my $dist = AWS::CloudFront::Distribution->new(
      0        
      0        
      0        
      0        
72             cf => $s,
73             Id => $xpc->findvalue('.//cf:Id', $node),
74             Status => $xpc->findvalue('.//cf:Status', $node),
75             LastModifiedTime => $xpc->findvalue('.//cf:LastModifiedTime', $node),
76             DomainName => $xpc->findvalue('.//cf:DomainName', $node),
77             Enabled => $xpc->findvalue('.//cf:Enabled') eq 'true' ? 1 : 0,
78             DefaultRootObject => $xpc->findvalue('.//cf:DefaultRootObject') || undef,
79             CNAME => $xpc->findvalue('.//cf:CNAME') || undef,
80             InProgressValidationBatches => $xpc->findvalue('.//cf:InProgressValidationBatches') || undef,
81             Comment => $xpc->findvalue('.//cf:Comment') || undef,
82             CallerReference => $xpc->findvalue('.//cf:CallerReference') || undef,
83             Origin => $origin,
84             # TODO: Logging, ActiveTrustedSigners.
85             );
86 0           return $dist;
87             }# end if()
88             }# end distribution()
89              
90              
91             sub add_distribution
92             {
93 0     0 0   my ($s, %args) = @_;
94            
95 0           my $type = 'CreateDistribution';
96 0           my $response = $s->request( $type, %args )->request();
97 0           my $xpc = $response->xpc;
98              
99 0 0         if( my ($node) = $xpc->findnodes('.//cf:Distribution') )
100             {
101 0           my $origin;
102 0 0         if( my ($origin_s3) = $xpc->findnodes('.//cf:S3Origin', $node) )
    0          
103             {
104 0           $origin = AWS::CloudFront::S3Origin->new(
105             OriginAccessIdentity => $xpc->findvalue('.//cf:OriginAccessIdentity', $origin_s3),
106             DNSName => $xpc->findvalue('.//cf:DNSName', $origin_s3),
107             );
108             }
109             elsif( my $origin_custom = $xpc->findnode('.//cf:CustomOrigin', $node) )
110             {
111            
112             }# end if()
113 0 0 0       my $dist = AWS::CloudFront::Distribution->new(
      0        
      0        
      0        
      0        
114             cf => $s,
115             Id => $xpc->findvalue('.//cf:Id', $node),
116             Status => $xpc->findvalue('.//cf:Status', $node),
117             LastModifiedTime => $xpc->findvalue('.//cf:LastModifiedTime', $node),
118             DomainName => $xpc->findvalue('.//cf:DomainName', $node),
119             Enabled => $xpc->findvalue('.//cf:Enabled') eq 'true' ? 1 : 0,
120             DefaultRootObject => $xpc->findvalue('.//cf:DefaultRootObject') || undef,
121             CNAME => $xpc->findvalue('.//cf:CNAME') || undef,
122             InProgressValidationBatches => $xpc->findvalue('.//cf:InProgressValidationBatches') || undef,
123             Comment => $xpc->findvalue('.//cf:Comment') || undef,
124             CallerReference => $xpc->findvalue('.//cf:CallerReference') || undef,
125             Origin => $origin,
126             # TODO: Logging, ActiveTrustedSigners.
127             );
128 0           return $dist;
129             }# end if()
130             }# end add_distribution()
131              
132              
133             sub distributions
134             {
135 0     0 0   my ($s) = @_;
136            
137 0           my $type = 'GetDistributionList';
138 0           my $response = $s->request( $type )->request();
139            
140 0           my $xpc = $response->xpc;
141 0           my @dists = ( );
142 0           foreach my $node ( $xpc->findnodes('.//cf:DistributionSummary') )
143             {
144 0           my $origin;
145 0 0         if( my ($origin_s3) = $xpc->findnodes('.//cf:S3Origin', $node) )
    0          
146             {
147 0           $origin = AWS::CloudFront::S3Origin->new(
148             OriginAccessIdentity => $xpc->findvalue('.//cf:OriginAccessIdentity', $origin_s3),
149             DNSName => $xpc->findvalue('.//cf:DNSName', $origin_s3),
150             );
151             }
152             elsif( my $origin_custom = $xpc->findnode('.//cf:CustomOrigin', $node) )
153             {
154             # TODO
155             }# end if()
156 0 0 0       my $dist = AWS::CloudFront::Distribution->new(
      0        
      0        
      0        
      0        
157             cf => $s,
158             Id => $xpc->findvalue('.//cf:Id', $node),
159             Status => $xpc->findvalue('.//cf:Status', $node),
160             LastModifiedTime => $xpc->findvalue('.//cf:LastModifiedTime', $node),
161             DomainName => $xpc->findvalue('.//cf:DomainName', $node),
162             Enabled => $xpc->findvalue('.//cf:Enabled') eq 'true' ? 1 : 0,
163             DefaultRootObject => $xpc->findvalue('.//cf:DefaultRootObject') || undef,
164             CNAME => $xpc->findvalue('.//cf:CNAME') || undef,
165             InProgressValidationBatches => $xpc->findvalue('.//cf:InProgressValidationBatches') || undef,
166             Comment => $xpc->findvalue('.//cf:Comment') || undef,
167             CallerReference => $xpc->findvalue('.//cf:CallerReference') || undef,
168             Origin => $origin,
169             # TODO: Logging, ActiveTrustedSigners.
170             );
171 0           push @dists, $dist;
172             }# end foreach()
173            
174 0           return @dists;
175             }# end distributions()
176              
177              
178             sub origin_access_identities
179             {
180 0     0 0   my ($s, %args) = @_;
181            
182 0           my @out = ( );
183 0           FETCH: {
184 0           my $response = $s->request( 'GetOriginAccessIdentityList', %args )->request();
185              
186 0           my $xpc = $response->xpc;
187 0           foreach my $node ( $xpc->findnodes('.//cf:CloudFrontOriginAccessIdentitySummary') )
188             {
189 0           my ($config) = $xpc->findnodes('.//cf:CloudFrontOriginAccessIdentityConfig');
190 0           my $ident = $s->origin_access_identity( $xpc->findvalue('.//cf:Id', $node) );
191 0           push @out, $ident;
192             }# end foreach()
193 0 0         if( $xpc->findvalue('.//cf:IsTruncated') eq 'true' )
194             {
195 0           $args{Marker} = $xpc->findvalue('.//cf:NextMarker');
196 0           next FETCH;
197             }# end if()
198             };
199            
200 0           return @out;
201             }# end origin_access_identities()
202              
203              
204             sub origin_access_identity
205             {
206 0     0 0   my ($s, $id) = @_;
207            
208 0           my $response = $s->request( 'GetOriginAccessIdentity', Id => $id )->request();
209 0           my $xpc = $response->xpc;
210 0           return AWS::CloudFront::OriginAccessIdentity->new(
211             Id => $xpc->findvalue('.//cf:Id'),
212             S3CanonicalUserId => $xpc->findvalue('.//cf:S3CanonicalUserId'),
213             CallerReference => $xpc->findvalue('.//cf:CallerReference'),
214             Comment => $xpc->findvalue('.//cf:Comment'),
215             );
216             }# end origin_access_identity()
217              
218              
219             1;# return true:
220              
221             =pod
222              
223             =head1 NAME
224              
225             AWS::CloudFront - Lightweight interface to Amazon CloudFront CDN
226              
227             =head1 SYNOPSIS
228              
229             # TBD
230              
231             =head1 DESCRIPTION
232              
233             CloudFront is the CDN part of Amazon's AWS Cloud environment.
234              
235             This module aims to wrap their REST API in a nice object-oriented interface.
236              
237             =head1 PUBLIC PROPERTIES
238              
239             TBD
240              
241             =head1 PUBLIC METHODS
242              
243             TBD
244              
245             =head1 AUTHOR
246              
247             John Drago
248              
249             =head1 LICENSE AND COPYRIGHT
250              
251             This software is Free software and may be used and redistributed under the same
252             terms as any version of perl itself.
253              
254             Copyright John Drago 2011 all rights reserved.
255              
256             =cut
257