File Coverage

lib/Webservice/OVH/Cloud/Project/Network/Private/Subnet.pm
Criterion Covered Total %
statement 12 84 14.2
branch 0 38 0.0
condition 0 6 0.0
subroutine 4 15 26.6
pod 7 8 87.5
total 23 151 15.2


line stmt bran cond sub pod time code
1              
2             =encoding utf-8
3              
4             =head1 NAME
5              
6             Webservice::OVH::Cloud::Project::Network::Private::Subnet
7              
8             =head1 SYNOPSIS
9              
10             use Webservice::OVH;
11            
12             my $ovh = Webservice::OVH->new_from_json("credentials.json");
13            
14             my $projects = $ovh->cloud->projects;
15             my $example_project = $projects->[0];
16            
17             my $networks = $example_project->network->privates;
18            
19             foreach my $network (@$networks) {
20            
21             my $subnets = $network->subnets;
22            
23             foreach my $subnet (@$subnets) {
24            
25             print $subnet->name;
26             }
27             }
28              
29             =head1 DESCRIPTION
30              
31             Gives access to subnet methods.
32              
33             =head1 METHODS
34              
35             =cut
36              
37             use strict;
38 36     36   224 use warnings;
  36         80  
  36         883  
39 36     36   164 use Carp qw{ carp croak };
  36         69  
  36         1171  
40 36     36   160 use JSON;
  36         74  
  36         1591  
41 36     36   210  
  36         95  
  36         293  
42             our $VERSION = 0.47;
43              
44             =head2 _new_existing
45              
46             Internal Method to create the Subnet object.
47             This method is not ment to be called directly.
48              
49             =over
50              
51             =item * Parameter: %params - key => value
52              
53             =item * Return: L<Webservice::OVH::Cloud::Project::Network::Private::Subnet>
54              
55             =item * Synopsis: Webservice::OVH::Cloud::Project::Network::Private::Subnet->_new(wrapper => $ovh_api_wrapper, project => $project, module => $module, id => $id );
56              
57             =back
58              
59             =cut
60              
61              
62             my ( $class, %params ) = @_;
63              
64 0     0     die "Missing id" unless $params{id};
65             die "Missing project" unless $params{project};
66 0 0         die "Missing wrapper" unless $params{wrapper};
67 0 0         die "Missing module" unless $params{module};
68 0 0          
69 0 0         # Special Case, because subnet properties can't be called individually
70             my $project_id = $params{project}->id;
71             my $properties = $params{properties};
72 0           my $api = $params{wrapper};
73 0           my $module = $params{module};
74 0           my $subnet_id = $properties->{id};
75 0           my $private = $params{private};
76 0            
77 0           # No api check possible, because single subnets can't be checked
78             my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api, _id => $subnet_id, _properties => $properties, _project => $params{project}, _network => $private }, $class;
79              
80 0           return $self;
81             }
82 0            
83             =head2 _new_existing
84              
85             Internal Method to create the Subnet object.
86             This method is not ment to be called directly.
87              
88             =over
89              
90             =item * Parameter: %params - key => value
91              
92             =item * Return: L<Webservice::OVH::Cloud::Project::Network::Private::Subnet>
93              
94             =item * Synopsis: Webservice::OVH::Cloud::Project::Network::Private::Subnet->_new(wrapper => $ovh_api_wrapper, project => $project, module => $module );
95              
96             =back
97              
98             =cut
99              
100              
101             my ( $class, %params ) = @_;
102              
103             die "Missing id" unless $params{project};
104 0     0     die "Missing project" unless $params{wrapper};
105             die "Missing wrapper" unless $params{module};
106 0 0         die "Missing module" unless $params{private};
107 0 0          
108 0 0         my $dhcp = $params{dhcp} && ( $params{dhcp} eq 'true' || $params{dhcp} eq '1' || $params{dhcp} eq 'yes' ) ? JSON::true : JSON::false;
109 0 0         my $no_gateway = $params{noGateway} && ( $params{no_gateway} eq 'true' || $params{no_gateway} eq '1' || $params{no_gateway} eq 'yes' ) ? JSON::true : JSON::false;
110              
111 0 0 0       my $project_id = $params{project}->id;
112 0 0 0       my $api = $params{wrapper};
113             my $module = $params{module};
114 0           my $private = $params{private};
115 0            
116 0           my @keys_needed = qw{ network end region start };
117 0           if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) {
118              
119 0           croak "Missing parameter: @missing_parameters";
120 0 0         }
  0            
