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