Zaqar.pm

filter: Statementstmt 69.4% 75 / 108 Branchbran 25.0% 4 / 16 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 76.9% 20 / 26 Podpod 66.6% 2 / 3 totaltotal 57.9% 102 / 176 CC 14 SCAR 28.6
CC
14
Cov
58%
__ANON__
72.0
BUILD
14.1
__ANON__
3.1
CRAP 17.5
↑ Summary Zaqar/Middleware/Auth/DynamicHeader.pm »
1 package WebService::Zaqar;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Net::HTTP::Spore;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Scalar::Util qw/blessed weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 our $VERSION = '0.010';
21  
22 3 has 'base_url' => (is => 'ro',
23 writer => '_set_base_url');
24 3 has 'token' => (is => 'ro',
25 writer => '_set_token',
26 clearer => '_clear_token',
27 predicate => 'has_token');
28 3 has 'spore_client' => (is => 'ro',
29 lazy => 1,
30 builder => '_build_spore_client');
31 has 'spore_description_file' => (is => 'ro',
32 0 ▶ default => sub { File::ShareDir::dist_file('WebService-Zaqar', 'marconi.spore.json') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
33 3 has 'client_uuid' => (is => 'ro',
34 lazy => 1,
35 builder => '_build_uuid');
36  
37 has 'wants_auth' => (is => 'ro',
38 6 ▶ default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
39 3 has 'rackspace_keystone_endpoint' => (is => 'ro',
40 predicate => 1);
41 3 has 'rackspace_username' => (is => 'ro',
42 predicate => 1);
43 3 has 'rackspace_api_key' => (is => 'ro',
44 predicate => 1);
45  
46 0 ▶sub _build_uuid {
Subroutine _build_uuid: not called CC=1 SCAR=6.9
47 0 return Data::UUID->new->create_str;
48 }
49  
50 6 ▶sub _build_spore_client {
Subroutine _build_spore_client: called CC=1 SCAR=0.0
51 6 my $self = shift;
52 6 my $client = Net::HTTP::Spore->new_from_spec($self->spore_description_file,
53 base_url => $self->base_url);
54 # all payloads serialized/deserialized to/from JSON -- except if
55 # you're receiving 401 or 403
56 6 $client->enable('+WebService::Zaqar::Middleware::Format::JSONSometimes');
57 # set X-Auth-Token header to the Cloud Identity token, if
58 # available (local instances don't use that, for instance)
59  
60 6 my $twin = $self; # gonna close over this one
61 6 weaken $twin;
62  
63 $client->enable('+WebService::Zaqar::Middleware::Auth::DynamicHeader',
64 header_name => 'X-Auth-Token',
65 5 ▶ header_value_callback => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
66 # HTTP::Headers says, if the value of the
67 # header is undef, the field is removed
68 5 ▶ return $twin ? $twin->has_token ? $twin->token : undef : undef
Branch: 2 branches
BranchTrueFalseTotal
$twin ? : 5 0 5
$twin->has_token ? : 3 2 5
69 6 });
70 # all requests should contain a Date header with an RFC 1123 date
71 6 $client->enable('+WebService::Zaqar::Middleware::DateHeader');
72 # each client using the queue should provide an UUID; the docs
73 # recommend that for a given client it should persist between
74 # restarts
75 6 $client->enable('Header',
76 header_name => 'Client-ID',
77 header_value => $self->client_uuid);
78 6 $client->enable('+WebService::Zaqar::Middleware::JustCallIt');
79 6 return $client;
80 }
81  
82 0 ▶sub do_request {
Subroutine do_request: not called CC=1 SCAR=6.9
Pod documented
83 0 my ($self, $coderef, $options, @rest) = @_;
84 # here undef retries means retry until it works, 0 retries means
85 # don't retry, other integers mean retry that many times
86 0 my $max_retries = $options->{retries};
87 0 my $current_retries = 0;
88 RETRY: {
89 0 my $return_value;
90 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 $return_value = $coderef->($self, @rest);
92 0 ▶ } catch {
Subroutine __ANON__: not called CC=8 SCAR=42.8
93 0 my $exception = $_;
94 0 ▶ if (blessed($exception)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($exception) and $exception->isa("Net::HTTP::Spore::Response")) 0 0 0
Condition0%0/3
builtin::blessed($exception) and $exception->isa("Net::HTTP::Spore::Response")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($exception) and $exception->isa("Net::HTTP::Spore::Response")
builtin::blessed($exception) $exception->isa("Net::HTTP::Spore::Response")
Truth table0%0/3
A and B
A= builtin::blessed($exception)B= $exception->isa("Net::HTTP::Spore::Response")
ABresult
0X0
100
111
95 and $exception->isa('Net::HTTP::Spore::Response')) {
96 0 ▶ if ($exception->code == 401) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exception->code == 401) 0 0 0
97 0 ▶ if (defined $max_retries and $current_retries >= $max_retries) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_retries and $current_retries >= $max_retries) 0 0 0
Condition0%0/3
defined $max_retries and $current_retries >= $max_retries
!ll&&!rl&&r
000
MC/DC0%0/2
defined $max_retries and $current_retries >= $max_retries
defined $max_retries $current_retries >= $max_retries
Truth table0%0/3
A and B
A= defined $max_retriesB= $current_retries >= $max_retries
ABresult
0X0
100
111
98 0 croak('Server returned 401 Unauthorized but we already retried too many times');
99 }
100 0 $current_retries++;
101 # re-authentication needed
102 0 ▶ if ($self->wants_auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->wants_auth) { } 0 0 0
103 0 $self->rackspace_authenticate(
104 $self->rackspace_keystone_endpoint,
105 $self->rackspace_username,
106 $self->rackspace_api_key);
107 0 goto RETRY;
108 } else {
109 # ... but not wanted!
110 0 croak('Server returned 401 Unauthorized but we are not planning on authenticating!');
111 }
112 }
113 # rethrow the contents of the exception instead of just
114 # the unhelpful HTTP 400
115 0 ▶ if ($exception->code == 599) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exception->code == 599) 0 0 0
116 0 croak($exception->body->{error});
117 }
118 # some other SPORE exception
119 0 croak $exception;
120 }
121 # wasn't a Spore exception, rethrow
122 0 croak $exception;
123 0 };
124 0 return $return_value;
125 }
126 }
127  
128 0 ▶sub rackspace_authenticate {
Subroutine rackspace_authenticate: not called CC=1 SCAR=6.9
Pod documented
129 0 my ($self, $cloud_identity_uri, $username, $apikey) = @_;
130 0 my $request = HTTP::Request->new('POST', $cloud_identity_uri,
131 [ 'Content-Type' => 'application/json' ],
132 JSON::encode_json({
133 auth => {
134 'RAX-KSKEY:apiKeyCredentials' => {
135 username => $username,
136 apiKey => $apikey } } }));
137 0 my $response = $self->spore_client->api_useragent->request($request);
138 0 my $content = $response->decoded_content;
139 0 my $structure = JSON::decode_json($content);
140 0 my $token = $structure->{access}->{token}->{id};
141 0 $self->_set_token($token);
142 # the doc says we should read the catalog to determine the
143 # endpoint...
144 # my $catalog = first { $_->{name} eq 'cloudQueues'
145 # and $_->{type} eq 'rax:queues' } @{$structure->{serviceCatalog}};
146 0 return $token;
147 }
148  
149 6 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=26.5
Pod undocumented
150 6 my $self = shift;
151 6 ▶ if ($self->wants_auth
Branch: 1 branch
BranchTrueFalseTotal
if ($self->wants_auth and not $self->has_rackspace_keystone_endpoint && $self->has_rackspace_username && $self->has_rackspace_api_key) 0 6 6
Condition11%1/9
$self->wants_auth and not $self->has_rackspace_keystone_endpoint && $self->has_rackspace_username && $self->has_rackspace_api_key
!ll&&!rl&&r
600
$self->has_rackspace_keystone_endpoint && $self->has_rackspace_username && $self->has_rackspace_api_key
!ll&&!rl&&r
000
$self->has_rackspace_keystone_endpoint and $self->has_rackspace_username
!ll&&!rl&&r
000
MC/DC0%0/4
$self->wants_auth and not $self->has_rackspace_keystone_endpoint && $self->has_rackspace_username && $self->has_rackspace_api_key
$self->wants_auth $self->has_rackspace_keystone_endpoint $self->has_rackspace_username $self->has_rackspace_api_key
Truth table20%1/5
A and not(B and C && D)
A= $self->wants_authB= $self->has_rackspace_keystone_endpointC= $self->has_rackspace_usernameD= $self->has_rackspace_api_key
ABCDresult
0XXX0
11110
10XX1
110X1
11101
152 and (not $self->has_rackspace_keystone_endpoint
153 or not $self->has_rackspace_username
154 or not $self->has_rackspace_api_key)) {
155 0 croak('Authentication required but not all Rackspace attributes provided');
156 }
157 # if ($self->has_rackspace_username) {
158 # # uhhh, ok, so SOME Rackspace docs say this header is
159 # # necessary, but others don't mention it; when I add it to a
160 # # request it always 403s and without it it seems to work, so
161 # # uh, yeah.
162 # $self->spore_client->enable('Header',
163 # header_name => 'X-Project-Id',
164 # header_value => '921182');
165 # }
166 }
167  
168 3 our $AUTOLOAD;
169 11 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
170 11 my $method_name = $AUTOLOAD;
171 11 my ($self, @rest) = @_;
172 11 my $current_class = ref $self;
173 11 $method_name =~ s/^${current_class}:://;
174 11 $self->spore_client->$method_name(@rest);
175 }
176  
177 3 1;
178 __END__
↑ Summary Zaqar/Middleware/Auth/DynamicHeader.pm »