121              
122 0           my $network_id = $private->id;
123              
124             my $body = {};
125 0           $body->{dhcp} = $dhcp;
126             $body->{end} = $params{end};
127 0           $body->{network} = $params{network};
128 0           $body->{noGateway} = $no_gateway;
129 0           $body->{region} = $params{region};
130 0           $body->{start} = $params{start};
131 0            
132 0           my $response = $api->rawCall( method => 'post', path => "/cloud/project/$project_id/network/private/$network_id/subnet", body => $body, noSignature => 0 );
133 0           die $response->error if $response->error;
134              
135 0           my $subnet_id = $response->content->{id};
136 0 0         my $properties = $response->content;
137              
138 0           my $self = bless { _network => $private, _module => $module, _valid => 1, _api_wrapper => $api, _id => $subnet_id, _properties => $properties, _project => $params{project} }, $class;
139 0            
140             return $self;
141 0           }
142              
143 0           =head2 project
144              
145             Root Project.
146              
147             =over
148              
149             =item * Return: L<Webservice::OVH::Cloud::Project>
150              
151             =item * Synopsis: my $project = $subnet->project;
152              
153             =back
154              
155             =cut
156              
157              
158             my ($self) = @_;
159              
160             return $self->{_project};
161             }
162 0     0 1    
163             =head2 project
164 0            
165             Root Network.
166              
167             =over
168              
169             =item * Return: L<Webservice::OVH::Cloud::Project::Network::Private>
170              
171             =item * Synopsis: my $project = $subnet->project;
172              
173             =back
174              
175             =cut
176              
177              
178             my ($self) = @_;
179              
180             return $self->{_network};
181             }
182              
183 0     0 0   =head2 is_valid
184              
185 0           When this object is deleted on the api side, this method returns 0.
186              
187             =over
188              
189             =item * Return: VALUE
190              
191             =item * Synopsis: print "Valid" if $subnet->is_valid;
192              
193             =back
194              
195             =cut
196              
197              
198             my ($self) = @_;
199              
200             return $self->{_valid};
201             }
202              
203             =head2 _is_valid
204 0     0 1    
205             Intern method to check validity.
206 0           Difference is that this method carps an error.
207              
208             =over
209              
210             =item * Return: VALUE
211              
212             =item * Synopsis: $subnet->_is_valid;
213              
214             =back
215              
216             =cut
217              
218              
219             my ($self) = @_;
220              
221             carp "Subnet is not valid anymore" unless $self->is_valid;
222             return $self->is_valid;
223             }
224              
225             =head2 id
226 0     0      
227             Returns the api id
228 0 0          
229 0           =over
230              
231             =item * Return: VALUE
232              
233             =item * Synopsis: my $id = $subnet->id;
234              
235             =back
236              
237             =cut
238              
239              
240             my ($self) = @_;
241              
242             return unless $self->_is_valid;
243              
244             return $self->{_id};
245             }
246              
247             =head2 gateway_ip
248 0     0 1    
249             Exposed property value.
250 0 0          
251             =over
252 0            
253             =item * Return: VALUE
254              
255             =item * Synopsis: my $gateway_ip = $subnet->gateway_ip;
256              
257             =back
258              
259             =cut
260              
261              
262             my ($self) = @_;
263              
264             return unless $self->_is_valid;
265              
266             return $self->{_properties}->{gatewayIp};
267             }
268              
269             =head2 cidr
270              
271 0     0 1   Exposed property value.
272              
273 0 0         =over
274              
275 0           =item * Return: VALUE
276              
277             =item * Synopsis: my $cidr = $subnet->cidr;
278              
279             =back
280              
281             =cut
282              
283              
284             my ($self) = @_;
285              
286             return unless $self->_is_valid;
287              
288             return $self->{_properties}->{cidr};
289             }
290              
291             =head2 ip_pools
292              
293             Exposed property value.
294 0     0 1    
295             =over
296 0 0          
297             =item * Return: ARRAY
298 0            
299             =item * Synopsis: my $ip_pools = $subnet->ip_pools;
300              
301             =back
302              
303             =cut
304              
305              
306             my ($self) = @_;
307              
308             return unless $self->_is_valid;
309              
310             return $self->{_properties}->{ipPools};
311             }
312              
313             =head2 delete
314              
315             Deletes the object api sided and sets it invalid.
316              
317 0     0 1   =over
318              
319 0 0         =item * Synopsis: $subnet->delete;
320              
321 0           =back
322              
323             =cut
324              
325              
326             my ($self) = @_;
327              
328             return unless $self->_is_valid;
329              
330             my $api = $self->{_api_wrapper};
331             my $project_id = $self->project->id;
332             my $network_id = $self->network->id;
333             my $subnet_id = $self->id;
334             my $response = $api->rawCall( method => 'delete', path => "/cloud/project/$project_id/network/private/$network_id/subnet/$subnet_id", noSignature => 0 );
335             croak $response->error if $response->error;
336              
337             $self->{_valid} = 0;
338 0     0 1   }
339              
340 0 0         1;