Etcd/Role/Actions.pm

filter: Statementstmt 27.2% 48 / 176 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 36.3% 12 / 33 Podpod 100% 6 / 6 totaltotal 22.3% 66 / 295 SCAR 66.3
CC
37
Cov
19%
_build_tls_ctx
72.0
_build_request
42.0
_build_json_args
12.0
CRAP 755.0
↑ Summary Etcd.pm »
1 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Net::Etcd::Role::Actions;
4  
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use AE;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Types::Standard qw(InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use AnyEvent::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =encoding utf8
20  
21 =head1 NAME
22  
23 Net::Etcd::Role::Actions
24  
25 =cut
26  
27 9 our $VERSION = '0.021';
28  
29 9 has etcd => (
30 is => 'ro',
31 isa => InstanceOf ['Net::Etcd'],
32 );
33  
34 =head2 json_args
35  
36 arguments that will be sent to the api
37  
38 =cut
39  
40 9 has json_args => ( is => 'lazy', );
41  
42 sub _build_json_args {
43 0 ▶ my ($self) = @_;
Subroutine _build_json_args: not called CC=3 SCAR=24.8
44 0 my $args;
45 0 for my $key ( keys %{$self} ) {
46 0 ▶ unless ( $key =~ /(?:etcd|cb|cv|hold|json_args|endpoint)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /(?:etcd|cb|cv|hold|json_args|endpoint)$/) 0 0 0
47 0 $args->{$key} = $self->{$key};
48 }
49 }
50 0 return to_json($args);
51 }
52  
53 =head2 cb
54  
55 AnyEvent callback must be a CodeRef
56  
57 =cut
58  
59 has cb => (
60 is => 'ro',
61 isa => sub {
62 0 ▶ die "$_[0] is not a CodeRef!" if ( $_[0] && ref( $_[0] ) ne 'CODE' );
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and ref $_[0] ne "CODE" 0 0 0
Condition0%0/3
$_[0] and ref $_[0] ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and ref $_[0] ne "CODE"
$_[0] ref $_[0] ne "CODE"
Truth table0%0/3
A and B
A= $_[0]B= ref $_[0] ne "CODE"
ABresult
0X0
100
111
63 },
64 9 );
65  
66 =head2 cv
67  
68 =cut
69  
70 9 has cv => ( is => 'ro', );
71  
72 =head2 init
73  
74 =cut
75  
76 sub init {
77 0 ▶ my ($self) = @_;
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
78 0 my $init = $self->json_args;
79 0 ▶ $init or return;
Branch: 1 branch
BranchTrueFalseTotal
$init or return 0 0 0
80 0 return $self;
81 }
82  
83 =head2 headers
84  
85 =cut
86  
87 9 has headers => (
88 is => 'lazy',
89 clearer => 1
90 );
91  
92 sub _build_headers {
93 0 ▶ my ($self) = @_;
Subroutine _build_headers: not called CC=3 SCAR=24.8
94 0 my $headers;
95 0 my $token = $self->etcd->auth_token;
96 0 $headers->{'Content-Type'} = 'application/json';
97 0 ▶ unless ( $self->endpoint =~ m/authenticate/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->endpoint =~ /authenticate/) 0 0 0
98 0 ▶ $headers->{'Authorization'} = $token if $token;
Branch: 1 branch
BranchTrueFalseTotal
if $token 0 0 0
99 }
100 0 return $headers;
101 }
102  
103 9 has tls_ctx => ( is => 'lazy', );
104  
105 sub _build_tls_ctx {
106 0 ▶ my ($self) = @_;
Subroutine _build_tls_ctx: not called CC=8 SCAR=42.8
107 0 my $ca_file = $self->etcd->ca_file;
108 0 my $key_file = $self->etcd->key_file;
109 0 my $cert_file = $self->etcd->cert_file;
110 0 my $cacert = $self->etcd->cacert;
111 0 my $tls;
112 0 ▶ $tls->{ca_file} = $ca_file if $ca_file;
Branch: 1 branch
BranchTrueFalseTotal
if $ca_file 0 0 0
113 0 ▶ $tls->{key_file} = $key_file if $key_file;
Branch: 1 branch
BranchTrueFalseTotal
if $key_file 0 0 0
114 0 ▶ $tls->{cert_file} = $cert_file if $cert_file;
Branch: 1 branch
BranchTrueFalseTotal
if $cert_file 0 0 0
115 0 ▶ $tls->{cacert} = $cacert if $cacert;
Branch: 1 branch
BranchTrueFalseTotal
if $cacert 0 0 0
116  
117 0 ▶ if ( $ca_file || $key_file || $cert_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ca_file or $key_file or $cert_file) 0 0 0
Condition0%0/6
$ca_file or $key_file or $cert_file
l!l&&r!l&&!r
000
$ca_file or $key_file
l!l&&r!l&&!r
000
MC/DC0%0/3
$ca_file or $key_file or $cert_file
$ca_file $key_file $cert_file
Truth table0%0/4
A or B or C
A= $ca_fileB= $key_fileC= $cert_file
ABCresult
1XX1
01X1
0011
0000
118 0 $tls->{verify} = 1;
119 0 return $tls;
120 }
121 0 return 'low';
122 }
123  
124 =head2 hold
125  
126 When set will not fire request.
127  
128 =cut
129  
130 9 has hold => ( is => 'ro' );
131  
132 =head2 response
133  
134 =cut
135  
136 9 has response => ( is => 'ro' );
137  
138 =head2 retry_auth
139  
140 When set will retry authentication request and update token
141  
142 =cut
143  
144 9 has retry_auth => (
145 is => 'ro',
146 default => 0
147 );
148  
149 =head2 request
150  
151 =cut
152  
153 9 has request => ( is => 'lazy', );
154  
155 sub _build_request {
156 0 ▶ my ($self) = @_;
Subroutine _build_request: not called CC=6 SCAR=37.4
157 0 ▶ if ( $self->{retry_auth} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"retry_auth"} > 1) 0 0 0
158 0 confess
159 "Error: Unable to authenticate, check your username and password";
160 0 $self->{retry_auth} = 0;
161 0 return;
162 }
163 0 $self->init;
164 0 my $cb = $self->cb;
165 0 ▶ my $cv = $self->cv ? $self->cv : AE::cv;
Branch: 1 branch
BranchTrueFalseTotal
$self->cv ? : 0 0 0
166 0 $cv->begin;
167  
168 http_request(
169 'POST',
170 $self->etcd->api_path . $self->{endpoint},
171 headers => $self->headers,
172 body => $self->json_args,
173 tls_ctx => $self->tls_ctx,
174 on_header => sub {
175 0 ▶ my ($headers) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
176 0 $self->{response}{headers} = $headers;
177 },
178 want_body_handle => 1,
179 sub {
180 0 ▶ my ( $handle, $hdr ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
181 my $json_reader = sub {
182 0 ▶ my ( $handle, $json ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
183 0 ▶ return unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
184 0 $self->{response}{content} = JSON::encode_json($json);
185 0 ▶ $cb->( $json, $hdr ) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
186 0 my $status = $hdr->{Status};
187 0 $self->check_hdr($status);
188 0 $cv->send;
189 0 };
190 my $chunk_reader = sub {
191 0 ▶ my ( $handle, $line ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
192 0 ▶ return unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
193  
194 #read chunk size
195 0 ▶ $line =~ /^([0-9a-fA-F]+)/
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^([0-9a-fA-F]+)/ or die 'bad chunk (incorrect length) -[' . $line . ']-' 0 0 0
196 or die 'bad chunk (incorrect length) -[' . $line . ']-';
197 0 my $len = hex $1;
198  
199 #read chunk
200 $handle->push_read(
201 chunk => $len,
202 sub {
203 0 ▶ my ( $handle, $chunk ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
204 $handle->push_read(
205 line => sub {
206 0 ▶ length $_[1]
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_[1] and die "bad chunk (missing last empty line)" 0 0 0
207 and die 'bad chunk (missing last empty line)';
208 }
209 0 );
210 0 $self->{response}{content} = $chunk;
211 0 ▶ $cb->( $chunk, $hdr ) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
212 0 my $status = $hdr->{Status};
213 0 $self->check_hdr($status);
214 0 $cv->send;
215 }
216 0 );
217 0 };
218  
219 0 ▶ if ( ( $hdr->{'transfer-encoding'} || '' ) =~ /\bchunked\b/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($hdr->{'transfer-encoding'} || '') =~ /\bchunked\b/i) { } 0 0 0
Condition0%0/2
$hdr->{'transfer-encoding'} || ''
l!l
00
MC/DC0%0/1
$hdr->{'transfer-encoding'} || ''
$hdr->{'transfer-encoding'}
Truth table0%0/2
A
A= $hdr->{'transfer-encoding'}
Aresult
11
00
220 $handle->on_read(
221 0 ▶ sub { $handle->push_read( line => $chunk_reader ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 }
223 else {
224 $handle->on_read(
225 0 ▶ sub { $handle->push_read( json => $json_reader ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
226 }
227  
228 0 ▶ $handle->on_eof( sub { $handle->destroy; $cv->end } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 ▶ $handle->on_error( sub { $handle->destroy; $cv->end } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
230 }
231 0 );
232 0 $cv->recv;
233 0 $self->clear_headers;
234  
235 0 ▶ if ( defined $self->{retry_auth} && $self->{retry_auth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"retry_auth"} and $self->{"retry_auth"}) 0 0 0
Condition0%0/3
defined $self->{"retry_auth"} and $self->{"retry_auth"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"retry_auth"} and $self->{"retry_auth"}
defined $self->{"retry_auth"} $self->{"retry_auth"}
Truth table0%0/3
A and B
A= defined $self->{"retry_auth"}B= $self->{"retry_auth"}
ABresult
0X0
100
111
236 0 my $auth = $self->etcd->auth()->authenticate;
237 0 ▶ if ( $auth->{response}{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth->{"response"}{"success"}) 0 0 0
238 0 $self->{retry_auth} = 0;
239 0 $self->request;
240 }
241 }
242 0 return $self;
243 }
244  
245 =head2 get_value
246  
247 returns single decoded value or the first.
248  
249 =cut
250  
251 sub get_value {
252 0 ▶ my ($self) = @_;
Subroutine get_value: not called CC=3 SCAR=24.8
Pod documented
253 0 local $@;
254 0 my $response = $self->response;
255 0 my $content;
256 0 eval { $content = from_json( $response->{content} ) };
257 0 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
258  
259 #print STDERR Dumper($content);
260 0 my $value = $content->{kvs}->[0]->{value};
261 0 ▶ $value or return;
Branch: 1 branch
BranchTrueFalseTotal
$value or return 0 0 0
262 0 return decode_base64($value);
263 }
264  
265 =head2 all
266  
267 returns list containing for example:
268  
269 {
270 'mod_revision' => '3',
271 'version' => '1',
272 'value' => 'bar',
273 'create_revision' => '3',
274 'key' => 'foo0'
275 }
276  
277 where key and value have been decoded for your pleasure.
278  
279 =cut
280  
281 sub all {
282 0 ▶ my ($self) = @_;
Subroutine all: not called CC=3 SCAR=24.8
Pod documented
283 0 local $@;
284 0 my $response = $self->response;
285 0 my $content;
286 0 eval { $content = from_json( $response->{content} ) };
287 0 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
288 0 my $kvs = $content->{kvs};
289 0 for my $row (@$kvs) {
290 0 $row->{value} = decode_base64( $row->{value} );
291 0 $row->{key} = decode_base64( $row->{key} );
292 }
293 0 return $kvs;
294 }
295  
296 =head2 is_success
297  
298 Success is returned if the response is a 200
299  
300 =cut
301  
302 sub is_success {
303 0 ▶ my ($self) = @_;
Subroutine is_success: not called CC=2 SCAR=17.9
Pod documented
304 0 my $response = $self->response;
305 0 ▶ if ( defined $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response->{"success"}) 0 0 0
306 0 return $response->{success};
307 }
308 0 return;
309 }
310  
311 =head2 content
312  
313 returns JSON decoded content hash
314  
315 =cut
316  
317 sub content {
318 0 ▶ my ($self) = @_;
Subroutine content: not called CC=3 SCAR=24.8
Pod documented
319 0 local $@;
320 0 my $response = $self->response;
321 0 my $content;
322 0 eval { $content = from_json( $response->{content} ) };
323 0 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
324 0 ▶ return $content if $content;
Branch: 1 branch
BranchTrueFalseTotal
if $content 0 0 0
325 0 return;
326 }
327  
328 =head2 check_hdr
329  
330 check response header then define success and retry_auth.
331  
332 =cut
333  
334 sub check_hdr {
335 0 ▶ my ( $self, $status ) = @_;
Subroutine check_hdr: not called CC=3 SCAR=24.8
Pod documented
336 0 ▶ my $success = $status == 200 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$status == 200 ? : 0 0 0
337 0 $self->{response}{success} = $success;
338 0 ▶ $self->{retry_auth}++ if $status == 401;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 401 0 0 0
339 0 return;
340 }
341  
342 9 1;
↑ Summary Etcd.pm »