File Coverage

blib/lib/Dancer2/Plugin/Etcd.pm
Criterion Covered Total %
statement 33 36 91.6
branch 2 4 50.0
condition 1 2 50.0
subroutine 9 9 100.0
pod 1 1 100.0
total 46 52 88.4


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Etcd;
2 2     2   190700 use utf8;
  2         24  
  2         11  
3 2     2   65 use strict;
  2         3  
  2         45  
4 2     2   8 use warnings;
  2         5  
  2         108  
5              
6             =encoding utf8
7              
8             =head1 NAME
9              
10             Dancer2::Plugin::Etcd
11              
12             =cut
13              
14             our $VERSION = '0.001';
15              
16 2     2   1355 use Dancer2::Core::Types qw/Bool HashRef Str/;
  2         33854  
  2         383  
17 2     2   2092 use Dancer2::Plugin;
  2         142818  
  2         14  
18 2     2   44398 use Data::Dumper;
  2         13516  
  2         138  
19 2     2   13 use File::Spec;
  2         3  
  2         37  
20 2     2   1032 use Etcd3;
  2         455072  
  2         763  
21              
22             =head1 SYNOPSIS
23              
24             get '/foo/:name' => sub {
25             $tokens = shift;
26             $etcd = etcd('foo');
27             $name = param('name');
28             $tokens->{name} = $etcd->range({ key => $name))->get_value;
29             [ ... ]
30             };
31              
32             # save development configs to etcd
33             shepherd put
34              
35             # retrieve lasted configs
36             shepherd get
37              
38             =head1 DESCRIPTION
39              
40             The C keyword which is exported by this plugin allows you to
41             connect directly to etcd v3 api.
42              
43             =head1 shepherd
44              
45             By far the most interesting part of this module is shepherd. shepherd
46             allows you to save Dancer App YAML configs to etcd by line as key/value.
47             Even more interesting is that it maintains your comments. An example of
48             usage would be spawning a container with the application then simply running
49             shepherd get --env=production would bring in the latest production configs for
50             your app.
51              
52             =head1 CONFIGURATION
53              
54             single e.g.:
55              
56             plugins:
57             Etcd:
58             host: 127.0.0.1
59             port: 4001
60             user: samb
61             password: h3xFu5ion
62             ssl: 1
63              
64             named e.g.:
65              
66             plugins:
67             Etcd:
68             connections:
69             foo:
70             host: 127.0.0.1
71             port: 4001
72             user: samb
73             password: h3xFu5ion
74             bar:
75             host: 127.0.0.1
76             port: 2379
77              
78             =cut
79              
80             =head2 connections
81              
82             =cut
83              
84             has connections => (
85             is => 'ro',
86             isa => HashRef,
87             from_config => 'connections',
88             default => sub { +{} },
89             );
90              
91             =head2 host
92              
93             default 127.0.0.1
94              
95             =cut
96              
97             has host => (
98             is => 'ro',
99             isa => Str,
100             default => sub {
101             $_[0]->config->{host} || '127.0.0.1'
102             }
103             );
104              
105             =head2 port
106              
107             default 2379
108              
109             =cut
110              
111             has port => (
112             is => 'ro',
113             default => sub {
114             $_[0]->config->{port} || '2379'
115             }
116             );
117              
118             =head2 etcd_connection_name
119              
120             =cut
121              
122             has etcd_connection_name => (
123             is => 'ro',
124             isa => Str,
125             );
126              
127             =head2 username
128              
129             Etcd username.
130              
131             =cut
132              
133             has username => (
134             is => 'ro',
135             default => sub {
136             $_[0]->config->{user}
137             }
138             );
139              
140              
141             =head2 password
142              
143             Etcd user password.
144              
145             =cut
146              
147             has password => (
148             is => 'ro',
149             default => sub {
150             $_[0]->config->{password}
151             }
152             );
153              
154             =head2 ssl
155              
156             Secure connection is recommended,
157              
158             =cut
159              
160             has ssl => (
161             is => 'ro',
162             default => sub {
163             $_[0]->config->{ssl}
164             }
165             );
166              
167             plugin_keywords 'etcd';
168              
169             =head1 KEYWORDS
170              
171             =head2 etcd
172              
173             =cut
174              
175             sub etcd {
176 2     2 1 183711 my( $plugin, $handle ) = @_;
177 2         21 $plugin->app->log( "debug", "etcd handle: ", $handle );
178             # print STDERR Dumper($handle);
179 2         8866 my $host;
180 2   50     18 $handle ||= '';
181 2         5 my $settings;
182 2 50       9 if ($handle) {
183             # print STDERR Dumper($plugin->connections);
184 2 50       29 $settings = $plugin->connections->{$handle} or return;
185 2         1358 $host = $settings->{host};
186             }
187             else {
188 0         0 for (qw(username password port)) {
189 0         0 $settings->{$_} = $plugin->{$_};
190 0         0 $host = $plugin->{host};
191             }
192             }
193 2         25 return Etcd3->connect( $host, $settings );
194             };
195              
196             =head1 AUTHOR
197              
198             Sam Batschelet,
199              
200             =head1 ACKNOWLEDGEMENTS
201              
202             The L developers and community for their great application framework
203             and for their quick and competent support.
204              
205             =head1 LICENSE AND COPYRIGHT
206              
207             Copyright 2016 Sam Batschelet (hexfusion).
208              
209             This program is free software; you can redistribute it and/or modify it
210             under the terms of either: the GNU General Public License as published
211             by the Free Software Foundation; or the Artistic License.
212              
213             See http://dev.perl.org/licenses/ for more information.
214              
215             =cut
216              
217              
218             1;