VE.pm

filter: Statementstmt 17.6% 44 / 249 Branchbran 0.0% 0 / 150 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 40.0% 12 / 30 Podpod 100% 13 / 13 totaltotal 12.9% 69 / 534 CC 97 SCAR 49.2
CC
97
Cov
10%
action
462.0
login
182.0
_load_auth
156.0
CRAP 137.2
↑ Summary VE/Nodes.pm »
1 #!/bin/false
2 # vim: softtabstop=4 tabstop=4 shiftwidth=4 ft=perl expandtab smarttab
3 # PODNAME: Net::Proxmox::VE
4 # ABSTRACT: Pure Perl API for Proxmox Virtual Environment
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Net::Proxmox::VE;
10 1 $Net::Proxmox::VE::VERSION = '0.44';
11 1 ▶use HTTP::Headers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Request::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON::MaybeXS qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Net::Proxmox::VE::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # done
19 1 ▶use Net::Proxmox::VE::Access;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Net::Proxmox::VE::Cluster;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Net::Proxmox::VE::Pools;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Net::Proxmox::VE::Storage;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # wip
25 1 ▶use Net::Proxmox::VE::Nodes;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 my $API2_BASE_URL = 'https://%s:%s/api2/%s/';
28 1 my $DEFAULT_FORMAT = 'json';
29 1 my $DEFAULT_PORT = 8006;
30 1 my $DEFAULT_REALM = 'pam';
31 1 my $DEFAULT_TIMEOUT = 10;
32 1 my $DEFAULT_USERNAME = 'root';
33  
34  
35 0 ▶sub _start_request {
Subroutine _start_request: not called CC=4 SCAR=30.0
36  
37 0 my ( $self, $params ) = @_;
38  
39 # Set up the request object
40 0 my $request = HTTP::Request->new();
41 0 $request->method( $params->{method} );
42 0 ▶ if ( defined $self->{ticket} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ticket"}) 0 0 0
43 $request->header(
44 0 'Cookie' => 'PVEAuthCookie=' . $self->{ticket}->{ticket} );
45  
46 # all methods other than GET require the prevention token
47 # (i.e. anything that makes modification)
48 0 ▶ if ( $params->{method} ne 'GET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"method"} ne "GET") 0 0 0
49 $request->header(
50 'CSRFPreventionToken' => $self->{ticket}->{CSRFPreventionToken}
51 0 );
52 }
53 }
54  
55 0 ▶ if ( defined $self->{pveapitoken} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pveapitoken"}) 0 0 0
56 $request->header(
57 0 'Authorization' => 'PVEAPIToken=' . $self->{pveapitoken} );
58 }
59  
60 0 return $request;
61  
62 }
63  
64 0 ▶sub action {
Subroutine action: not called CC=21 SCAR=61.4
Pod documented
65  
66 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
67 0 my %params = @_;
68  
69 0 ▶ unless (%params) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%params) 0 0 0
70 0 Net::Proxmox::VE::Exception->throw(
71 'action() requires a hash for params');
72 }
73 Net::Proxmox::VE::Exception->throw('path param is required')
74 0 ▶ unless $params{path};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"path"} 0 0 0
75  
76 0 ▶ $params{method} ||= 'GET';
Condition0%0/2
$params{"method"} ||= "GET"
l!l
00
MC/DC0%0/1
$params{"method"} ||= "GET"
$params{"method"}
Truth table0%0/2
A
A= $params{"method"}
Aresult
11
00
77 0 ▶ $params{post_data} ||= {};
Condition0%0/2
$params{"post_data"} ||= {}
l!l
00
MC/DC0%0/1
$params{"post_data"} ||= {}
$params{"post_data"}
Truth table0%0/2
A
A= $params{"post_data"}
Aresult
11
00
78  
79 # Check for a valid method
80 Net::Proxmox::VE::Exception->throw(
81 "invalid http method specified: $params{method}")
82 0 ▶ unless $params{method} =~ m/^(GET|PUT|POST|DELETE)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"method"} =~ /^(GET|PUT|POST|DELETE)$/ 0 0 0
83  
84 # Strip prefixed / to path if present
85 0 $params{path} =~ s{^/}{};
86  
87 # Collapse duplicate slashes
88 0 $params{path} =~ s{//+}{/};
89  
90 0 ▶ unless ( $params{path} eq 'access/domains'
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"path"} eq "access/domains" or $self->check_login_ticket) 0 0 0
Condition0%0/3
$params{"path"} eq "access/domains" or $self->check_login_ticket
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"path"} eq "access/domains" or $self->check_login_ticket
$params{"path"} eq "access/domains" $self->check_login_ticket
Truth table0%0/3
A or B
A= $params{"path"} eq "access/domains"B= $self->check_login_ticket
ABresult
1X1
011
000
91 or $self->check_login_ticket )
92 {
93 print "DEBUG: invalid login ticket\n"
94 0 ▶ if $self->{params}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"}{"debug"} 0 0 0
95 0 ▶ return unless $self->login();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->login 0 0 0
96 }
97  
98 0 my $url = $self->url_prefix . $params{path};
99 0 my $request = $self->_start_request( \%params );
100  
101 # Grab useragent for convenience
102 0 my $ua = $self->{ua};
103  
104 0 my $response;
105 0 ▶ if ( $params{method} =~ m/^(PUT|POST)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"method"} =~ /^(PUT|POST)$/) 0 0 0
106 0 $request->uri($url);
107 my $content = join( '&',
108 0 map { $_ . '=' . $params{post_data}->{$_} }
109 0 sort keys %{ $params{post_data} } );
110 0 $request->content($content);
111 0 $response = $ua->request($request);
112 }
113 0 ▶ if ( $params{method} =~ m/^(GET|DELETE)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"method"} =~ /^(GET|DELETE)$/) 0 0 0
114 0 ▶ if ( %{ $params{post_data} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$params{'post_data'};}) { } 0 0 0
115 my $qstring = join( '&',
116 0 map { $_ . '=' . $params{post_data}->{$_} }
117 0 sort keys %{ $params{post_data} } );
118 0 $request->uri("$url?$qstring");
119 }
120 else {
121 0 $request->uri($url);
122 }
123 0 $response = $ua->request($request);
124 }
125 0 ▶ unless ( defined $response ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response) 0 0 0
126 # this shouldnt happen
127 Net::Proxmox::VE::Exception->throw(
128 0 'This shouldnt happen. Unknown method: ' . $params{method} );
129 }
130  
131 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
132 print 'DEBUG: successful request: ' . $request->as_string . "\n"
133 0 ▶ if $self->{params}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"}{"debug"} 0 0 0
134  
135 0 my $data = decode_json( $response->decoded_content );
136  
137 0 ▶ if ( ref $data eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH" and exists $data->{"data"}) 0 0 0
Condition0%0/3
ref $data eq "HASH" and exists $data->{"data"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $data eq "HASH" and exists $data->{"data"}
ref $data eq "HASH" exists $data->{"data"}
Truth table0%0/3
A and B
A= ref $data eq "HASH"B= exists $data->{"data"}
ABresult
0X0
100
111
138 && exists $data->{data} )
139 {
140 0 ▶ if ( ref $data->{data} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{"data"} eq "ARRAY") 0 0 0
141  
142 return wantarray
143 0 ? @{ $data->{data} }
144 0 ▶ : $data->{data};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
145  
146 }
147  
148 0 return $data->{data};
149  
150 }
151  
152 # just return true
153 0 return 1;
154  
155 }
156 else {
157 0 Net::Proxmox::VE::Exception->throw( 'WARNING: request failed: '
158 . $request->as_string . "\n"
159 . 'WARNING: response status: '
160 . $response->status_line
161 . "\n" );
162 }
163 0 return;
164  
165 }
166  
167  
168 0 ▶sub api_version {
Subroutine api_version: not called CC=2 SCAR=17.9
Pod documented
169 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
170 0 return $self->action( path => '/version', method => 'GET' );
171 }
172  
173  
174 0 ▶sub api_version_check {
Subroutine api_version_check: not called CC=5 SCAR=34.0
Pod documented
175  
176 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
177  
178 0 my $data = $self->api_version;
179  
180 0 ▶ if ( ref $data eq 'HASH' && $data->{version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH" and $data->{"version"}) 0 0 0
Condition0%0/3
ref $data eq "HASH" and $data->{"version"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $data eq "HASH" and $data->{"version"}
ref $data eq "HASH" $data->{"version"}
Truth table0%0/3
A and B
A= ref $data eq "HASH"B= $data->{"version"}
ABresult
0X0
100
111
181 0 my ($version) = $data->{version} =~ m/^(\d+)/;
182 0 ▶ return 1 if $version > 2.0;
Branch: 1 branch
BranchTrueFalseTotal
if $version > 2 0 0 0
183 }
184  
185 0 return;
186 }
187  
188  
189 0 ▶sub check_login_ticket {
Subroutine check_login_ticket: not called CC=10 SCAR=47.0
Pod documented
190  
191 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
192  
193 # API Tokens are always valid
194 0 ▶ return 1 if $self->{pveapitoken};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pveapitoken"} 0 0 0
195  
196 # Check we have a ticket loaded
197 0 ▶ my $ticket = $self->{ticket} // return;
Condition0%0/2
$self->{"ticket"} // (return)
l!l
00
MC/DC0%0/1
$self->{"ticket"} // (return)
$self->{"ticket"}
Truth table0%0/2
A
A= $self->{"ticket"}
Aresult
11
00
198 0 ▶ return unless ref $ticket eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ticket eq "HASH" 0 0 0
199  
200 # Check ticket appears valid
201 my $is_valid =
202 $ticket->{ticket}
203 && $ticket->{CSRFPreventionToken}
204 && $ticket->{username} eq "$self->{params}{username}\@$self->{params}{realm}"
205 && $self->{ticket_timestamp}
206 0 ▶ && ( $self->{ticket_timestamp} + $self->{ticket_life} ) > time();
Condition0%0/12
$ticket->{'ticket'} && $ticket->{'CSRFPreventionToken'} && $ticket->{'username'} eq "$self->{'params'}{'username'}\@$self->{'params'}{'realm'}" && $self->{'ticket_timestamp'} && $self->{'ticket_timestamp'} + $self->{'ticket_life'} > time
!ll&&!rl&&r
000
$ticket->{"ticket"} and $ticket->{"CSRFPreventionToken"} and $ticket->{"username"} eq qq[$self->{"params"}{"username"}\@$self->{"params"}{"realm"}] and $self->{"ticket_timestamp"}
!ll&&!rl&&r
000
$ticket->{"ticket"} and $ticket->{"CSRFPreventionToken"} and $ticket->{"username"} eq qq[$self->{"params"}{"username"}\@$self->{"params"}{"realm"}]
!ll&&!rl&&r
000
$ticket->{"ticket"} and $ticket->{"CSRFPreventionToken"}
!ll&&!rl&&r
000
MC/DC0%0/5
$ticket->{'ticket'} && $ticket->{'CSRFPreventionToken'} && $ticket->{'username'} eq "$self->{'params'}{'username'}\@$self->{'params'}{'realm'}" && $self->{'ticket_timestamp'} && $self->{'ticket_timestamp'} + $self->{'ticket_life'} > time
$ticket->{"ticket"} $ticket->{"CSRFPreventionToken"} $ticket->{"username"} eq qq[$self->{"params"}{"username"}\@$self->{"params"}{"realm"}] $self->{"ticket_timestamp"} $self->{'ticket_timestamp'} + $self->{'ticket_life'} > time
Truth table0%0/6
A and B and C and D && E
A= $ticket->{"ticket"}B= $ticket->{"CSRFPreventionToken"}C= $ticket->{"username"} eq qq[$self->{"params"}{"username"}\@$self->{"params"}{"realm"}]D= $self->{"ticket_timestamp"}E= $self->{'ticket_timestamp'} + $self->{'ticket_life'} > time
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
207  
208 # Clear invalid ticket
209 0 ▶ $self->clear_login_ticket unless $is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_valid 0 0 0
210  
211 # Report if ticket seems valid
212 0 return $is_valid;
213  
214 }
215  
216  
217 0 ▶sub clear_login_ticket {
Subroutine clear_login_ticket: not called CC=4 SCAR=30.0
Pod documented
218  
219 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
220  
221 0 ▶ if ( $self->{ticket} or $self->{timestamp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ticket"} or $self->{"timestamp"}) 0 0 0
Condition0%0/3
$self->{"ticket"} or $self->{"timestamp"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ticket"} or $self->{"timestamp"}
$self->{"ticket"} $self->{"timestamp"}
Truth table0%0/3
A or B
A= $self->{"ticket"}B= $self->{"timestamp"}
ABresult
1X1
011
000
222 0 $self->{ticket} = undef;
223 0 $self->{ticket_timestamp} = undef;
224 0 return 1;
225 }
226  
227 0 return;
228  
229 }
230  
231  
232 0 ▶sub debug {
Subroutine debug: not called CC=5 SCAR=34.0
Pod documented
233 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
234 0 my $d = shift;
235  
236 0 ▶ if ($d) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d) { } 0 0 0
elsif (defined $d) { } 0 0 0
237 0 $self->{params}->{debug} = 1;
238 }
239 elsif ( defined $d ) {
240 0 $self->{params}->{debug} = 0;
241 }
242  
243 0 ▶ return 1 if $self->{params}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"}{"debug"} 0 0 0
244 0 return;
245  
246 }
247  
248  
249 0 ▶sub delete {
Subroutine delete: not called CC=5 SCAR=34.0
Pod documented
250  
251 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
252 0 my $delete_data;
253 0 ▶ $delete_data = pop
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] 0 0 0
254 if ref $_[-1];
255 0 ▶ my @path = @_ or return; # using || breaks this
Branch: 1 branch
BranchTrueFalseTotal
my(@path) = or return 0 0 0
256  
257 0 ▶ if ( $self->nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nodes) 0 0 0
258  
259 0 return $self->action(
260 path => join( '/', @path ),
261 method => 'DELETE',
262 post_data => $delete_data
263 );
264  
265 }
266 0 return;
267 }
268  
269  
270 0 ▶sub _get {
Subroutine _get: not called CC=1 SCAR=6.9
271  
272 0 my $self = shift;
273 0 my $post_data = pop;
274 0 my @path = @_;
275 0 return $self->action(
276 path => join( '/', @path ),
277 method => 'GET',
278 post_data => $post_data
279 );
280 }
281  
282 0 ▶sub get {
Subroutine get: not called CC=5 SCAR=34.0
Pod documented
283  
284 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
285 0 my $post_data;
286 0 ▶ $post_data = pop
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] 0 0 0
287 if ref $_[-1];
288 0 ▶ my @path = @_ or return; # using || breaks this
Branch: 1 branch
BranchTrueFalseTotal
my(@path) = or return 0 0 0
289  
290 # Calling nodes method here would call get method itself and so on
291 # Commented out to avoid an infinite loop
292 0 ▶ if ( $self->nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nodes) 0 0 0
293 0 return $self->_get( @path, $post_data );
294 }
295 0 return;
296 }
297  
298  
299 0 ▶sub _handle_tfa {
Subroutine _handle_tfa: not called CC=3 SCAR=24.8
300  
301 0 my ($self, $challenge) = @_;
302  
303 0 ▶ my $totp = $self->{params}->{totp} // '';
Condition0%0/2
$self->{"params"}{"totp"} // ""
l!l
00
MC/DC0%0/1
$self->{"params"}{"totp"} // ""
$self->{"params"}{"totp"}
Truth table0%0/2
A
A= $self->{"params"}{"totp"}
Aresult
11
00
304 # if $totp is a code ref then call it
305 0 ▶ if (ref $totp eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $totp eq "CODE") 0 0 0
306 $totp = $totp->(
307 username => $self->{params}->{username},
308 host => $self->{params}->{host},
309 realm => $self->{params}->{realm},
310 0 );
311 }
312  
313 # Prepare login request w/ totp
314 0 my $url = $self->url_prefix . 'access/ticket';
315 my $data = {
316 'username' => $self->{params}->{username} . '@'
317 . $self->{params}->{realm},
318 0 'password' => "totp:$totp",
319 'tfa-challenge' => $challenge,
320 };
321  
322 # Perform login request w/ totp
323 0 return $self->{ua}->post( $url, $data );
324 }
325  
326 0 ▶sub login {
Subroutine login: not called CC=13 SCAR=52.0
Pod documented
327  
328 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
329  
330 0 ▶ if ( defined $self->{pveapitoken} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pveapitoken"}) 0 0 0
331 print "DEBUG: API Tokens are always logged in\n"
332 0 ▶ if $self->{params}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"}{"debug"} 0 0 0
333 0 return 1;
334 }
335  
336 # Prepare login request
337 0 my $request_time = time();
338 0 my $url = $self->url_prefix . 'access/ticket';
339 my $request = {
340 'username' => $self->{params}->{username} . '@'
341 . $self->{params}->{realm},
342 'password' => $self->{params}->{password},
343 0 };
344  
345 # Perform login request
346 0 my $response = $self->{ua}->post( $url, $request );
347  
348 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
349 0 my $login_ticket_data = decode_json( $response->decoded_content );
350 0 my $data = $login_ticket_data->{data};
351 # Take care of TFA if needed
352 0 ▶ if ( $data->{NeedTFA} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'NeedTFA'}) { } 0 0 0
353 0 ▶ unless ( defined $self->{totp} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"totp"}) 0 0 0
354 print "DEBUG: totp required but not provided\n"
355 0 ▶ if $self->{params}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"}{"debug"} 0 0 0
356 0 return;
357 }
358 0 $response = $self->_handle_tfa($data->{ticket});
359 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
360 print "DEBUG: tfa successful\n"
361 0 ▶ if $self->{params}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"}{"debug"} 0 0 0
362 0 my $tfa_ticket_data = decode_json( $response->decoded_content );
363 0 $self->{ticket} = $tfa_ticket_data->{data};
364 }
365 }
366 else {
367 0 $self->{ticket} = $data;
368 }
369  
370 0 ▶ if ($data->{ticket}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"ticket"}) 0 0 0
371 # We use the request time because the time to get the json ticket is undetermined.
372 # It seems wiser to discard a ticket a few seconds before it expires rather than to incorrectly
373 # continue using it after it has expired
374 0 $self->{ticket_timestamp} = $request_time;
375 print "DEBUG: login successful\n"
376 0 ▶ if $self->{params}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"}{"debug"} 0 0 0
377 0 return 1;
378 }
379 }
380  
381 # If we get here then Login has failed
382 0 ▶ if ( $self->{params}->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"params"}{"debug"}) 0 0 0
383 0 print "DEBUG: login not successful\n";
384 0 print "DEBUG: " . $response->status_line . "\n";
385 }
386  
387 0 return;
388 }
389  
390  
391 0 ▶sub _load_auth {
Subroutine _load_auth: not called CC=12 SCAR=50.5
392  
393 0 my ( $self, $params ) = @_;
394  
395 0 ▶ if ( ( $params->{password} or $params->{totp} )
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"password"} || $params->{"totp"} and $params->{"tokenid"} || $params->{"secret"}) 0 0 0
Condition0%0/9
$params->{"password"} || $params->{"totp"} and $params->{"tokenid"} || $params->{"secret"}
!ll&&!rl&&r
000
$params->{"password"} or $params->{"totp"}
l!l&&r!l&&!r
000
$params->{"tokenid"} or $params->{"secret"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$params->{"password"} || $params->{"totp"} and $params->{"tokenid"} || $params->{"secret"}
$params->{"password"} $params->{"totp"} $params->{"tokenid"} $params->{"secret"}
Truth table0%0/7
A or B and C or D
A= $params->{"password"}B= $params->{"totp"}C= $params->{"tokenid"}D= $params->{"secret"}
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
396 and ( $params->{tokenid} or $params->{secret} ) )
397 {
398 0 Net::Proxmox::VE::Exception->throw(
399 'Both password and API Token credentials provided.'
400 . ' Please pick one authentication method'
401 );
402 }
403  
404 0 ▶ my $realm = delete $params->{realm} || $DEFAULT_REALM;
Condition0%0/3
delete $params->{'realm'} || $DEFAULT_REALM
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $params->{'realm'} || $DEFAULT_REALM
delete $params->{'realm'} $DEFAULT_REALM
Truth table0%0/3
A || B
A= delete $params->{'realm'}B= $DEFAULT_REALM
ABresult
1X1
011
000
405 0 ▶ my $username = delete $params->{username} || $DEFAULT_USERNAME;
Condition0%0/3
delete $params->{'username'} || $DEFAULT_USERNAME
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $params->{'username'} || $DEFAULT_USERNAME
delete $params->{'username'} $DEFAULT_USERNAME
Truth table0%0/3
A || B
A= delete $params->{'username'}B= $DEFAULT_USERNAME
ABresult
1X1
011
000
406  
407 0 ▶ if ( $params->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"password"}) 0 0 0
408 my $password = delete $params->{password}
409 0 ▶ or Net::Proxmox::VE::Exception->throw('password param is required');
Branch: 1 branch
BranchTrueFalseTotal
my $password = delete $params->{"password"} or "Net::Proxmox::VE::Exception"->throw("password param is required") 0 0 0
410 0 $self->{'params'}->{'password'} = $password;
411 0 $self->{'params'}->{'realm'} = $realm;
412 0 $self->{'params'}->{'username'} = $username;
413 $self->{'params'}->{'totp'} = delete $params->{totp}
414 0 ▶ if defined $params->{totp};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"totp"} 0 0 0
415 0 $self->{'ticket'} = undef;
416 0 $self->{'ticket_timestamp'} = undef;
417 0 $self->{'ticket_life'} = 7200; # 2 Hours
418 0 return 1;
419 }
420  
421 0 ▶ if ( $params->{tokenid} and $params->{secret} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"tokenid"} and $params->{"secret"}) 0 0 0
Condition0%0/3
$params->{"tokenid"} and $params->{"secret"}
!ll&&!rl&&r
000
MC/DC0%0/2
$params->{"tokenid"} and $params->{"secret"}
$params->{"tokenid"} $params->{"secret"}
Truth table0%0/3
A and B
A= $params->{"tokenid"}B= $params->{"secret"}
ABresult
0X0
100
111
422 0 my $tokenid = delete $params->{tokenid};
423 0 my $secret = delete $params->{secret};
424 0 $self->{'pveapitoken'} = sprintf( '%s@%s!%s=%s', $username, $realm, $tokenid, $secret );
425 0 return 1;
426 }
427  
428 Net::Proxmox::VE::Exception->throw(
429 0 'Incomplete authentication credentials provided.'
430 . 'Either a password or tokenid and secret must be provided' );
431  
432 }
433  
434 0 ▶sub _create_ua {
Subroutine _create_ua: not called CC=2 SCAR=17.9
435  
436 0 my ( $self, $params ) = @_;
437  
438 0 my $ssl_opts = delete $params->{ssl_opts};
439 0 my %lwpUserAgentOptions;
440 0 ▶ if ($ssl_opts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssl_opts) 0 0 0
441 0 $lwpUserAgentOptions{ssl_opts} = $ssl_opts;
442 }
443 0 my $ua = LWP::UserAgent->new(%lwpUserAgentOptions);
444 0 $ua->timeout( $self->{params}->{timeout} );
445 0 $self->{ua} = $ua;
446  
447 0 return 1;
448  
449 }
450  
451 0 ▶sub new {
Subroutine new: not called CC=10 SCAR=47.0
Pod documented
452  
453 0 my $c = shift;
454 0 my @p = @_;
455 0 ▶ my $class = ref($c) || $c;
Condition0%0/3
ref $c || $c
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $c || $c
ref $c $c
Truth table0%0/3
A || B
A= ref $cB= $c
ABresult
1X1
011
000
456  
457 0 my %params;
458  
459 0 ▶ if ( scalar @p == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @p == 1) { } 0 0 0
elsif (scalar @p % 2 != 0) { } 0 0 0
460  
461 0 ▶ Net::Proxmox::VE::Exception->throw('new() requires a hash for params')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p[0] eq "HASH" 0 0 0
462 unless ref $p[0] eq 'HASH';
463  
464 0 %params = %{ $p[0] };
465  
466 }
467 elsif ( scalar @p % 2 != 0 ) {
468 0 Net::Proxmox::VE::Exception->throw(
469 'new() called with an odd number of parameters');
470  
471 }
472 else {
473 0 ▶ %params = @p
Branch: 1 branch
BranchTrueFalseTotal
%params = @p or "Net::Proxmox::VE::Exception"->throw("new() requires a hash for params") 0 0 0
474 or Net::Proxmox::VE::Exception->throw(
475 'new() requires a hash for params');
476 }
477  
478 0 my $debug = delete $params{debug};
479 my $host = delete $params{host}
480 0 ▶ or Net::Proxmox::VE::Exception->throw('host param is required');
Branch: 1 branch
BranchTrueFalseTotal
my $host = delete $params{"host"} or "Net::Proxmox::VE::Exception"->throw("host param is required") 0 0 0
481 0 ▶ my $port = delete $params{port} || $DEFAULT_PORT;
Condition0%0/3
delete $params{'port'} || $DEFAULT_PORT
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $params{'port'} || $DEFAULT_PORT
delete $params{'port'} $DEFAULT_PORT
Truth table0%0/3
A || B
A= delete $params{'port'}B= $DEFAULT_PORT
ABresult
1X1
011
000
482 0 ▶ my $timeout = delete $params{timeout} || $DEFAULT_TIMEOUT;
Condition0%0/3
delete $params{'timeout'} || $DEFAULT_TIMEOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $params{'timeout'} || $DEFAULT_TIMEOUT
delete $params{'timeout'} $DEFAULT_TIMEOUT
Truth table0%0/3
A || B
A= delete $params{'timeout'}B= $DEFAULT_TIMEOUT
ABresult
1X1
011
000
483  
484 my $self->{params} = {
485 0 debug => $debug,
486 host => $host,
487 port => $port,
488 timeout => $timeout,
489 };
490  
491 0 bless $self, $class;
492  
493 0 $self->_load_auth( \%params );
494 0 $self->_create_ua( \%params );
495  
496 0 ▶ Net::Proxmox::VE::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if keys %params 0 0 0
497 'unknown parameters to new(): ' . join( ', ', keys %params ) )
498 if keys %params;
499  
500 0 return $self;
501  
502 }
503  
504  
505 0 ▶sub post {
Subroutine post: not called CC=5 SCAR=34.0
Pod documented
506  
507 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
508 0 my $post_data;
509 0 ▶ $post_data = pop
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] 0 0 0
510 if ref $_[-1];
511 0 ▶ my @path = @_ or return; # using || breaks this
Branch: 1 branch
BranchTrueFalseTotal
my(@path) = or return 0 0 0
512  
513 0 ▶ if ( $self->nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nodes) 0 0 0
514  
515 0 return $self->action(
516 path => join( '/', @path ),
517 method => 'POST',
518 post_data => $post_data
519 );
520  
521 }
522 0 return;
523 }
524  
525  
526 0 ▶sub put {
Subroutine put: not called CC=5 SCAR=34.0
Pod documented
527  
528 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
529 0 my $post_data;
530 0 ▶ $post_data = pop
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] 0 0 0
531 if ref $_[-1];
532 0 ▶ my @path = @_ or return; # using || breaks this
Branch: 1 branch
BranchTrueFalseTotal
my(@path) = or return 0 0 0
533  
534 0 ▶ if ( $self->nodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nodes) 0 0 0
535  
536 0 return $self->action(
537 path => join( '/', @path ),
538 method => 'PUT',
539 post_data => $post_data
540 );
541  
542 }
543 0 return;
544 }
545  
546  
547 0 ▶sub url_prefix {
Subroutine url_prefix: not called CC=2 SCAR=17.9
Pod documented
548  
549 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
550  
551 # Prepare prefix for request
552 my $url_prefix = sprintf( $API2_BASE_URL,
553 $self->{params}->{host},
554 $self->{params}->{port},
555 0 $DEFAULT_FORMAT);
556  
557 0 return $url_prefix;
558  
559 }
560  
561  
562 1 1;
563  
564 __END__
↑ Summary VE/Nodes.pm »