EtcDv2/Node.pm

filter: Statementstmt 41.7% 63 / 151 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 63.3% 19 / 30 Podpod 100% 3 / 3 totaltotal 34.0% 85 / 250 CC 30 SCAR 39.4
CC
30
Cov
30%
__ANON__
182.0
new
42.0
__ANON__
42.0
CRAP 51.5
↑ Summary EtcDv2/Node/Directory.pm »
1 6 package Net::EtcDv2::Node {
2 6 ▶ use v5.30;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶ use strictures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶ use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶ use feature ":5.30";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶ use feature 'lexical_subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶ use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶ use feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶ no warnings "experimental::signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶ no warnings "experimental::smartmatch";
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶ use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶ use Errno qw(:POSIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶ use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶ use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶ use Throw qw(throw classify);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶ use Try::Tiny qw(try catch);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 6 my $debug = false;
25 6 my $host = undef;
26 6 my $port = undef;
27 6 my $user = undef;
28 6 my $password = undef;
29  
30 =head1 NAME
31  
32 Net::EtcDv2::Node - A object oriented Perl module to stat entries in an EtcD v2 API key/value store
33  
34 =head1 VERSION
35  
36 Version 0.0.3
37  
38 =head1 SYNOPSIS
39  
40 use feature say;
41 use Data::Dumper;
42 use Net::EtcDv2::EntryStat;
43  
44 # create an un-authenticated object for an etcd running on localhost on
45 # port 2379
46 my $foo = Net::EtcDv2::EntryStat->new(
47 'host' => "http://localhost",
48 'port' => 2379
49 );
50
51 my $stat_struct = $foo->stat('/myDir');
52 say Dumper $stat_struct;
53  
54 # outputs the following if '/myDir' exists and is a directory
55 # $VAR1 = {
56 # 'type' => 'dir',
57 # 'uri' => 'http://localhost:2379/myDir',
58 # 'ace' => '*:POST, GET, OPTIONS, PUT, DELETE',
59 # 'entryId' => 'cdf818194f3b8d32:23'
60 # };
61 #
62 # The ACE is the access allowed methods and and origin for that path, and
63 # the 'entryId' is made up from the cluster ID and the etcd item index ID
64  
65 =head1 DESCRIPTION
66  
67 The Net::EtcDv2::EntryStat module is an internal module to the Net::EtcDv2
68 distribution. It allows code to stat key/value entries in an etcd cluster.
69  
70 =head1 METHODS
71  
72 =head2 new
73  
74 The constructor for the class. For now, we only support HTTP basic
75 authentication.
76  
77 If the DEBUG environment variable is set, the class will emit debugging
78 output on STDERR.
79  
80 B<Parameters:>
81  
82 - class, SCALAR: The class name
83 - args, HASH: A hash of named parameters:
84 - host: the hostname of the etcd endpoint
85 - port: the port number of the etcd endpoint
86 - user: the username authorized for the etcd environment
87 - password: the password for the user authorized for the etcd
88 environment
89  
90 =cut
91 0 ▶ sub new ($class, %args) {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
92 0 ▶ if (exists $args{'debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"debug"}) 0 0 0
93 0 ▶ if (defined $args{'debug'} eq true) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"debug"} eq &true()) 0 0 0
94 0 say "DEBUG: Enable debug output";
95 0 $debug = true;
96 }
97 }
98  
99 0 my $sub = (caller(0))[3];
100 0 ▶ if ($debug eq true) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug eq &true()) 0 0 0
101 0 say "DEBUG: Sub: $sub";
102 0 say "DEBUG: Constructing object";
103 }
104  
105 0 $host = $args{'host'};
106 0 $port = $args{'port'};
107  
108 0 ▶ if (exists $args{'user'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"user"}) 0 0 0
109 0 $user = $args{'user'};
110 }
111 0 ▶ if (exists $args{'password'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"password"}) 0 0 0
112 0 $password = $args{'password'};
113 }
114  
115 0 my $self = {};
116 0 bless $self, $class;
117 }
118  
119 =head2 stat
120  
121 This method takes a path and gathers information about the etcd object. If the
122 item doesn't exist, it throws an exception (error code 6).
123  
124 B<Parameters:>
125  
126 - self, SCALAR REF: the object reference
127 - path, SCALAR: the path segment of the URI to get info for
128  
129 B<Return type:>
130  
131 - stat_struct: HASH: the stat information for the path
132  
133 B<Exceptions:>
134  
135 If the object is not found (HTTP 404), the method will emit error ENXIO
136  
137 =cut
138 0 ▶ our sub stat ($self, $path) {
Subroutine stat: not called CC=3 SCAR=24.8
Pod documented
139 0 my $sub = (caller(0))[3];
140 0 ▶ say "DEBUG: debug == $debug" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
141 0 ▶ say "DEBUG: Sub: $sub" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
142  
143 0 my $stat_struct = undef;
144 0 my $response = undef;
145 0 ▶ try {
Subroutine __ANON__: not called CC=6 SCAR=37.4
146 0 my $ua = LWP::UserAgent->new();
147 0 ▶ unless (defined $user && defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user and defined $password) { } 0 0 0
Condition0%0/3
defined $user and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $user and defined $password
defined $user defined $password
Truth table0%0/3
A and B
A= defined $userB= defined $password
ABresult
0X0
100
111
148 0 $response = $ua->get("$host:$port/v2/keys${path}");
149 0 say "DEBUG: " . Dumper $response;
150 0 my $rc = $response->code();
151 0 ▶ if ($rc ne HTTP_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc ne 200) { } 0 0 0
152 0 throw(
153 "HTTP I/O error", {
154 'type' => $rc,
155 'uri' => $response->base,
156 'info' => "Attempt to stat entry from etcd cluster"
157 }
158 );
159 } else {
160 0 my $content = decode_json($response->content);
161 0 my $uri = $response->base->as_string;
162 0 my $cluster_id = $response->header('x-etcd-cluster-id');
163 0 my $ace_allow_origin = $response->header('access-control-allow-origin');
164 0 my $ace_allow_methods = $response->header('access-control-allow-methods');
165 0 my $heirarchy_index = $response->header('x-etcd-index');
166 0 my $type = 'key';
167 0 ▶ if (exists $content->{'node'}->{'dir'} && $content->{'node'}->{'dir'} eq true) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $content->{"node"}{"dir"} and $content->{"node"}{"dir"} eq &true()) 0 0 0
Condition0%0/3
exists $content->{"node"}{"dir"} and $content->{"node"}{"dir"} eq &true()
!ll&&!rl&&r
000
MC/DC0%0/2
exists $content->{"node"}{"dir"} and $content->{"node"}{"dir"} eq &true()
exists $content->{"node"}{"dir"} $content->{"node"}{"dir"} eq &true()
Truth table0%0/3
A and B
A= exists $content->{"node"}{"dir"}B= $content->{"node"}{"dir"} eq &true()
ABresult
0X0
100
111
168 0 $type = 'dir';
169 }
170 $stat_struct = {
171 0 'uri' => $uri,
172 'type' => $type,
173 'entryId' => "$cluster_id:$heirarchy_index",
174 'ace' => "$ace_allow_origin:$ace_allow_methods"
175 };
176 }
177 } else {
178 0 $response = $ua->credentials("$host:$port", 'Basic', $user, $password);
179 0 $response = $ua->get("$host:$port");
180 }
181 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
182 0 ▶ say "DEBUG: catch args: $_" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
183 classify $_, {
184 0 ▶ 404 => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
185 # rethrow
186 throw("$_->{'error'}",
187 {
188 'type' => $_->{'type'},
189 0 'info' => $_->{'info'}
190 }
191 );
192 },
193 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 # Dunno what this is, so be fatal
195 0 exit EPERM;
196 }
197 0 };
198 0 };
199  
200 0 say "DEBUG: Stat struct: " . Dumper($stat_struct);
201 0 return $stat_struct;
202 }
203  
204 =head2 ls
205  
206 This method, like stat, takes a path and gathers information about the etcd
207 object. If the item doesn't exist, it throws an exception (error code 6).
208  
209 Unlike C<stat>, it only returns in the JSON response the node name, type, and
210 whether it has any children. In addition, while C<stat> only returns specific
211 information about the named node, ls can do recursive listing, and if the item
212 requested was a directory, it's immediate children; any child items are nested
213 in the response.
214  
215 B<Parameters:>
216  
217 - self, SCALAR REF: the object reference
218 - path, SCALAR: the path segment of the URI to get info for
219 - recursive, SCALAR boolean [OPTIONAL]: Whether to recursively descend into
220 sub directories
221  
222 B<Return type:>
223  
224 - status, SCALAR boolean: Whether the listing was successful or not
225 - stat_struct: HASH: the stat information for the path
226  
227 B<Exceptions:>
228  
229 If the object is not found (HTTP 404), the method will emit error ENXIO
230  
231 =cut
232 0 ▶ our sub ls ($self, $path, $recursive = false) {
Subroutine ls: not called CC=4 SCAR=30.0
Pod documented
233 0 my $sub = (caller(0))[3];
234 0 ▶ say "DEBUG: debug == $debug" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
235 0 ▶ say "DEBUG: Sub: $sub" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
236  
237 0 my $stat_struct = undef;
238 0 my $response = undef;
239  
240 0 ▶ try {
Subroutine __ANON__: not called CC=13 SCAR=52.0
241 0 my $ua = LWP::UserAgent->new();
242 0 ▶ unless (defined $user && defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user and defined $password) { } 0 0 0
Condition0%0/3
defined $user and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $user and defined $password
defined $user defined $password
Truth table0%0/3
A and B
A= defined $userB= defined $password
ABresult
0X0
100
111
243 0 ▶ if ($recursive eq true) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive eq &true()) { } 0 0 0
244 0 $response = $ua->get("$host:$port/v2/keys${path}?recursive=true");
245 } else {
246 0 $response = $ua->get("$host:$port/v2/keys${path}");
247 }
248 0 ▶ say "DEBUG: " . Dumper($response) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
249 0 my $rc = $response->code();
250 0 ▶ if ($rc ne HTTP_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc ne 200) { } 0 0 0
251 0 throw(
252 "HTTP I/O error", {
253 'type' => $rc,
254 'uri' => $response->base,
255 'info' => "Attempt to list entries from etcd cluster"
256 }
257 );
258 } else {
259 0 my $content = decode_json($response->content);
260 0 my $uri = $response->base->as_string;
261 0 my $type = 'key';
262 0 ▶ if (exists $content->{'node'}->{'dir'} && $content->{'node'}->{'dir'} eq true) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $content->{"node"}{"dir"} and $content->{"node"}{"dir"} eq &true()) 0 0 0
Condition0%0/3
exists $content->{"node"}{"dir"} and $content->{"node"}{"dir"} eq &true()
!ll&&!rl&&r
000
MC/DC0%0/2
exists $content->{"node"}{"dir"} and $content->{"node"}{"dir"} eq &true()
exists $content->{"node"}{"dir"} $content->{"node"}{"dir"} eq &true()
Truth table0%0/3
A and B
A= exists $content->{"node"}{"dir"}B= $content->{"node"}{"dir"} eq &true()
ABresult
0X0
100
111
263 0 $type = 'dir';
264 }
265 0 my @nodes = ();
266 0 ▶ if (defined $content->{'node'}->{'nodes'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content->{"node"}{"nodes"}) 0 0 0
267 0 ▶ say "DEBUG: nodes? " . Dumper($content->{'node'}->{'nodes'}) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
268 0 foreach my $node (@{$content->{'node'}->{'nodes'}}) {
269 0 ▶ say "DEBUG: node: ". Dumper($node) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
270 0 my $key = $node->{'key'};
271 0 my $l_type = 'key';
272 0 ▶ if ($content->{'node'}->{'dir'} eq true) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{"node"}{"dir"} eq &true()) 0 0 0
273 0 $l_type = 'dir';
274 }
275 0 push(@nodes, {'key' => $key, 'type' => $type});
276 }
277 }
278 $stat_struct = {
279 0 'uri' => $uri,
280 'type' => $type,
281 'children' => \@nodes
282 };
283 }
284 } else {
285 0 $response = $ua->credentials("$host:$port", 'Basic', $user, $password);
286 0 $response = $ua->get("$host:$port");
287 }
288 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
289 0 ▶ say "DEBUG: catch args: $ARG" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
290 classify $ARG, {
291 0 ▶ 404 => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
292 # rethrow
293 throw("$ARG->{'error'}",
294 {
295 'type' => $ARG->{'type'},
296 0 'info' => $ARG->{'info'}
297 }
298 );
299 },
300 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
301 # Dunno what this is, so be fatal
302 0 exit EPERM;
303 }
304 0 };
305 0 };
306  
307 0 say "DEBUG: Stat struct: " . Dumper($stat_struct);
308 0 return $stat_struct;
309 }
310  
311 6 true;
312 }
↑ Summary EtcDv2/Node/Directory.pm »