lib/WWW/Hetzner/Cloud/API/Servers.pm

filter: Statementstmt 87.3% 117 / 134 Branchbran 62.2% 56 / 90 Conditioncond 83.3% 15 / 18 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 82.7% 24 / 29 Podpod 100% 22 / 22 totaltotal 79.4% 240 / 302 CC 55 SCAR 23.1
CC
55
Cov
78%
create
25.1
attach_iso
12.0
disable_rescue
6.0
CRAP 10.1
« lib/WWW/Hetzner/Robot/CLI/Cmd/Rdns.pm ↑ Summary lib/WWW/Hetzner/CLI/Cmd/PrimaryIp/Cmd/List.pm »
1 package WWW::Hetzner::Cloud::API::Servers;
2 # ABSTRACT: Hetzner Cloud Servers API
3  
4 60 our $VERSION = '0.102';
5  
6 60 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 60 with 'WWW::Hetzner::Role::Pagination';
8 60 with 'WWW::Hetzner::Role::JSONBool';
9 60 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 60 ▶use WWW::Hetzner::Cloud::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 60 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 60 has client => (
15 is => 'ro',
16 required => 1,
17 weak_ref => 1,
18 );
19  
20 60 with 'WWW::Hetzner::Role::HasActions';
21  
22 63 ▶sub _wrap {
Subroutine _wrap: called CC=1 SCAR=0.0
23 63 my ($self, $data, %extra) = @_;
24 63 return WWW::Hetzner::Cloud::Server->new(
25 client => $self->client,
26 %$data,
27 %extra,
28 );
29 }
30  
31 28 ▶sub _wrap_list {
Subroutine _wrap_list: called CC=1 SCAR=0.0
32 28 my ($self, $list) = @_;
33 28 return [ map { $self->_wrap($_) } @$list ];
34 }
35  
36  
37 31 ▶sub _list_page {
Subroutine _list_page: called CC=2 SCAR=7.1
38 31 my ($self, %params) = @_;
39  
40 31 my $result = $self->client->get('/servers', params => \%params);
41 28 ▶ return ($self->_wrap_list($result->{servers} // []), $result->{meta});
Condition50%1/2
$result->{"servers"} // []
l!l
280
MC/DC0%0/1
$result->{"servers"} // []
$result->{"servers"}
Truth table50%1/2
A
A= $result->{"servers"}
Aresult
11
00
42 }
43  
44 7 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
45 7 my ($self, %params) = @_;
46  
47 7 my ($entities) = $self->_list_page(%params);
48 5 return $entities;
49 }
50  
51  
52  
53 1 ▶sub list_by_label {
Subroutine list_by_label: called CC=1 SCAR=0.0
Pod documented
54 1 my ($self, $label_selector) = @_;
55 1 return $self->list(label_selector => $label_selector);
56 }
57  
58  
59 7 ▶sub get {
Subroutine get: called CC=2 SCAR=7.0
Pod documented
60 7 my ($self, $id) = @_;
61 7 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 7 7
62  
63 7 my $result = $self->client->get("/servers/$id");
64 7 return $self->_wrap($result->{server});
65 }
66  
67  
68 11 ▶sub create {
Subroutine create: called CC=25 SCAR=32.2
Pod documented
69 11 my ($self, %params) = @_;
70  
71 11 ▶ croak "name required" unless $params{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"name"} 1 10 11
72 10 ▶ croak "server_type required" unless $params{server_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"server_type"} 1 9 10
73 9 ▶ croak "image required" unless $params{image};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"image"} 1 8 9
74  
75 my $body = {
76 name => $params{name},
77 server_type => $params{server_type},
78 image => $params{image},
79 8 };
80  
81 # Location/Datacenter (mutually exclusive)
82 8 ▶ $body->{location} = $params{location} if $params{location};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"location"} 2 6 8
83 8 ▶ $body->{datacenter} = $params{datacenter} if $params{datacenter};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"datacenter"} 0 8 8
84  
85 # SSH Keys (array of names or IDs)
86 8 ▶ $body->{ssh_keys} = $params{ssh_keys} if $params{ssh_keys};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"ssh_keys"} 1 7 8
87  
88 # Labels (hash)
89 8 ▶ $body->{labels} = $params{labels} if $params{labels};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"labels"} 1 7 8
90  
91 # Cloud-init user data
92 8 ▶ $body->{user_data} = $params{user_data} if $params{user_data};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"user_data"} 0 8 8
93  
94 # Start server after create (default: true)
95 8 ▶ $body->{start_after_create} = _json_bool($params{start_after_create} // 1);
Condition100%2/2
$params{"start_after_create"} // 1
l!l
35
MC/DC100%1/1
$params{"start_after_create"} // 1
$params{"start_after_create"}
Truth table100%2/2
A
A= $params{"start_after_create"}
Aresult
11
00
96  
97 # Placement group (ID or name)
98 8 ▶ $body->{placement_group} = $params{placement_group} if $params{placement_group};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"placement_group"} 1 7 8
99  
100 # Networks (array of network IDs)
101 8 ▶ $body->{networks} = $params{networks} if $params{networks};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"networks"} 1 7 8
102  
103 # Volumes (array of volume IDs)
104 8 ▶ $body->{volumes} = $params{volumes} if $params{volumes};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"volumes"} 1 7 8
105  
106 # Automount volumes
107 8 ▶ $body->{automount} = _json_bool($params{automount}) if exists $params{automount};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"automount"} 2 6 8
108  
109 # Firewalls (array of firewall IDs)
110 8 ▶ if ($params{firewalls}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"firewalls"}) 1 7 8
111 $body->{firewalls} = [
112 1 map { { firewall => $_ } } @{$params{firewalls}}
113 ];
114 }
115  
116 # Public network configuration
117 8 ▶ if ($params{public_net} || exists $params{enable_ipv4} || exists $params{enable_ipv6}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"public_net"} or exists $params{"enable_ipv4"} or exists $params{"enable_ipv6"}) 5 3 8
Condition83%5/6
$params{"public_net"} or exists $params{"enable_ipv4"} or exists $params{"enable_ipv6"}
l!l&&r!l&&!r
503
$params{"public_net"} or exists $params{"enable_ipv4"}
l!l&&r!l&&!r
143
MC/DC66%2/3
$params{"public_net"} or exists $params{"enable_ipv4"} or exists $params{"enable_ipv6"}
$params{"public_net"} exists $params{"enable_ipv4"} exists $params{"enable_ipv6"}
Truth table75%3/4
A or B or C
A= $params{"public_net"}B= exists $params{"enable_ipv4"}C= exists $params{"enable_ipv6"}
ABCresult
1XX1
01X1
0011
0000
118 # Copy so the caller's hash is not modified by the coercion below
119 5 ▶ my $public_net = $body->{public_net} = { %{ $params{public_net} // {} } };
Condition100%2/2
$params{"public_net"} // {}
l!l
14
MC/DC100%1/1
$params{"public_net"} // {}
$params{"public_net"}
Truth table100%2/2
A
A= $params{"public_net"}
Aresult
11
00
120 5 ▶ $public_net->{enable_ipv4} = $params{enable_ipv4} if exists $params{enable_ipv4};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"enable_ipv4"} 4 1 5
121 5 ▶ $public_net->{enable_ipv6} = $params{enable_ipv6} if exists $params{enable_ipv6};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"enable_ipv6"} 3 2 5
122 5 for my $flag (qw(enable_ipv4 enable_ipv6)) {
123 10 ▶ $public_net->{$flag} = _json_bool($public_net->{$flag}) if exists $public_net->{$flag};
Branch: 1 branch
BranchTrueFalseTotal
if exists $public_net->{$flag} 9 1 10
124 }
125 5 ▶ $body->{public_net}{ipv4} = $params{ipv4} if $params{ipv4};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"ipv4"} 0 5 5
126 5 ▶ $body->{public_net}{ipv6} = $params{ipv6} if $params{ipv6};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"ipv6"} 1 4 5
127 }
128  
129 8 my $result = $self->client->post('/servers', $body);
130 return $self->_wrap(
131 $result->{server},
132 action => $self->_wrap_action($result->{action}),
133 8 next_actions => $self->_wrap_actions($result->{next_actions}),
134 );
135 }
136  
137  
138 4 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.0
Pod documented
139 4 my ($self, $id) = @_;
140 4 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 4 4
141  
142 4 my $result = $self->client->delete("/servers/$id");
143 4 return $self->_wrap_action($result->{action});
144 }
145  
146  
147 3 ▶sub power_on {
Subroutine power_on: called CC=2 SCAR=7.1
Pod documented
148 3 my ($self, $id) = @_;
149 3 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
150  
151 return $self->_wrap_action(
152 $self->client->post("/servers/$id/actions/poweron", {})->{action}
153 3 );
154 }
155  
156  
157 3 ▶sub power_off {
Subroutine power_off: called CC=2 SCAR=7.1
Pod documented
158 3 my ($self, $id) = @_;
159 3 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
160  
161 return $self->_wrap_action(
162 $self->client->post("/servers/$id/actions/poweroff", {})->{action}
163 3 );
164 }
165  
166  
167 3 ▶sub reboot {
Subroutine reboot: called CC=2 SCAR=7.1
Pod documented
168 3 my ($self, $id) = @_;
169 3 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
170  
171 return $self->_wrap_action(
172 $self->client->post("/servers/$id/actions/reboot", {})->{action}
173 3 );
174 }
175  
176  
177 3 ▶sub shutdown {
Subroutine shutdown: called CC=2 SCAR=7.1
Pod documented
178 3 my ($self, $id) = @_;
179 3 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
180  
181 return $self->_wrap_action(
182 $self->client->post("/servers/$id/actions/shutdown", {})->{action}
183 3 );
184 }
185  
186  
187 4 ▶sub rebuild {
Subroutine rebuild: called CC=3 SCAR=11.4
Pod documented
188 4 my ($self, $id, $image) = @_;
189 4 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 4 4
190 4 ▶ croak "Image required" unless $image;
Branch: 1 branch
BranchTrueFalseTotal
unless $image 0 4 4
191  
192 4 return $self->_wrap_action_result(
193 $self->client->post("/servers/$id/actions/rebuild", { image => $image })
194 );
195 }
196  
197  
198 3 ▶sub change_type {
Subroutine change_type: called CC=4 SCAR=14.2
Pod documented
199 3 my ($self, $id, $server_type, %opts) = @_;
200 3 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
201 3 ▶ croak "Server type required" unless $server_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $server_type 0 3 3
202  
203 return $self->_wrap_action(
204 $self->client->post("/servers/$id/actions/change_type", {
205 server_type => $server_type,
206 upgrade_disk => _json_bool($opts{upgrade_disk} // 1),
207 })->{action}
208 3 ▶ );
Condition100%2/2
$opts{"upgrade_disk"} // 1
l!l
12
MC/DC100%1/1
$opts{"upgrade_disk"} // 1
$opts{"upgrade_disk"}
Truth table100%2/2
A
A= $opts{"upgrade_disk"}
Aresult
11
00
209 }
210  
211  
212 1 ▶sub reset {
Subroutine reset: called CC=2 SCAR=7.1
Pod documented
213 1 my ($self, $id) = @_;
214 1 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
215  
216 return $self->_wrap_action(
217 $self->client->post("/servers/$id/actions/reset", {})->{action}
218 1 );
219 }
220  
221  
222 3 ▶sub enable_rescue {
Subroutine enable_rescue: called CC=4 SCAR=14.1
Pod documented
223 3 my ($self, $id, %opts) = @_;
224 3 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
225  
226 3 ▶ my $body = { type => $opts{type} // 'linux64' };
Condition100%2/2
$opts{"type"} // "linux64"
l!l
21
MC/DC100%1/1
$opts{"type"} // "linux64"
$opts{"type"}
Truth table100%2/2
A
A= $opts{"type"}
Aresult
11
00
227 3 ▶ $body->{ssh_keys} = $opts{ssh_keys} if $opts{ssh_keys};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"ssh_keys"} 0 3 3
228  
229 3 return $self->_wrap_action_result(
230 $self->client->post("/servers/$id/actions/enable_rescue", $body)
231 );
232 }
233  
234  
235 0 ▶sub disable_rescue {
Subroutine disable_rescue: not called CC=2 SCAR=17.9
Pod documented
236 0 my ($self, $id) = @_;
237 0 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
238  
239 return $self->_wrap_action(
240 $self->client->post("/servers/$id/actions/disable_rescue", {})->{action}
241 0 );
242 }
243  
244  
245 1 ▶sub request_console {
Subroutine request_console: called CC=2 SCAR=7.1
Pod documented
246 1 my ($self, $id) = @_;
247 1 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
248  
249 1 return $self->_wrap_action_result(
250 $self->client->post("/servers/$id/actions/request_console", {})
251 );
252 }
253  
254  
255 1 ▶sub reset_password {
Subroutine reset_password: called CC=2 SCAR=7.1
Pod documented
256 1 my ($self, $id) = @_;
257 1 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
258  
259 1 return $self->_wrap_action_result(
260 $self->client->post("/servers/$id/actions/reset_password", {})
261 );
262 }
263  
264  
265 0 ▶sub attach_iso {
Subroutine attach_iso: not called CC=3 SCAR=24.8
Pod documented
266 0 my ($self, $id, $iso) = @_;
267 0 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
268 0 ▶ croak "ISO required" unless $iso;
Branch: 1 branch
BranchTrueFalseTotal
unless $iso 0 0 0
269  
270 return $self->_wrap_action(
271 $self->client->post("/servers/$id/actions/attach_iso", { iso => $iso })->{action}
272 0 );
273 }
274  
275  
276 0 ▶sub detach_iso {
Subroutine detach_iso: not called CC=2 SCAR=17.9
Pod documented
277 0 my ($self, $id) = @_;
278 0 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
279  
280 return $self->_wrap_action(
281 $self->client->post("/servers/$id/actions/detach_iso", {})->{action}
282 0 );
283 }
284  
285  
286 0 ▶sub enable_backup {
Subroutine enable_backup: not called CC=2 SCAR=17.9
Pod documented
287 0 my ($self, $id) = @_;
288 0 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
289  
290 return $self->_wrap_action(
291 $self->client->post("/servers/$id/actions/enable_backup", {})->{action}
292 0 );
293 }
294  
295  
296 0 ▶sub disable_backup {
Subroutine disable_backup: not called CC=2 SCAR=17.9
Pod documented
297 0 my ($self, $id) = @_;
298 0 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
299  
300 return $self->_wrap_action(
301 $self->client->post("/servers/$id/actions/disable_backup", {})->{action}
302 0 );
303 }
304  
305  
306 1 ▶sub update {
Subroutine update: called CC=4 SCAR=14.3
Pod documented
307 1 my ($self, $id, %params) = @_;
308 1 ▶ croak "Server ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
309  
310 1 my $body = {};
311 1 ▶ $body->{name} = $params{name} if exists $params{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"name"} 1 0 1
312 1 ▶ $body->{labels} = $params{labels} if exists $params{labels};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"labels"} 1 0 1
313  
314 1 my $result = $self->client->put("/servers/$id", $body);
315 1 return $self->_wrap($result->{server});
316 }
317  
318  
319 1 ▶sub wait_for_status {
Subroutine wait_for_status: called CC=4 SCAR=14.2
Pod documented
320 1 my ($self, $id, $status, $timeout) = @_;
321 1 ▶ $timeout //= 120;
Condition50%1/2
$timeout //= 120
l!l
10
MC/DC0%0/1
$timeout //= 120
$timeout
Truth table50%1/2
A
A= $timeout
Aresult
11
00
322  
323 1 my $start = time;
324 1 ▶ while (time - $start < $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $start < $timeout) 3 0 3
325 3 my $server = $self->get($id);
326 3 ▶ return $server if $server->status eq $status;
Branch: 1 branch
BranchTrueFalseTotal
if $server->status eq $status 1 2 3
327 2 sleep 2;
328 }
329  
330 0 croak "Timeout waiting for server $id to reach status '$status'";
331 }
332  
333  
334 60 1;
335  
336 __END__
« lib/WWW/Hetzner/Robot/CLI/Cmd/Rdns.pm ↑ Summary lib/WWW/Hetzner/CLI/Cmd/PrimaryIp/Cmd/List.pm »