File Coverage

lib/Webservice/OVH/Cloud/Project.pm
Criterion Covered Total %
statement 24 229 10.4
branch 0 78 0.0
condition 0 18 0.0
subroutine 8 35 22.8
pod 26 26 100.0
total 58 386 15.0


line stmt bran cond sub pod time code
1              
2             =encoding utf-8
3              
4             =head1 NAME
5              
6             Webservice::OVH::Cloud::Project
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 $images = $project->images;
18             my $instances = $project->instances;
19             my $regions = $project->regions;
20             my $flavors = $project->flavors;
21             my $ssh_keys = $project->ssh_keys;
22             my $networks = $project->network->privates;
23              
24             =head1 DESCRIPTION
25              
26             Provides access to all sub objects of a specific projects.
27              
28             =head1 METHODS
29              
30             =cut
31              
32             use strict;
33 36     36   244 use warnings;
  36         72  
  36         953  
34 36     36   163 use Carp qw{ carp croak };
  36         74  
  36         809  
35 36     36   152  
  36         63  
  36         1938  
36             our $VERSION = 0.47;
37              
38             use Webservice::OVH::Cloud::Project::IP;
39 36     36   13850 use Webservice::OVH::Cloud::Project::Instance;
  36         88  
  36         1047  
40 36     36   15546 use Webservice::OVH::Cloud::Project::Network;
  36         102  
  36         1169  
41 36     36   15299 use Webservice::OVH::Cloud::Project::Image;
  36         113  
  36         1137  
42 36     36   15390 use Webservice::OVH::Cloud::Project::SSH;
  36         93  
  36         1063  
43 36     36   13403  
  36         97  
  36         89242  
