Etcd.pm

filter: Statementstmt 64.9% 63 / 97 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 57.1% 16 / 28 Podpod 100% 11 / 11 totaltotal 46.1% 90 / 195 SCAR 50.7
CC
26
Cov
42%
_build_api_path
42.0
version
12.0
watch
12.0
CRAP 159.8
« Etcd/Role/Actions.pm ↑ Summary Etcd/KV.pm »
1 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Net::Etcd;
4  
5 # ABSTRACT: Provide access to the etcd v3 API.
6  
7 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Net::Etcd::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Net::Etcd::Auth::RolePermission;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Net::Etcd::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Net::Etcd::Watch;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Net::Etcd::Lease;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Net::Etcd::Maintenance;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Net::Etcd::Member;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Net::Etcd::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use Types::Standard qw(Str Int Bool HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 9 with('Net::Etcd::KV');
24  
25 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =encoding utf8
28  
29 =head1 NAME
30  
31 Net::Etcd - etcd v3 REST API.
32  
33 =cut
34  
35 9 our $VERSION = '0.020';
36  
37 =head1 SYNOPSIS
38  
39 Etcd v3.1.0 or greater is required. To use the v3 API make sure to set environment
40 variable ETCDCTL_API=3. Precompiled binaries can be downloaded at https://github.com/coreos/etcd/releases.
41  
42 $etcd = Net::Etcd->new(); # host: 127.0.0.1 port: 2379
43 $etcd = Net::Etcd->new({ host => $host, port => $port, ssl => 1 });
44  
45 # put key
46 $put_key = $etcd->put({ key =>'foo1', value => 'bar' });
47  
48 # check for success of a transaction
49 $put_key->is_success;
50  
51 # get single key
52 $key = $etcd->range({ key =>'test0' });
53  
54 # return single key value or the first in a list.
55 $key->get_value
56  
57 # get range of keys
58 $range = $etcd->range({ key =>'test0', range_end => 'test100' });
59  
60 # return array { key => value } pairs from range request.
61 my @users = $range->all
62  
63 # delete single key
64 $etcd->deleterange({ key => 'test0' });
65  
66 # watch key range, streaming.
67 $watch = $etcd->watch( { key => 'foo', range_end => 'fop'}, sub {
68 my ($result) = @_;
69 print STDERR Dumper($result);
70 })->create;
71  
72 # create/grant 20 second lease
73 $etcd->lease( { ID => 7587821338341002662, TTL => 20 } )->grant;
74  
75 # attach lease to put
76 $etcd->put( { key => 'foo2', value => 'bar2', lease => 7587821338341002662 } );
77  
78 # add new user
79 $etcd->user( { name => 'samba', password => 'foo' } )->add;
80  
81 # add new user role
82 $role = $etcd->role( { name => 'myrole' } )->add;
83  
84 # grant read permission for the foo key to myrole
85 $etcd->role_perm( { name => 'myrole', key => 'foo', permType => 'READWRITE' } )->grant;
86  
87 # grant role
88 $etcd->user_role( { user => 'samba', role => 'myrole' } )->grant;
89  
90 # defrag member's backend database
91 $defrag = $etcd->maintenance()->defragment;
92 print "Defrag request complete!" if $defrag->is_success;
93  
94 # member version
95 $v = $etcd->version;
96  
97 # list members
98 $etcd->member()->list;
99  
100 =head1 DESCRIPTION
101  
102 L<Net::Etcd> is object oriented interface to the v3 REST API provided by the etcd L<grpc-gateway|https://github.com/grpc-ecosystem/grpc-gateway>.
103  
104 =head1 ACCESSORS
105  
106 =head2 host
107  
108 The etcd host. Defaults to 127.0.0.1
109  
110 =cut
111  
112 9 has host => (
113 is => 'ro',
114 isa => Str,
115 default => '127.0.0.1'
116 );
117  
118 =head2 port
119  
120 Default 2379.
121  
122 =cut
123  
124 9 has port => (
125 is => 'ro',
126 isa => Int,
127 default => '2379'
128 );
129  
130 =head2 name
131  
132 Username for authentication, defaults to $ENV{ETCD_CLIENT_USERNAME}
133  
134 =cut
135  
136 has name => (
137 is => 'ro',
138 default => $ENV{ETCD_CLIENT_USERNAME}
139 9 );
140  
141 =head2 password
142  
143 Authentication credentials, defaults to $ENV{ETCD_CLIENT_PASSWORD}
144  
145 =cut
146  
147 has password => (
148 is => 'ro',
149 default => $ENV{ETCD_CLIENT_PASSWORD}
150 9 );
151  
152 =head2 ca_file
153  
154 Path to ca_file, defaults to $ENV{ETCD_CLIENT_CA_FILE}
155  
156 =cut
157  
158 has ca_file => (
159 is => 'ro',
160 default => $ENV{ETCD_CLIENT_CA_FILE}
161 9 );
162  
163 =head2 key_file
164  
165 Path to key_file, defaults to $ENV{ETCD_CLIENT_KEY_FILE}
166  
167 =cut
168  
169 has key_file => (
170 is => 'ro',
171 default => $ENV{ETCD_CLIENT_KEY_FILE}
172 9 );
173  
174 =head2 cert_file
175  
176 Path to cert_file, defaults to $ENV{ETCD_CLIENT_CERT_FILE}
177  
178 =cut
179  
180 has cert_file => (
181 is => 'ro',
182 default => $ENV{ETCD_CLIENT_CERT_FILE}
183 9 );
184  
185 =head2 cacert
186  
187 Path to cacert, defaults to $ENV{ETCD_CLIENT_CACERT_FILE}.
188  
189 =cut
190  
191 has cacert => (
192 is => 'ro',
193 default => $ENV{ETCD_CLIENT_CACERT_FILE}
194 9 );
195  
196  
197 =head2 ssl
198  
199 To enable set to 1
200  
201 =cut
202  
203 9 has ssl => (
204 is => 'ro',
205 isa => Bool,
206 );
207  
208 =head2 api_version
209  
210 defaults to /v3beta
211  
212 =cut
213  
214 9 has api_version => (
215 is => 'ro',
216 isa => Str,
217 default => '/v3beta'
218 );
219  
220 =head2 api_path
221  
222 The full api path. Defaults to http://127.0.0.1:2379/v3alpha
223  
224 =cut
225  
226 9 has api_path => ( is => 'lazy' );
227  
228 sub _build_api_path {
229 0 ▶ my ($self) = @_;
Subroutine _build_api_path: not called CC=6 SCAR=37.4
230 return ( $self->{ssl}
231 || $self->{ca_file}
232 || $self->{key_file}
233 || $self->{cafile}
234 0 ▶ || $self->{cert_file} ? 'https' : 'http' )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ssl'} || $self->{'ca_file'} || $self->{'key_file'} || $self->{'cafile'} || $self->{'cert_file'} ? : 0 0 0
Condition0%0/12
$self->{"ssl"} or $self->{"ca_file"} or $self->{"key_file"} or $self->{"cafile"} or $self->{"cert_file"}
l!l&&r!l&&!r
000
$self->{"ssl"} or $self->{"ca_file"} or $self->{"key_file"} or $self->{"cafile"}
l!l&&r!l&&!r
000
$self->{"ssl"} or $self->{"ca_file"} or $self->{"key_file"}
l!l&&r!l&&!r
000
$self->{"ssl"} or $self->{"ca_file"}
l!l&&r!l&&!r
000
MC/DC0%0/5
$self->{"ssl"} or $self->{"ca_file"} or $self->{"key_file"} or $self->{"cafile"} or $self->{"cert_file"}
$self->{"ssl"} $self->{"ca_file"} $self->{"key_file"} $self->{"cafile"} $self->{"cert_file"}
Truth table0%0/6
A or B or C or D or E
A= $self->{"ssl"}B= $self->{"ca_file"}C= $self->{"key_file"}D= $self->{"cafile"}E= $self->{"cert_file"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
235 . '://'
236 . $self->host . ':'
237 . $self->port
238 . $self->api_version;
239 }
240  
241 =head2 auth_token
242  
243 The token that is passed during authentication. This is generated during the
244 authentication process and stored until no longer valid or username is changed.
245  
246 =cut
247  
248 9 has auth_token => (
249 is => 'rwp',
250 clearer => 1,
251 );
252  
253 =head1 PUBLIC METHODS
254  
255 =head2 version
256  
257 Returns the etcd member version
258  
259 $etcd->version()
260  
261 =cut
262  
263 sub version {
264 0 ▶ my ( $self, $options ) = @_;
Subroutine version: not called CC=3 SCAR=24.8
Pod documented
265 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
266 0 ▶ my $member = Net::Etcd::Maintenance->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
267 etcd => $self,
268 cb => $cb,
269 ( $options ? %$options : () ),
270 );
271 0 return $member->version;
272 }
273  
274 =head2 watch
275  
276 See L<Net::Etcd::Watch>
277  
278 $etcd->watch({ key =>'foo', range_end => 'fop' })
279  
280 =cut
281  
282 sub watch {
283 0 ▶ my ( $self, $options ) = @_;
Subroutine watch: not called CC=3 SCAR=24.8
Pod documented
284 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
285 0 ▶ return Net::Etcd::Watch->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
286 etcd => $self,
287 cb => $cb,
288 ( $options ? %$options : () ),
289 );
290 }
291  
292 =head2 role
293  
294 See L<Net::Etcd::Auth::Role>
295  
296 $etcd->role({ role => 'foo' });
297  
298 =cut
299  
300 sub role {
301 0 ▶ my ( $self, $options ) = @_;
Subroutine role: not called CC=3 SCAR=24.8
Pod documented
302 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
303 0 ▶ return Net::Etcd::Auth::Role->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
304 etcd => $self,
305 cb => $cb,
306 ( $options ? %$options : () ),
307 );
308 }
309  
310 =head2 role_perm
311  
312 See L<Net::Etcd::Auth::RolePermission>
313  
314 Grants or revoke permission of a specified key or range to a specified role.
315  
316 =cut
317  
318 sub role_perm {
319 0 ▶ my ( $self, $options ) = @_;
Subroutine role_perm: not called CC=3 SCAR=24.8
Pod documented
320 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
321 0 ▶ my $perm = Net::Etcd::Auth::RolePermission->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
322 etcd => $self,
323 cb => $cb,
324 ( $options ? %$options : () ),
325 );
326 }
327  
328 =head2 user_role
329  
330 See L<Net::Etcd::User::Role>
331  
332 $etcd->user_role({ name => 'samba', role => 'foo' });
333  
334 =cut
335  
336 sub user_role {
337 0 ▶ my ( $self, $options ) = @_;
Subroutine user_role: not called CC=3 SCAR=24.8
Pod documented
338 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
339 0 ▶ return Net::Etcd::User::Role->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
340 etcd => $self,
341 cb => $cb,
342 ( $options ? %$options : () ),
343 );
344 }
345  
346 =head2 auth
347  
348 See L<Net::Etcd::Auth>
349  
350 $etcd->auth({ name => 'samba', password => 'foo' })->authenticate;
351 $etcd->auth()->enable;
352 $etcd->auth()->disable
353  
354 =cut
355  
356 sub auth {
357 0 ▶ my ( $self, $options ) = @_;
Subroutine auth: not called CC=3 SCAR=24.8
Pod documented
358 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
359 0 ▶ return Net::Etcd::Auth->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
360 etcd => $self,
361 cb => $cb,
362 ( $options ? %$options : () ),
363 );
364 }
365  
366 =head2 lease
367  
368 See L<Net::Etcd::Lease>
369  
370 $etcd->lease( { ID => 7587821338341002662, TTL => 20 } )->grant;
371  
372 =cut
373  
374 sub lease {
375 0 ▶ my ( $self, $options ) = @_;
Subroutine lease: not called CC=3 SCAR=24.8
Pod documented
376 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
377 0 ▶ return Net::Etcd::Lease->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
378 etcd => $self,
379 cb => $cb,
380 ( $options ? %$options : () ),
381 );
382 }
383  
384 =head2 maintenance
385  
386 See L<Net::Etcd::Maintenance>
387  
388 $etcd->maintenance()->snapshot
389  
390 =cut
391  
392 sub maintenance {
393 0 ▶ my ( $self, $options ) = @_;
Subroutine maintenance: not called CC=3 SCAR=24.8
Pod documented
394 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
395 0 ▶ return Net::Etcd::Maintenance->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
396 etcd => $self,
397 cb => $cb,
398 ( $options ? %$options : () ),
399 );
400 }
401  
402 =head2 member
403  
404 See L<Net::Etcd::Member>
405  
406 $etcd->member()->list
407  
408 =cut
409  
410 sub member {
411 0 ▶ my ( $self, $options ) = @_;
Subroutine member: not called CC=3 SCAR=24.8
Pod documented
412 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
413 0 ▶ return Net::Etcd::Member->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
414 etcd => $self,
415 cb => $cb,
416 ( $options ? %$options : () ),
417 );
418 }
419  
420 =head2 user
421  
422 See L<Net::Etcd::User>
423  
424 $etcd->user( { name => 'samba', password => 'foo' } )->add;
425  
426 =cut
427  
428 sub user {
429 0 ▶ my ( $self, $options ) = @_;
Subroutine user: not called CC=3 SCAR=24.8
Pod documented
430 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
431 0 ▶ return Net::Etcd::User->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
432 etcd => $self,
433 cb => $cb,
434 ( $options ? %$options : () ),
435 );
436 }
437  
438 =head2 put
439  
440 See L<Net::Etcd::KV::Put>
441  
442 $etcd->put({ key =>'foo1', value => 'bar' });
443  
444 =cut
445  
446 =head2 deleterange
447  
448 See L<Net::Etcd::KV::DeleteRange>
449  
450 $etcd->deleterange({ key=>'test0' });
451  
452 =cut
453  
454 =head2 range
455  
456 See L<Net::Etcd::KV::Range>
457  
458 $etcd->range({ key =>'test0', range_end => 'test100' });
459  
460 =cut
461  
462 =head2 txn
463  
464 See L<Net::Etcd::KV::Txn>
465  
466 $etcd->txn({ compare => \@compare, success => \@op });
467  
468 =cut
469  
470 =head2 op
471  
472 See L<Net::Etcd::KV::Op>
473  
474 $etcd->op({ request_put => $put });
475 $etcd->op({ request_delete_range => $range });
476  
477 =cut
478  
479 =head2 compare
480  
481 See L<Net::Etcd::KV::Compare>
482  
483 $etcd->compare( { key => 'foo', result => 'EQUAL', target => 'VALUE', value => 'baz' });
484 $etcd->compare( { key => 'foo', target => 'CREATE', result => 'NOT_EQUAL', create_revision => '2' });
485  
486 =cut
487  
488 =head2 configuration
489  
490 Initialize configuration checks to see if etcd is installed locally.
491  
492 =cut
493  
494 sub configuration {
495 0 ▶ Net::Etcd::Config->configuration;
Subroutine configuration: not called CC=1 SCAR=6.9
Pod documented
496 }
497  
498 =head1 AUTHOR
499  
500 Sam Batschelet (hexfusion)
501  
502 =head1 CONTRIBUTORS
503  
504 Ananth Kavuri
505  
506 =head1 ACKNOWLEDGEMENTS
507  
508 The L<etcd|https://github.com/coreos/etcd> developers and community.
509  
510 =head1 CAVEATS
511  
512 The L<etcd|https://github.com/coreos/etcd> v3 API is in heavy development and can change at anytime please see
513 L<api_reference_v3|https://github.com/coreos/etcd/blob/master/Documentation/dev-guide/api_reference_v3.md>
514 for latest details.
515  
516 Authentication provided by this module will only work with etcd v3.3.0+
517  
518 =head1 LICENSE AND COPYRIGHT
519  
520 Copyright 2018 Sam Batschelet (hexfusion).
521  
522 This program is free software; you can redistribute it and/or modify it
523 under the terms of either: the GNU General Public License as published
524 by the Free Software Foundation; or the Artistic License.
525  
526 See http://dev.perl.org/licenses/ for more information.
527  
528 =cut
529  
530 9 1;
« Etcd/Role/Actions.pm ↑ Summary Etcd/KV.pm »