VastAI/API/Instances.pm

filter: Statementstmt 91.6% 77 / 84 Branchbran 58.0% 29 / 50 Conditioncond 34.3% 11 / 32 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 94.7% 18 / 19 Podpod 100% 12 / 12 totaltotal 69.1% 148 / 214 CC 38 SCAR 19.7
CC
38
Cov
70%
logs
19.0
ssh_keys
9.7
create
8.9
CRAP 7.2
« VastAI/Role/HTTP.pm ↑ Summary VastAI/Role/OperationMap.pm »
1 package WWW::VastAI::API::Instances;
2 10 our $VERSION = '0.001';
3 # ABSTRACT: Instance lifecycle and helper methods for Vast.ai
4  
5 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use WWW::VastAI::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 has client => (
12 is => 'ro',
13 required => 1,
14 weak_ref => 1,
15 );
16  
17 6 ▶sub _wrap {
Subroutine _wrap: called CC=1 SCAR=0.0
18 6 my ($self, $data) = @_;
19 6 return WWW::VastAI::Instance->new(
20 client => $self->client,
21 data => $data,
22 );
23 }
24  
25 8 ▶sub _extract_instance {
Subroutine _extract_instance: called CC=6 SCAR=18.2
26 8 my ($self, $result) = @_;
27  
28 8 ▶ return $result unless ref $result eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result eq "HASH" 0 8 8
29 8 ▶ return $result->{instance} if ref $result->{instance} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result->{"instance"} eq "HASH" 0 8 8
30 8 ▶ return $result->{instances} if ref $result->{instances} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result->{"instances"} eq "HASH" 4 4 8
31 4 ▶ return $result->{instances}[0] if ref $result->{instances} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result->{"instances"} eq "ARRAY" 0 4 4
32 4 ▶ return $result->{new_contract} if ref $result->{new_contract} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result->{"new_contract"} eq "HASH" 1 3 4
33  
34 3 return;
35 }
36  
37 1 ▶sub list {
Subroutine list: called CC=5 SCAR=18.9
Pod documented
38 1 my ($self) = @_;
39 1 my $result = $self->client->request_op('listInstances');
40 1 ▶ my $instances = ref $result eq 'HASH' ? ($result->{instances} || $result->{results} || []) : ($result || []);
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'HASH' ? : 1 0 1
Condition28%2/7
$result->{"instances"} or $result->{"results"} or []
l!l
10
$result->{"instances"} or $result->{"results"}
l!l&&r!l&&!r
100
$result or []
l!l
00
MC/DC0%0/2
$result->{"instances"} or $result->{"results"} or []
$result->{"instances"} $result->{"results"}
MC/DC0%0/1
$result or []
$result
Truth table33%1/3
A or B
A= $result->{"instances"}B= $result->{"results"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $result
Aresult
11
00
41 1 return [ map { $self->_wrap($_) } @{$instances} ];
42 }
43  
44 4 ▶sub get {
Subroutine get: called CC=3 SCAR=11.8
Pod documented
45 4 my ($self, $id) = @_;
46 4 ▶ croak "instance id required" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 4 4
47  
48 4 my $result = $self->client->request_op('getInstance', path => { id => $id });
49 4 ▶ my $instance = $self->_extract_instance($result) || $result;
Condition33%1/3
$self->_extract_instance($result) || $result
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->_extract_instance($result) || $result
$self->_extract_instance($result) $result
Truth table33%1/3
A || B
A= $self->_extract_instance($result)B= $result
ABresult
1X1
011
000
50 4 return $self->_wrap($instance);
51 }
52  
53 4 ▶sub create {
Subroutine create: called CC=8 SCAR=21.8
Pod documented
54 4 my ($self, $offer_id, %params) = @_;
55 4 ▶ croak "offer_id required" unless defined $offer_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offer_id 1 3 4
56 croak "image or template_hash_id required"
57 3 ▶ unless $params{image} || $params{template_hash_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"image"} or $params{"template_hash_id"} 1 2 3
Condition66%2/3
$params{"image"} or $params{"template_hash_id"}
l!l&&r!l&&!r
201
MC/DC50%1/2
$params{"image"} or $params{"template_hash_id"}
$params{"image"} $params{"template_hash_id"}
Truth table66%2/3
A or B
A= $params{"image"}B= $params{"template_hash_id"}
ABresult
1X1
011
000
58  
59 2 my $result = $self->client->request_op(
60 'createInstance',
61 path => { id => $offer_id },
62 body => \%params,
63 );
64  
65 2 my $instance = $self->_extract_instance($result);
66 2 ▶ return $self->_wrap($instance) if $instance;
Branch: 1 branch
BranchTrueFalseTotal
if $instance 1 1 2
67  
68 1 ▶ my $new_id = ref $result eq 'HASH' ? $result->{new_contract} : $result;
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'HASH' ? : 1 0 1
69 1 ▶ return $self->get($new_id) if defined $new_id && !ref $new_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_id and not ref $new_id 1 0 1
Condition33%1/3
defined $new_id and not ref $new_id
!ll&&!rl&&r
001
MC/DC0%0/2
defined $new_id and not ref $new_id
defined $new_id not ref $new_id
Truth table33%1/3
A and not(B)
A= defined $new_idB= not ref $new_id
ABresult
0X0
100
111
70  
71 0 croak 'create instance did not return an instance payload or contract id';
72 }
73  
74 2 ▶sub update {
Subroutine update: called CC=3 SCAR=11.2
Pod documented
75 2 my ($self, $id, %params) = @_;
76 2 ▶ croak "instance id required" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 2 2
77  
78 2 my $result = $self->client->request_op(
79 'manageInstance',
80 path => { id => $id },
81 body => \%params,
82 );
83  
84 2 my $instance = $self->_extract_instance($result);
85 2 ▶ return $self->_wrap($instance) if $instance;
Branch: 1 branch
BranchTrueFalseTotal
if $instance 0 2 2
86  
87 2 return $self->get($id);
88 }
89  
90 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
91 0 my ($self, $id) = @_;
92 0 return $self->update($id, state => 'running');
93 }
94  
95 1 ▶sub stop {
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
96 1 my ($self, $id) = @_;
97 1 return $self->update($id, state => 'stopped');
98 }
99  
100 1 ▶sub label {
Subroutine label: called CC=1 SCAR=0.0
Pod documented
101 1 my ($self, $id, $label) = @_;
102 1 return $self->update($id, label => $label);
103 }
104  
105 1 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.1
Pod documented
106 1 my ($self, $id) = @_;
107 1 ▶ croak "instance id required" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 1 1
108 1 return $self->client->request_op('deleteInstance', path => { id => $id });
109 }
110  
111 1 ▶sub logs {
Subroutine logs: called CC=8 SCAR=29.4
Pod documented
112 1 my ($self, $id, %params) = @_;
113 1 ▶ croak "instance id required" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 1 1
114  
115 1 my %body = (
116 tail => 100,
117 timestamps => 0,
118 %params,
119 );
120  
121 1 my $result = $self->client->request_op(
122 'requestInstanceLogs',
123 path => { id => $id },
124 body => \%body,
125 );
126  
127 1 ▶ if (ref $result eq 'HASH' && $result->{result_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result eq "HASH" and $result->{"result_url"}) 0 1 1
Condition33%1/3
ref $result eq "HASH" and $result->{"result_url"}
!ll&&!rl&&r
010
MC/DC0%0/2
ref $result eq "HASH" and $result->{"result_url"}
ref $result eq "HASH" $result->{"result_url"}
Truth table33%1/3
A and B
A= ref $result eq "HASH"B= $result->{"result_url"}
ABresult
0X0
100
111
128 0 my $ua = LWP::UserAgent->new(
129 agent => 'WWW-VastAI',
130 timeout => 30,
131 );
132 0 my $response = $ua->get($result->{result_url});
133 0 ▶ croak 'Failed to fetch instance logs: ' . $response->status_line
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
134 unless $response->is_success;
135 0 return $response->decoded_content;
136 }
137  
138 1 ▶ return ref $result eq 'HASH' ? ($result->{logs} || $result->{result} || $result) : $result;
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'HASH' ? : 1 0 1
Condition33%2/6
$result->{"logs"} or $result->{"result"} or $result
l!l&&r!l&&!r
100
$result->{"logs"} or $result->{"result"}
l!l&&r!l&&!r
100
MC/DC0%0/3
$result->{"logs"} or $result->{"result"} or $result
$result->{"logs"} $result->{"result"} $result
Truth table25%1/4
A or B or C
A= $result->{"logs"}B= $result->{"result"}C= $result
ABCresult
1XX1
01X1
0011
0000
139 }
140  
141 1 ▶sub ssh_keys {
Subroutine ssh_keys: called CC=6 SCAR=22.7
Pod documented
142 1 my ($self, $id) = @_;
143 1 ▶ croak "instance id required" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 1 1
144 1 my $result = $self->client->request_op('listInstanceSSHKeys', path => { id => $id });
145 1 ▶ return ref $result eq 'HASH' ? ($result->{ssh_keys} || $result->{results} || []) : ($result || []);
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'HASH' ? : 1 0 1
Condition28%2/7
$result->{"ssh_keys"} or $result->{"results"} or []
l!l
10
$result->{"ssh_keys"} or $result->{"results"}
l!l&&r!l&&!r
100
$result or []
l!l
00
MC/DC0%0/2
$result->{"ssh_keys"} or $result->{"results"} or []
$result->{"ssh_keys"} $result->{"results"}
MC/DC0%0/1
$result or []
$result
Truth table33%1/3
A or B
A= $result->{"ssh_keys"}B= $result->{"results"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $result
Aresult
11
00
146 }
147  
148 1 ▶sub attach_ssh_key {
Subroutine attach_ssh_key: called CC=3 SCAR=11.4
Pod documented
149 1 my ($self, $id, $ssh_key) = @_;
150 1 ▶ croak "instance id required" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 1 1
151 1 ▶ croak "ssh key required" unless defined $ssh_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ssh_key 0 1 1
152  
153 1 return $self->client->request_op(
154 'attachInstanceSSHKey',
155 path => { id => $id },
156 body => { ssh_key => $ssh_key },
157 );
158 }
159  
160 1 ▶sub detach_ssh_key {
Subroutine detach_ssh_key: called CC=3 SCAR=11.4
Pod documented
161 1 my ($self, $id, $ssh_key_id) = @_;
162 1 ▶ croak "instance id required" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 1 1
163 1 ▶ croak "ssh key id required" unless defined $ssh_key_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ssh_key_id 0 1 1
164  
165 1 return $self->client->request_op(
166 'detachInstanceSSHKey',
167 path => {
168 id => $id,
169 ssh_key_id => $ssh_key_id,
170 },
171 );
172 }
173  
174 10 1;
175  
176 __END__
« VastAI/Role/HTTP.pm ↑ Summary VastAI/Role/OperationMap.pm »