44             =head2 _new_existing
45              
46             Internal Method to create the project object.
47             This method is not ment to be called directly.
48              
49             =over
50              
51             =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object, $id - api id
52              
53             =item * Return: L<Webservice::OVH::Cloud::Project>
54              
55             =item * Synopsis: Webservice::OVH::Cloud::Project->_new($ovh_api_wrapper, $project_name, $module);
56              
57             =back
58              
59             =cut
60              
61              
62             my ( $class, %params ) = @_;
63              
64 0     0     die "Missing module" unless $params{module};
65             die "Missing wrapper" unless $params{wrapper};
66 0 0         die "Missing id" unless $params{id};
67 0 0          
68 0 0         my $project_id = $params{id};
69             my $api_wrapper = $params{wrapper};
70 0           my $module = $params{module};
71 0            
72 0           my $self = bless { _module => $module, _api_wrapper => $api_wrapper, _properties => undef, _id => $project_id, _instances => {}, _available_instances => [], _images => {}, _available_images => [], _ssh_keys => {}, _available_ssh_keys => [] }, $class;
73              
74 0           my $instance = Webservice::OVH::Cloud::Project::Instance->_new_empty( wrapper => $api_wrapper, project => $self, module => $module );
75             my $network = Webservice::OVH::Cloud::Project::Network->_new( wrapper => $api_wrapper, project => $self, module => $module );
76 0           my $ip = Webservice::OVH::Cloud::Project::IP->_new( wrapper => $api_wrapper, project => $self, module => $module );
77 0            
78 0           $self->{_instance} = $instance;
79             $self->{_network} = $network;
80 0           $self->{_ip} = $ip;
81 0            
82 0           $self->properties;
83              
84 0           return $self;
85             }
86 0            
87             =head2 id
88              
89             Returns the api id
90              
91             =over
92              
93             =item * Return: VALUE
94              
95             =item * Synopsis: my $id = $project->id;
96              
97             =back
98              
99             =cut
100              
101              
102             my ($self) = @_;
103             return $self->{_id};
104             }
105 0     0 1    
106 0           =head2 properties
107              
108             Returns the raw properties as a hash.
109             This is the original return value of the web-api.
110              
111             =over
112              
113             =item * Return: HASH
114              
115             =item * Synopsis: my $properties = $project->properties;
116              
117             =back
118              
119             =cut
120              
121              
122             my ($self) = @_;
123              
124             my $api = $self->{_api_wrapper};
125             my $id = $self->id;
126 0     0 1   my $response = $api->rawCall( method => 'get', path => "/cloud/project/$id", noSignature => 0 );
127             croak $response->error if $response->error;
128 0           $self->{_properties} = $response->content;
129 0           return $self->{_properties};
130 0           }
131 0 0          
132 0           =head2 description
133 0            
134             Exposed property value.
135              
136             =over
137              
138             =item * Return: VALUE
139              
140             =item * Synopsis: my $description = $project->description;
141              
142             =back
143              
144             =cut
145              
146              
147             my ($self) = @_;
148              
149             return $self->{_properties}->{description};
150             }
151              
152 0     0 1   =head2 unleash
153              
154 0           Exposed property value.
155              
156             =over
157              
158             =item * Return: VALUE
159              
160             =item * Synopsis: my $sub_domain = $project->unleash;
161              
162             =back
163              
164             =cut
165              
166              
167             my ($self) = @_;
168              
169             return $self->{_properties}->{unleash} eq 'true' ? 1 : 0;
170             }
171              
172             =head2 order
173 0     0 1    
174             Exposed property value.
175 0 0          
176             =over
177              
178             =item * Return: <Webservice::OVH::Me::Order>
179              
180             =item * Synopsis: my $order = $project->order;
181              
182             =back
183              
184             =cut
185              
186              
187             my ($self) = @_;
188              
189             my $order_id = $self->{_properties}->{orderId};
190              
191             return $self->{_module}->me->order($order_id) if $order_id;
192              
193             return undef;
194 0     0 1   }
195              
196 0           =head2 status
197              
198 0 0         Exposed property value.
199              
200 0           =over
201              
202             =item * Return: VALUE
203              
204             =item * Synopsis: my $status = $project->status;
205              
206             =back
207              
208             =cut
209              
210              
211             my ($self) = @_;
212              
213             return $self->{_properties}->{status};
214             }
215              
216             =head2 access
217              
218             Exposed property value.
219 0     0 1    
220             =over
221 0            
222             =item * Return: VALUE
223              
224             =item * Synopsis: my $access = $project->access;
225              
226             =back
227              
228             =cut
229              
230              
231             my ($self) = @_;
232              
233             return $self->{_properties}->{access};
234             }
235              
236             =head2 change
237              
238             Changes the project.
239              
240 0     0 1   =over
241              
242 0           =item * Parameter: %params - key => value description
243              
244             =item * Synopsis: $project->change(description => 'Beschreibung');
245              
246             =back
247              
248             =cut
249              
250              
251             my ( $self, $description ) = @_;
252              
253             my $api = $self->{_api_wrapper};
254             my $project_id = $self->id;
255              
256             my $response = $api->rawCall( method => 'put', path => "/cloud/project/$project_id", body => { description => $description }, noSignature => 0 );
257             croak $response->error if $response->error;
258              
259             $self->properties;
260             }
261 0     0 1    
262             =head2 vrack
263 0            
264 0           Get associated vrack.
265              
266 0           =over
267 0 0          
268             =item * Return: HASH
269 0            
270             =item * Synopsis: $project->vrack;
271              
272             =back
273              
274             =cut
275              
276              
277             my ($self) = @_;
278              
279             my $api = $self->{_api_wrapper};
280             my $project_id = $self->id;
281              
282             my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/vrack", noSignature => 0 );
283             croak $response->error if $response->error;
284              
285             return $response->content;
286             }
287              
288 0     0 1   =head2 instance_exists
289              
290 0           Returns 1 if object is available for the connected account, 0 if not.
291 0            
292             =over
293 0            
294 0 0         =item * Parameter: $instance_id - api id, $no_recheck - (optional)only for internal usage
295              
296 0           =item * Return: VALUE
297              
298             =item * Synopsis: print "instance exists" if $project->instance_exists($id);
299              
300             =back
301              
302             =cut
303              
304              
305             my ( $self, $instance_id, $no_recheck ) = @_;
306              
307             if ( !$no_recheck ) {
308              
309             my $api = $self->{_api_wrapper};
310             my $project_id = $self->id;
311             my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/instance", noSignature => 0 );
312             croak $response->error if $response->error;
313              
314             my $list = $response->content;
315             my @instance_ids = grep { $_ = $_->{id} } @$list;
316              
317 0     0 1   return ( grep { $_ eq $instance_id } @instance_ids ) ? 1 : 0;
318              
319 0 0         } else {
320              
321 0           my $list = $self->{_available_instances};
322 0            
323 0           return ( grep { $_ eq $instance_id } @$list ) ? 1 : 0;
324 0 0         }
325             }
326 0            
327 0           =head2 instances
  0            
