vCloudDirector2/API.pm

filter: Statementstmt 75.5% 275 / 364 Branchbran 37.8% 62 / 164 Conditioncond 47.3% 9 / 19 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 86.4% 64 / 74 Podpod n/a totaltotal 65.0% 411 / 632 CC 93 SCAR 23.5
CC
93
Cov
63%
_request
34.4
PUT
33.7
POST
33.7
CRAP 10.5
« vCloudDirector2/ObjectContent.pm ↑ Summary vCloudDirector2.pm »
1 package VMware::vCloudDirector2::API;
2  
3 # ABSTRACT: Module to do stuff!
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use v5.10; # needed for state variable
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION = '0.100'; # VERSION
10 4 our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY
11  
12 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use MooseX::Types::Path::Tiny qw(Path);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use MooseX::Types::URI qw(Uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use LWP::UserAgent::Determined;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Mozilla::CA;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Ref::Util qw(is_plain_hashref is_plain_arrayref);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Syntax::Keyword::Try 0.04; # Earlier versions throw errors
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use VMware::vCloudDirector2::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use VMware::vCloudDirector2::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use XML::Fast qw(); # just for the versions document
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use Data::Dump qw(pp);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # ------------------------------------------------------------------------
30  
31  
32 4 has hostname => ( is => 'ro', isa => 'Str', required => 1 );
33 4 has username => ( is => 'ro', isa => 'Str', required => 1 );
34 4 has password => ( is => 'ro', isa => 'Str', required => 1 );
35 4 has orgname => ( is => 'ro', isa => 'Str', required => 1, default => 'System' );
36 4 has ssl_verify => ( is => 'ro', isa => 'Bool', default => 1 );
37 4 has debug => ( is => 'rw', isa => 'Int', default => 0, );
38 4 has timeout => ( is => 'rw', isa => 'Int', default => 120 ); # Defaults to 120 seconds
39 4 has _debug_trace_directory =>
40 ( is => 'ro', isa => Path, coerce => 1, predicate => '_has_debug_trace_directory' );
41  
42 4 has default_accept_header => (
43 is => 'ro',
44 isa => 'Str',
45 lazy => 1,
46 builder => '_build_default_accept_header',
47 clearer => '_clear_default_accept_header',
48 );
49  
50 4 has _base_url => (
51 is => 'ro',
52 isa => Uri,
53 lazy => 1,
54 builder => '_build_base_url',
55 writer => '_set_base_url',
56 clearer => '_clear_base_url',
57 );
58  
59 4 has ssl_ca_file => (
60 is => 'ro',
61 isa => Path,
62 coerce => 1,
63 lazy => 1,
64 builder => '_build_ssl_ca_file'
65 );
66  
67 4 ▶method _build_ssl_ca_file () { return path( Mozilla::CA::SSL_ca_file() ); }
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine _build_ssl_ca_file: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
68 4 ▶method _build_base_url () { return URI->new( sprintf( 'https://%s/', $self->hostname ) ); }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_base_url: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
69 4 ▶method _build_default_accept_header () { return ( 'application/*+json;version=' . $self->api_version ); }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_default_accept_header: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
70 4 ▶method _debug (@parameters) { warn join( '', '# ', @parameters, "\n" ) if ( $self->debug ); }
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine _debug: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
71  
72 # ------------------------------------------------------------------------
73  
74 4 ▶method BUILD ($args) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BUILD: called CC=5 SCAR=18.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
if scalar @_ > 1 0 1 1
75  
76 # deal with setting debug if needed
77 1 my $env_debug = $ENV{VCLOUD_API_DEBUG};
78 1 ▶ if ( defined($env_debug) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env_debug) 0 1 1
79 0 ▶ $self->debug($env_debug) if ( looks_like_number($env_debug) );
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($env_debug) 0 0 0
80 }
81 }
82  
83 # ------------------------------------------------------------------------
84 4 has _ua => (
85 is => 'ro',
86 isa => 'LWP::UserAgent',
87 lazy => 1,
88 clearer => '_clear_ua',
89 builder => '_build_ua'
90 );
91  
92 has _ua_module_version => (
93 is => 'ro',
94 isa => 'Str',
95 1 ▶ default => sub { our $VERSION //= '0.00'; sprintf( '%s/%s', __PACKAGE__, $VERSION ) }
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
our $VERSION //= "0.00"
l!l
10
MC/DC0%0/1
our $VERSION //= "0.00"
our $VERSION
Truth table50%1/2
A
A= our $VERSION
Aresult
11
00
96 4 );
97  
98 4 ▶method _build_ua () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine _build_ua: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
99 0 return LWP::UserAgent::Determined->new(
100 agent => $self->_ua_module_version . ' ',
101 cookie_jar => {},
102 ssl_opts => { verify_hostname => $self->ssl_verify, SSL_ca_file => $self->ssl_ca_file },
103 timeout => $self->timeout,
104 env_proxy => 1,
105 );
106 }
107  
108 # ------------------------------------------------------------------------
109 4 has _json => (
110 is => 'ro',
111 isa => 'Cpanel::JSON::XS',
112 lazy => 1,
113 builder => '_build_json',
114 );
115  
116 4 ▶method _build_json () { return Cpanel::JSON::XS->new->utf8; }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_json: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
117  
118 # ------------------------------------------------------------------------
119 4 ▶method _decode_xml_response ($response) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _decode_xml_response: called CC=6 SCAR=19.5
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
if scalar @_ > 1 0 1 1
120 1 ▶ VMware::vCloudDirector2::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $response->content_type =~ /\bxml\b/ 0 1 1
121 { message => "Not a XML response as expected", response => $response } )
122 unless ( $response->content_type() =~ m|\bxml\b| );
123 try {
124 1 my $xml = $response->decoded_content;
125 1 ▶ return unless ( defined($xml) and length($xml) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xml and length $xml 0 1 1
Condition33%1/3
defined $xml and length $xml
!ll&&!rl&&r
001
MC/DC0%0/2
defined $xml and length $xml
defined $xml length $xml
Truth table33%1/3
A and B
A= defined $xmlB= length $xml
ABresult
0X0
100
111
126 1 return XML::Fast::xml2hash($xml);
127 }
128 1 catch {
129 0 VMware::vCloudDirector::Error->throw(
130 { message => "XML decode failed - " . join( ' ', $@ ),
131 response => $response
132 }
133 );
134 }
135 }
136  
137 # ------------------------------------------------------------------------
138 4 ▶method _decode_json_response ($response) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _decode_json_response: called CC=6 SCAR=19.5
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 3 3
if scalar @_ > 1 0 3 3
139 3 ▶ VMware::vCloudDirector2::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $response->content_type =~ /\bjson\b/ 0 3 3
140 { message => "Not a JSON response as expected", response => $response } )
141 unless ( $response->content_type() =~ m|\bjson\b| );
142  
143 try {
144 3 my $json = $response->decoded_content;
145 3 ▶ return unless ( defined($json) and length($json) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $json and length $json 0 3 3
Condition33%1/3
defined $json and length $json
!ll&&!rl&&r
003
MC/DC0%0/2
defined $json and length $json
defined $json length $json
Truth table33%1/3
A and B
A= defined $jsonB= length $json
ABresult
0X0
100
111
146 3 return $self->_json->decode($json);
147 }
148 3 catch {
149 0 VMware::vCloudDirector2::Error->throw(
150 { message => "JSON decode failed - " . join( ' ', $@ ),
151 response => $response
152 }
153 );
154 }
155 }
156  
157 # ------------------------------------------------------------------------
158 4 ▶method _encode_json_content ($hash) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine _encode_json_content: not called CC=3 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
159 0 return $self->_json->encode($hash);
160 }
161  
162 # ------------------------------------------------------------------------
163 4 ▶method _request ($method, $url, $content?, $headers?) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _request: called CC=17 SCAR=35.4
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 4 4
unless scalar @_ > 1 0 4 4
if scalar @_ > 4 0 4 4
164 4 my $uri = URI->new_abs( $url, $self->_base_url );
165 4 ▶ $self->_debug("API: _request [$method] $uri") if ( $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 4 4
166  
167 4 my $request = HTTP::Request->new( $method => $uri );
168  
169 # build headers
170 4 ▶ if ( defined $content && length($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and length $content) { } 0 4 4
Condition33%1/3
defined $content and length $content
!ll&&!rl&&r
400
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table33%1/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
171 0 $request->content($content);
172 0 $request->header( 'Content-Length', length($content) );
173 }
174 else {
175 4 $request->header( 'Content-Length', 0 );
176 }
177  
178 # add any supplied headers
179 4 my $seen_accept;
180 4 ▶ if ( defined($headers) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers) 2 2 4
181 2 foreach my $h_name ( keys %{$headers} ) {
182 2 $request->header( $h_name, $headers->{$h_name} );
183 2 ▶ $seen_accept = 1 if ( lc($h_name) eq 'accept' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $h_name eq "accept" 1 1 2
184 }
185 }
186  
187 # set accept header
188 4 ▶ $request->header( 'Accept', $self->default_accept_header ) unless ($seen_accept);
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_accept 3 1 4
189  
190 # set auth header
191 4 ▶ $request->header( 'x-vcloud-authorization', $self->authorization_token )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_authorization_token 2 2 4
192 if ( $self->has_authorization_token );
193  
194 # do request
195 4 my $response;
196 4 try { $response = $self->_ua->request($request); }
197 4 catch {
198 0 VMware::vCloudDirector2::Error->throw(
199 { message => "$method request bombed",
200 uri => $uri,
201 request => $request,
202 }
203 );
204 }
205  
206 # if _debug_trace_directory is set - we dump info from each request out into
207 # a pair of files, one with the dumped response object, the other with the content
208 4 ▶ if ( $self->_has_debug_trace_directory ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_debug_trace_directory) 0 4 4
209 0 state $xcount = 0;
210 0 ▶ die "No trace directory - " . $self->_debug_trace_directory
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_debug_trace_directory->is_dir 0 0 0
211 unless ( $self->_debug_trace_directory->is_dir );
212 0 $self->_debug_trace_directory->child( sprintf( '%06d.txt', ++$xcount ) )
213 ->spew( pp($response) );
214 0 ▶ my $ext = ( $response->content_type =~ /json/ ) ? 'json' : 'xml';
Branch: 1 branch
BranchTrueFalseTotal
$response->content_type =~ /json/ ? : 0 0 0
215 0 $self->_debug_trace_directory->child( sprintf( '%06d.%s', $xcount, $ext ) )
216 ->spew( $response->decoded_content );
217 }
218  
219 # Throw if this went wrong
220 4 ▶ if ( $response->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 4 4
221 0 my $message = "$method request failed [$uri] - ";
222 try {
223 0 my $decoded_response = $self->_decode_json_response($response);
224 $message .=
225 ( exists( $decoded_response->{Error}{'-message'} ) )
226 0 ▶ ? $decoded_response->{Error}{'-message'}
Branch: 1 branch
BranchTrueFalseTotal
exists $decoded_response->{'Error'}{'-message'} ? : 0 0 0
227 : 'Unknown after decode';
228 }
229 0 catch { $message .= 'Unknown'; }
230 0 VMware::vCloudDirector2::Error->throw(
231 { message => $message,
232 uri => $uri,
233 request => $request,
234 response => $response
235 }
236 );
237 }
238  
239 4 return $response;
240 }
241  
242 # ------------------------------------------------------------------------
243  
244  
245 4 has api_version => (
246 is => 'ro',
247 isa => 'Str',
248 lazy => 1,
249 clearer => '_clear_api_version',
250 builder => '_build_api_version'
251 );
252 4 has _url_login => (
253 is => 'rw',
254 isa => Uri,
255 lazy => 1,
256 clearer => '_clear_url_login',
257 builder => '_build_url_login'
258 );
259 4 has _raw_version => (
260 is => 'rw',
261 isa => 'HashRef',
262 lazy => 1,
263 clearer => '_clear_raw_version',
264 builder => '_build_raw_version'
265 );
266 4 has _raw_version_full => (
267 is => 'rw',
268 isa => 'HashRef',
269 lazy => 1,
270 clearer => '_clear_raw_version_full',
271 builder => '_build_raw_version_full'
272 );
273  
274 4 ▶method _build_api_version () { return $self->_raw_version->{Version}; }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_api_version: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
275 4 ▶method _build_url_login () { return URI->new( $self->_raw_version->{LoginUrl} ); }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_url_login: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
276  
277 4 ▶method _build_raw_version () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_raw_version: called CC=8 SCAR=21.2
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
278 1 my $hash = $self->_raw_version_full;
279 1 my $version = 0;
280 1 my $version_block;
281 1 for my $verblock ( @{ $hash->{SupportedVersions}{VersionInfo} } ) {
282 13 ▶ next if ( ( $verblock->{-deprecated} || '' ) eq 'true' );
Branch: 1 branch
BranchTrueFalseTotal
if ($verblock->{"-deprecated"} || "") eq "true" 9 4 13
Condition50%1/2
$verblock->{'-deprecated'} || ''
l!l
130
MC/DC0%0/1
$verblock->{'-deprecated'} || ''
$verblock->{'-deprecated'}
Truth table50%1/2
A
A= $verblock->{'-deprecated'}
Aresult
11
00
283 4 ▶ if ( $verblock->{Version} > $version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verblock->{"Version"} > $version) 4 0 4
284 4 $version_block = $verblock;
285 4 $version = $verblock->{Version};
286 }
287 }
288  
289 1 ▶ $self->_debug("API: version used: $version") if ( $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
290 1 ▶ die "No valid version block seen" unless ($version_block);
Branch: 1 branch
BranchTrueFalseTotal
unless $version_block 0 1 1
291  
292 1 return $version_block;
293 }
294  
295 4 ▶method _build_raw_version_full () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_raw_version_full: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
296 1 my $response = $self->_request( 'GET', '/api/versions', undef, { Accept => 'text/xml' } );
297 1 return $self->_decode_xml_response($response);
298 }
299  
300 # ------------------------ ------------------------------------------------
301  
302  
303 4 has authorization_token => (
304 is => 'ro',
305 isa => 'Str',
306 writer => '_set_authorization_token',
307 clearer => '_clear_authorization_token',
308 predicate => 'has_authorization_token'
309 );
310  
311 4 has current_session => (
312 is => 'ro',
313 isa => 'VMware::vCloudDirector2::Object',
314 clearer => '_clear_current_session',
315 predicate => 'has_current_session',
316 lazy => 1,
317 builder => '_build_current_session'
318 );
319  
320 4 ▶method _build_current_session () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_current_session: called CC=4 SCAR=14.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
321 1 my $login_id = join( '@', $self->username, $self->orgname );
322 1 my $encoded_auth = 'Basic ' . MIME::Base64::encode( join( ':', $login_id, $self->password ) );
323 1 ▶ $self->_debug("API: attempting login as: $login_id") if ( $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
324 1 my $response =
325 $self->_request( 'POST', $self->_url_login, undef, { Authorization => $encoded_auth } );
326  
327 # if we got here then it succeeded, since we throw on failure
328 1 my $token = $response->header('x-vcloud-authorization');
329 1 $self->_set_authorization_token($token);
330 1 ▶ $self->_debug("API: authentication token: $token") if ( $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
331  
332 # we also reset the base url to match the login URL
333 ## $self->_set_base_url( $self->_url_login->clone->path('') );
334  
335 1 my ($session) = $self->_build_returned_objects($response);
336 1 return $session;
337 }
338  
339 4 ▶method login () { return $self->current_session; }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine login: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
340  
341 4 ▶method logout () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine logout: not called CC=3 SCAR=21.3
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
342 0 ▶ if ( $self->has_current_session ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_current_session) 0 0 0
343  
344 # just do this - it might fail, but little you can do now
345 0 try { $self->DELETE( $self->_url_login ); }
346 0 catch { warn "DELETE of session failed: ", @_; }
347 }
348 0 $self->_clear_api_data;
349 }
350  
351 # ------------------------------------------------------------------------
352 4 ▶method _build_returned_objects ($response) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _build_returned_objects: called CC=15 SCAR=30.0
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 2 2
if scalar @_ > 1 0 2 2
353  
354 2 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 2 0 2
355 2 ▶ $self->_debug("API: building objects") if ( $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 2 2
356  
357 2 my $hash = $self->_decode_json_response($response);
358 2 ▶ unless ( defined($hash) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash) 0 2 2
359 0 ▶ $self->_debug("API: returned null object") if ( $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
360 0 return;
361 }
362  
363 # See if this is a list of things, in which case the type element will
364 # be thingList and it will have a set of thing in it
365 2 my $mime_type = $hash->{type};
366 2 ▶ my $type = ( $mime_type =~ m|^application/vnd\..*\.(\w+)\+json$| ) ? $1 : $mime_type;
Branch: 1 branch
BranchTrueFalseTotal
$mime_type =~ m[^application/vnd\..*\.(\w+)\+json$] ? : 2 0 2
367 2 ▶ my $thing_type = ( substr( $type, -4, 4 ) eq 'List' ) ? substr( $type, 0, -4 ) : $type;
Branch: 1 branch
BranchTrueFalseTotal
substr($type, -4, 4) eq 'List' ? : 1 1 2
368  
369 2 ▶ if ( ( $type ne $thing_type )
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne $thing_type and exists $hash->{$thing_type} and is_plain_arrayref($hash->{$thing_type})) { } 1 1 2
Condition66%4/6
$type ne $thing_type and exists $hash->{$thing_type} and is_plain_arrayref($hash->{$thing_type})
!ll&&!rl&&r
101
$type ne $thing_type and exists $hash->{$thing_type}
!ll&&!rl&&r
101
MC/DC33%1/3
$type ne $thing_type and exists $hash->{$thing_type} and is_plain_arrayref($hash->{$thing_type})
$type ne $thing_type exists $hash->{$thing_type} is_plain_arrayref($hash->{$thing_type})
Truth table50%2/4
A and B and C
A= $type ne $thing_typeB= exists $hash->{$thing_type}C= is_plain_arrayref($hash->{$thing_type})
ABCresult
0XX0
10X0
1100
1111
370 and ( exists( $hash->{$thing_type} ) )
371 and is_plain_arrayref( $hash->{$thing_type} ) ) {
372 1 my @thing_objects;
373 1 ▶ $self->_debug("API: building a set of [$thing_type] objects") if ( $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
374 1 foreach my $thing ( $self->_listify( $hash->{$thing_type} ) ) {
375 5 my $object = VMware::vCloudDirector2::Object->new(
376 hash => { $thing_type => $thing },
377 api => $self,
378 _partial_object => 1
379 );
380 5 push @thing_objects, $object;
381 }
382 1 return @thing_objects;
383 }
384  
385 # was not a list of things, so just objectify the one thing here
386 else {
387 1 ▶ $self->_debug("API: building a single [$thing_type] object") if ( $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
388 1 return VMware::vCloudDirector2::Object->new( hash => $hash, api => $self );
389 }
390 }
391  
392 # there was an error here - so bomb out
393 else {
394 0 VMware::vCloudDirector2::Error->throw(
395 { message => 'Error reponse passed to object builder', response => $response } );
396 }
397 }
398  
399 # ------------------------------------------------------------------------
400  
401  
402 4 ▶method GET ($url) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine GET: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
if scalar @_ > 1 0 1 1
403 1 $self->current_session; # ensure/force valid session in place
404 1 my $response = $self->_request( 'GET', $url );
405 1 return $self->_build_returned_objects($response);
406 }
407  
408 4 ▶method GET_hash ($url) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine GET_hash: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
if scalar @_ > 1 0 1 1
409 1 $self->current_session; # ensure/force valid session in place
410 1 my $response = $self->_request( 'GET', $url );
411 1 return $self->_decode_json_response($response);
412 }
413  
414 4 ▶method PUT ($url, $hash, $content_type) {
Subroutine BEGIN: called CC=1 SCAR=5.3
Subroutine PUT: not called CC=6 SCAR=35.2
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
unless scalar @_ > 2 0 0 0
if scalar @_ > 3 0 0 0
415 0 $self->current_session; # ensure/force valid session in place
416 0 ▶ my $content = is_plain_hashref($hash) ? $self->_encode_json_content($hash) : $hash;
Branch: 1 branch
BranchTrueFalseTotal
is_plain_hashref($hash) ? : 0 0 0
417 0 my $response = $self->_request( 'PUT', $url, $content, { 'Content-Type' => $content_type } );
418 0 return $self->_build_returned_objects($response);
419 }
420  
421 4 ▶method POST ($url, $hash, $content_type) {
Subroutine BEGIN: called CC=1 SCAR=5.3
Subroutine POST: not called CC=6 SCAR=35.2
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
unless scalar @_ > 2 0 0 0
if scalar @_ > 3 0 0 0
422 0 $self->current_session; # ensure/force valid session in place
423 0 ▶ my $content = is_plain_hashref($hash) ? $self->_encode_json_content($hash) : $hash;
Branch: 1 branch
BranchTrueFalseTotal
is_plain_hashref($hash) ? : 0 0 0
424 0 my $response = $self->_request( 'POST', $url, $content, { 'Content-Type' => $content_type } );
425 0 return $self->_build_returned_objects($response);
426 }
427  
428 4 ▶method DELETE ($url) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine DELETE: not called CC=3 SCAR=21.3
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
429 0 $self->current_session; # ensure/force valid session in place
430 0 my $response = $self->_request( 'DELETE', $url );
431 0 return $self->_build_returned_objects($response);
432 }
433  
434 # ------------------------------------------------------------------------
435  
436  
437 4 has query_uri => (
438 is => 'ro',
439 isa => Uri,
440 lazy => 1,
441 builder => '_build_query_uri',
442 clearer => '_clear_query_uri',
443 );
444  
445 4 ▶method _build_query_uri () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine _build_query_uri: not called CC=3 SCAR=20.7
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
446 0 my @links = $self->current_session->find_links( rel => 'down', type => 'queryList' );
447 0 ▶ VMware::vCloudDirector2::Error->throw('Cannot find single query URL')
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @links == 1 0 0 0
448 unless ( scalar(@links) == 1 );
449 0 return $links[0]->href;
450 }
451  
452 # ------------------------------------------------------------------------
453  
454  
455 4 ▶method _clear_api_data () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine _clear_api_data: not called CC=2 SCAR=15.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
456 0 $self->_clear_default_accept_header;
457 0 $self->_clear_base_url;
458 0 $self->_clear_ua;
459 0 $self->_clear_api_version;
460 0 $self->_clear_url_login;
461 0 $self->_clear_raw_version;
462 0 $self->_clear_raw_version_full;
463 0 $self->_clear_authorization_token;
464 0 $self->_clear_current_session;
465 0 $self->_clear_query_uri;
466 }
467  
468 # ------------------------------------------------------------------------
469 4 ▶method _listify ($thing) { !defined $thing ? () : ( ( ref $thing eq 'ARRAY' ) ? @{$thing} : $thing ) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine _listify: called CC=5 SCAR=16.8
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
if scalar @_ > 1 0 1 1
!defined($thing) ? : 0 1 1
ref $thing eq 'ARRAY' ? : 1 0 1
470  
471 # ------------------------------------------------------------------------
472  
473 4 __PACKAGE__->meta->make_immutable;
474  
475 4 1;
476  
477 __END__
478  
479 =pod
480  
481 =encoding UTF-8
482  
483 =head1 NAME
484  
485 VMware::vCloudDirector2::API - Module to do stuff!
486  
487 =head1 VERSION
488  
489 version 0.100
490  
491 =head2 Attributes
492  
493 =head3 hostname
494  
495 Hostname of the vCloud server. Must have a vCloud instance listening for https
496 on port 443.
497  
498 =head3 username
499  
500 Username to use to login to vCloud server.
501  
502 =head3 password
503  
504 Password to use to login to vCloud server.
505  
506 =head3 orgname
507  
508 Org name to use to login to vCloud server - this defaults to C<System>.
509  
510 =head3 timeout
511  
512 Command timeout in seconds. Defaults to 120.
513  
514 =head3 default_accept_header
515  
516 The default MIME types to accept. This is automatically set based on the
517 information received back from the API versions.
518  
519 =head3 ssl_verify
520  
521 Whether to do standard SSL certificate verification. Defaults to set.
522  
523 =head3 ssl_ca_file
524  
525 The SSL CA set to trust packaged in a file. This defaults to those set in the
526 L<Mozilla::CA>
527  
528 =head2 debug
529  
530 Set debug level. The higher the debug level, the more chatter is exposed.
531  
532 Defaults to 0 (no output) unless the environment variable C<VCLOUD_API_DEBUG>
533 is set to something that is non-zero. Picked up at create time in C<BUILD()>.
534  
535 =head2 API SHORTHAND METHODS
536  
537 =head3 api_version
538  
539 The C<api_version> holds the version number of the highest discovered non-
540 deprecated API, it is initialised by connecting to the C</api/versions>
541 endpoint, and is called implicitly during the login setup. Once filled the
542 values are cached.
543  
544 =head3 authorization_token
545  
546 The C<authorization_token> holds the vCloud authentication token that has been
547 handed out. It is set by L<login>, and can be tested for by using the
548 predicate C<has_authorization_token>.
549  
550 =head3 current_session
551  
552 The current session object for this login. Attempting to access this forces a
553 login and creation of a current session.
554  
555 =head3 login
556  
557 Returns the L<current_session> which co-incidently forces a login.
558  
559 =head3 logout
560  
561 If there is a current session, DELETEs it, and clears the current session state
562 data.
563  
564 =head3 GET ($url)
565  
566 Forces a session establishment, and does a GET operation on the given URL,
567 returning the objects that were built.
568  
569 =head3 GET_hash ($url)
570  
571 Forces a session establishment, and does a GET operation on the given URL,
572 returning the JSON equivalent hash that was built.
573  
574 =head3 PUT ($url, $hash, $content_type)
575  
576 Forces a session establishment, and does a PUT operation on the given URL,
577 passing the JSON string or encoded hash, returning the objects that were built.
578  
579 =head3 POST ($url, $hash, $content_type)
580  
581 Forces a session establishment, and does a POST operation on the given URL,
582 passing the JSON string or encoded hash, returning the objects that were built.
583  
584 =head3 DELETE ($url)
585  
586 Forces a session establishment, and does a DELETE operation on the given URL,
587 returning the objects that were built.
588  
589 =head3 query_uri
590  
591 Returns the URI for query operations, as taken from the initial session object.
592  
593 =head2 _clear_api_data
594  
595 Clears out all the API state data, including the current login state. This is
596 not intended to be used from outside the module, and will completely trash the
597 current state requiring a new login. The basic information passed at object
598 construction time is not deleted, so a new session could be created.
599  
600 =head1 AUTHOR
601  
602 Nigel Metheringham <nigelm@cpan.org>
603  
604 =head1 COPYRIGHT AND LICENSE
605  
606 This software is copyright (c) 2019 by Nigel Metheringham.
607  
608 This is free software; you can redistribute it and/or modify it under
609 the same terms as the Perl 5 programming language system itself.
610  
611 =cut
« vCloudDirector2/ObjectContent.pm ↑ Summary vCloudDirector2.pm »