File Coverage

blib/lib/Etcd3/Client.pm
Criterion Covered Total %
statement 72 125 57.6
branch 0 38 0.0
condition 0 3 0.0
subroutine 24 46 52.1
pod 16 17 94.1
total 112 229 48.9


line stmt bran cond sub pod time code
1 4     4   11 use utf8;
  4         4  
  4         13  
2             package Etcd3::Client;
3              
4 4     4   108 use strict;
  4         4  
  4         54  
5 4     4   9 use warnings;
  4         4  
  4         66  
6              
7 4     4   1587 use Moo;
  4         37183  
  4         16  
8 4     4   6599 use JSON;
  4         40582  
  4         17  
9 4     4   2564 use HTTP::Tiny;
  4         133705  
  4         169  
10 4     4   1598 use MIME::Base64;
  4         1756  
  4         260  
11 4     4   1207 use Etcd3::Auth::Authenticate;
  4         9  
  4         112  
12 4     4   1226 use Etcd3::Auth::Enable;
  4         6  
  4         103  
13 4     4   1221 use Etcd3::Auth::UserAdd;
  4         8  
  4         92  
14 4     4   1184 use Etcd3::Auth::UserDelete;
  4         6  
  4         95  
15 4     4   1167 use Etcd3::Auth::RoleAdd;
  4         6  
  4         93  
16 4     4   1166 use Etcd3::Auth::RoleDelete;
  4         6  
  4         109  
17 4     4   1310 use Etcd3::Auth::UserGrantRole;
  4         6  
  4         94  
18 4     4   1193 use Etcd3::Auth::UserRevokeRole;
  4         7  
  4         93  
19 4     4   1112 use Etcd3::Config;
  4         7  
  4         90  
20 4     4   1165 use Etcd3::Range;
  4         7  
  4         99  
21 4     4   1125 use Etcd3::DeleteRange;
  4         8  
  4         96  
22 4     4   1101 use Etcd3::Put;
  4         9  
  4         106  
23 4     4   1284 use Etcd3::Watch;
  4         9  
  4         109  
24 4     4   1238 use Etcd3::Lease;
  4         8  
  4         131  
25 4     4   20 use Types::Standard qw(Str Int Bool HashRef);
  4         6  
  4         17  
26 4     4   2061 use Data::Dumper;
  4         5  
  4         160  
27              
28 4     4   15 use namespace::clean;
  4         5  
  4         14  