328              
329 0 0         Produces an array of all available instances that are connected to the project.
  0            
330              
331             =over
332              
333 0           =item * Return: ARRAY
334              
335 0 0         =item * Synopsis: my $instances = $project->instances;
  0            
336              
337             =back
338              
339             =cut
340              
341              
342             my ( $self, $region ) = @_;
343              
344             my $filter = $region ? Webservice::OVH::Helper->construct_filter( "region" => $region ) : "";
345             my $api = $self->{_api_wrapper};
346             my $project_id = $self->id;
347             my $response = $api->rawCall( method => 'get', path => sprintf( "/cloud/project/$project_id/instance%s", $filter ), noSignature => 0 );
348             croak $response->error if $response->error;
349              
350             my $instance_array = $response->content;
351             my $instances = [];
352             my @instance_ids = grep { $_ = $_->{id} } @$instance_array;
353             $self->{_available_images} = \@instance_ids;
354              
355 0     0 1   foreach my $instance_id (@instance_ids) {
356              
357 0 0         if ( $self->instance_exists( $instance_id, 1 ) ) {
358 0           my $instance = $self->{_instances}{$instance_id} = $self->{_instances}{$instance_id} || Webservice::OVH::Cloud::Project::Instance->_new_existing( wrapper => $api, project => $self, id => $instance_id, module => $self->{_module} );
359 0           push @$instances, $instance;
360 0           }
361 0 0         }
362              
363 0           return $instances;
364 0           }
365 0            
  0            
