| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 5 |  |  | 5 |  | 17136 | use utf8; | 
|  | 5 |  |  |  |  | 59 |  | 
|  | 5 |  |  |  |  | 22 |  | 
| 2 |  |  |  |  |  |  | package Etcd3; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: Provide access to the etcd v3 API. | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 5 |  |  | 5 |  | 154 | use strict; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 77 |  | 
| 6 | 5 |  |  | 5 |  | 18 | use warnings; | 
|  | 5 |  |  |  |  | 11 |  | 
|  | 5 |  |  |  |  | 90 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 5 |  |  | 5 |  | 2138 | use Moo; | 
|  | 5 |  |  |  |  | 47958 |  | 
|  | 5 |  |  |  |  | 22 |  | 
| 9 | 5 |  |  | 5 |  | 8014 | use JSON; | 
|  | 5 |  |  |  |  | 48755 |  | 
|  | 5 |  |  |  |  | 49 |  | 
| 10 | 5 |  |  | 5 |  | 2440 | use MIME::Base64; | 
|  | 5 |  |  |  |  | 2363 |  | 
|  | 5 |  |  |  |  | 251 |  | 
| 11 | 5 |  |  | 5 |  | 1413 | use Etcd3::Auth; | 
|  | 5 |  |  |  |  | 16 |  | 
|  | 5 |  |  |  |  | 149 |  | 
| 12 | 5 |  |  | 5 |  | 1513 | use Etcd3::Config; | 
|  | 5 |  |  |  |  | 14 |  | 
|  | 5 |  |  |  |  | 117 |  | 
| 13 | 5 |  |  | 5 |  | 1564 | use Etcd3::Watch; | 
|  | 5 |  |  |  |  | 15 |  | 
|  | 5 |  |  |  |  | 151 |  | 
| 14 | 5 |  |  | 5 |  | 1752 | use Etcd3::Lease; | 
|  | 5 |  |  |  |  | 16 |  | 
|  | 5 |  |  |  |  | 140 |  | 
| 15 | 5 |  |  | 5 |  | 1633 | use Etcd3::User; | 
|  | 5 |  |  |  |  | 18 |  | 
|  | 5 |  |  |  |  | 178 |  | 
| 16 | 5 |  |  | 5 |  | 32 | use Types::Standard qw(Str Int Bool HashRef); | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 23 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | with('Etcd3::KV'); | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 5 |  |  | 5 |  | 3937 | use namespace::clean; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 20 |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | =encoding utf8 | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | =head1 NAME | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | Etcd3 | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =cut | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | our $VERSION = '0.006'; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | Etcd v3.1.0 or greater is required.   To use the v3 API make sure to set environment | 
| 35 |  |  |  |  |  |  | variable ETCDCTL_API=3.  Precompiled binaries can be downloaded at https://github.com/coreos/etcd/releases. | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | $etcd = Etcd3->new(); # host: 127.0.0.1 port: 2379 | 
| 38 |  |  |  |  |  |  | $etcd = Etcd3->new({ host => $host, port => $port, ssl => 1 }); | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | # put key | 
| 41 |  |  |  |  |  |  | $result = $etcd->put({ key =>'foo1', value => 'bar' }); | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # get single key | 
| 44 |  |  |  |  |  |  | $key = $etcd->range({ key =>'test0' }); | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | # return single key value or the first in a list. | 
| 47 |  |  |  |  |  |  | $key->get_value | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | # get range of keys | 
| 50 |  |  |  |  |  |  | $range = $etcd->range({ key =>'test0', range_end => 'test100' }); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | # return array { key => value } pairs from range request. | 
| 53 |  |  |  |  |  |  | my @users = $range->all | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | # watch key range, streaming. | 
| 56 |  |  |  |  |  |  | $watch = $etcd->watch( { key => 'foo', range_end => 'fop'}, sub { | 
| 57 |  |  |  |  |  |  | my ($result) =  @_; | 
| 58 |  |  |  |  |  |  | print STDERR Dumper($result); | 
| 59 |  |  |  |  |  |  | })->create; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | # create/grant 20 second lease | 
| 62 |  |  |  |  |  |  | $etcd->lease( { ID => 7587821338341002662, TTL => 20 } )->grant; | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # attach lease to put | 
| 65 |  |  |  |  |  |  | $etcd->put( { key => 'foo2', value => 'bar2', lease => 7587821338341002662 } ); | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | C An object oriented interface to the v3 REST API provided by the etcd grpc gateway. | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =head1 ACCESSORS | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =head2 host | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =cut | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | has host => ( | 
| 78 |  |  |  |  |  |  | is      => 'ro', | 
| 79 |  |  |  |  |  |  | isa     => Str, | 
| 80 |  |  |  |  |  |  | default => '127.0.0.1' | 
| 81 |  |  |  |  |  |  | ); | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =head2 port | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =cut | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | has port => ( | 
| 88 |  |  |  |  |  |  | is      => 'ro', | 
| 89 |  |  |  |  |  |  | isa     => Int, | 
| 90 |  |  |  |  |  |  | default => '2379' | 
| 91 |  |  |  |  |  |  | ); | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =head2 username | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | =cut | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | has username => ( | 
| 98 |  |  |  |  |  |  | is  => 'ro', | 
| 99 |  |  |  |  |  |  | isa => Str | 
| 100 |  |  |  |  |  |  | ); | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =head2 password | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =cut | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | has password => ( | 
| 107 |  |  |  |  |  |  | is  => 'ro', | 
| 108 |  |  |  |  |  |  | isa => Str | 
| 109 |  |  |  |  |  |  | ); | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =head2 ssl | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =cut | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | has ssl => ( | 
| 116 |  |  |  |  |  |  | is  => 'ro', | 
| 117 |  |  |  |  |  |  | isa => Bool, | 
| 118 |  |  |  |  |  |  | ); | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | =head2 auth | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =cut | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | has auth => ( | 
| 125 |  |  |  |  |  |  | is  => 'lazy', | 
| 126 |  |  |  |  |  |  | isa => Bool, | 
| 127 |  |  |  |  |  |  | ); | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | sub _build_auth { | 
| 130 | 0 |  |  | 0 |  |  | my ($self) = @_; | 
| 131 | 0 | 0 | 0 |  |  |  | return 1 if ( $self->username and $self->password ); | 
| 132 | 0 |  |  |  |  |  | return; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head2 api_root | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =cut | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | has api_root => ( is => 'lazy' ); | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | sub _build_api_root { | 
| 142 | 0 |  |  | 0 |  |  | my ($self) = @_; | 
| 143 |  |  |  |  |  |  | return | 
| 144 | 0 | 0 |  |  |  |  | ( $self->ssl ? 'https' : 'http' ) . '://' | 
| 145 |  |  |  |  |  |  | . $self->host . ':' | 
| 146 |  |  |  |  |  |  | . $self->port; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =head2 api_prefix | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | defaults to /v3alpha | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =cut | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | has api_prefix => ( | 
| 156 |  |  |  |  |  |  | is      => 'ro', | 
| 157 |  |  |  |  |  |  | isa     => Str, | 
| 158 |  |  |  |  |  |  | default => '/v3alpha' | 
| 159 |  |  |  |  |  |  | ); | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =head2 api_path | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | =cut | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | has api_path => ( is => 'lazy' ); | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | sub _build_api_path { | 
| 168 | 0 |  |  | 0 |  |  | my ($self) = @_; | 
| 169 | 0 |  |  |  |  |  | return $self->api_root . $self->api_prefix; | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | =head2 auth_token | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | =cut | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | has auth_token => ( is => 'lazy' ); | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | sub _build_auth_token { | 
| 179 | 0 |  |  | 0 |  |  | my ($self) = @_; | 
| 180 | 0 |  |  |  |  |  | return Etcd3::Auth::Authenticate->new( | 
| 181 |  |  |  |  |  |  | etcd => $self, | 
| 182 |  |  |  |  |  |  | %$self | 
| 183 |  |  |  |  |  |  | )->token; | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =head1 PUBLIC METHODS | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | =head2 watch | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | Returns a L object. | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | $etcd->watch({ key =>'foo', range_end => 'fop' }) | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =cut | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | sub watch { | 
| 197 | 0 |  |  | 0 | 1 |  | my ( $self, $options ) = @_; | 
| 198 | 0 | 0 |  |  |  |  | my $cb = pop if ref $_[-1] eq 'CODE'; | 
| 199 | 0 | 0 |  |  |  |  | return Etcd3::Watch->new( | 
| 200 |  |  |  |  |  |  | etcd => $self, | 
| 201 |  |  |  |  |  |  | cb   => $cb, | 
| 202 |  |  |  |  |  |  | ( $options ? %$options : () ), | 
| 203 |  |  |  |  |  |  | ); | 
| 204 |  |  |  |  |  |  | } | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | =head2 role | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | Returns a L object. | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | $etcd->role({ role => 'foo' }); | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | =cut | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | sub role { | 
| 215 | 0 |  |  | 0 | 1 |  | my ( $self, $options ) = @_; | 
| 216 | 0 | 0 |  |  |  |  | my $cb = pop if ref $_[-1] eq 'CODE'; | 
| 217 | 0 | 0 |  |  |  |  | return Etcd3::Auth::Role->new( | 
| 218 |  |  |  |  |  |  | etcd => $self, | 
| 219 |  |  |  |  |  |  | cb   => $cb, | 
| 220 |  |  |  |  |  |  | ( $options ? %$options : () ), | 
| 221 |  |  |  |  |  |  | ); | 
| 222 |  |  |  |  |  |  | } | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | =head2 user_role | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | Returns a L object. | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | $etcd->user_role({ name => 'samba', role => 'foo' }); | 
| 229 |  |  |  |  |  |  |  | 
| 230 |  |  |  |  |  |  | =cut | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | sub user_role { | 
| 233 | 0 |  |  | 0 | 1 |  | my ( $self, $options ) = @_; | 
| 234 | 0 | 0 |  |  |  |  | my $cb = pop if ref $_[-1] eq 'CODE'; | 
| 235 | 0 | 0 |  |  |  |  | return Etcd3::User::Role->new( | 
| 236 |  |  |  |  |  |  | etcd => $self, | 
| 237 |  |  |  |  |  |  | cb   => $cb, | 
| 238 |  |  |  |  |  |  | ( $options ? %$options : () ), | 
| 239 |  |  |  |  |  |  | ); | 
| 240 |  |  |  |  |  |  | } | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | =head2 auth_enable | 
| 243 |  |  |  |  |  |  |  | 
| 244 |  |  |  |  |  |  | Currently not available. | 
| 245 |  |  |  |  |  |  |  | 
| 246 |  |  |  |  |  |  | =cut | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  | sub auth_enable { | 
| 249 | 0 |  |  | 0 | 1 |  | my ( $self, $options ) = @_; | 
| 250 | 0 |  |  |  |  |  | my $auth = Etcd3::Auth::Enable->new( etcd => $self )->init; | 
| 251 | 0 |  |  |  |  |  | return $auth->request; | 
| 252 |  |  |  |  |  |  | } | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  |  | 
| 255 |  |  |  |  |  |  | =head2 lease | 
| 256 |  |  |  |  |  |  |  | 
| 257 |  |  |  |  |  |  | Returns a L object. | 
| 258 |  |  |  |  |  |  |  | 
| 259 |  |  |  |  |  |  | =cut | 
| 260 |  |  |  |  |  |  |  | 
| 261 |  |  |  |  |  |  | sub lease { | 
| 262 | 0 |  |  | 0 | 1 |  | my ( $self, $options ) = @_; | 
| 263 | 0 | 0 |  |  |  |  | my $cb = pop if ref $_[-1] eq 'CODE'; | 
| 264 | 0 | 0 |  |  |  |  | return Etcd3::Lease->new( | 
| 265 |  |  |  |  |  |  | etcd => $self, | 
| 266 |  |  |  |  |  |  | cb   => $cb, | 
| 267 |  |  |  |  |  |  | ( $options ? %$options : () ), | 
| 268 |  |  |  |  |  |  | ); | 
| 269 |  |  |  |  |  |  | } | 
| 270 |  |  |  |  |  |  |  | 
| 271 |  |  |  |  |  |  | =head2 user | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | Returns a L object. | 
| 274 |  |  |  |  |  |  |  | 
| 275 |  |  |  |  |  |  | =cut | 
| 276 |  |  |  |  |  |  |  | 
| 277 |  |  |  |  |  |  | sub user { | 
| 278 | 0 |  |  | 0 | 1 |  | my ( $self, $options ) = @_; | 
| 279 | 0 | 0 |  |  |  |  | my $cb = pop if ref $_[-1] eq 'CODE'; | 
| 280 | 0 | 0 |  |  |  |  | return Etcd3::User->new( | 
| 281 |  |  |  |  |  |  | etcd => $self, | 
| 282 |  |  |  |  |  |  | cb   => $cb, | 
| 283 |  |  |  |  |  |  | ( $options ? %$options : () ), | 
| 284 |  |  |  |  |  |  | ); | 
| 285 |  |  |  |  |  |  | } | 
| 286 |  |  |  |  |  |  |  | 
| 287 |  |  |  |  |  |  | =head2 put | 
| 288 |  |  |  |  |  |  |  | 
| 289 |  |  |  |  |  |  | Returns a L object. | 
| 290 |  |  |  |  |  |  |  | 
| 291 |  |  |  |  |  |  | =cut | 
| 292 |  |  |  |  |  |  |  | 
| 293 |  |  |  |  |  |  | =head2 range | 
| 294 |  |  |  |  |  |  |  | 
| 295 |  |  |  |  |  |  | Returns a L object. | 
| 296 |  |  |  |  |  |  |  | 
| 297 |  |  |  |  |  |  | =cut | 
| 298 |  |  |  |  |  |  |  | 
| 299 |  |  |  |  |  |  | =head2 configuration | 
| 300 |  |  |  |  |  |  |  | 
| 301 |  |  |  |  |  |  | Initialize configuration checks to see it etcd is installed locally. | 
| 302 |  |  |  |  |  |  |  | 
| 303 |  |  |  |  |  |  | =cut | 
| 304 |  |  |  |  |  |  |  | 
| 305 |  |  |  |  |  |  | sub configuration { | 
| 306 | 0 |  |  | 0 | 1 |  | Etcd3::Config->configuration; | 
| 307 |  |  |  |  |  |  | } | 
| 308 |  |  |  |  |  |  |  | 
| 309 |  |  |  |  |  |  | sub BUILD { | 
| 310 | 0 |  |  | 0 | 0 |  | my ( $self, $args ) = @_; | 
| 311 | 0 | 0 |  |  |  |  | if ( not -e $self->configuration->etcd ) { | 
| 312 | 0 |  |  |  |  |  | my $msg = "No etcd executable found\n"; | 
| 313 | 0 |  |  |  |  |  | $msg .= ">> Please install etcd - https://coreos.com/etcd/docs/latest/"; | 
| 314 | 0 |  |  |  |  |  | die $msg; | 
| 315 |  |  |  |  |  |  | } | 
| 316 |  |  |  |  |  |  | } | 
| 317 |  |  |  |  |  |  |  | 
| 318 |  |  |  |  |  |  | =head1 AUTHOR | 
| 319 |  |  |  |  |  |  |  | 
| 320 |  |  |  |  |  |  | Sam Batschelet, | 
| 321 |  |  |  |  |  |  |  | 
| 322 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 323 |  |  |  |  |  |  |  | 
| 324 |  |  |  |  |  |  | The L developers and community. | 
| 325 |  |  |  |  |  |  |  | 
| 326 |  |  |  |  |  |  | =head1 CAVEATS | 
| 327 |  |  |  |  |  |  |  | 
| 328 |  |  |  |  |  |  | The L v3 API is in heavy development and can change at anytime please see | 
| 329 |  |  |  |  |  |  | https://github.com/coreos/etcd/blob/master/Documentation/dev-guide/api_reference_v3.md | 
| 330 |  |  |  |  |  |  | for latest details. | 
| 331 |  |  |  |  |  |  |  | 
| 332 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 333 |  |  |  |  |  |  |  | 
| 334 |  |  |  |  |  |  | Copyright 2017 Sam Batschelet (hexfusion). | 
| 335 |  |  |  |  |  |  |  | 
| 336 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 337 |  |  |  |  |  |  | under the terms of either: the GNU General Public License as published | 
| 338 |  |  |  |  |  |  | by the Free Software Foundation; or the Artistic License. | 
| 339 |  |  |  |  |  |  |  | 
| 340 |  |  |  |  |  |  | See http://dev.perl.org/licenses/ for more information. | 
| 341 |  |  |  |  |  |  |  | 
| 342 |  |  |  |  |  |  | =cut | 
| 343 |  |  |  |  |  |  |  | 
| 344 |  |  |  |  |  |  | 1; |