| 1 | package SolusVM::Client; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | $SolusVM::Client::VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use re '/aa'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Cpanel::JSON::XS (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use HTTP::Tiny (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Sub::Install (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Time::Piece (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use SolusVM::Client::Specification (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | # ABSTRACT: Client for the SolusVM 2 API, built from its own specification | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # What counts as "about to expire". A token good for another half minute is one | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # that will be rejected mid-provision. | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | our $EXPIRY_MARGIN = 60; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # The methods this class defines itself. An operation named after one of these | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # would be silently shadowed by it, so the spec is checked against the list | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # rather than trusted. | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | our @RESERVED = qw{new catalog login token host base_url paginate last_meta last_links last_response}; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub new ( $class, %options ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my $self = bless { | |||||||||||||||||||||||||||||||||||||||||
| 29 | scheme => 'https', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | prefix => '/api/v1', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | timeout => 60, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | verify_SSL => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | debug => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | %options, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | die "SolusVM::Client needs the host of a management node.\n" unless defined $self->{host} && length $self->{host}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"host"} and length $self->{"host"}
MC/DC0%0/2
defined $self->{"host"} and length $self->{"host"}
defined $self->{"host"}
length $self->{"host"}
Truth table0%0/3
A and B
A= defined $self->{"host"}B= length $self->{"host"}
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | die "SolusVM::Client needs either a token, or the email and password to get one with.\n" | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | unless defined $self->{token} || ( defined $self->{email} && defined $self->{password} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $self->{"token"} or defined $self->{"email"} and defined $self->{"password"}
defined $self->{"email"} and defined $self->{"password"}
MC/DC0%0/3
defined $self->{"token"} or defined $self->{"email"} and defined $self->{"password"}
defined $self->{"token"}
defined $self->{"email"}
defined $self->{"password"}
Truth table0%0/4
A or B and C
A= defined $self->{"token"}B= defined $self->{"email"}C= defined $self->{"password"}
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $self->{spec} = SolusVM::Client::Specification::load( defined $self->{spec_file} ? ( file => $self->{spec_file} ) : () ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | $self->{ua} //= HTTP::Tiny->new( | ||||||||||||||||||||||||||||||||||||||||||
| 43 | agent => 'SolusVM-Client/' . ( __PACKAGE__->VERSION // 'dev' ), | ||||||||||||||||||||||||||||||||||||||||||
| 44 | timeout => $self->{timeout}, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | verify_SSL => $self->{verify_SSL}, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$self->{"ua"} //= "HTTP::Tiny"->new("agent", 'SolusVM-Client/' . (__PACKAGE__->VERSION // "dev"), "timeout", $self->{"timeout"}, "verify_SSL", $self->{"verify_SSL"})
__PACKAGE__->VERSION // "dev"
MC/DC0%0/2
$self->{"ua"} //= "HTTP::Tiny"->new("agent", 'SolusVM-Client/' . (__PACKAGE__->VERSION // "dev"), "timeout", $self->{"timeout"}, "verify_SSL", $self->{"verify_SSL"})
$self->{"ua"}
"HTTP::Tiny"->new("agent", 'SolusVM-Client/' . (__PACKAGE__->VERSION // "dev"), "timeout", $self->{"timeout"}, "verify_SSL", $self->{"verify_SSL"})
MC/DC0%0/1
__PACKAGE__->VERSION // "dev"
__PACKAGE__->VERSION
Truth table0%0/3
A //= B
A= $self->{"ua"}B= "HTTP::Tiny"->new("agent", 'SolusVM-Client/' . (__PACKAGE__->VERSION // "dev"), "timeout", $self->{"timeout"}, "verify_SSL", $self->{"verify_SSL"})
Truth table0%0/2
A
A= __PACKAGE__->VERSION
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $self->_build_subs(); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub host ($self) { return $self->{host} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
host: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub base_url ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
base_url: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $port = defined $self->{port} && length $self->{port} ? ":$self->{port}" : q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"port"} and length $self->{"port"}
MC/DC0%0/2
defined $self->{"port"} and length $self->{"port"}
defined $self->{"port"}
length $self->{"port"}
Truth table0%0/3
A and B
A= defined $self->{"port"}B= length $self->{"port"}
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | return "$self->{scheme}://$self->{host}$port$self->{prefix}"; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub token ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
token: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $self->login() if $self->_needs_token(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | return $self->{token}; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub catalog ( $self, %options ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
catalog: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $like = $options{like}; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my @wanted = sort { $a->{name} cmp $b->{name} } values %{ $self->{spec} }; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | @wanted = grep { "$_->{name} $_->{uri} $_->{summary}" =~ $like } @wanted if $like; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $listing = q{}; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | foreach my $entry (@wanted) { | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my %required = map { $_ => 1 } @{ $entry->{required} }; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $listing .= sprintf "%-46s %-6s %s\n", $entry->{name}, $entry->{method}, $entry->{uri}; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $listing .= sprintf "%-46s %s\n", q{}, $entry->{summary} if length $entry->{summary}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $listing .= sprintf "%-46s query: %s\n", q{}, join q{ }, @{ $entry->{query_params} } if @{ $entry->{query_params} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $listing .= sprintf "%-46s body: %s\n", q{}, join q{ }, map { $required{$_} ? "$_*" : $_ } @{ $entry->{body_params} } if @{ $entry->{body_params} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | return $listing; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub login ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
login: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | die "There is no token left and no email and password to get another with.\n" | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | unless defined $self->{email} && defined $self->{password}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"email"} and defined $self->{"password"}
MC/DC0%0/2
defined $self->{"email"} and defined $self->{"password"}
defined $self->{"email"}
defined $self->{"password"}
Truth table0%0/3
A and B
A= defined $self->{"email"}B= defined $self->{"password"}
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # Not authorized, and not only because there is nothing to authorize it | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # with: asking for the token would call this, which would ask again. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | my $answer = $self->_http( | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 'login', 'POST', | ||||||||||||||||||||||||||||||||||||||||||
| 96 | $self->base_url . '/auth/login', | ||||||||||||||||||||||||||||||||||||||||||
| 97 | body => { email => $self->{email}, password => $self->{password} }, | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | authorize => 0, | |||||||||||||||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $credentials = $answer->{data}{credentials}; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | die "The management node accepted the login but sent back no access token.\n" unless ref $credentials eq 'HASH' && defined $credentials->{access_token}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $credentials eq "HASH" and defined $credentials->{"access_token"}
MC/DC0%0/2
ref $credentials eq "HASH" and defined $credentials->{"access_token"}
ref $credentials eq "HASH"
defined $credentials->{"access_token"}
Truth table0%0/3
A and B
A= ref $credentials eq "HASH"B= defined $credentials->{"access_token"}
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $self->{token} = $credentials->{access_token}; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $self->{expires_at} = $credentials->{expires_at}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | return $credentials; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub paginate ( $self, $name, %params ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
paginate: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my @everything; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my $page = $params{page} // 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params{"page"} // 1
MC/DC0%0/1
$params{"page"} // 1
$params{"page"}
Truth table0%0/2
A
A= $params{"page"}
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my $answer = $self->_request( $name, %params, page => $page ); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my $data = $answer->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | push @everything, ref $data eq 'ARRAY' ? @{$data} : ($data); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my $meta = $answer->{meta}; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | last unless ref $meta eq 'HASH' && defined $meta->{last_page}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $meta eq "HASH" and defined $meta->{"last_page"}
MC/DC0%0/2
ref $meta eq "HASH" and defined $meta->{"last_page"}
ref $meta eq "HASH"
defined $meta->{"last_page"}
Truth table0%0/3
A and B
A= ref $meta eq "HASH"B= defined $meta->{"last_page"}
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | last if $page >= $meta->{last_page}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $page++; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | return @everything; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub last_meta ($self) { return $self->{last_meta} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_meta: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub last_links ($self) { return $self->{last_links} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_links: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub last_response ($self) { return $self->{last_response} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_response: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | # One closure per operation, installed into the package rather than the object | ||||||||||||||||||||||||||||||||||||||||||
| 135 | # because that is where perl looks for a method. Guarded by can(), so a spec | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # that grows an operation called `login` cannot quietly replace this class's | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # own -- and there is a test that fails if one ever does, because the guard on | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # its own would only make the collision silent. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub _build_subs ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_subs: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | foreach my $name ( keys %{ $self->{spec} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | next if __PACKAGE__->can($name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | Sub::Install::install_sub( | ||||||||||||||||||||||||||||||||||||||||||
| 143 | { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | code => sub { my $invocant = shift; return $invocant->_request( $name, @_ ) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | as => $name, | |||||||||||||||||||||||||||||||||||||||||
| 146 | into => __PACKAGE__, | ||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | sub _request ( $self, $name, %params ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_request: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my $entry = $self->{spec}{$name} or die "The SolusVM API has no operation called $name.\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my $uri = $entry->{uri}; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | foreach my $placeholder ( @{ $entry->{path_params} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my $value = delete $params{$placeholder}; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | die "$name needs a $placeholder: its path is $entry->{uri}.\n" unless defined $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | $uri =~ s/[{]\Q$placeholder\E[}]/$value/g; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my $url = $self->base_url . $uri; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | my $body; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ( $entry->{has_body} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $body = \%params; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | elsif ( $entry->{method} eq 'GET' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | $url .= '?' . $self->{ua}->www_form_urlencode( \%params ) if %params; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | elsif (%params) { | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | die "$name takes nothing but its path, so there is nowhere to put " . join( q{, }, sort keys %params ) . ".\n"; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $answer = eval { $self->_http( $name, $entry->{method}, $url, body => $body ) }; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my $failure = $@; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | return $answer unless $failure; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | die $failure unless $self->_rejected_the_token(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | die $failure . "Pass email and password as well as the token, and this client will renew it for you.\n" unless $self->_can_renew(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | # The token was good as far as we knew and the API disagreed -- its clock, | ||||||||||||||||||||||||||||||||||||||||||
| 183 | # a revoked token, a restarted management node. Worth exactly one more go. | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | $self->login(); | |||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | return $self->_http( $name, $entry->{method}, $url, body => $body ); | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub _http ( $self, $name, $method, $url, %options ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_http: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my %request = ( | |||||||||||||||||||||||||||||||||||||||||
| 190 | headers => { | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 'Accept' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 193 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 194 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | $request{headers}{'Authorization'} = 'Bearer ' . $self->token() if $options{authorize} // 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$options{"authorize"} // 1
MC/DC0%0/1
$options{"authorize"} // 1
$options{"authorize"}
Truth table0%0/2
A
A= $options{"authorize"}
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | $request{content} = Cpanel::JSON::XS->new->utf8->canonical->encode( $options{body} ) if defined $options{body}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | say {*STDERR} "SolusVM: $method $url" . ( defined $request{content} ? ' ' . _redact( $request{content} ) : q{} ) if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | my $response = $self->{ua}->request( $method, $url, \%request ); | |||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | $self->{last_response} = $response; | |||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | say {*STDERR} "SolusVM: $response->{status} $response->{reason} " . _redact( $response->{content} // q{} ) if $self->{debug}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table0%0/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | die "SolusVM $name ($method $url) failed: $response->{status} $response->{reason}\n" . _redact( $response->{content} // q{} ) . "\n" | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | unless $response->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table0%0/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | my $answer = length( $response->{content} // q{} ) ? Cpanel::JSON::XS->new->utf8->decode( $response->{content} ) : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table0%0/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | @{$self}{qw{last_meta last_links}} = ( $answer->{meta}, $answer->{links} ) if ref $answer eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | return $answer; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | # A password that reached a log line or an exception is a password in a ticket. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | sub _redact ($content) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_redact: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $content =~ s/("password"\s*:\s*")[^"]*"/$1********"/g; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | return $content; | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | sub _can_renew ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_can_renew: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | return defined $self->{email} && defined $self->{password} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"email"} and defined $self->{"password"}
MC/DC0%0/2
defined $self->{"email"} and defined $self->{"password"}
defined $self->{"email"}
defined $self->{"password"}
Truth table0%0/3
A and B
A= defined $self->{"email"}B= defined $self->{"password"}
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | sub _rejected_the_token ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rejected_the_token: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | return ref $self->{last_response} eq 'HASH' && $self->{last_response}{status} == 401 ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $self->{"last_response"} eq "HASH" and $self->{"last_response"}{"status"} == 401
MC/DC0%0/2
ref $self->{"last_response"} eq "HASH" and $self->{"last_response"}{"status"} == 401
ref $self->{"last_response"} eq "HASH"
$self->{"last_response"}{"status"} == 401
Truth table0%0/3
A and B
A= ref $self->{"last_response"} eq "HASH"B= $self->{"last_response"}{"status"} == 401
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | sub _needs_token ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_needs_token: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | return 1 unless defined $self->{token}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | return 0 unless $self->_can_renew(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | my $expiry = _epoch_of( $self->{expires_at} ); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | return 0 unless defined $expiry; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | return time >= $expiry - $EXPIRY_MARGIN ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | # The spec calls expires_at a string and leaves it at that, so this parses the | ||||||||||||||||||||||||||||||||||||||||||
| 238 | # two shapes the API has been seen to use and gives up quietly on anything else. | ||||||||||||||||||||||||||||||||||||||||||
| 239 | # Giving up means falling back on the retry in _request, which is a slower way | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # to notice an expired token but not a wrong one. | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | sub _epoch_of ($when) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_epoch_of: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | return undef unless defined $when && length $when; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $when and length $when
MC/DC0%0/2
defined $when and length $when
defined $when
length $when
Truth table0%0/3
A and B
A= defined $whenB= length $when
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | # strptime warns its way through a format that does not fit before failing, | ||||||||||||||||||||||||||||||||||||||||||
| 245 | # and trying formats until one fits is the whole method here. | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | local $SIG{__WARN__} = sub { return 1 }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | foreach my $format ( '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d %H:%M:%S' ) { | |||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | my $parsed = eval { Time::Piece->strptime( substr( $when, 0, 19 ), $format ) }; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | return $parsed->epoch if $parsed; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | __END__ | ||||||||||||||||||||||||||||||||||||||||||