File Coverage

lib/Webservice/OVH/Cloud/Project/Network/Private.pm
Criterion Covered Total %
statement 12 129 9.3
branch 0 56 0.0
condition 0 3 0.0
subroutine 4 22 18.1
pod 15 15 100.0
total 31 225 13.7


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
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             print $network->name;
22             }
23              
24             =head1 DESCRIPTION
25              
26             Gives access Private Network methods.
27              
28             =head1 METHODS
29              
30             =cut
31              
32             use strict;
33 36     36   225 use warnings;
  36         72  
  36         960  
34 36     36   172 use Carp qw{ carp croak };
  36         74  
  36         913  
35 36     36   176  
  36         74  
  36         2274  
36             our $VERSION = 0.47;
37              
38             use Webservice::OVH::Cloud::Project::Network::Private::Subnet;
39 36     36   14379  
  36         103  
  36         48642  
40             =head2 _new_existing
41              
42             Internal Method to create the Private object.
43             This method is not ment to be called directly.
44              
45             =over
46              
47             =item * Parameter: %params - key => value
48              
49             =item * Return: L<Webservice::OVH::Cloud::Project::Network::Private>
50              
51             =item * Synopsis: Webservice::OVH::Cloud::Project::Network::Private->_new(wrapper => $ovh_api_wrapper, project => $project, module => $module, id => $id );
52              
53             =back
54              
55             =cut
56              
57              
58             my ( $class, %params ) = @_;
59              
60 0     0     die "Missing id" unless $params{id};
61             die "Missing project" unless $params{project};
62 0 0         die "Missing wrapper" unless $params{wrapper};
63 0 0         die "Missing module" unless $params{module};
64 0 0          
65 0 0         my $project_id = $params{project}->id;
66             my $network_id = $params{id};
67 0           my $api = $params{wrapper};
68 0           my $module = $params{module};
69 0           my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/network/private/$network_id", noSignature => 0 );
70 0           carp $response->error if $response->error;
71 0            
72 0 0         if ( !$response->error ) {
73              
74 0 0         my $porperties = $response->content;
75             my $self = bless { _available_subnets => [], _subnets => {}, _module => $module, _valid => 1, _api_wrapper => $api, _id => $network_id, _properties => $porperties, _project => $params{project} }, $class;
76 0            
77 0           return $self;
78             } else {
79 0            
80             return undef;
81             }
82 0           }
83              
84             =head2 _new_existing
85              
86             Internal Method to create the Private object.
87             This method is not ment to be called directly.
88              
89             =over
90              
91             =item * Parameter: %params - key => value
92              
93             =item * Return: L<Webservice::OVH::Cloud::Project::Network::Private>
94              
95             =item * Synopsis: Webservice::OVH::Cloud::Project::Network::Private->_new(wrapper => $ovh_api_wrapper, project => $project, module => $module );
96              
97             =back
98              
99             =cut
100              
101              
102             my ( $class, %params ) = @_;
103              
104             my $project_id = $params{project}->id;
105 0     0     my $api = $params{wrapper};
106             my $module = $params{module};
107 0            
108 0           my @keys_needed = qw{ project wrapper module vlan_id name };
109 0           if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) {
110              
111 0           croak "Missing parameter: @missing_parameters";
112 0 0         }
  0            
