File Coverage

blib/lib/Etcd3/Client.pm
Criterion Covered Total %
statement 69 114 60.5
branch 0 30 0.0
condition 0 3 0.0
subroutine 23 41 56.1
pod 12 13 92.3
total 104 201 51.7


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