blib/lib/Oracle/ZFSSA/Client.pm

filter: Statementstmt 26.3% 19 / 72 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 60.0% 6 / 10 Podpod 50.0% 1 / 2 totaltotal 21.3% 26 / 122 CC 16 SCAR 47.8
CC
16
Cov
18%
new
210.0
call
12.0
__ANON__
2.0
CRAP 119.4
↑ Summary
1 package Oracle::ZFSSA::Client;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.01';
12  
13 # HTTP Return Codes
14 1 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶sub new {
Subroutine new: not called CC=14 SCAR=53.5
Pod undocumented
17  
18 0 my $class = shift;
19  
20 0 my %param = @_;
21  
22 0 my $self->{user} = delete $param{user};
23 0 $self->{password} = delete $param{password};
24 0 $self->{host} = delete $param{host};
25 0 ▶ $self->{port} = delete $param{port} || 215;
Condition0%0/2
delete $param{'port'} || 215
l!l
00
MC/DC0%0/1
delete $param{'port'} || 215
delete $param{'port'}
Truth table0%0/2
A
A= delete $param{'port'}
Aresult
11
00
26 0 ▶ $self->{debug} = delete $param{debug} || 0;
Condition0%0/2
delete $param{'debug'} || 0
l!l
00
MC/DC0%0/1
delete $param{'debug'} || 0
delete $param{'debug'}
Truth table0%0/2
A
A= delete $param{'debug'}
Aresult
11
00
27  
28 0 ▶ if (!defined($param{ssl}) || $param{ssl} != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $param{'ssl'} or $param{'ssl'} != 0) { } 0 0 0
Condition0%0/3
not defined $param{"ssl"} or $param{"ssl"} != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $param{"ssl"} or $param{"ssl"} != 0
not defined $param{"ssl"} $param{"ssl"} != 0
Truth table0%0/3
not(A) or B
A= not defined $param{"ssl"}B= $param{"ssl"} != 0
ABresult
1X1
011
000
29 0 $self->{ssl} = 1;
30 } else {
31 0 $self->{ssl} = 0;
32 }
33  
34 0 ▶ if (!defined($param{verify_hostname}) || $param{verify_hostname} != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $param{'verify_hostname'} or $param{'verify_hostname'} != 0) { } 0 0 0
Condition0%0/3
not defined $param{"verify_hostname"} or $param{"verify_hostname"} != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $param{"verify_hostname"} or $param{"verify_hostname"} != 0
not defined $param{"verify_hostname"} $param{"verify_hostname"} != 0
Truth table0%0/3
not(A) or B
A= not defined $param{"verify_hostname"}B= $param{"verify_hostname"} != 0
ABresult
1X1
011
000
35 0 $self->{verify_hostname} = 1;
36 } else {
37 0 $self->{verify_hostname} = 0;
38 }
39  
40 0 ▶ die "A user must be defined" if (!defined($self->{user}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"user"} 0 0 0
41 0 ▶ die "A password must be defined" if (!defined($self->{password}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"password"} 0 0 0
42 0 ▶ die "A host must be defined" if (!defined($self->{host}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"host"} 0 0 0
43  
44 0 my $url = $self->{host} . ":" . $self->{port};
45  
46 0 ▶ if ($self->{ssl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ssl'}) { } 0 0 0
47 0 $url = "https://" . $url;
48 } else {
49 0 $url = "http://" . $url;
50 }
51  
52 0 $self->{url} = $url;
53  
54 # Generate the first request and get a valid Session
55 #
56 0 $url = $url . '/api/access/v1';
57  
58 my $header = ['X-Auth-User' => $self->{user},
59 'X-Auth-Key' => $self->{password},
60 0 'Content-Type' => 'application/json; charset=utf-8'];
61 0 my $r = HTTP::Request->new('POST', $url, $header);
62 0 my $ua = LWP::UserAgent->new();
63  
64 0 ▶ if ($self->{debug} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} == 1) 0 0 0
65 0 ▶ $ua->add_handler("request_send", sub { shift->dump; return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 ▶ $ua->add_handler("response_done", sub { shift->dump; return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 }
68  
69 0 ▶ if ($self->{verify_hostname} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verify_hostname"} == 0) 0 0 0
70 # Self signed Cert
71 0 $ua->ssl_opts( verify_hostname => 0,
72 SSL_verify_mode => 'SSL_VERIFY_NONE' );
73 }
74  
75 # We'll reuse this User Agent
76 0 $self->{ua} = $ua;
77  
78 0 my $res = $ua->request($r);
79  
80 0 my $session = "";
81 0 ▶ if ($res->code == HTTP_CREATED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 201) { } 0 0 0
82 # All proceeding request should had this header set
83 0 $session = $res->header('X-Auth-Session');
84 } else {
85 # You did not get a valid session
86 0 die $res->status_line . ": Unable to get valid X-Auth-Session";
87 }
88 #
89 # End Session Building
90  
91 0 $self->{session} = $session;
92  
93 0 bless $self, $class;
94 0 return $self;
95 }
96  
97 0 ▶sub call {
Subroutine call: not called CC=3 SCAR=24.8
Pod documented
98  
99 0 my ($self,$method,$uri,$json) = @_;
100  
101 0 ▶ $json = JSON->new->utf8->encode($json) if (defined($json));
Branch: 1 branch
BranchTrueFalseTotal
if defined $json 0 0 0
102  
103 0 my $url = $self->{url} . $uri;
104 my $header = ['X-Auth-Session' => $self->{session},
105 0 'Content-Type' => 'application/json; charset=utf-8'];
106 0 my $r = HTTP::Request->new($method, $url, $header, $json);
107  
108 0 my $ua = $self->{ua};
109  
110 0 my $res = $ua->request($r);
111  
112 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
113 0 return JSON->new->utf8->decode($res->decoded_content);
114 }
115 0 return;
116 }
117  
118 1 1;
119  
120 =pod
121  
122 =head1 NAME
123  
124 Oracle::ZFSSA::Client - Oracle ZFS Storage RESTful API Connector
125  
126 =head1 SYNOPSIS
127  
128 use Oracle::ZFSSA::Client;
129  
130 $zfssa = new Oracle::ZFSSA::Client(
131 user => $user,
132 password => $password,
133 host => "your.appliance.org"
134 );
135  
136 $json_param = {
137 your => 'params',
138 but => 'not required',
139 };
140  
141 $json_result = $zfssa->call('POST','/api/storage/v1/method',$json_param)
142  
143 # See ex/example.pl for more examples:
144 # https://github.com/whindsx/Oracle-ZFSSA-Client/blob/master/ex/example.pl
145  
146 =head1 DESCRIPTION
147  
148 This Perl module provides a simplified means of connecting to and
149 executing commands against an Oracle ZFSSA RESTful Application
150 Programming Interface. Responses are Perl C<JSON> data structures.
151  
152 =head1 CONSTRUCTOR
153  
154 $zfssa = Oracle::ZFSSA::Client->new( %options )
155  
156 This method creates a new C<Oracle::ZFSSA::Client> and returns it.
157  
158 Key Default
159 ----------- -----------
160 user undef (Required)
161 password undef (Required)
162 host undef (Required)
163 port 215
164 ssl 1
165 verify_hostname 1
166 debug 0
167  
168 ssl - Connect to the API over HTTPS.
169  
170 verify_hostname - Disable SSL certificate verification.
171  
172 debug - Turns on raw HTTP request/response output from LWP::UserAgent.
173  
174 =head1 METHODS
175  
176 There is only one c<Oracle::ZFSSA::Client> method:
177  
178 =head2 call
179  
180 $zfssa->call('GET','/api/storage/v1/pools');
181  
182 The first parameter is the HTTP method, this is required.
183 The second parameter is the ZFSSA API method, this is also required.
184 The third and optional parameter is a hash of API parameters that will
185 become C<JSON> encoded.
186
187 For more information see:
188 https://docs.oracle.com/cd/E51475_01/html/E52433/index.html
189  
190 =head1 AUTHOR
191  
192 Wesley Hinds wesley.hinds@gmail.com
193  
194 =head1 AVAILABILITY
195  
196 The latest branch is avaiable from Github.
197  
198 https://github.com/whindsx/Oracle-ZFSSA-Client
199  
200 =head1 LICENSE AND COPYRIGHT
201  
202 Copyright 2018 Wesley Hinds.
203  
204 This program is free software; you can redistribute it and/or modify it
205 under the terms of the the Artistic License (2.0). You may obtain a
206 copy of the full license at:
207  
208 L<http://www.perlfoundation.org/artistic_license_2_0>
209  
210 Any use, modification, and distribution of the Standard or Modified
211 Versions is governed by this Artistic License. By using, modifying or
212 distributing the Package, you accept this license. Do not use, modify,
213 or distribute the Package, if you do not accept this license.
214  
215 If your Modified Version has been derived from a Modified Version made
216 by someone other than you, you are nevertheless required to ensure that
217 your Modified Version complies with the requirements of this license.
218  
219 This license does not grant you the right to use any trademark, service
220 mark, tradename, or logo of the Copyright Holder.
221  
222 This license includes the non-exclusive, worldwide, free-of-charge
223 patent license to make, have made, use, offer to sell, sell, import and
224 otherwise transfer the Package with respect to any patent claims
225 licensable by the Copyright Holder that are necessarily infringed by the
226 Package. If you institute patent litigation (including a cross-claim or
227 counterclaim) against any party alleging that the Package constitutes
228 direct or contributory patent infringement, then this Artistic License
229 to you shall terminate on the date that such litigation is filed.
230  
231 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
232 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
233 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
234 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
235 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
236 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
237 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
238 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
239  
240  
241 =cut
↑ Summary