366 0           =head2 instance
367              
368 0           Returns a single instance by id
369              
370 0 0         =over
371 0   0        
372 0           =item * Parameter: $instance_id - api id
373              
374             =item * Return: L<Webservice::OVH::Cloud::Project::Instance>
375              
376 0           =item * Synopsis: my $instance = $project->instance($id);
377              
378             =back
379              
380             =cut
381              
382              
383             my ( $self, $instance_id ) = @_;
384              
385             if ( !$instance_id ) {
386              
387             return $self->{_instance};
388              
389             } else {
390              
391             if ( $self->instance_exists($instance_id) ) {
392              
393             my $api = $self->{_api_wrapper};
394             my $instance = $self->{_instances}{$instance_id} = $self->{_instances}{$instance_id} || Webservice::OVH::Cloud::Project::Instance->_new_existing( wrapper => $api, project => $self, id => $instance_id, module => $self->{_module} );
395              
396             return $instance;
397 0     0 1   } else {
398              
399 0 0         carp "Instance $instance_id doesn't exists";
400             return undef;
401 0           }
402             }
403             }
404              
405 0 0         =head2 create_instance
406              
407 0           Creates a new instance. Flavor image and ssh key need to be fetched first.
408 0   0       There is an example in examples/cloud.pl
409              
410 0           =over
411              
412             =item * Parameter: %params - key => value (required) flavor_id image_id name region (optional) group_id monthly_billing ssh_key_id user_data networks
413 0            
414 0           =item * Return: <Webservice::OVH::Cloud::Project::Instance>
415              
416             =item * Synopsis: my $instance = $project->create_instance(flavor_id => $flavor->id, image_id => $image->id, name => 'test', region => 'GRA1', ssh_key => $key->id networks => [ {ip => '0.0.0.0', network_id => 1 }, {ip => '0.0.0.0', network_id => 2 } ] );
417              
418             =back
419              
420             =cut
421              
422              
423             my ( $self, %params ) = @_;
424              
425             my $api = $self->{_api_wrapper};
426             my $instance = Webservice::OVH::Cloud::Project::Instance->_new( wrapper => $api, module => $self->{_module}, project => $self, %params, );
427             return $instance;
428             }
429              
430             =head2 regions
431              
432             Simple list of all available regions
433              
434             =over
435              
436             =item * Return: ARRAY
437              
438 0     0 1   =item * Synopsis: my $regions = $project->regions;
439              
440 0           =back
441 0            
442 0           =cut
443              
444              
445             my ($self) = @_;
446              
447             my $api = $self->{_api_wrapper};
448             my $project_id = $self->id;
449             my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/region", noSignature => 0 );
450             croak $response->error if $response->error;
451              
452             return $response->content;
453             }
454              
455             =head2 regions
456              
457             Get additional info about a specific region
458              
459             =over
460              
461 0     0 1   =item * Return: HASH
462              
463 0           =item * Synopsis: my $region_info = $project->region('GRA1');
464 0            
465 0           =back
466 0 0          
467             =cut
468 0            
469              
470             my ( $self, $region_name ) = @_;
471              
472             my $api = $self->{_api_wrapper};
473             my $project_id = $self->id;
474             my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/region/$region_name", noSignature => 0 );
475             croak $response->error if $response->error;
476              
477             return $response->content;
478             }
479              
480             =head2 flavors
481              
482             Returns a hash of all available flavors.
483              
484             =over
485              
486             =item * Return: ARRAY
487 0     0 1    
488             =item * Synopsis: my $flavors = $project->flavors;
489 0            
490 0           =back
491 0            
492 0 0         =cut
493              
494 0            
495             my ($self) = @_;
496              
497             my $api = $self->{_api_wrapper};
498             my $project_id = $self->id;
499             my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/flavor", noSignature => 0 );
500             croak $response->error if $response->error;
501              
502             my @flavor_ids = grep { $_->{id} } @{ $response->content };
503              
504             return \@flavor_ids;
505             }
506              
507             =head2 flavor
508              
509             Returns info about a specific flavor by id.
510              
511             =over
512              
513 0     0 1   =item * Return: HASH
514              
515 0           =item * Synopsis: my $flavors = $project->flavors;
516 0            
517 0           =back
518 0 0          
519             =cut
520 0            
  0            
  0            
521              
522 0           my ( $self, $flavor_id ) = @_;
523              
524             my $api = $self->{_api_wrapper};
525             my $project_id = $self->id;
526             my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/flavor/$flavor_id", noSignature => 0 );
527             croak $response->error if $response->error;
528              
529             return $response->content;
530             }
531              
532             =head2 image_exists
533              
534             Returns 1 if image is available for the project, 0 if not.
535              
536             =over
537              
538             =item * Parameter: $image_id - api id, $no_recheck - (optional)only for internal usage
539              
540             =item * Return: VALUE
541 0     0 1    
542             =item * Synopsis: print "image exists" if $project->image_exists(1234);
543 0            
544 0           =back
545 0            
546 0 0         =cut
547              
548 0            
549             my ( $self, $image_id, $no_recheck ) = @_;
550              
551             if ( !$no_recheck ) {
552              
553             my $api = $self->{_api_wrapper};
554             my $project_id = $self->id;
555             my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/image", noSignature => 0 );
556             croak $response->error if $response->error;
557              
558             my $list = $response->content;
559              
560             my @image_ids = grep { $_ = $_->{id} } @$list;
561              
562             return ( grep { $_ eq $image_id } @image_ids ) ? 1 : 0;
563              
564             } else {
565              
566             my $list = $self->{_available_images};
567              
568             return ( grep { $_ eq $image_id } @$list ) ? 1 : 0;
569 0     0 1   }
570             }
571 0 0          
572             =head2 images
573 0            
574 0           Produces an array of all available images that are connected to the project.
575 0            
576 0 0         =over
577              
578 0           =item * Parameter: %filter - key => value flavor_type os_type region
579              
580 0           =item * Return: ARRAY
  0            
