Webservice/OVH/Cloud/Project/Network/Private.pm

filter: Statementstmt 10.6% 14 / 131 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 18.1% 4 / 22 Podpod 100% 15 / 15 totaltotal 14.4% 33 / 229 CC 31 SCAR 28.4
CC
31
Cov
7%
_new_existing
56.0
_new
20.0
change
20.0
CRAP 17.1
« Webservice/OVH/Cloud/Project/Image.pm ↑ Summary Webservice/OVH/Helper.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 36 ▶use Carp qw{ carp croak };
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 36 our $VERSION = 0.48;
38  
39 36 ▶use Webservice::OVH::Cloud::Project::Network::Private::Subnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 0 ▶sub _new_existing {
Subroutine _new_existing: not called CC=7 SCAR=40.3
59  
60 0 my ( $class, %params ) = @_;
61  
62 0 ▶ die "Missing id" unless $params{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"id"} 0 0 0
63 0 ▶ die "Missing project" unless $params{project};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"project"} 0 0 0
64 0 ▶ die "Missing wrapper" unless $params{wrapper};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"wrapper"} 0 0 0
65 0 ▶ die "Missing module" unless $params{module};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"module"} 0 0 0
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 ▶ carp $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
73  
74 0 ▶ if ( !$response->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->error) { } 0 0 0
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 0 ▶sub _new {
Subroutine _new: not called CC=4 SCAR=30.0
104  
105 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 ▶ if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing_parameters) = grep({not $params{$_};} @keys_needed)) 0 0 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 ▶ $body->{regions} = $params{region} if exists $params{region};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"region"} 0 0 0
121 0 my $response = $api->rawCall( method => 'post', path => "/cloud/project/$project_id/network/private", body => $body, noSignature => 0 );
122 0 ▶ croak $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
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 0 ▶sub project {
Subroutine project: not called CC=1 SCAR=6.9
Pod documented
147  
148 0 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 0 ▶sub is_valid {
Subroutine is_valid: not called CC=1 SCAR=6.9
Pod documented
168  
169 0 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 0 ▶sub _is_valid {
Subroutine _is_valid: not called CC=2 SCAR=17.9
190  
191 0 my ($self) = @_;
192  
193 0 ▶ carp "Network is not valid anymore" unless $self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid 0 0 0
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 0 ▶sub id {
Subroutine id: not called CC=2 SCAR=17.9
Pod documented
212  
213 0 my ($self) = @_;
214  
215 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
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 0 ▶sub properties {
Subroutine properties: not called CC=3 SCAR=24.8
Pod documented
236  
237 0 my ($self) = @_;
238  
239 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
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 ▶ croak $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
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 0 ▶sub regions {
Subroutine regions: not called CC=2 SCAR=17.9
Pod documented
265  
266 0 my ($self) = @_;
267  
268 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
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 0 ▶sub status {
Subroutine status: not called CC=2 SCAR=17.9
Pod documented
288  
289 0 my ($self) = @_;
290  
291 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
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 0 ▶sub name {
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
311  
312 0 my ($self) = @_;
313  
314 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
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 0 ▶sub type {
Subroutine type: not called CC=2 SCAR=17.9
Pod documented
334  
335 0 my ($self) = @_;
336  
337 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
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 0 ▶sub vlan_id {
Subroutine vlan_id: not called CC=2 SCAR=17.9
Pod documented
357  
358 0 my ($self) = @_;
359  
360 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
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 0 ▶sub change {
Subroutine change: not called CC=4 SCAR=30.0
Pod documented
380  
381 0 my ( $self, $name ) = @_;
382  
383 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
384  
385 0 ▶ croak "Missing name" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
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 ▶ croak $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
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 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
410  
411 0 my ($self) = @_;
412  
413 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
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 ▶ croak $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
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 0 ▶sub region {
Subroutine region: not called CC=4 SCAR=30.0
Pod documented
440  
441 0 my ( $self, $region ) = @_;
442  
443 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
444  
445 0 ▶ croak "Missing region" unless $region;
Branch: 1 branch
BranchTrueFalseTotal
unless $region 0 0 0
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 ▶ croak $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
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 0 ▶sub subnets {
Subroutine subnets: not called CC=4 SCAR=30.0
Pod documented
472  
473 0 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 ▶ croak $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
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 ▶ $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 );
Condition0%0/3
$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)
l!l&&r!l&&!r
000
MC/DC0%0/2
$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)
$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)
Truth table0%0/3
A || B
A= $self->{'_subnets'}{$subnet_id}B= 'Webservice::OVH::Cloud::Project::Network::Private::Subnet'->_new_existing('wrapper', $api, 'module', $self->{'_module'}, 'project', $self->project, 'id', $subnet_id, 'network', $self, 'properties', $subnet)
ABresult
1X1
011
000
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 0 ▶sub subnet {
Subroutine subnet: not called CC=2 SCAR=17.9
Pod documented
513  
514 0 my ( $self, $subnet_id ) = @_;
515  
516 0 my $subnets = $self->subnets;
517  
518 0 my @subnet_search = grep { $_->id eq $subnet_id } @$subnet_id;
519  
520 0 ▶ return scalar @subnet_search > 0 ? $subnet_search[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar @subnet_search > 0 ? : 0 0 0
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 0 ▶sub create_subnet {
Subroutine create_subnet: not called CC=1 SCAR=6.9
Pod documented
540  
541 0 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 36 1;
« Webservice/OVH/Cloud/Project/Image.pm ↑ Summary Webservice/OVH/Helper.pm »