113              
114 0           my $body = {};
115             $body->{vlanId} = $params{vlan_id};
116             $body->{name} = $params{name};
117 0           $body->{regions} = $params{region} if exists $params{region};
118 0           my $response = $api->rawCall( method => 'post', path => "/cloud/project/$project_id/network/private", body => $body, noSignature => 0 );
119 0           croak $response->error if $response->error;
120 0 0          
121 0           my $network_id = $response->content->{id};
122 0 0         my $properties = $response->content;
123              
124 0           my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api, _id => $network_id, _properties => $properties, _project => $params{project} }, $class;
125 0            
126             return $self;
127 0           }
128              
129 0           =head2 project
130              
131             Root Project.
132              
133             =over
134              
135             =item * Return: L<Webservice::OVH::Cloud::Project>
136              
137             =item * Synopsis: my $project = $private_network->project;
138              
139             =back
140              
141             =cut
142              
143              
144             my ($self) = @_;
145              
146             return $self->{_project};
147             }
148 0     0 1    
149             =head2 is_valid
150 0            
151             When this object is deleted on the api side, this method returns 0.
152              
153             =over
154              
155             =item * Return: VALUE
156              
157             =item * Synopsis: print "Valid" if $private_network->is_valid;
158              
159             =back
160              
161             =cut
162              
163              
164             my ($self) = @_;
165              
166             return $self->{_valid};
167             }
168              
169 0     0 1   =head2 _is_valid
170              
171 0           Intern method to check validity.
172             Difference is that this method carps an error.
173              
174             =over
175              
176             =item * Return: VALUE
177              
178             =item * Synopsis: $private_network->_is_valid;
179              
180             =back
181              
182             =cut
183              
184              
185             my ($self) = @_;
186              
187             carp "Network is not valid anymore" unless $self->is_valid;
188             return $self->is_valid;
189             }
190              
191 0     0     =head2 id
192              
193 0 0         Returns the api id
194 0            
195             =over
196              
197             =item * Return: VALUE
198              
199             =item * Synopsis: my $id = $private_network->id;
200              
201             =back
202              
203             =cut
204              
205              
206             my ($self) = @_;
207              
208             return unless $self->_is_valid;
209              
210             return $self->{_id};
211             }
212              
213 0     0 1   =head2 properties
214              
215 0 0         Returns the raw properties as a hash.
216             This is the original return value of the web-api.
217 0            
218             =over
219              
220             =item * Return: HASH
221              
222             =item * Synopsis: my $properties = $private_network->properties;
223              
224             =back
225              
226             =cut
227              
228              
229             my ($self) = @_;
230              
231             return unless $self->_is_valid;
232              
233             my $api = $self->{_api_wrapper};
234             my $project_id = $self->project->id;
235             my $network_id = $self->id;
236             my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/network/private/$network_id", noSignature => 0 );
237 0     0 1   croak $response->error if $response->error;
238             $self->{_properties} = $response->content;
239 0 0         return $self->{_properties};
240             }
241 0            
242 0           =head2 regions
243 0            
244 0           Exposed property value.
245 0 0          
246 0           =over
247 0            
248             =item * Return: ARRAY
249              
250             =item * Synopsis: my $regions = $private_network->regions;
251              
252             =back
253              
254             =cut
255              
256              
257             my ($self) = @_;
258              
259             return unless $self->_is_valid;
260              
261             return $self->{_properties}->{regions};
262             }
263              
264             =head2 status
265              
266 0     0 1   Exposed property value.
267              
268 0 0         =over
269              
270 0           =item * Return: VALUE
271              
272             =item * Synopsis: my $status = $private_network->status;
273              
274             =back
275              
276             =cut
277              
278              
279             my ($self) = @_;
280              
281             return unless $self->_is_valid;
282              
283             return $self->{_properties}->{status};
284             }
285              
286             =head2 name
287              
288             Exposed property value.
289 0     0 1    
290             =over
291 0 0          
292             =item * Return: VALUE
293 0            
294             =item * Synopsis: my $name = $private_network->name;
295              
296             =back
297              
298             =cut
299              
300              
301             my ($self) = @_;
302              
303             return unless $self->_is_valid;
304              
305             return $self->{_properties}->{name};
306             }
307              
308             =head2 type
309              
310             Exposed property value.
311              
312 0     0 1   =over
313              
314 0 0         =item * Return: VALUE
315              
316 0           =item * Synopsis: my $type = $private_network->type;
317              
318             =back
319              
320             =cut
321              
322              
323             my ($self) = @_;
324              
325             return unless $self->_is_valid;
326              
327             return $self->{_properties}->{type};
328             }
329              
330             =head2 vlan_id
331              
332             Exposed property value.
333              
334             =over
335 0     0 1    
336             =item * Return: VALUE
337 0 0          
338             =item * Synopsis: my $vlan_id = $private_network->vlan_id;
339 0            
340             =back
341              
342             =cut
343              
344              
345             my ($self) = @_;
346              
347             return unless $self->_is_valid;
348              
349             return $self->{_properties}->{vlanId};
350             }
351              
352             =head2 change
353              
354             Changes the private network.
355              
356             =over
357              
358 0     0 1   =item * Parameter: $name - name to be changed
359              
360 0 0         =item * Synopsis: $private_network->change(name => 'Test network');
361              
362 0           =back
363              
364             =cut
365              
366              
367             my ( $self, $name ) = @_;
368              
369             return unless $self->_is_valid;
370              
371             croak "Missing name" unless $name;
372              
373             my $api = $self->{_api_wrapper};
374             my $project_id = $self->project->id;
375             my $network_id = $self->id;
376              
377             my $response = $api->rawCall( method => 'put', path => "/cloud/project/$project_id/network/private/$network_id", body => { name => $name }, noSignature => 0 );
378             croak $response->error if $response->error;
379              
380             $self->properties;
381 0     0 1   }
382              
383 0 0         =head2 delete
384              
385 0 0         Deletes the object api sided and sets it invalid.
386              
387 0           =over
388 0            
389 0           =item * Synopsis: $private_network->delete;
390              
391 0           =back
392 0 0          
393             =cut
394 0            
395              
396             my ($self) = @_;
397              
398             return unless $self->_is_valid;
399              
400             my $api = $self->{_api_wrapper};
401             my $project_id = $self->project->id;
402             my $network_id = $self->id;
403              
404             my $response = $api->rawCall( method => 'delete', path => "/cloud/project/$project_id/network/private/$network_id", noSignature => 0 );
405             croak $response->error if $response->error;
406              
407             $self->{_valid} = 0;
408             }
409              
410             =head2 region
411 0     0 1    
412             Activate private network in a new region
413 0 0          
414             =over
415 0            
416 0           =item * Parameter: $region - region name in which the network should be activated
417 0            
418             =item * Synopsis: $private_network->region('GRA1');
419 0            
420 0 0         =back
421              
422 0           =cut
423              
424              
425             my ( $self, $region ) = @_;
426              
427             return unless $self->_is_valid;
428              
429             croak "Missing region" unless $region;
430              
431             my $api = $self->{_api_wrapper};
432             my $project_id = $self->project->id;
433             my $network_id = $self->id;
434              
435             my $response = $api->rawCall( method => 'post', path => "/cloud/project/$project_id/network/private/$network_id/region", body => { region => $region }, noSignature => 0 );
436             croak $response->error if $response->error;
437              
438             return $response->content;
439             }
440              
441 0     0 1   =head2 subnets
442              
443 0 0         Produces an array of all available subnets.
444              
445 0 0         =over
446              
447 0           =item * Return: ARRAY
448 0            
449 0           =item * Synopsis: my $subnets = $private_network->subnets;
450              
451 0           =back
452 0 0          
453             =cut
454 0            
455              
456             my ($self) = @_;
457              
458             my $api = $self->{_api_wrapper};
459             my $project_id = $self->project->id;
460             my $network_id = $self->id;
461             my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/network/private/$network_id/subnet", noSignature => 0 );
462             croak $response->error if $response->error;
463              
464             my $subnet_array = $response->content;
465             my $subnets = [];
466             $self->{_available_subnets} = $subnet_array;
467              
468             foreach my $subnet (@$subnet_array) {
469              
470             my $subnet_id = $subnet->{id};
471             my $subnet = $self->{_subnets}{$subnet_id} =
472             $self->{_subnets}{$subnet_id} || Webservice::OVH::Cloud::Project::Network::Private::Subnet->_new_existing( wrapper => $api, module => $self->{_module}, project => $self->project, id => $subnet_id, network => $self, properties => $subnet );
473 0     0 1   push @$subnets, $subnet;
474             }
475 0            
476 0           return $subnets;
477 0           }
478 0            
479 0 0         =head2 subnet
480              
481 0           Returns a single subnet by id
482 0            
483 0           =over
484              
485 0           =item * Parameter: $subnet_id - api id
486              
487 0           =item * Return: L<Webservice::OVH::Cloud::Project::Network::Private::Subnet>
488              
489 0   0       =item * Synopsis: my $subnet = $private_network->subnet($id);
490 0            
491             =back
492              
493 0           =cut
494              
495              
496             my ( $self, $subnet_id ) = @_;
497              
498             my $subnets = $self->subnets;
499              
500             my @subnet_search = grep { $_->id eq $subnet_id } @$subnet_id;
501              
502             return scalar @subnet_search > 0 ? $subnet_search[0] : undef;
503             }
504              
505             =head2 create_subnet
506              
507             Create a new network subnet.
508              
509             =over
510              
511             =item * Parameter: %params - key => value (required) dhcp no_gateway end network region start
512              
513             =item * Return: <Webservice::OVH::Cloud::Project::Network::Private::Subnet>
514 0     0 1    
515             =item * Synopsis: my $subnet = $project->create_subnet( dhcp => 1, no_gateway => 1, end => "192.168.1.24", start => "192.168.1.12", network => "192.168.1.0/24", region => "GRA1" );
516 0            
517             =back
518 0            
  0            
519             =cut
520 0 0          
521              
522             my ( $self, %params ) = @_;
523              
524             my $api = $self->{_api_wrapper};
525             my $subnet = Webservice::OVH::Cloud::Project::Network::Private::Subnet->_new( project => $self->project, wrapper => $api, module => $self->{_module}, private => $self, %params );
526              
527             return $subnet;
528             }
529              
530             1;