File Coverage

blib/lib/WWW/Hetzner/Cloud/PrimaryIP.pm
Criterion Covered Total %
statement 13 45 28.8
branch 0 18 0.0
condition 0 4 0.0
subroutine 6 12 50.0
pod 9 9 100.0
total 28 88 31.8


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