File Coverage

blib/lib/WWW/Hetzner/Cloud/Network.pm
Criterion Covered Total %
statement 11 52 21.1
branch 0 32 0.0
condition 0 2 0.0
subroutine 4 11 36.3
pod 8 8 100.0
total 23 105 21.9


line stmt bran cond sub pod time code
1             package WWW::Hetzner::Cloud::Network;
2             # ABSTRACT: Hetzner Cloud Network object
3              
4             our $VERSION = '0.100';
5              
6 25     25   160 use Moo;
  25         46  
  25         163  
7 25     25   9359 use Carp qw(croak);
  25         44  
  25         1536  
8 25     25   127 use namespace::clean;
  25         47  
  25         152  
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 ip_range => ( is => 'ro' );
25              
26              
27             has subnets => ( is => 'ro', default => sub { [] } );
28              
29              
30             has routes => ( is => 'ro', default => sub { [] } );
31              
32              
33             has servers => ( is => 'ro', default => sub { [] } );
34              
35              
36             has labels => ( is => 'rw', default => sub { {} } );
37              
38              
39             has protection => ( is => 'ro', default => sub { {} } );
40              
41              
42             has created => ( is => 'ro' );
43              
44              
45             has load_balancers => ( is => 'ro', default => sub { [] } );
46              
47              
48             has expose_routes_to_vswitch => ( is => 'ro', default => 0 );
49              
50              
51             # Actions
52             sub update {
53 0     0 1 0 my ($self) = @_;
54 0 0       0 croak "Cannot update network without ID" unless $self->id;
55              
56 0         0 my $result = $self->_client->put("/networks/" . $self->id, {
57             name => $self->name,
58             labels => $self->labels,
59             });
60 0         0 return $self;
61             }
62              
63              
64             sub delete {
65 0     0 1 0 my ($self) = @_;
66 0 0       0 croak "Cannot delete network without ID" unless $self->id;
67              
68 0         0 $self->_client->delete("/networks/" . $self->id);
69 0         0 return 1;
70             }
71              
72              
73             sub add_subnet {
74 0     0 1 0 my ($self, %opts) = @_;
75 0 0       0 croak "Cannot modify network without ID" unless $self->id;
76 0 0       0 croak "ip_range required" unless $opts{ip_range};
77 0 0       0 croak "network_zone required" unless $opts{network_zone};
78 0 0       0 croak "type required" unless $opts{type};
79              
80             my $body = {
81             ip_range => $opts{ip_range},
82             network_zone => $opts{network_zone},
83             type => $opts{type},
84 0         0 };
85 0 0       0 $body->{vswitch_id} = $opts{vswitch_id} if $opts{vswitch_id};
86              
87 0         0 $self->_client->post("/networks/" . $self->id . "/actions/add_subnet", $body);
88 0         0 return $self;
89             }
90              
91              
92             sub delete_subnet {
93 0     0 1 0 my ($self, $ip_range) = @_;
94 0 0       0 croak "Cannot modify network without ID" unless $self->id;
95 0 0       0 croak "ip_range required" unless $ip_range;
96              
97 0         0 $self->_client->post("/networks/" . $self->id . "/actions/delete_subnet", {
98             ip_range => $ip_range,
99             });
100 0         0 return $self;
101             }
102              
103              
104             sub add_route {
105 0     0 1 0 my ($self, %opts) = @_;
106 0 0       0 croak "Cannot modify network without ID" unless $self->id;
107 0 0       0 croak "destination required" unless $opts{destination};
108 0 0       0 croak "gateway required" unless $opts{gateway};
109              
110             $self->_client->post("/networks/" . $self->id . "/actions/add_route", {
111             destination => $opts{destination},
112             gateway => $opts{gateway},
113 0         0 });
114 0         0 return $self;
115             }
116              
117              
118             sub delete_route {
119 0     0 1 0 my ($self, %opts) = @_;
120 0 0       0 croak "Cannot modify network without ID" unless $self->id;
121 0 0       0 croak "destination required" unless $opts{destination};
122 0 0       0 croak "gateway required" unless $opts{gateway};
123              
124             $self->_client->post("/networks/" . $self->id . "/actions/delete_route", {
125             destination => $opts{destination},
126             gateway => $opts{gateway},
127 0         0 });
128 0         0 return $self;
129             }
130              
131              
132             sub refresh {
133 0     0 1 0 my ($self) = @_;
134 0 0       0 croak "Cannot refresh network without ID" unless $self->id;
135              
136 0         0 my $result = $self->_client->get("/networks/" . $self->id);
137 0         0 my $data = $result->{network};
138              
139 0         0 $self->name($data->{name});
140 0   0     0 $self->labels($data->{labels} // {});
141              
142 0         0 return $self;
143             }
144              
145              
146             sub data {
147 1     1 1 48 my ($self) = @_;
148             return {
149 1         22 id => $self->id,
150             name => $self->name,
151             ip_range => $self->ip_range,
152             subnets => $self->subnets,
153             routes => $self->routes,
154             servers => $self->servers,
155             labels => $self->labels,
156             protection => $self->protection,
157             created => $self->created,
158             };
159             }
160              
161              
162              
163             1;
164              
165             __END__