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

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