File Coverage

blib/lib/WWW/Hetzner/Cloud/LoadBalancer.pm
Criterion Covered Total %
statement 12 64 18.7
branch 0 36 0.0
condition 0 2 0.0
subroutine 6 17 35.2
pod 14 14 100.0
total 32 133 24.0


line stmt bran cond sub pod time code
1             package WWW::Hetzner::Cloud::LoadBalancer;
2             # ABSTRACT: Hetzner Cloud Load Balancer object
3              
4             our $VERSION = '0.100';
5              
6 25     25   175 use Moo;
  25         54  
  25         177  
7 25     25   9950 use Carp qw(croak);
  25         101  
  25         1740  
8 25     25   164 use namespace::clean;
  25         51  
  25         214  
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 public_net => ( is => 'ro', default => sub { {} } );
25              
26              
27             has private_net => ( is => 'ro', default => sub { [] } );
28              
29              
30             has location => ( is => 'ro', default => sub { {} } );
31              
32              
33             has load_balancer_type => ( is => 'ro', default => sub { {} } );
34              
35              
36             has protection => ( is => 'ro', default => sub { {} } );
37              
38              
39             has labels => ( is => 'rw', default => sub { {} } );
40              
41              
42             has targets => ( is => 'ro', default => sub { [] } );
43              
44              
45             has services => ( is => 'ro', default => sub { [] } );
46              
47              
48             has algorithm => ( is => 'ro', default => sub { {} } );
49              
50              
51             has created => ( is => 'ro' );
52              
53              
54             has outgoing_traffic => ( is => 'ro' );
55              
56              
57             has ingoing_traffic => ( is => 'ro' );
58              
59              
60             has included_traffic => ( is => 'ro' );
61              
62              
63             # Convenience
64 1     1 1 7 sub location_name { shift->location->{name} }
65              
66              
67 1     1 1 9 sub type_name { shift->load_balancer_type->{name} }
68              
69              
70 1     1 1 5531 sub ipv4 { shift->public_net->{ipv4}{ip} }
71              
72              
73 0     0 1   sub ipv6 { shift->public_net->{ipv6}{ip} }
74              
75              
76             # Actions
77             sub update {
78 0     0 1   my ($self) = @_;
79 0 0         croak "Cannot update load balancer without ID" unless $self->id;
80              
81 0           my $result = $self->_client->put("/load_balancers/" . $self->id, {
82             name => $self->name,
83             labels => $self->labels,
84             });
85 0           return $self;
86             }
87              
88              
89             sub delete {
90 0     0 1   my ($self) = @_;
91 0 0         croak "Cannot delete load balancer without ID" unless $self->id;
92              
93 0           $self->_client->delete("/load_balancers/" . $self->id);
94 0           return 1;
95             }
96              
97              
98             sub add_target {
99 0     0 1   my ($self, %opts) = @_;
100 0 0         croak "Cannot modify load balancer without ID" unless $self->id;
101 0 0         croak "type required" unless $opts{type};
102              
103 0           $self->_client->post("/load_balancers/" . $self->id . "/actions/add_target", \%opts);
104 0           return $self;
105             }
106              
107              
108             sub remove_target {
109 0     0 1   my ($self, %opts) = @_;
110 0 0         croak "Cannot modify load balancer without ID" unless $self->id;
111 0 0         croak "type required" unless $opts{type};
112              
113 0           $self->_client->post("/load_balancers/" . $self->id . "/actions/remove_target", \%opts);
114 0           return $self;
115             }
116              
117              
118             sub add_service {
119 0     0 1   my ($self, %opts) = @_;
120 0 0         croak "Cannot modify load balancer without ID" unless $self->id;
121 0 0         croak "protocol required" unless $opts{protocol};
122 0 0         croak "listen_port required" unless $opts{listen_port};
123 0 0         croak "destination_port required" unless $opts{destination_port};
124              
125 0           $self->_client->post("/load_balancers/" . $self->id . "/actions/add_service", \%opts);
126 0           return $self;
127             }
128              
129              
130             sub delete_service {
131 0     0 1   my ($self, $listen_port) = @_;
132 0 0         croak "Cannot modify load balancer without ID" unless $self->id;
133 0 0         croak "listen_port required" unless $listen_port;
134              
135 0           $self->_client->post("/load_balancers/" . $self->id . "/actions/delete_service", {
136             listen_port => $listen_port,
137             });
138 0           return $self;
139             }
140              
141              
142             sub attach_to_network {
143 0     0 1   my ($self, $network_id, %opts) = @_;
144 0 0         croak "Cannot modify load balancer without ID" unless $self->id;
145 0 0         croak "network required" unless $network_id;
146              
147 0           my $body = { network => $network_id };
148 0 0         $body->{ip} = $opts{ip} if $opts{ip};
149              
150 0           $self->_client->post("/load_balancers/" . $self->id . "/actions/attach_to_network", $body);
151 0           return $self;
152             }
153              
154              
155             sub detach_from_network {
156 0     0 1   my ($self, $network_id) = @_;
157 0 0         croak "Cannot modify load balancer without ID" unless $self->id;
158 0 0         croak "network required" unless $network_id;
159              
160 0           $self->_client->post("/load_balancers/" . $self->id . "/actions/detach_from_network", {
161             network => $network_id,
162             });
163 0           return $self;
164             }
165              
166              
167             sub refresh {
168 0     0 1   my ($self) = @_;
169 0 0         croak "Cannot refresh load balancer without ID" unless $self->id;
170              
171 0           my $result = $self->_client->get("/load_balancers/" . $self->id);
172 0           my $data = $result->{load_balancer};
173              
174 0           $self->name($data->{name});
175 0   0       $self->labels($data->{labels} // {});
176              
177 0           return $self;
178             }
179              
180              
181             sub data {
182 0     0 1   my ($self) = @_;
183             return {
184 0           id => $self->id,
185             name => $self->name,
186             public_net => $self->public_net,
187             private_net => $self->private_net,
188             location => $self->location,
189             load_balancer_type => $self->load_balancer_type,
190             protection => $self->protection,
191             labels => $self->labels,
192             targets => $self->targets,
193             services => $self->services,
194             algorithm => $self->algorithm,
195             created => $self->created,
196             };
197             }
198              
199              
200              
201             1.
202              
203             __END__