File Coverage

blib/lib/WWW/Hetzner/Cloud/Volume.pm
Criterion Covered Total %
statement 13 45 28.8
branch 0 20 0.0
condition 0 2 0.0
subroutine 6 12 50.0
pod 9 9 100.0
total 28 88 31.8


line stmt bran cond sub pod time code
1             package WWW::Hetzner::Cloud::Volume;
2             # ABSTRACT: Hetzner Cloud Volume object
3              
4             our $VERSION = '0.100';
5              
6 25     25   184 use Moo;
  25         50  
  25         166  
7 25     25   9438 use Carp qw(croak);
  25         49  
  25         1564  
8 25     25   144 use namespace::clean;
  25         62  
  25         159  
9              
10              
11             has _client => (
12             is => 'ro',
13             required => 1,
14             weak_ref => 1,
15             init_arg => 'client',
16             );
17              
18             has id => ( is => 'ro' );
19              
20              
21             has name => ( is => 'rw' );
22              
23              
24             has status => ( is => 'rwp' );
25              
26              
27             has size => ( is => 'ro' );
28              
29              
30             has server => ( is => 'ro' );
31              
32              
33             has created => ( is => 'ro' );
34              
35              
36             has labels => ( is => 'rw', default => sub { {} } );
37              
38              
39             has linux_device => ( is => 'ro' );
40              
41              
42             has format => ( is => 'ro' );
43              
44              
45             has protection => ( is => 'ro', default => sub { {} } );
46              
47              
48             # Nested data
49             has location_data => ( is => 'ro', init_arg => 'location', default => sub { {} } );
50              
51             # Convenience accessors
52 1     1 1 8769 sub location { shift->location_data->{name} }
53              
54              
55 2     2 1 18 sub is_attached { defined shift->server }
56              
57              
58             # Actions
59             sub update {
60 0     0 1 0 my ($self) = @_;
61 0 0       0 croak "Cannot update volume without ID" unless $self->id;
62              
63 0         0 my $result = $self->_client->put("/volumes/" . $self->id, {
64             name => $self->name,
65             labels => $self->labels,
66             });
67 0         0 return $self;
68             }
69              
70              
71             sub delete {
72 0     0 1 0 my ($self) = @_;
73 0 0       0 croak "Cannot delete volume without ID" unless $self->id;
74              
75 0         0 $self->_client->delete("/volumes/" . $self->id);
76 0         0 return 1;
77             }
78              
79              
80             sub attach {
81 0     0 1 0 my ($self, $server_id, %opts) = @_;
82 0 0       0 croak "Cannot attach volume without ID" unless $self->id;
83 0 0       0 croak "Server ID required" unless $server_id;
84              
85 0         0 my $body = { server => $server_id };
86 0 0       0 $body->{automount} = $opts{automount} ? \1 : \0 if exists $opts{automount};
    0          
87              
88 0         0 $self->_client->post("/volumes/" . $self->id . "/actions/attach", $body);
89 0         0 return $self;
90             }
91              
92              
93             sub detach {
94 0     0 1 0 my ($self) = @_;
95 0 0       0 croak "Cannot detach volume without ID" unless $self->id;
96              
97 0         0 $self->_client->post("/volumes/" . $self->id . "/actions/detach", {});
98 0         0 return $self;
99             }
100              
101              
102             sub resize {
103 0     0 1 0 my ($self, $size) = @_;
104 0 0       0 croak "Cannot resize volume without ID" unless $self->id;
105 0 0       0 croak "Size required" unless $size;
106              
107 0         0 $self->_client->post("/volumes/" . $self->id . "/actions/resize", { size => $size });
108 0         0 return $self;
109             }
110              
111              
112             sub refresh {
113 0     0 1 0 my ($self) = @_;
114 0 0       0 croak "Cannot refresh volume without ID" unless $self->id;
115              
116 0         0 my $result = $self->_client->get("/volumes/" . $self->id);
117 0         0 my $data = $result->{volume};
118              
119 0         0 $self->_set_status($data->{status});
120 0         0 $self->name($data->{name});
121 0   0     0 $self->labels($data->{labels} // {});
122              
123 0         0 return $self;
124             }
125              
126              
127             sub data {
128 1     1 1 75 my ($self) = @_;
129             return {
130 1         31 id => $self->id,
131             name => $self->name,
132             status => $self->status,
133             size => $self->size,
134             server => $self->server,
135             created => $self->created,
136             labels => $self->labels,
137             linux_device => $self->linux_device,
138             format => $self->format,
139             protection => $self->protection,
140             location => $self->location_data,
141             };
142             }
143              
144              
145              
146             1.
147              
148             __END__