File Coverage

lib/Webservice/OVH/Cloud.pm
Criterion Covered Total %
statement 12 64 18.7
branch 0 26 0.0
condition 0 6 0.0
subroutine 4 10 40.0
pod 5 5 100.0
total 21 111 18.9


line stmt bran cond sub pod time code
1              
2             =encoding utf-8
3              
4             =head1 NAME
5              
6             Webservice::OVH::Domain
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             foreach my $project (@$project) {
16            
17             print $project->name;
18             }
19            
20             print "I have a project" if $ovh->cloud->project_exists("Name");
21              
22             =head1 DESCRIPTION
23              
24             Gives access to projects connected to the used account.
25              
26             =head1 METHODS
27              
28             =cut
29              
30             use strict;
31 36     36   237 use warnings;
  36         73  
  36         1059  
32 36     36   169 use Carp qw{ carp croak };
  36         72  
  36         1034  
33 36     36   164  
  36         67  
  36         2247  
34             our $VERSION = 0.47;
35              
36             use Webservice::OVH::Cloud::Project;
37 36     36   14860  
  36         127  
  36         22983  
38             =head2 _new
39              
40             Internal Method to create the cloud object.
41             This method is not ment to be called directly.
42              
43             =over
44              
45             =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object
46              
47             =item * Return: L<Webservice::OVH::Project>
48              
49             =item * Synopsis: Webservice::OVH::Clooud->_new($ovh_api_wrapper, $self);
50              
51             =back
52              
53             =cut
54              
55              
56             my ( $class, %params ) = @_;
57              
58 0     0     die "Missing module" unless $params{module};
59             die "Missing wrapper" unless $params{wrapper};
60 0 0          
61 0 0         my $module = $params{module};
62             my $api_wrapper = $params{wrapper};
63 0            
64 0           my $self = bless { _module => $module, _api_wrapper => $api_wrapper, _projects => {}, _avaiable_projects => [] }, $class;
65              
66 0           return $self;
67             }
68 0            
69             =head2 project_exists
70              
71             Returns 1 if project is available for the connected account, 0 if not.
72              
73             =over
74              
75             =item * Parameter: $project_name - Domain name, $no_recheck - (optional)only for internal usage
76              
77             =item * Return: VALUE
78              
79             =item * Synopsis: print "Name exists" if $ovh->domain->project_exists("Name");
80              
81             =back
82              
83             =cut
84              
85              
86             my ( $self, $id, $no_recheck ) = @_;
87              
88             if ( !$no_recheck ) {
89 0     0 1    
90             my $api = $self->{_api_wrapper};
91 0 0         my $response = $api->rawCall( method => 'get', path => "/cloud/project", noSignature => 0 );
92             croak $response->error if $response->error;
93 0            
94 0           my $list = $response->content;
95 0 0          
96             return ( grep { $_ eq $id } @$list ) ? 1 : 0;
97 0            
98             } else {
99 0 0          
  0            
100             my $list = $self->{_avaiable_projects};
101              
102             return ( grep { $_ eq $id } @$list ) ? 1 : 0;
103 0           }
104             }
105 0 0          
  0            
106             =head2 projects
107              
108             Produces an array of all available projects that are connected to the used account.
109              
110             =over
111              
112             =item * Return: ARRAY
113              
114             =item * Synopsis: my $projects = $ovh->order->projects();
115              
116             =back
117              
118             =cut
119              
120              
121             my ($self) = @_;
122              
123             my $api = $self->{_api_wrapper};
124             my $response = $api->rawCall( method => 'get', path => "/cloud/project", noSignature => 0 );
125 0     0 1   croak $response->error if $response->error;
126              
127 0           my $project_array = $response->content;
128 0           my $projects = [];
129 0 0         $self->{_avaiable_projects} = $project_array;
130              
131 0           foreach my $project_id (@$project_array) {
132 0           if ( $self->project_exists( $project_id, 1 ) ) {
133 0           my $project = $self->{_projects}{$project_id} = $self->{_projects}{$project_id} || Webservice::OVH::Cloud::Project->_new_existing( wrapper => $api, id => $project_id, module => $self->{_module} );
134             push @$projects, $project;
135 0           }
136 0 0         }
137 0   0        
138 0           return $projects;
139             }
140              
141             =head2 project
142 0            
143             Returns a single project by name
144              
145             =over
146              
147             =item * Parameter: $project_name - project name
148              
149             =item * Return: L<Webservice::OVH::Cloud::Project>
150              
151             =item * Synopsis: my $project = $ovh->cloud->project("Name");
152              
153             =back
154              
155             =cut
156              
157              
158             my ( $self, $project_id ) = @_;
159              
160             if ( $self->project_exists($project_id) ) {
161              
162             my $api = $self->{_api_wrapper};
163 0     0 1   my $project = $self->{_projects}{$project_id} = $self->{_projects}{$project_id} || Webservice::OVH::Cloud::Project->_new_existing( wrapper => $api, id => $project_id, module => $self->{_module} );
164              
165 0 0         return $project;
166             } else {
167 0            
168 0   0       carp "Service $project_id doesn't exists";
169             return undef;
170 0           }
171             }
172              
173 0           =head2 create_project
174 0            
175             Price information for projects and other running cloud services
176              
177             =over
178              
179             =item * Parameter: $description - (optional) description, $voucher - (optional)
180              
181             =item * Return: Return: L<Webservice::OVH::Cloud::Project>
182              
183             =item * Synopsis: my $order = $ovh->cloud->create_project;
184              
185             =back
186              
187             =cut
188              
189              
190             my ( $self, %params ) = @_;
191              
192             my $api = $self->{_api_wrapper};
193             my $body = {};
194             $body->{description} = $params{description} if exists $params{description};
195             $body->{voucher} = $params{voucher} if exists $params{voucher};
196 0     0 1   my $response = $api->rawCall( method => 'post', path => "/cloud/createProject", body => $body, noSignature => 0 );
197             croak $response->error if $response->error;
198 0            
199 0           my $order_id = $response->content->{orderId};
200 0 0          
201 0 0         return $self->{_module}->me->order($order_id);
202 0           }
203 0 0          
204             =head2 price
205 0            
206             Price information for projects and other running cloud services
207 0            
208             =over
209              
210             =item * Parameter: $flavor_id - Cloud flavor id, $region - region
211              
212             =item * Return: HASH
213              
214             =item * Synopsis: my $prices = $ovh->cloud->price;
215              
216             =back
217              
218             =cut
219              
220              
221             my ( $self, $flavor_id, $region ) = @_;
222              
223             my $filter = Webservice::OVH::Helper->construct_filter( "flavorId" => $flavor_id, "region" => $region );
224              
225             my $api = $self->{_api_wrapper};
226             my $response = $api->rawCall( method => 'get', path => sprintf( "/cloud/price%s", $filter ), noSignature => 0 );
227             croak $response->error if $response->error;
228 0     0 1    
229             return $response->content;
230 0           }
231              
232 0           1;