blib/lib/Net/EC2/Tiny.pm

filter: Statementstmt 44.1% 34 / 77 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.1% 8 / 19 Podpod 100% 1 / 1 totaltotal 40.9% 43 / 105 CC 5 SCAR 14.2
CC
5
Cov
40%
_sign
12.0
send
12.0
__ANON__
2.0
CRAP 4.1
↑ Summary
1 package Net::EC2::Tiny;
2 {
3 2 $Net::EC2::Tiny::VERSION = '0.03';
4 }
5  
6 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Digest::SHA qw(hmac_sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use XML::Simple qw(XMLin);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # ABSTRACT: Basic EC2 client
18  
19  
20  
21 2 has 'AWSAccessKey' => ( is => 'ro', required => 1 );
22  
23  
24 2 has 'AWSSecretKey' => ( is => 'ro', required => 1 );
25  
26  
27 0 ▶has 'debug' => ( is => 'ro', required => 0, default => sub { 0 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
28  
29  
30 0 ▶has 'version' => ( is => 'ro', required => 1, default => sub { '2012-07-20' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
31  
32  
33 0 ▶has 'region' => ( is => 'ro', required => 1, default => sub { 'us-east-1' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
34  
35  
36 has 'base_url' => (
37 is => 'ro',
38 required => 1,
39 lazy => 1,
40 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 'https://ec2.' . $_[0]->region . '.amazonaws.com';
42 }
43 2 );
44  
45  
46 has 'ua' => (
47 is => 'ro',
48 required => 1,
49 lazy => 1,
50 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 HTTP::Tiny->new(
52 'agent' => 'Net::EC2::Tiny ',
53 );
54 }
55 2 );
56  
57 has '_base_url_host' => (
58 is => 'ro',
59 required => 1,
60 lazy => 1,
61 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 ($_[0]->ua->_split_url($_[0]->base_url))[1]
63 }
64 2 );
65  
66 0 ▶sub _timestamp {
Subroutine _timestamp: not called CC=1 SCAR=6.9
67 0 return strftime("%Y-%m-%dT%H:%M:%SZ",gmtime);
68 }
69
70 0 ▶sub _sign {
Subroutine _sign: not called CC=3 SCAR=24.8
71 0 my $self = shift;
72 0 my %args = @_;
73 0 my $action = delete $args{Action};
74
75 0 ▶ croak "Action must be defined!\n" if not defined $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 0 0 0
76  
77 0 my %sign_hash = %args;
78 0 my $timestamp = $self->_timestamp;
79  
80 0 $sign_hash{AWSAccessKeyId} = $self->AWSAccessKey;
81 0 $sign_hash{Action} = $action;
82 0 $sign_hash{Timestamp} = $timestamp;
83 0 $sign_hash{Version} = $self->version;
84 0 $sign_hash{SignatureVersion} = "2";
85 0 $sign_hash{SignatureMethod} = "HmacSHA256";
86  
87 0 my $sign_this = "POST\n";
88 0 $sign_this .= $self->_base_url_host . "\n";
89 0 $sign_this .= "/\n";
90  
91  
92 0 $sign_this .= $self->ua->www_form_urlencode(\%sign_hash);
93  
94 0 ▶ warn "QUERY TO SIGN: $sign_this" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
95 0 my $encoded = encode_base64(hmac_sha256($sign_this, $self->AWSSecretKey), '');
96  
97 0 my %params = (
98 Action => $action,
99 SignatureVersion => "2",
100 SignatureMethod => "HmacSHA256",
101 AWSAccessKeyId => $self->AWSAccessKey,
102 Timestamp => $timestamp,
103 Version => $self->version,
104 Signature => $encoded,
105 %args
106 );
107  
108 0 return \%params;
109 }
110  
111 0 ▶sub _request {
Subroutine _request: not called CC=1 SCAR=6.9
112 0 my $self = shift;
113 0 my $params = shift;
114  
115 0 return $self->ua->post_form( $self->base_url, $params );
116 }
117  
118 0 ▶sub _process {
Subroutine _process: not called CC=1 SCAR=6.9
119 0 my $self = shift;
120 0 my $data = shift;
121  
122 0 my $xml = XMLin( $data,
123 ForceArray => qr/(?:item|Errors)/i,
124 KeyAttr => '',
125 SuppressEmpty => undef,
126 );
127  
128 0 return $xml;
129 }
130  
131  
132 0 ▶sub send {
Subroutine send: not called CC=3 SCAR=24.8
Pod documented
133 0 my $self = shift;
134 0 my $request = $self->_sign(@_);
135 0 my $response = $self->_request($request);
136  
137 0 ▶ if ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"success"}) 0 0 0
138 0 my $xml = $self->_process( $response->{content} );
139 0 ▶ if ( defined $xml->{Errors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xml->{"Errors"}) 0 0 0
140 0 croak "Error: $response->{content}\n";
141 }
142 0 return $xml;
143 }
144  
145 0 croak "POST Request failed: $response->{status} $response->{reason} $response->{content}\n";
146 }
147  
148  
149 2 1;
150  
151 __END__
↑ Summary