EtcDv2/Node/Directory.pm

filter: Statementstmt 38.9% 67 / 172 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 60.6% 20 / 33 Podpod 100% 3 / 3 totaltotal 28.5% 90 / 315 CC 45 SCAR 34.4
CC
45
Cov
25%
__ANON__
110.0
new
42.0
__ANON__
42.0
CRAP 31.3
« EtcDv2/Node.pm ↑ Summary EtcDv2.pm »
1 6 package Net::EtcDv2::Node::Directory {
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 ▶ use Net::EtcDv2::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # class references
27 6 my $node = undef;
28  
29 # class member data
30 6 my $debug = false;
31 6 my $host = undef;
32 6 my $port = undef;
33 6 my $user = undef;
34 6 my $password = undef;
35  
36 0 ▶ sub new ($class, %args) {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
37 0 ▶ if (exists $args{'debug'} && $args{'debug'} eq true) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"debug"} and $args{"debug"} eq &true()) 0 0 0
Condition0%0/3
exists $args{"debug"} and $args{"debug"} eq &true()
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"debug"} and $args{"debug"} eq &true()
exists $args{"debug"} $args{"debug"} eq &true()
Truth table0%0/3
A and B
A= exists $args{"debug"}B= $args{"debug"} eq &true()
ABresult
0X0
100
111
38 0 $debug = true;
39 }
40  
41 0 my $sub = (caller(0))[3];
42 0 ▶ if ($debug eq true) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug eq &true()) 0 0 0
43 0 say "DEBUG: Sub: $sub";
44 0 say "DEBUG: Constructing object";
45 }
46  
47 0 $host = $args{'host'};
48 0 $port = $args{'port'};
49  
50 0 ▶ if (exists $args{'user'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"user"}) 0 0 0
51 0 $user = $args{'user'};
52 }
53 0 ▶ if (exists $args{'password'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"password"}) 0 0 0
54 0 $password = $args{'password'};
55 }
56  
57 0 $node = Net::EtcDv2::Node->new(
58 'debug' => $debug,
59 'host' => $host,
60 'password' => $password,
61 'port' => $port,
62 'user' => $user
63 );
64  
65 0 my $self = {};
66 0 bless $self, $class;
67 }
68  
69 0 ▶ our sub mkdir ($self, $path) {
Subroutine mkdir: not called CC=3 SCAR=24.8
Pod documented
70 0 my $sub = (caller(0))[3];
71 0 ▶ say STDERR "DEBUG: Sub: $sub" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
72  
73 0 my $content = undef;
74 0 my $request_struct = undef;
75 0 my $status = undef;
76 # we cannot create a directory if it already exists
77 0 ▶ try {
Subroutine __ANON__: not called CC=6 SCAR=37.4
78 0 ▶ $content = $node->stat($path) or throw "HTTP error", {
Branch: 1 branch
BranchTrueFalseTotal
$content = $node->stat($path) or throw("HTTP error", {"type", int $Net::EtcDv2::Node::Directory::ERRNO, "error_string", $Net::EtcDv2::Node::Directory::ERRNO, "info", "Attempted to create directory in cluster"}) 0 0 0
79 'type' => int($ERRNO),
80 'error_string' => $ERRNO,
81 'info' => 'Attempted to create directory in cluster'
82 };
83 0 ▶ if (defined $content && $content->{'type'} eq 'dir') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $content and $content->{'type'} eq 'dir') { } 0 0 0
elsif (defined $content and $content->{'type'} ne 'dir') { } 0 0 0
Condition0%0/3
defined $content and $content->{"type"} eq "dir"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and $content->{"type"} eq "dir"
defined $content $content->{"type"} eq "dir"
Truth table0%0/3
A and B
A= defined $contentB= $content->{"type"} eq "dir"
ABresult
0X0
100
111
84 0 throw 'Directory Exists', {
85 'type' => 200,
86 'error_string' => 'item exists',
87 'info' => 'Attempted to create directory in cluster'
88 };
89 ▶ } elsif (defined $content && $content->{'type'} ne 'dir') {
Condition0%0/3
defined $content and $content->{"type"} ne "dir"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and $content->{"type"} ne "dir"
defined $content $content->{"type"} ne "dir"
Truth table0%0/3
A and B
A= defined $contentB= $content->{"type"} ne "dir"
ABresult
0X0
100
111
90 0 throw 'Not a Directory', {
91 'type' => 400,
92 'error_string' => 'item exists, not a directory',
93 'info' => 'Attempted to create directory in cluster'
94 }
95 } else {
96 0 throw 'No Such Directory', {
97 'type' => 404,
98 'error_string' => 'item not found',
99 'info' => 'Attempted to create directory in cluster'
100 }
101 }
102 0 ▶ } catch {
Subroutine __ANON__: not called CC=3 SCAR=24.8
103 0 ▶ say "DEBUG: args" . Dumper($ARG) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
104 0 my $exception_type = $ARG->{'error'};
105 0 my $error_code = $ARG->{'type'};
106 0 my $error_string = $ARG->{'error_string'};
107 0 my $info = $ARG->{'info'};
108 0 ▶ say "DEBUG: code; $error_code" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
109 classify $ARG, {
110 0 ▶ 200 => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
111 0 ▶ say "DEBUG: Path '$path' already exists. Cannot create directory" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
112 0 $status = false;
113 0 throw $exception_type, {
114 'type' => $error_code,
115 'error_string' => $error_string,
116 'info' => $info
117 }
118 },
119 0 ▶ 404 => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
120 0 ▶ say "DEBUG: Path '$path' does not exist. Creating directory" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
121 # now we can actually create the directory
122 0 my $ua = LWP::UserAgent->new();
123 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
124 0 my %args = ('dir' => 'true');
125 0 my $request = $ua->put("$host:$port/v2/keys$path", \%args);
126 0 ▶ say "DEBUG: request: ". Dumper($request) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
127 0 $status = true;
128 0 $request_struct = $request->content();
129 } else {
130 0 my $response = $ua->credentials("$host:$port", 'Basic', $user, $password);
131 0 my %args = ('dir' => 'true');
132 0 my $request = $ua->put("$host:$port/v2/keys$path". \%args);
133 0 ▶ say "DEBUG: request: ". Dumper($request) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
134 0 $status = true;
135 0 $request_struct = $request->content();
136 }
137 },
138 0 ▶ default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
139 0 ▶ say STDERR "DEBUG: Got a default error:" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
140 0 ▶ say STDERR "DEBUG: ". Dumper($ARG) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
141 throw $ARG->{'error'}, {
142 'type' => $ARG->{'type'},
143 'error_string' => $ARG->{'error_string'},
144 0 'info' => $ARG->{'info'}
145 };
146 }
147 0 };
148 0 };
149 0 ▶ if ($status = true) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status = &true()) { } 0 0 0
150 0 return ($JSON::true, $request_struct);
151 } else {
152 0 return ($JSON::false, '{"error":"Entry already exists"}');
153 }
154 }
155  
156 0 ▶ our sub rmdir ($self, $path, $recursive = false) {
Subroutine rmdir: not called CC=4 SCAR=30.0
Pod documented
157 0 my $sub = (caller(0))[3];
158 0 ▶ say STDERR "DEBUG: Sub: $sub" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
159  
160 0 my $content = undef;
161 0 my $request_struct = undef;
162 0 my $status = undef;
163 # we cannot create a directory if it already exists
164 0 ▶ try {
Subroutine __ANON__: not called CC=6 SCAR=37.4
165 0 ▶ $content = $node->stat($path) or throw "HTTP error", {
Branch: 1 branch
BranchTrueFalseTotal
$content = $node->stat($path) or throw("HTTP error", {"type", int $Net::EtcDv2::Node::Directory::ERRNO, "error_string", $Net::EtcDv2::Node::Directory::ERRNO, "info", "Attempted to create directory in cluster"}) 0 0 0
166 'type' => int($ERRNO),
167 'error_string' => $ERRNO,
168 'info' => 'Attempted to create directory in cluster'
169 };
170 0 ▶ if (defined $content && $content->{'type'} eq 'dir') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $content and $content->{'type'} eq 'dir') { } 0 0 0
elsif (defined $content and $content->{'type'} ne 'dir') { } 0 0 0
Condition0%0/3
defined $content and $content->{"type"} eq "dir"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and $content->{"type"} eq "dir"
defined $content $content->{"type"} eq "dir"
Truth table0%0/3
A and B
A= defined $contentB= $content->{"type"} eq "dir"
ABresult
0X0
100
111
171 0 throw 'Directory Exists', {
172 'type' => 200,
173 'error_string' => 'item exists',
174 'info' => 'Attempted to remove directory in cluster'
175 };
176 ▶ } elsif (defined $content && $content->{'type'} ne 'dir') {
Condition0%0/3
defined $content and $content->{"type"} ne "dir"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and $content->{"type"} ne "dir"
defined $content $content->{"type"} ne "dir"
Truth table0%0/3
A and B
A= defined $contentB= $content->{"type"} ne "dir"
ABresult
0X0
100
111
177 0 throw 'Not a Directory', {
178 'type' => 400,
179 'error_string' => 'item exists, not a directory',
180 'info' => 'Attempted to remove directory in cluster'
181 }
182 } else {
183 0 throw 'No Such Directory', {
184 'type' => 404,
185 'error_string' => 'item not found',
186 'info' => 'Attempted to remove directory in cluster'
187 }
188 }
189 0 ▶ } catch {
Subroutine __ANON__: not called CC=3 SCAR=24.8
190 0 ▶ say "DEBUG: args" . Dumper($ARG) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
191 0 my $exception_type = $ARG->{'error'};
192 0 my $error_code = $ARG->{'type'};
193 0 my $error_string = $ARG->{'error_string'};
194 0 my $info = $ARG->{'info'};
195 0 ▶ say "DEBUG: code; $error_code" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
196 classify $ARG, {
197 0 ▶ 200 => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
198 0 ▶ say "DEBUG: Path '$path' exists. Attempting to delete directory" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
199 0 $status = true;
200 0 my $ua = LWP::UserAgent->new();
201 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
202 0 my $request = undef;
203 0 ▶ if ($recursive eq true) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive eq &true()) { } 0 0 0
204 0 $request = $ua->delete("$host:$port/v2/keys$path?recursive=true");
205 0 ▶ say "DEBUG: request: ". Dumper($request) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
206 } else {
207 0 $request = $ua->delete("$host:$port/v2/keys$path?dir=true");
208 0 ▶ say "DEBUG: request: ". Dumper($request) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
209 }
210 0 $status = true;
211 0 $request_struct = $request->content();
212 } else {
213 0 my $response = $ua->credentials("$host:$port", 'Basic', $user, $password);
214 0 my $request = undef;
215 0 ▶ if ($recursive eq true) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive eq &true()) { } 0 0 0
216 0 $request = $ua->delete("$host:$port/v2/keys$path?recursive=true");
217 0 ▶ say "DEBUG: request: ". Dumper($request) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
218 } else {
219 0 $request = $ua->delete("$host:$port/v2/keys$path?dir=true");
220 0 ▶ say "DEBUG: request: ". Dumper($request) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
221 }
222 0 $status = true;
223 0 $request_struct = $request->content();
224 }
225 },
226 0 ▶ 404 => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
227 0 ▶ say "DEBUG: Path '$path' does not exist. Cannot remove non-existing directory" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
228 0 $status = false;
229 0 throw $exception_type, {
230 'type' => $error_code,
231 'error_string' => $error_string,
232 'info' => $info
233 }
234 },
235 0 ▶ default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
236 0 ▶ say STDERR "DEBUG: Got a default error:" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
237 0 ▶ say STDERR "DEBUG: ". Dumper($ARG) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
238 throw $ARG->{'error'}, {
239 'type' => $ARG->{'type'},
240 'error_string' => $ARG->{'error_string'},
241 0 'info' => $ARG->{'info'}
242 };
243 }
244 0 };
245 0 };
246 0 ▶ if ($status = true) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status = &true()) { } 0 0 0
247 0 return ($JSON::true, $request_struct);
248 } else {
249 0 return ($JSON::false, '{"error":"Entry does not exist"}');
250 }
251 }
252  
253 6 true; # End of Net::EtcDv2
254 }
255  
256 =head1 NAME
257  
258 Net::EtcDv2::DirectoryActions - A object oriented Perl module to create or remove directories with the EtcD version 2 API
259  
260 =head1 VERSION
261  
262 Version 0.0.3
263  
264 =head1 SYNOPSIS
265  
266 use feature say;
267 use Data::Dumper;
268 use Net::EtcDv2::DirectoryActions;
269  
270 # create an un-authenticated object for an etcd running on localhost on
271 # port 2379
272 my $foo = Net::EtcDv2->new(
273 'host' => "http://localhost",
274 'port' => 2379
275 );
276
277 my $stat_struct = $foo->stat('/myDir');
278 say Dumper $stat_struct;
279  
280 # outputs the following if '/myDir' exists and is a directory
281 # $VAR1 = {
282 # 'type' => 'dir',
283 # 'uri' => 'http://localhost:2379/myDir',
284 # 'ace' => '*:POST, GET, OPTIONS, PUT, DELETE',
285 # 'entryId' => 'cdf818194f3b8d32:23'
286 # };
287 #
288 # The ACE is the access allowed methods and and origin for that path, and
289 # the 'entryId' is made up from the cluster ID and the etcd item index ID
290  
291 =head1 DESCRIPTION
292  
293 The Net::EtcDv2::DirectoryActions is an internal module to the Net::EtcDv2
294 distribution. This module allows code to create, list, and delete directories
295 in an etcd cluster.
296  
297 =head1 METHODS
298  
299 =head2 new
300  
301 The constructor for the class. For now, we only support HTTP basic
302 authentication.
303  
304 If the DEBUG environment variable is set, the class will emit debugging
305 output on STDERR.
306  
307 B<Parameters:>
308  
309 - class, SCALAR: The class name
310 - args, HASH: A hash of named parameters:
311 - host: the hostname of the etcd endpoint
312 - port: the port number of the etcd endpoint
313 - user: the username authorized for the etcd environment
314 - password: the password for the user authorized for the etcd
315 environment
316  
317 =head2 mkdir
318  
319 This method creates a directory if the named object does not already exist
320 at that level of the heirarchy. If it already exists, no matter if it is a
321 key or directory, it emits an exception.
322  
323 B<Parameters:>
324  
325 - self, SCALAR REF: the object reference
326 - path, SCALAR: the full path to create
327  
328 B<Return type:>
329  
330 - path, SCALAR: the full path that was created
331  
332 B<Exceptions:>
333  
334 If the named entry, irregardless of the type, exists, the method will emit
335 error EEXIST
336  
337 =head2 rmdir
338  
339 This method will delete a directory if the named object exists, and is empty.
340 If any of the following are true, the method emits and appropriate exception:
341  
342 - If the directory does not exist
343 - If the directory is not empty unless the recursive flag is passed
344 - If the object is not a directory
345  
346 B<Parameters:>
347  
348 - self, SCALAR REF: the object reference
349 - path, SCALAR: the full path to delete
350 - recursive, boolean: whether to recursively delete the directory if it
351 contains any content
352  
353 B<Return type:>
354  
355 - path: SCALAR: the path that was created
356  
357 B<Exceptions:>
358  
359 The following exceptions are emitted during error events:
360  
361 - If the directory does not exist, a 404 exception is thrown
362 - If the directory is not empty, a 400 exception is thrown
363 - If the object is not a directory, a 400 exception is thrown
364  
365 =head1 AUTHOR
366  
367 Gary L. Greene, Jr., C<< <greeneg at tolharadys.net> >>
368  
369 =head1 BUGS
370  
371 Please report any bugs or feature requests via this module's GitHub Issues tracker, here:
372 L<https://github.com/greeneg/Net-EtcDv2>.
373  
374 =head1 SUPPORT
375  
376 You can find documentation for this module with the perldoc command.
377  
378 perldoc Net::EtcDv2
379  
380 You can also look for information at:
381  
382 =over 4
383  
384 =item * AnnoCPAN: Annotated CPAN documentation
385  
386 L<http://annocpan.org/dist/Net-EtcDv2>
387  
388 =item * CPAN Ratings
389  
390 L<https://cpanratings.perl.org/d/Net-EtcDv2>
391  
392 =item * Search CPAN
393  
394 L<https://metacpan.org/release/Net-EtcDv2>
395  
396 =back
397  
398 =head1 LICENSE AND COPYRIGHT
399  
400 This software is Copyright (c) 2020 by Gary L. Greene, Jr.
401  
402 This is free software, licensed under:
403  
404 The Apache License, Version 2.0, January 2004
405  
406 =cut
« EtcDv2/Node.pm ↑ Summary EtcDv2.pm »