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