File Coverage

blib/lib/WWW/Hetzner/Cloud/FloatingIP.pm
Criterion Covered Total %
statement 13 44 29.5
branch 0 18 0.0
condition 0 2 0.0
subroutine 6 12 50.0
pod 9 9 100.0
total 28 85 32.9


line stmt bran cond sub pod time code
1             package WWW::Hetzner::Cloud::FloatingIP;
2             # ABSTRACT: Hetzner Cloud Floating IP object
3              
4             our $VERSION = '0.100';
5              
6 25     25   211 use Moo;
  25         56  
  25         151  
7 25     25   9298 use Carp qw(croak);
  25         57  
  25         1474  
8 25     25   136 use namespace::clean;
  25         67  
  25         196  
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 description => ( is => 'rw' );
25              
26              
27             has ip => ( is => 'ro' );
28              
29              
30             has type => ( is => 'ro' );
31              
32              
33             has server => ( is => 'ro' );
34              
35              
36             has dns_ptr => ( is => 'ro', default => sub { [] } );
37              
38              
39             has home_location => ( is => 'ro', default => sub { {} } );
40              
41              
42             has blocked => ( is => 'ro' );
43              
44              
45             has labels => ( is => 'rw', default => sub { {} } );
46              
47              
48             has protection => ( is => 'ro', default => sub { {} } );
49              
50              
51             has created => ( is => 'ro' );
52              
53              
54             # Convenience
55 2     2 1 15313 sub is_assigned { defined shift->server }
56              
57              
58 1     1 1 10 sub location { shift->home_location->{name} }
59              
60              
61             # Actions
62             sub update {
63 0     0 1 0 my ($self) = @_;
64 0 0       0 croak "Cannot update floating IP without ID" unless $self->id;
65              
66 0         0 my $result = $self->_client->put("/floating_ips/" . $self->id, {
67             name => $self->name,
68             description => $self->description,
69             labels => $self->labels,
70             });
71 0         0 return $self;
72             }
73              
74              
75             sub delete {
76 0     0 1 0 my ($self) = @_;
77 0 0       0 croak "Cannot delete floating IP without ID" unless $self->id;
78              
79 0         0 $self->_client->delete("/floating_ips/" . $self->id);
80 0         0 return 1;
81             }
82              
83              
84             sub assign {
85 0     0 1 0 my ($self, $server_id) = @_;
86 0 0       0 croak "Cannot assign floating IP without ID" unless $self->id;
87 0 0       0 croak "Server ID required" unless $server_id;
88              
89 0         0 $self->_client->post("/floating_ips/" . $self->id . "/actions/assign", {
90             server => $server_id,
91             });
92 0         0 return $self;
93             }
94              
95              
96             sub unassign {
97 0     0 1 0 my ($self) = @_;
98 0 0       0 croak "Cannot unassign floating IP without ID" unless $self->id;
99              
100 0         0 $self->_client->post("/floating_ips/" . $self->id . "/actions/unassign", {});
101 0         0 return $self;
102             }
103              
104              
105             sub change_dns_ptr {
106 0     0 1 0 my ($self, $ip, $dns_ptr) = @_;
107 0 0       0 croak "Cannot modify floating IP without ID" unless $self->id;
108 0 0       0 croak "IP required" unless $ip;
109 0 0       0 croak "dns_ptr required" unless defined $dns_ptr;
110              
111 0         0 $self->_client->post("/floating_ips/" . $self->id . "/actions/change_dns_ptr", {
112             ip => $ip,
113             dns_ptr => $dns_ptr,
114             });
115 0         0 return $self;
116             }
117              
118              
119             sub refresh {
120 0     0 1 0 my ($self) = @_;
121 0 0       0 croak "Cannot refresh floating IP without ID" unless $self->id;
122              
123 0         0 my $result = $self->_client->get("/floating_ips/" . $self->id);
124 0         0 my $data = $result->{floating_ip};
125              
126 0         0 $self->name($data->{name});
127 0         0 $self->description($data->{description});
128 0   0     0 $self->labels($data->{labels} // {});
129              
130 0         0 return $self;
131             }
132              
133              
134             sub data {
135 1     1 1 41 my ($self) = @_;
136             return {
137 1         23 id => $self->id,
138             name => $self->name,
139             description => $self->description,
140             ip => $self->ip,
141             type => $self->type,
142             server => $self->server,
143             dns_ptr => $self->dns_ptr,
144             home_location => $self->home_location,
145             blocked => $self->blocked,
146             labels => $self->labels,
147             protection => $self->protection,
148             created => $self->created,
149             };
150             }
151              
152              
153              
154             1.
155              
156             __END__