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 CC 26 SCAR 24.8
CC
26
Cov
42%
_build_api_path
42.0
version
12.0
watch
12.0
CRAP 11.9
« Etcd/Auth.pm ↑ Summary Etcd/KV/Txn.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 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(Str Int Bool HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use Net::Etcd::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Net::Etcd::Auth::RolePermission;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Net::Etcd::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Net::Etcd::Watch;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Net::Etcd::Lease;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Net::Etcd::Maintenance;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Net::Etcd::Member;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use Net::Etcd::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 with('Net::Etcd::KV');
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.022';
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 =head2 ssl
197  
198 To enable set to 1
199  
200 =cut
201  
202 9 has ssl => (
203 is => 'ro',
204 isa => Bool,
205 );
206  
207 =head2 api_version
208  
209 defaults to /v3beta
210  
211 =cut
212  
213 9 has api_version => (
214 is => 'ro',
215 isa => Str,
216 default => '/v3beta'
217 );
218  
219 =head2 api_path
220  
221 The full api path. Defaults to http://127.0.0.1:2379/v3alpha
222  
223 =cut
224  
225 9 has api_path => ( is => 'lazy' );
226  
227 0 ▶sub _build_api_path {
Subroutine _build_api_path: not called CC=6 SCAR=37.4
228 0 my ($self) = @_;
229 return ( $self->{ssl}
230 || $self->{ca_file}
231 || $self->{key_file}
232 || $self->{cafile}
233 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
234 . '://'
235 . $self->host . ':'
236 . $self->port
237 . $self->api_version;
238 }
239  
240 =head2 auth_token
241  
242 The token that is passed during authentication. This is generated during the
243 authentication process and stored until no longer valid or username is changed.
244  
245 =cut
246  
247 9 has auth_token => (
248 is => 'rwp',
249 clearer => 1,
250 );
251  
252 =head1 PUBLIC METHODS
253  
254 =head2 version
255  
256 Returns the etcd member version
257  
258 $etcd->version()
259  
260 =cut
261  
262 0 ▶sub version {
Subroutine version: not called CC=3 SCAR=24.8
Pod documented
263 0 my ( $self, $options ) = @_;
264 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
265 0 ▶ my $member = Net::Etcd::Maintenance->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
266 etcd => $self,
267 cb => $cb,
268 ( $options ? %$options : () ),
269 );
270 0 return $member->version;
271 }
272  
273 =head2 watch
274  
275 See L<Net::Etcd::Watch>
276  
277 $etcd->watch({ key =>'foo', range_end => 'fop' })
278  
279 =cut
280  
281 0 ▶sub watch {
Subroutine watch: not called CC=3 SCAR=24.8
Pod documented
282 0 my ( $self, $options ) = @_;
283 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
284 0 ▶ return Net::Etcd::Watch->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
285 etcd => $self,
286 cb => $cb,
287 ( $options ? %$options : () ),
288 );
289 }
290  
291 =head2 role
292  
293 See L<Net::Etcd::Auth::Role>
294  
295 $etcd->role({ role => 'foo' });
296  
297 =cut
298  
299 0 ▶sub role {
Subroutine role: not called CC=3 SCAR=24.8
Pod documented
300 0 my ( $self, $options ) = @_;
301 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
302 0 ▶ return Net::Etcd::Auth::Role->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
303 etcd => $self,
304 cb => $cb,
305 ( $options ? %$options : () ),
306 );
307 }
308  
309 =head2 role_perm
310  
311 See L<Net::Etcd::Auth::RolePermission>
312  
313 Grants or revoke permission of a specified key or range to a specified role.
314  
315 $etcd->role_perm(
316 { name => 'myrole', key => 'bar', permType => 'READ', prefix => 1 } )->grant;
317  
318 =cut
319  
320 0 ▶sub role_perm {
Subroutine role_perm: not called CC=3 SCAR=24.8
Pod documented
321 0 my ( $self, $options ) = @_;
322 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
323 0 ▶ my $perm = Net::Etcd::Auth::RolePermission->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
324 etcd => $self,
325 cb => $cb,
326 ( $options ? %$options : () ),
327 );
328 }
329  
330 =head2 user_role
331  
332 See L<Net::Etcd::User::Role>
333  
334 $etcd->user_role({ name => 'samba', role => 'foo' });
335  
336 =cut
337  
338 0 ▶sub user_role {
Subroutine user_role: not called CC=3 SCAR=24.8
Pod documented
339 0 my ( $self, $options ) = @_;
340 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
341 0 ▶ return Net::Etcd::User::Role->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
342 etcd => $self,
343 cb => $cb,
344 ( $options ? %$options : () ),
345 );
346 }
347  
348 =head2 auth
349  
350 See L<Net::Etcd::Auth>
351  
352 $etcd->auth({ name => 'samba', password => 'foo' })->authenticate;
353 $etcd->auth()->enable;
354 $etcd->auth()->disable
355  
356 =cut
357  
358 0 ▶sub auth {
Subroutine auth: not called CC=3 SCAR=24.8
Pod documented
359 0 my ( $self, $options ) = @_;
360 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
361 0 ▶ return Net::Etcd::Auth->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
362 etcd => $self,
363 cb => $cb,
364 ( $options ? %$options : () ),
365 );
366 }
367  
368 =head2 lease
369  
370 See L<Net::Etcd::Lease>
371  
372 $etcd->lease( { ID => 7587821338341002662, TTL => 20 } )->grant;
373  
374 =cut
375  
376 0 ▶sub lease {
Subroutine lease: not called CC=3 SCAR=24.8
Pod documented
377 0 my ( $self, $options ) = @_;
378 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
379 0 ▶ return Net::Etcd::Lease->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
380 etcd => $self,
381 cb => $cb,
382 ( $options ? %$options : () ),
383 );
384 }
385  
386 =head2 maintenance
387  
388 See L<Net::Etcd::Maintenance>
389  
390 $etcd->maintenance()->snapshot
391  
392 =cut
393  
394 0 ▶sub maintenance {
Subroutine maintenance: not called CC=3 SCAR=24.8
Pod documented
395 0 my ( $self, $options ) = @_;
396 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
397 0 ▶ return Net::Etcd::Maintenance->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
398 etcd => $self,
399 cb => $cb,
400 ( $options ? %$options : () ),
401 );
402 }
403  
404 =head2 member
405  
406 See L<Net::Etcd::Member>
407  
408 $etcd->member()->list
409  
410 =cut
411  
412 0 ▶sub member {
Subroutine member: not called CC=3 SCAR=24.8
Pod documented
413 0 my ( $self, $options ) = @_;
414 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
415 0 ▶ return Net::Etcd::Member->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
416 etcd => $self,
417 cb => $cb,
418 ( $options ? %$options : () ),
419 );
420 }
421  
422 =head2 user
423  
424 See L<Net::Etcd::User>
425  
426 $etcd->user( { name => 'samba', password => 'foo' } )->add;
427  
428 =cut
429  
430 0 ▶sub user {
Subroutine user: not called CC=3 SCAR=24.8
Pod documented
431 0 my ( $self, $options ) = @_;
432 0 ▶ my $cb = pop if ref $_[-1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "CODE" 0 0 0
433 0 ▶ return Net::Etcd::User->new(
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 0 0
434 etcd => $self,
435 cb => $cb,
436 ( $options ? %$options : () ),
437 );
438 }
439  
440 =head2 put
441  
442 See L<Net::Etcd::KV::Put>
443  
444 $etcd->put({ key =>'foo1', value => 'bar' });
445  
446 =cut
447  
448 =head2 deleterange
449  
450 See L<Net::Etcd::KV::DeleteRange>
451  
452 $etcd->deleterange({ key=>'test0' });
453  
454 =cut
455  
456 =head2 range
457  
458 See L<Net::Etcd::KV::Range>
459  
460 $etcd->range({ key =>'test0', range_end => 'test100' });
461  
462 =cut
463  
464 =head2 txn
465  
466 See L<Net::Etcd::KV::Txn>
467  
468 $etcd->txn({ compare => \@compare, success => \@op });
469  
470 =cut
471  
472 =head2 op
473  
474 See L<Net::Etcd::KV::Op>
475  
476 $etcd->op({ request_put => $put });
477 $etcd->op({ request_delete_range => $range });
478  
479 =cut
480  
481 =head2 compare
482  
483 See L<Net::Etcd::KV::Compare>
484  
485 $etcd->compare( { key => 'foo', result => 'EQUAL', target => 'VALUE', value => 'baz' });
486 $etcd->compare( { key => 'foo', target => 'CREATE', result => 'NOT_EQUAL', create_revision => '2' });
487  
488 =cut
489  
490 =head2 configuration
491  
492 Initialize configuration checks to see if etcd is installed locally.
493  
494 =cut
495  
496 0 ▶sub configuration {
Subroutine configuration: not called CC=1 SCAR=6.9
Pod documented
497 0 Net::Etcd::Config->configuration;
498 }
499  
500 =head1 AUTHOR
501  
502 Sam Batschelet (hexfusion)
503  
504 =head1 CONTRIBUTORS
505  
506 Ananth Kavuri
507 Michael Fung
508  
509 =head1 ACKNOWLEDGEMENTS
510  
511 The L<etcd|https://github.com/coreos/etcd> developers and community.
512  
513 =head1 CAVEATS
514  
515 The L<etcd|https://github.com/coreos/etcd> v3 API is in heavy development and can change at anytime please see
516 L<api_reference_v3|https://github.com/coreos/etcd/blob/master/Documentation/dev-guide/api_reference_v3.md>
517 for latest details.
518  
519 Authentication provided by this module will only work with etcd v3.3.0+
520  
521 =head1 LICENSE AND COPYRIGHT
522  
523 Copyright 2018 Sam Batschelet (hexfusion).
524  
525 This program is free software; you can redistribute it and/or modify it
526 under the terms of either: the GNU General Public License as published
527 by the Free Software Foundation; or the Artistic License.
528  
529 See http://dev.perl.org/licenses/ for more information.
530  
531 =cut
532  
533 9 1;
« Etcd/Auth.pm ↑ Summary Etcd/KV/Txn.pm »