lib/WWW/Hetzner/Cloud/API/Networks.pm

filter: Statementstmt 90.5% 67 / 74 Branchbran 47.9% 23 / 48 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 93.7% 15 / 16 Podpod 100% 9 / 9 totaltotal 76.6% 115 / 150 CC 26 SCAR 17.5
CC
26
Cov
73%
update
20.0
create
7.3
add_subnet
6.8
CRAP 5.7
« lib/WWW/Hetzner/Cloud/API/Certificates.pm ↑ Summary lib/WWW/Hetzner/CLI/Cmd/Record/Cmd/List.pm »
1 package WWW::Hetzner::Cloud::API::Networks;
2 # ABSTRACT: Hetzner Cloud Networks API
3  
4 60 our $VERSION = '0.102';
5  
6 60 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 60 with 'WWW::Hetzner::Role::Pagination';
8 60 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 60 ▶use WWW::Hetzner::Cloud::Network;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 60 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 60 has client => (
14 is => 'ro',
15 required => 1,
16 weak_ref => 1,
17 );
18  
19 60 with 'WWW::Hetzner::Role::HasActions';
20  
21 7 ▶sub _wrap {
Subroutine _wrap: called CC=1 SCAR=0.0
22 7 my ($self, $data) = @_;
23 7 return WWW::Hetzner::Cloud::Network->new(
24 client => $self->client,
25 %$data,
26 );
27 }
28  
29 2 ▶sub _wrap_list {
Subroutine _wrap_list: called CC=1 SCAR=0.0
30 2 my ($self, $list) = @_;
31 2 return [ map { $self->_wrap($_) } @$list ];
32 }
33  
34  
35 2 ▶sub _list_page {
Subroutine _list_page: called CC=2 SCAR=7.1
36 2 my ($self, %params) = @_;
37  
38 2 my $result = $self->client->get('/networks', params => \%params);
39 2 ▶ return ($self->_wrap_list($result->{networks} // []), $result->{meta});
Condition50%1/2
$result->{"networks"} // []
l!l
20
MC/DC0%0/1
$result->{"networks"} // []
$result->{"networks"}
Truth table50%1/2
A
A= $result->{"networks"}
Aresult
11
00
40 }
41  
42 1 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
43 1 my ($self, %params) = @_;
44  
45 1 my ($entities) = $self->_list_page(%params);
46 1 return $entities;
47 }
48  
49  
50  
51 3 ▶sub get {
Subroutine get: called CC=2 SCAR=7.0
Pod documented
52 3 my ($self, $id) = @_;
53 3 ▶ croak "Network ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
54  
55 3 my $result = $self->client->get("/networks/$id");
56 3 return $self->_wrap($result->{network});
57 }
58  
59  
60 4 ▶sub create {
Subroutine create: called CC=7 SCAR=19.9
Pod documented
61 4 my ($self, %params) = @_;
62  
63 4 ▶ croak "name required" unless $params{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"name"} 1 3 4
64 3 ▶ croak "ip_range required" unless $params{ip_range};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"ip_range"} 1 2 3
65  
66 my $body = {
67 name => $params{name},
68 ip_range => $params{ip_range},
69 2 };
70  
71 2 ▶ $body->{labels} = $params{labels} if $params{labels};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"labels"} 0 2 2
72 2 ▶ $body->{subnets} = $params{subnets} if $params{subnets};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"subnets"} 0 2 2
73 2 ▶ $body->{routes} = $params{routes} if $params{routes};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"routes"} 0 2 2
74 $body->{expose_routes_to_vswitch} = $params{expose_routes_to_vswitch}
75 2 ▶ if exists $params{expose_routes_to_vswitch};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"expose_routes_to_vswitch"} 0 2 2
76  
77 2 my $result = $self->client->post('/networks', $body);
78 2 return $self->_wrap($result->{network});
79 }
80  
81  
82 0 ▶sub update {
Subroutine update: not called CC=4 SCAR=30.0
Pod documented
83 0 my ($self, $id, %params) = @_;
84 0 ▶ croak "Network ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
85  
86 0 my $body = {};
87 0 ▶ $body->{name} = $params{name} if exists $params{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"name"} 0 0 0
88 0 ▶ $body->{labels} = $params{labels} if exists $params{labels};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"labels"} 0 0 0
89  
90 0 my $result = $self->client->put("/networks/$id", $body);
91 0 return $self->_wrap($result->{network});
92 }
93  
94  
95 2 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.1
Pod documented
96 2 my ($self, $id) = @_;
97 2 ▶ croak "Network ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 2 2
98  
99 2 return $self->client->delete("/networks/$id");
100 }
101  
102  
103 3 ▶sub add_subnet {
Subroutine add_subnet: called CC=6 SCAR=19.1
Pod documented
104 3 my ($self, $id, %opts) = @_;
105 3 ▶ croak "Network ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
106 3 ▶ croak "ip_range required" unless $opts{ip_range};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"ip_range"} 0 3 3
107 3 ▶ croak "network_zone required" unless $opts{network_zone};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"network_zone"} 0 3 3
108 3 ▶ croak "type required" unless $opts{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"type"} 0 3 3
109  
110 my $body = {
111 ip_range => $opts{ip_range},
112 network_zone => $opts{network_zone},
113 type => $opts{type},
114 3 };
115 3 ▶ $body->{vswitch_id} = $opts{vswitch_id} if $opts{vswitch_id};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"vswitch_id"} 0 3 3
116  
117 return $self->_wrap_action(
118 $self->client->post("/networks/$id/actions/add_subnet", $body)->{action}
119 3 );
120 }
121  
122  
123 2 ▶sub delete_subnet {
Subroutine delete_subnet: called CC=3 SCAR=11.4
Pod documented
124 2 my ($self, $id, $ip_range) = @_;
125 2 ▶ croak "Network ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 2 2
126 2 ▶ croak "ip_range required" unless $ip_range;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_range 0 2 2
127  
128 return $self->_wrap_action(
129 $self->client->post("/networks/$id/actions/delete_subnet", {
130 ip_range => $ip_range,
131 })->{action}
132 2 );
133 }
134  
135  
136 3 ▶sub add_route {
Subroutine add_route: called CC=4 SCAR=14.6
Pod documented
137 3 my ($self, $id, %opts) = @_;
138 3 ▶ croak "Network ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
139 3 ▶ croak "destination required" unless $opts{destination};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"destination"} 0 3 3
140 3 ▶ croak "gateway required" unless $opts{gateway};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"gateway"} 0 3 3
141  
142 return $self->_wrap_action(
143 $self->client->post("/networks/$id/actions/add_route", {
144 destination => $opts{destination},
145 gateway => $opts{gateway},
146 })->{action}
147 3 );
148 }
149  
150  
151 2 ▶sub delete_route {
Subroutine delete_route: called CC=4 SCAR=14.6
Pod documented
152 2 my ($self, $id, %opts) = @_;
153 2 ▶ croak "Network ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 2 2
154 2 ▶ croak "destination required" unless $opts{destination};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"destination"} 0 2 2
155 2 ▶ croak "gateway required" unless $opts{gateway};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"gateway"} 0 2 2
156  
157 return $self->_wrap_action(
158 $self->client->post("/networks/$id/actions/delete_route", {
159 destination => $opts{destination},
160 gateway => $opts{gateway},
161 })->{action}
162 2 );
163 }
164  
165  
166 60 1;
167  
168 __END__
« lib/WWW/Hetzner/Cloud/API/Certificates.pm ↑ Summary lib/WWW/Hetzner/CLI/Cmd/Record/Cmd/List.pm »