29              
30             =encoding utf8
31              
32             =head1 NAME
33              
34             Etcd3::Client
35              
36             =cut
37              
38             our $VERSION = '0.005';
39              
40             =head1 DESCRIPTION
41              
42             Client data for etcd connection
43              
44             =head2 host
45              
46             =cut
47              
48             has host => (
49             is => 'ro',
50             isa => Str,
51             default => '127.0.0.1'
52             );
53              
54             =head2 port
55              
56             =cut
57              
58             has port => (
59             is => 'ro',
60             isa => Int,
61             default => '2379'
62             );
63              
64             =head2 username
65              
66             =cut
67              
68             has username => (
69             is => 'ro',
70             isa => Str
71             );
72              
73             =head2 password
74              
75             =cut
76              
77             has password => (
78             is => 'ro',
79             isa => Str
80             );
81              
82             =head2 ssl
83              
84             =cut
85              
86             has ssl => (
87             is => 'ro',
88             isa => Bool,
89             );
90              
91             =head2 auth
92              
93             =cut
94              
95             has auth => (
96             is => 'lazy',
97             isa => Bool,
98             );
99              
100             sub _build_auth {
101 0     0     my ($self) = @_;
102 0 0 0       return 1 if ( $self->username and $self->password );
103 0           return;
104             }
105              
106             =head2 api_root
107              
108             =cut
109              
110             has api_root => ( is => 'lazy' );
111              
112             sub _build_api_root {
113 0     0     my ($self) = @_;
114             return
115 0 0         ( $self->ssl ? 'https' : 'http' ) . '://'
116             . $self->host . ':'
117             . $self->port;
118             }
119              
120             =head2 api_prefix
121              
122             base endpoint for api call, refers to api version.
123              
124             =cut
125              
126             has api_prefix => (
127             is => 'ro',
128             isa => Str,
129             default => '/v3alpha'
130             );
131              
132             =head2 api_path
133              
134             =cut
135              
136             has api_path => ( is => 'lazy' );
137              
138             sub _build_api_path {
139 0     0     my ($self) = @_;
140 0           return $self->api_root . $self->api_prefix;
141             }
142              
143             =head2 auth_token
144              
145             =cut
146              
147             has auth_token => ( is => 'lazy' );
148              
149             sub _build_auth_token {
150 0     0     my ($self) = @_;
151 0           return Etcd3::Auth::Authenticate->new(
152             _client => $self,
153             %$self
154             )->token;
155             }
156              
157             =head2 headers
158              
159             =cut
160              
161             has headers => ( is => 'lazy' );
162              
163             sub _build_headers {
164 0     0     my ($self) = @_;
165 0           my $headers;
166 0 0         my $auth_token = $self->auth_token if $self->auth;
167 0           $headers->{'Content-Type'} = 'application/json';
168 0 0         $headers->{'authorization'} = 'Bearer ' . encode_base64( $auth_token, "" ) if $auth_token;
169 0           return $headers;
170             }
171              
172             =head2 watch
173              
174             returns a Etcd3::Watch object.
175              
176             $etcd->watch({ key =>'foo', range_end => 'fop' })
177              
178             =cut
179              
180             sub watch {
181 0     0 1   my ( $self, $options ) = @_;
182 0 0         return Etcd3::Watch->new(
183             _client => $self,
184             ( $options ? %$options : () ),
185             )->init;
186             }
187              
188             =head2 user_add
189              
190             $etcd->user_add({ name =>'foo' password => 'bar' })
191              
192             =cut
193              
194             sub user_add {
195 0     0 1   my ( $self, $options ) = @_;
196 0 0         return Etcd3::Auth::UserAdd->new(
197             _client => $self,
198             ( $options ? %$options : () ),
199             )->init;
200             }
201              
202             =head2 user_delete
203              
204             $etcd->user_delete({ name =>'foo' })
205              
206             =cut
207              
208             sub user_delete {
209 0     0 1   my ( $self, $options ) = @_;
210 0 0         return Etcd3::Auth::UserDelete->new(
211             _client => $self,
212             ( $options ? %$options : () ),
213             )->init;
214             }
215              
216             =head2 role_add
217              
218             name is the name of the role to add to the authentication system.
219              
220             $etcd->role_add({ name =>'foo' })
221              
222             =cut
223              
224             sub role_add {
225 0     0 1   my ( $self, $options ) = @_;
226 0 0         return Etcd3::Auth::RoleAdd->new(
227             _client => $self,
228             ( $options ? %$options : () ),
229             )->init;
230             }
231              
232             =head2 role_delete
233              
234             $etcd->role_delete({ name =>'foo' })
235              
236             =cut
237              
238             sub role_delete {
239 0     0 1   my ( $self, $options ) = @_;
240 0 0         return Etcd3::Auth::RoleDelete->new(
241             _client => $self,
242             ( $options ? %$options : () ),
243             )->init;
244             }
245              
246              
247             =head2 grant_role
248              
249             =cut
250              
251             sub grant_role {
252 0     0 1   my ( $self, $options ) = @_;
253 0 0         return Etcd3::Auth::UserGrantRole->new(
254             _client => $self,
255             ( $options ? %$options : () ),
256             )->init;
257             }
258              
259             =head2 revoke_role
260              
261             =cut
262              
263             sub revoke_role {
264 0     0 1   my ( $self, $options ) = @_;
265 0 0         return Etcd3::Auth::UserRevokeRole->new(
266             _client => $self,
267             ( $options ? %$options : () ),
268             )->init;
269             }
270              
271             =head2 auth_enable
272              
273             =cut
274              
275             sub auth_enable {
276 0     0 1   my ( $self, $options ) = @_;
277 0           my $auth = Etcd3::Auth::Enable->new( _client => $self )->init;
278 0           return $auth->request;
279             }
280              
281             =head2 delete_range
282              
283             $etcd->delete_range({ key =>'test0', range_end => 'test100', prev_key => 1 })
284              
285             =cut
286              
287             sub delete_range {
288 0     0 1   my ( $self, $options ) = @_;
289 0 0         return Etcd3::DeleteRange->new(
290             _client => $self,
291             ( $options ? %$options : () ),
292             )->init;
293             }
294              
295             =head2 put
296              
297             returns a Etcd3::Put object.
298              
299             =cut
300              
301             sub put {
302 0     0 1   my ( $self, $options ) = @_;
303 0 0         return Etcd3::Put->new(
304             _client => $self,
305             ( $options ? %$options : () ),
306             )->init;
307             }
308              
309             =head2 range
310              
311             returns a Etcd3::Range object
312              
313             $etcd->range({ key =>'test0', range_end => 'test100', serializable => 1 })
314              
315             =cut
316              
317             sub range {
318 0     0 1   my ( $self, $options ) = @_;
319 0 0         return Etcd3::Range->new(
320             _client => $self,
321             ( $options ? %$options : () ),
322             )->init;
323             }
324              
325             =head1 LEASE
326              
327             =head2 lease_grant
328              
329             returns a Etcd3::Lease::Grant object
330             If ID is set to 0, the lessor chooses an ID.
331              
332             $etcd->lease_grant({ TTL => 20, ID => 7587821338341002662 })
333              
334             =cut
335              
336             sub lease_grant {
337 0     0 1   my ( $self, $options ) = @_;
338 0 0         return Etcd3::Lease::Grant->new(
339             _client => $self,
340             ( $options ? %$options : () ),
341             )->init;
342             }
343              
344             =head2 lease_revoke
345              
346             returns a Etcd3::Lease::Revoke object
347              
348             $etcd->lease_revoke({ ID => 7587821338341002662 })
349              
350             =cut
351              
352             sub lease_revoke {
353 0     0 1   my ( $self, $options ) = @_;
354 0 0         return Etcd3::Lease::Revoke->new(
355             _client => $self,
356             ( $options ? %$options : () ),
357             )->init;
358             }
359              
360             =head2 lease_keep_alive
361              
362             returns a Etcd3::Lease::KeepAlive object
363              
364             $etcd->lease_keep_alive({ ID => 7587821338341002662 })
365              
366             =cut
367              
368             sub lease_keep_alive {
369 0     0 1   my ( $self, $options ) = @_;
370 0 0         return Etcd3::Lease::KeepAlive->new(
371             _client => $self,
372             ( $options ? %$options : () ),
373             )->init;
374             }
375              
376             =head2 lease_ttl
377              
378             returns a Etcd3::Lease::TimeToLive object
379              
380             $etcd->lease_ttl({ ID => 7587821338341002662, keys => 1 })
381              
382             =cut
383              
384             sub lease_ttl {
385 0     0 1   my ( $self, $options ) = @_;
386 0 0         return Etcd3::Lease::TimeToLive->new(
387             _client => $self,
388             ( $options ? %$options : () ),
389             )->init;
390             }
391              
392             =head2 configuration
393              
394             Initialize configuration checks to see it etcd is installed locally.
395              
396             =cut
397              
398             sub configuration {
399 0     0 1   Etcd3::Config->configuration;
400             }
401              
402             sub BUILD {
403 0     0 0   my ( $self, $args ) = @_;
404 0           $self->headers;
405 0 0         if ( not -e $self->configuration->etcd ) {
406 0           my $msg = "No etcd executable found\n";
407 0           $msg .= ">> Please install etcd - https://coreos.com/etcd/docs/latest/";
408 0           die $msg;
409             }
410             }
411              
412             =head1 AUTHOR
413              
414             Sam Batschelet,
415              
416             =head1 ACKNOWLEDGEMENTS
417              
418             The L developers and community.
419              
420             =head1 CAVEATS
421              
422             The L v3 API is in heavy development and can change at anytime please see
423             https://github.com/coreos/etcd/blob/master/Documentation/dev-guide/api_reference_v3.md
424             for latest details.
425              
426              
427             =head1 LICENSE AND COPYRIGHT
428              
429             Copyright 2017 Sam Batschelet (hexfusion).
430              
431             This program is free software; you can redistribute it and/or modify it
432             under the terms of either: the GNU General Public License as published
433             by the Free Software Foundation; or the Artistic License.
434              
435             See http://dev.perl.org/licenses/ for more information.
436              
437             =cut
438              
439             1;
440