| 1 | package WWW::Hetzner::Cloud::API::Volumes; | ||||||||||||||
| 2 | # ABSTRACT: Hetzner Cloud Volumes 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::Volume; | ||||||||||||
|
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 | 11 | ▶ | sub _wrap { | ||||||||||||
|
Subroutine
_wrap: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 11 | my ($self, $data, %extra) = @_; | |||||||||||||
| 23 | 11 | return WWW::Hetzner::Cloud::Volume->new( | |||||||||||||
| 24 | client => $self->client, | ||||||||||||||
| 25 | %$data, | ||||||||||||||
| 26 | %extra, | ||||||||||||||
| 27 | ); | ||||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | 3 | ▶ | sub _wrap_list { | ||||||||||||
|
Subroutine
_wrap_list: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 3 | my ($self, $list) = @_; | |||||||||||||
| 32 | 3 | return [ map { $self->_wrap($_) } @$list ]; | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | |||||||||||||||
| 36 | 3 | ▶ | sub _list_page { | ||||||||||||
|
Subroutine
_list_page: called CC=2 SCAR=7.1
| |||||||||||||||
| 37 | 3 | my ($self, %params) = @_; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 3 | my $result = $self->client->get('/volumes', params => \%params); | |||||||||||||
| 40 | 3 | ▶ | return ($self->_wrap_list($result->{volumes} // []), $result->{meta}); | ||||||||||||
|
Condition50%1/2
$result->{"volumes"} // []
MC/DC0%0/1
$result->{"volumes"} // []
$result->{"volumes"}
Truth table50%1/2
A
A= $result->{"volumes"}
| |||||||||||||||
| 41 | } | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 1 | ▶ | sub list { | ||||||||||||
|
Subroutine
list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 44 | 1 | my ($self, %params) = @_; | |||||||||||||
| 45 | |||||||||||||||
| 46 | 1 | my ($entities) = $self->_list_page(%params); | |||||||||||||
| 47 | 1 | return $entities; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | |||||||||||||||
| 51 | |||||||||||||||
| 52 | 3 | ▶ | sub get { | ||||||||||||
|
Subroutine
get: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 53 | 3 | my ($self, $id) = @_; | |||||||||||||
| 54 | 3 | ▶ | croak "Volume ID required" unless $id; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 3 | my $result = $self->client->get("/volumes/$id"); | |||||||||||||
| 57 | 3 | return $self->_wrap($result->{volume}); | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | |||||||||||||||
| 61 | 5 | ▶ | sub create { | ||||||||||||
|
Subroutine
create: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||
| 62 | 5 | my ($self, %params) = @_; | |||||||||||||
| 63 | |||||||||||||||
| 64 | 5 | ▶ | croak "name required" unless $params{name}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | 4 | ▶ | croak "size required" unless $params{size}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 3 | ▶ | croak "location required" unless $params{location}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | |||||||||||||||
| 68 | my $body = { | ||||||||||||||
| 69 | name => $params{name}, | ||||||||||||||
| 70 | size => $params{size}, | ||||||||||||||
| 71 | location => $params{location}, | ||||||||||||||
| 72 | 2 | }; | |||||||||||||
| 73 | |||||||||||||||
| 74 | 2 | ▶ | $body->{format} = $params{format} if $params{format}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | 2 | ▶ | $body->{labels} = $params{labels} if $params{labels}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 2 | ▶ | $body->{automount} = $params{automount} if exists $params{automount}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 2 | ▶ | $body->{server} = $params{server} if $params{server}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | |||||||||||||||
| 79 | 2 | my $result = $self->client->post('/volumes', $body); | |||||||||||||
| 80 | return $self->_wrap( | ||||||||||||||
| 81 | $result->{volume}, | ||||||||||||||
| 82 | action => $self->_wrap_action($result->{action}), | ||||||||||||||
| 83 | 2 | next_actions => $self->_wrap_actions($result->{next_actions}), | |||||||||||||
| 84 | ); | ||||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | |||||||||||||||
| 88 | 2 | ▶ | sub delete { | ||||||||||||
|
Subroutine
delete: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||
| 89 | 2 | my ($self, $id) = @_; | |||||||||||||
| 90 | 2 | ▶ | croak "Volume ID required" unless $id; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | |||||||||||||||
| 92 | 2 | return $self->client->delete("/volumes/$id"); | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | |||||||||||||||
| 96 | 3 | ▶ | sub attach { | ||||||||||||
|
Subroutine
attach: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||
| 97 | 3 | my ($self, $id, $server_id, %opts) = @_; | |||||||||||||
| 98 | 3 | ▶ | croak "Volume ID required" unless $id; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 3 | ▶ | croak "Server ID required" unless $server_id; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | |||||||||||||||
| 101 | 3 | my $body = { server => $server_id }; | |||||||||||||
| 102 | 3 | ▶ | $body->{automount} = $opts{automount} ? \1 : \0 if exists $opts{automount}; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 103 | |||||||||||||||
| 104 | return $self->_wrap_action( | ||||||||||||||
| 105 | $self->client->post("/volumes/$id/actions/attach", $body)->{action} | ||||||||||||||
| 106 | 3 | ); | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | |||||||||||||||
| 110 | 3 | ▶ | sub detach { | ||||||||||||
|
Subroutine
detach: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||
| 111 | 3 | my ($self, $id) = @_; | |||||||||||||
| 112 | 3 | ▶ | croak "Volume ID required" unless $id; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | |||||||||||||||
| 114 | return $self->_wrap_action( | ||||||||||||||
| 115 | $self->client->post("/volumes/$id/actions/detach", {})->{action} | ||||||||||||||
| 116 | 3 | ); | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | |||||||||||||||
| 120 | 3 | ▶ | sub resize { | ||||||||||||
|
Subroutine
resize: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||
| 121 | 3 | my ($self, $id, $size) = @_; | |||||||||||||
| 122 | 3 | ▶ | croak "Volume ID required" unless $id; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 3 | ▶ | croak "Size required" unless $size; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | |||||||||||||||
| 125 | return $self->_wrap_action( | ||||||||||||||
| 126 | $self->client->post("/volumes/$id/actions/resize", { size => $size })->{action} | ||||||||||||||
| 127 | 3 | ); | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | |||||||||||||||
| 131 | 56 | 1; | |||||||||||||
| 132 | |||||||||||||||
| 133 | __END__ | ||||||||||||||