File Coverage

blib/lib/WWW/Hetzner/Cloud/API/PrimaryIPs.pm
Criterion Covered Total %
statement 46 59 77.9
branch 16 38 42.1
condition 2 4 50.0
subroutine 12 14 85.7
pod 8 8 100.0
total 84 123 68.2


line stmt bran cond sub pod time code
1             package WWW::Hetzner::Cloud::API::PrimaryIPs;
2             # ABSTRACT: Hetzner Cloud Primary IPs API
3              
4             our $VERSION = '0.100';
5              
6 25     25   160 use Moo;
  25         60  
  25         226  
7 25     25   9810 use Carp qw(croak);
  25         82  
  25         1742  
8 25     25   14324 use WWW::Hetzner::Cloud::PrimaryIP;
  25         105  
  25         1628  
9 25     25   215 use namespace::clean;
  25         52  
  25         139  
10              
11              
12             has client => (
13             is => 'ro',
14             required => 1,
15             weak_ref => 1,
16             );
17              
18             sub _wrap {
19 4     4   12 my ($self, $data) = @_;
20 4         135 return WWW::Hetzner::Cloud::PrimaryIP->new(
21             client => $self->client,
22             %$data,
23             );
24             }
25              
26             sub _wrap_list {
27 1     1   2 my ($self, $list) = @_;
28 1         2 return [ map { $self->_wrap($_) } @$list ];
  1         3  
29             }
30              
31              
32             sub list {
33 1     1 1 949 my ($self, %params) = @_;
34              
35 1         10 my $result = $self->client->get('/primary_ips', params => \%params);
36 1   50     7 return $self->_wrap_list($result->{primary_ips} // []);
37             }
38              
39              
40             sub get {
41 2     2 1 69 my ($self, $id) = @_;
42 2 50       12 croak "Primary IP ID required" unless $id;
43              
44 2         21 my $result = $self->client->get("/primary_ips/$id");
45 2         14 return $self->_wrap($result->{primary_ip});
46             }
47              
48              
49             sub create {
50 5     5 1 3036 my ($self, %params) = @_;
51              
52 5 100       259 croak "name required" unless $params{name};
53 4 100       172 croak "type required (ipv4 or ipv6)" unless $params{type};
54 3 100       201 croak "assignee_type required" unless $params{assignee_type};
55 2 100       174 croak "datacenter required" unless $params{datacenter};
56              
57             my $body = {
58             name => $params{name},
59             type => $params{type},
60             assignee_type => $params{assignee_type},
61             datacenter => $params{datacenter},
62 1         7 };
63              
64 1 50       4 $body->{assignee_id} = $params{assignee_id} if $params{assignee_id};
65 1 50       5 $body->{auto_delete} = $params{auto_delete} if exists $params{auto_delete};
66 1 50       5 $body->{labels} = $params{labels} if $params{labels};
67              
68 1         11 my $result = $self->client->post('/primary_ips', $body);
69 1         8 return $self->_wrap($result->{primary_ip});
70             }
71              
72              
73             sub update {
74 0     0 1 0 my ($self, $id, %params) = @_;
75 0 0       0 croak "Primary IP ID required" unless $id;
76              
77 0         0 my $body = {};
78 0 0       0 $body->{name} = $params{name} if exists $params{name};
79 0 0       0 $body->{auto_delete} = $params{auto_delete} if exists $params{auto_delete};
80 0 0       0 $body->{labels} = $params{labels} if exists $params{labels};
81              
82 0         0 my $result = $self->client->put("/primary_ips/$id", $body);
83 0         0 return $self->_wrap($result->{primary_ip});
84             }
85              
86              
87             sub delete {
88 1     1 1 62 my ($self, $id) = @_;
89 1 50       6 croak "Primary IP ID required" unless $id;
90              
91 1         12 return $self->client->delete("/primary_ips/$id");
92             }
93              
94              
95             sub assign {
96 1     1 1 29 my ($self, $id, $assignee_id, $assignee_type) = @_;
97 1 50       6 croak "Primary IP ID required" unless $id;
98 1 50       8 croak "Assignee ID required" unless $assignee_id;
99 1   50     3 $assignee_type //= 'server';
100              
101 1         13 return $self->client->post("/primary_ips/$id/actions/assign", {
102             assignee_id => $assignee_id,
103             assignee_type => $assignee_type,
104             });
105             }
106              
107              
108             sub unassign {
109 1     1 1 35 my ($self, $id) = @_;
110 1 50       5 croak "Primary IP ID required" unless $id;
111              
112 1         9 return $self->client->post("/primary_ips/$id/actions/unassign", {});
113             }
114              
115              
116             sub change_dns_ptr {
117 0     0 1   my ($self, $id, $ip, $dns_ptr) = @_;
118 0 0         croak "Primary IP ID required" unless $id;
119 0 0         croak "IP required" unless $ip;
120 0 0         croak "dns_ptr required" unless defined $dns_ptr;
121              
122 0           return $self->client->post("/primary_ips/$id/actions/change_dns_ptr", {
123             ip => $ip,
124             dns_ptr => $dns_ptr,
125             });
126             }
127              
128              
129             1;
130              
131             __END__