581              
582 0 0         =item * Synopsis: my $images = $project->images( flavor_type => 'ovh.ssd.eg', os_type => 'linux', region => 'GRA1' );
  0            
583              
584             =back
585              
586 0           =cut
587              
588 0 0          
  0            
589             my ( $self, %filter ) = @_;
590              
591             my %filter_values;
592             $filter_values{flavorType} = $filter{flavor_type} unless exists $filter{flavor_type};
593             $filter_values{osType} = $filter{os_type} unless exists $filter{os_type};
594             $filter_values{region} = $filter{region} unless exists $filter{region};
595             my $filter = Webservice::OVH::Helper->construct_filter(%filter);
596              
597             my $api = $self->{_api_wrapper};
598             my $project_id = $self->id;
599             my $response = $api->rawCall( method => 'get', path => sprintf( "/cloud/project/$project_id/image%s", $filter ), noSignature => 0 );
600             croak $response->error if $response->error;
601              
602             my $image_array = $response->content;
603             my $images = [];
604             my @image_ids = grep { $_ = $_->{id} } @$image_array;
605             $self->{_available_images} = \@image_ids;
606              
607             foreach my $image_id (@image_ids) {
608              
609             if ( $self->image_exists( $image_id, 1 ) ) {
610 0     0 1    
611             my $image = $self->{_images}{$image_id} = $self->{_images}{$image_id} || Webservice::OVH::Cloud::Project::Image->_new_existing( wrapper => $api, module => $self->{_module}, project => $self, id => $image_id );
612 0           push @$images, $image;
613 0 0         }
614 0 0         }
615 0 0          
616 0           return $images;
617             }
618 0            
619 0           =head2 image
620 0            
621 0 0         Returns a single image by id
622              
623 0           =over
624 0            
625 0           =item * Parameter: $image_id - api id
  0            
626 0            
627             =item * Return: L<Webservice::OVH::Cloud::Project::Image>
628 0            
629             =item * Synopsis: my $image = $project->image($id);
630 0 0          
631             =back
632 0   0        
633 0           =cut
634              
635              
636             my ( $self, $image_id ) = @_;
637 0            
638             if ( $self->image_exists($image_id) ) {
639              
640             my $api = $self->{_api_wrapper};
641             my $instance = $self->{_image}{$image_id} = $self->{_image}{$image_id} || Webservice::OVH::Cloud::Project::Image->_new_existing( wrapper => $api, module => $self->{_module}, project => $self, id => $image_id );
642              
643             return $instance;
644             } else {
645              
646             carp "Instance $image_id doesn't exists";
647             return undef;
648             }
649             }
650              
651             =head2 ssh_key_exists
652              
653             Returns 1 if key is available for the project, 0 if not.
654              
655             =over
656              
657             =item * Parameter: $key_id - api id, $no_recheck - (optional)only for internal usage
658 0     0 1    
659             =item * Return: VALUE
660 0 0          
661             =item * Synopsis: print "image exists" if $project->image_exists(1234);
662 0            
663 0   0       =back
664              
665 0           =cut
666              
667              
668 0           my ( $self, $key_id, $no_recheck ) = @_;
669 0            
670             if ( !$no_recheck ) {
671              
672             my $api = $self->{_api_wrapper};
673             my $project_id = $self->id;
674             my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/sshkey", noSignature => 0 );
675             croak $response->error if $response->error;
676              
677             my $list = $response->content;
678             my @key_ids = grep { $_ = $_->{id} } @$list;
679              
680             return ( grep { $_ eq $key_id } @key_ids ) ? 1 : 0;
681              
682             } else {
683              
684             my $list = $self->{_available_ssh_keys};
685              
686             return ( grep { $_ eq $key_id } @$list ) ? 1 : 0;
687             }
688             }
689              
690             =head2 ssh_keys
691 0     0 1    
692             Produces an array of all available ssh_keys that are connected to the project.
693 0 0          
694             =over
695 0            
696 0           =item * Parameter: $region - filters for specific region
697 0            
698 0 0         =item * Return: ARRAY
699              
700 0           =item * Synopsis: my $keys = $project->images( region => 'GRA1' );
701 0            
  0            
