DynamoDB.pm

filter: Statementstmt 23.4% 23 / 98 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 31.8% 7 / 22 Podpod 10.0% 1 / 10 totaltotal 16.5% 31 / 187 CC 21 SCAR 44.8
CC
21
Cov
16%
new
240.0
cached_iam_credentials
12.0
__ANON__
6.0
CRAP 88.3
↑ Summary DynamoDB/20120810.pm »
1 package WebService::Amazon::DynamoDB;
2 # ABSTRACT: Abstract API support for Amazon DynamoDB
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.005';
7  
8 =head1 NAME
9  
10 WebService::Amazon::DynamoDB - support for the AWS DynamoDB API
11  
12 =head1 VERSION
13  
14 version 0.005
15  
16 =head1 SYNOPSIS
17  
18 # Using access key
19 my $ddb = WebService::Amazon::DynamoDB->new(
20 version => '20120810',
21 access_key => 'access_key',
22 secret_key => 'secret_key',
23 uri => 'http://localhost:8000',
24 );
25 $ddb->batch_get_item(
26 sub {
27 my $tbl = shift;
28 my $data = shift;
29 warn "Batch get: $tbl had " . join(',', %$data) . "\n";
30 },
31 items => {
32 $table_name => {
33 keys => [
34 name => 'some test name here',
35 ],
36 fields => [qw(name age)],
37 }
38 },
39 )->get;
40  
41 # Using the IAM role from the current EC2 instance
42 my $ddb = WebService::Amazon::DynamoDB->new(
43 security => 'iam',
44 );
45  
46 # Using a specific IAM role
47 my $ddb = WebService::Amazon::DynamoDB->new(
48 security => 'iam',
49 iam_role => 'role_name',
50 );
51  
52 =head1 BEFORE YOU START
53  
54 B<NOTE>: I'd recommend looking at the L<Amazon::DynamoDB> module first.
55 It is a fork of this one with better features, more comprehensive tests,
56 and overall it's maintained much more actively.
57  
58 =head1 DESCRIPTION
59  
60 Provides a L<Future>-based API for Amazon's DynamoDB REST API.
61 See L<WebService::Amazon::DynamoDB::20120810> for available methods.
62  
63 Current implementations for issuing the HTTP requests:
64  
65 =over 4
66  
67 =item * L<WebService::Async::UserAgent::NaHTTP> - use L<Net::Async::HTTP>
68 for applications based on L<IO::Async> (this gives nonblocking behaviour)
69  
70 =item * L<WebService::Async::UserAgent::LWP> - use L<LWP::UserAgent> (will
71 block, timeouts are unlikely to work)
72  
73 =item * L<WebService::Async::UserAgent::MojoUA> - use L<Mojo::UserAgent>,
74 should be suitable for integration into a L<Mojolicious> application (could
75 be adapted for nonblocking, although the current version does not do this).
76  
77 =back
78  
79 Only the L<Net::Async::HTTP> implementation has had any significant testing or use.
80  
81 =cut
82  
83 2 ▶use WebService::Amazon::IAM::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 2 ▶use WebService::Amazon::DynamoDB::20120810;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 =head1 METHODS
92  
93 =cut
94  
95 0 ▶sub new {
Subroutine new: not called CC=15 SCAR=54.8
Pod undocumented
96 0 my $class = shift;
97 0 my %args = @_;
98 0 ▶ $args{implementation} //= 'WebService::Async::UserAgent::NaHTTP';
Condition0%0/2
$args{"implementation"} //= "WebService::Async::UserAgent::NaHTTP"
l!l
00
MC/DC0%0/1
$args{"implementation"} //= "WebService::Async::UserAgent::NaHTTP"
$args{"implementation"}
Truth table0%0/2
A
A= $args{"implementation"}
Aresult
11
00
99 0 ▶ unless(ref $args{implementation}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args{"implementation"}) 0 0 0
100 0 $log->debugf("Loading module for HTTP implementation [%s]", $args{implementation});
101 0 Module::Load::load($args{implementation});
102 0 $log->debugf("Instantiating [%s]", $args{implementation});
103 $args{implementation} = $args{implementation}->new(
104 0 ▶ (exists $args{loop} ? (loop => delete $args{loop}) : ())
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'loop'} ? : 0 0 0
105 );
106 }
107 0 ▶ my $version = delete $args{version} || '20120810';
Condition0%0/2
delete $args{'version'} || '20120810'
l!l
00
MC/DC0%0/1
delete $args{'version'} || '20120810'
delete $args{'version'}
Truth table0%0/2
A
A= delete $args{'version'}
Aresult
11
00
108 0 my $pkg = __PACKAGE__ . '::' . $version;
109 0 $log->debugf("Look for ->new in [%s]", $pkg);
110 0 ▶ if(my $code = $pkg->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $pkg->can("new")) 0 0 0
111 0 ▶ $class = $pkg if $class eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $class eq __PACKAGE__ 0 0 0
112 0 ▶ $args{security} ||= 'key';
Condition0%0/2
$args{"security"} ||= "key"
l!l
00
MC/DC0%0/1
$args{"security"} ||= "key"
$args{"security"}
Truth table0%0/2
A
A= $args{"security"}
Aresult
11
00
113 0 ▶ $args{region} ||= 'us-west-1';
Condition0%0/2
$args{"region"} ||= "us-west-1"
l!l
00
MC/DC0%0/1
$args{"region"} ||= "us-west-1"
$args{"region"}
Truth table0%0/2
A
A= $args{"region"}
Aresult
11
00
114 0 ▶ $args{iam_role} = Future->done($args{iam_role}) if exists $args{iam_role} && !ref $args{iam_role};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"iam_role"} and not ref $args{"iam_role"} 0 0 0
Condition0%0/3
exists $args{"iam_role"} and not ref $args{"iam_role"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"iam_role"} and not ref $args{"iam_role"}
exists $args{"iam_role"} not ref $args{"iam_role"}
Truth table0%0/3
A and not(B)
A= exists $args{"iam_role"}B= not ref $args{"iam_role"}
ABresult
0X0
100
111
115 0 ▶ if(exists $args{host} or exists $args{port}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'host'} or exists $args{'port'}) { } 0 0 0
Condition0%0/3
exists $args{"host"} or exists $args{"port"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $args{"host"} or exists $args{"port"}
exists $args{"host"} exists $args{"port"}
Truth table0%0/3
A or B
A= exists $args{"host"}B= exists $args{"port"}
ABresult
1X1
011
000
116 0 $args{uri} = URI->new('http://' . $args{host} . ':' . $args{port});
117 } else {
118 0 ▶ $args{uri} ||= 'https://dynamodb.' . $args{region} . '.amazonaws.com/';
Condition0%0/2
$args{"uri"} ||= 'https://dynamodb.' . $args{"region"} . '.amazonaws.com/'
l!l
00
MC/DC0%0/1
$args{"uri"} ||= 'https://dynamodb.' . $args{"region"} . '.amazonaws.com/'
$args{"uri"}
Truth table0%0/2
A
A= $args{"uri"}
Aresult
11
00
119 0 ▶ $args{uri} = URI->new($args{uri}) unless ref $args{uri};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"uri"} 0 0 0
120 }
121 0 return $code->($class, %args)
122 }
123 0 die "No support for version $version";
124 }
125  
126 0 ▶sub security { shift->{security} }
Subroutine security: not called CC=1 SCAR=6.9
Pod undocumented
127  
128 0 ▶sub uri { shift->{uri} }
Subroutine uri: not called CC=1 SCAR=6.9
Pod undocumented
129  
130 0 ▶sub api_version { ... }
Subroutine api_version: not called CC=1 SCAR=6.9
Pod undocumented
131  
132 =head2 make_request
133  
134 Generates an L<HTTP::Request>.
135  
136 =cut
137  
138 0 ▶sub make_request {
Subroutine make_request: not called CC=1 SCAR=6.9
Pod documented
139 0 my $self = shift;
140 0 my %args = @_;
141 0 my $target = $args{target};
142 0 my $js = JSON::MaybeXS->new;
143 0 my $req = HTTP::Request->new(
144 POST => $self->uri
145 );
146 0 $req->header( host => $self->uri->host );
147 0 my $http_date = strftime('%a, %d %b %Y %H:%M:%S %Z', localtime);
148 0 $req->protocol('HTTP/1.1');
149 0 $req->header( 'Date' => $http_date );
150 0 $req->header( 'x-amz-date' => strftime('%Y%m%dT%H%M%SZ', gmtime) );
151 0 $req->header( 'x-amz-target', 'DynamoDB_'. $self->api_version. '.'. $target );
152 0 $req->header( 'content-type' => 'application/x-amz-json-1.0' );
153 0 my $payload = $js->encode($args{payload});
154 0 $req->content($payload);
155 0 $req->header( 'Content-Length' => length($payload));
156 0 ▶ $self->credentials->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
157 0 my ($creds) = @_;
158 # Don't show these by default
159 # $log->debugf("Using [%s] for credentials", $creds);
160 0 my $token = delete $creds->{token};
161 0 my $amz = WebService::Amazon::Signature->new(
162 version => 4,
163 algorithm => $self->algorithm,
164 scope => $self->scope,
165 %$creds,
166 );
167 0 $amz->from_http_request($req);
168 0 $req->header(Authorization => $amz->calculate_signature);
169 0 ▶ $req->header('X-Amz-Security-Token' => $token) if defined $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 0 0 0
170 0 Future->done($req)
171 })
172 0 }
173  
174 0 ▶sub credentials {
Subroutine credentials: not called CC=2 SCAR=17.9
Pod undocumented
175 0 my ($self) = @_;
176 0 ▶ if($self->security eq 'key') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->security eq "key") 0 0 0
177 0 $log->debugf("Using key-based security");
178 # We don't bother caching the hashref, since we'd need to be passing
179 # a copy anyway (caller may change the values)
180 0 return Future->done({
181 access_key => $self->access_key,
182 secret_key => $self->secret_key,
183 })
184 }
185  
186 0 ▶ return $self->cached_iam_credentials->else(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
187 0 $log->debug("No cached credentials (or already expired)");
188 0 ▶ $self->find_iam_role->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 my ($role) = @_;
190 0 $log->debugf("Found role [%s]", $role);
191 0 $self->retrieve_iam_credentials(
192 $role
193 )
194 })
195 0 });
196 }
197  
198 0 ▶sub cached_iam_credentials {
Subroutine cached_iam_credentials: not called CC=3 SCAR=24.8
Pod undocumented
199 0 my ($self) = @_;
200 0 ▶ return Future->fail('no cached credentials') unless exists $self->{cached_iam_credentials};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"cached_iam_credentials"} 0 0 0
201  
202 # Assume expired if we're within 5 seconds
203 0 ▶ return Future->fail('cached credentials expire') if $self->{cached_iam_credentials}{expire_at} <= time - 5;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cached_iam_credentials"}{"expire_at"} <= time - 5 0 0 0
204  
205 # Shallow copy, so we're not affected if the caller changes the values
206 return Future->done({
207 0 %{ $self->{cached_iam_credentials}{details} }
208 })
209 }
210  
211 0 ▶sub find_iam_role {
Subroutine find_iam_role: not called CC=2 SCAR=17.9
Pod undocumented
212 0 my ($self) = @_;
213 0 ▶ $self->{iam_role} //= $self->iam->active_roles
Condition0%0/3
$self->{"iam_role"} //= $self->iam->active_roles
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"iam_role"} //= $self->iam->active_roles
$self->{"iam_role"} $self->iam->active_roles
Truth table0%0/3
A //= B
A= $self->{"iam_role"}B= $self->iam->active_roles
ABresult
1X1
011
000
214 }
215  
216 0 ▶sub retrieve_iam_credentials {
Subroutine retrieve_iam_credentials: not called CC=1 SCAR=6.9
Pod undocumented
217 0 my ($self, $role) = @_;
218 0 ▶ $self->iam->credentials_for_role($role)->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 my ($creds, $expiry) = @_;
220 0 $log->debugf("New credentials received, will expire in %s seconds", strftime '%H:%M:%S', gmtime($expiry - time));
221 $self->{cached_iam_credentials} = {
222 0 expire_at => $expiry,
223 details => $creds
224 };
225 # Shallow copy, so we're not affected if the caller changes the values
226 0 Future->done({ %$creds })
227 })
228 0 }
229  
230 0 ▶sub _request {
Subroutine _request: not called CC=1 SCAR=6.9
231 0 my $self = shift;
232 0 my $req = shift;
233 # Don't show requests by default
234 # $log->debugf("Issuing request [%s]", $req->as_string("\n"));
235 0 $self->implementation->request($req)
236 }
237  
238 0 ▶sub iam {
Subroutine iam: not called CC=2 SCAR=17.9
Pod undocumented
239 0 ▶ $_[0]->{iam} //= WebService::Amazon::IAM::Client->new(
Condition0%0/3
$_[0]{"iam"} //= "WebService::Amazon::IAM::Client"->new("ua", $_[0]->implementation)
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"iam"} //= "WebService::Amazon::IAM::Client"->new("ua", $_[0]->implementation)
$_[0]{"iam"} "WebService::Amazon::IAM::Client"->new("ua", $_[0]->implementation)
Truth table0%0/3
A //= B
A= $_[0]{"iam"}B= "WebService::Amazon::IAM::Client"->new("ua", $_[0]->implementation)
ABresult
1X1
011
000
240 ua => $_[0]->implementation,
241 )
242 }
243  
244 2 1;
245  
246 __END__
↑ Summary DynamoDB/20120810.pm »