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