702             =back
703 0 0          
  0            
704             =cut
705              
706              
707 0           my ( $self, $region ) = @_;
708              
709 0 0         my $filter = $region ? Webservice::OVH::Helper->construct_filter( region => $region ) : "";
  0            
710              
711             my $api = $self->{_api_wrapper};
712             my $project_id = $self->id;
713             my $response = $api->rawCall( method => 'get', path => sprintf( "/cloud/project/$project_id/sshkey%s", $filter ), noSignature => 0 );
714             croak $response->error if $response->error;
715              
716             my $key_array = $response->content;
717             my $keys = [];
718             my @key_ids = grep { $_ = $_->{id} } @$key_array;
719             $self->{_available_ssh_keys} = \@key_ids;
720              
721             foreach my $key_id (@key_ids) {
722              
723             if ( $self->ssh_key_exists( $key_id, 1 ) ) {
724              
725             my $ssh_key = $self->{_ssh_keys}{$key_id} = $self->{_ssh_keys}{$key_id} || Webservice::OVH::Cloud::Project::SSH->_new_existing( wrapper => $api, module => $self->{_module}, project => $self, id => $key_id );
726             push @$keys, $ssh_key;
727             }
728             }
729              
730             return $keys;
731 0     0 1   }
732              
733 0 0         =head2 ssh_key
734              
735 0           Returns a single ssh_key by id
736 0            
737 0           =over
738 0 0          
739             =item * Parameter: $key_id - api id
740 0            
741 0           =item * Return: L<Webservice::OVH::Cloud::Project::SSH>
742 0            
  0            
743 0           =item * Synopsis: my $ssh_key = $project->ssh_key($id);
744              
745 0           =back
746              
747 0 0         =cut
748              
749 0   0        
750 0           my ( $self, $key_id ) = @_;
751              
752             if ( $self->ssh_key_exists($key_id) ) {
753              
754 0           my $api = $self->{_api_wrapper};
755             my $instance = $self->{_ssh_keys}{$key_id} = $self->{_ssh_keys}{$key_id} || Webservice::OVH::Cloud::Project::SSH->_new_existing( wrapper => $api, module => $self->{_module}, project => $self, id => $key_id );
756              
757             return $instance;
758             } else {
759              
760             carp "SSH-Key $key_id doesn't exists";
761             return undef;
762             }
763             }
764              
765             =head2 create_ssh_key
766              
767             Creates a new ssh key.
768              
769             =over
770              
771             =item * Parameter: %params - key => value (required) name public_key (optional) region
772              
773             =item * Return: <Webservice::OVH::Cloud::Project::SSH>
774              
775 0     0 1   =item * Synopsis: my $ssh_key = $project->create_ssh_key( name => 'Test key', public_key => $key );
776              
777 0 0         =back
778              
779 0           =cut
780 0   0        
781              
782 0           my ( $self, %params ) = @_;
783              
784             my $api = $self->{_api_wrapper};
785 0           my $ssh_key = Webservice::OVH::Cloud::Project::SSH->_new( wrapper => $api, module => $self->{_module}, project => $self, %params, );
786 0           return $ssh_key;
787             }
788              
789             =head2 network
790              
791             Access to /cloud/project/network api methods
792              
793             =over
794              
795             =item * Return: L<Webservice::OVH::Cloud::Project::Network>
796              
797             =item * Synopsis: $project->network;
798              
799             =back
800              
801             =cut
802              
803              
804             my ($self) = @_;
805              
806             return $self->{_network};
807             }
808 0     0 1    
809             =head2 ip
810 0            
811 0           Access to /cloud/project/ip api methods
812 0            
813             =over
814              
815             =item * Return: L<Webservice::OVH::Cloud::Project::IP>
816              
817             =item * Synopsis: $project->ip;
818              
819             =back
820              
821             =cut
822              
823              
824             my ($self) = @_;
825              
826             return $self->{_ip};
827             }
828              
829             1;