vCloudDirector/API.pm

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