« blib/lib/Webservice/OVH/Email/Domain/Domain.pm
↑ Summary
blib/lib/Webservice/OVH/Domain/Zone/Record.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 | use strict; | ||
| 34 | use warnings; | ||
| 35 | use Carp qw{ carp croak }; | ||
| 36 | |||
| 37 | our $VERSION = 0.47; | ||
| 38 | |||
| 39 | use Webservice::OVH::Cloud::Project::Network::Private::Subnet; | ||
| 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 | sub _new_existing { | ||
| 59 | |||
| 60 | my ( $class, %params ) = @_; | ||
| 61 | |||
| 62 | die "Missing id" unless $params{id}; | ||
| 63 | die "Missing project" unless $params{project}; | ||
| 64 | die "Missing wrapper" unless $params{wrapper}; | ||
| 65 | die "Missing module" unless $params{module}; | ||
| 66 | |||
| 67 | my $project_id = $params{project}->id; | ||
| 68 | my $network_id = $params{id}; | ||
| 69 | my $api = $params{wrapper}; | ||
| 70 | my $module = $params{module}; | ||
| 71 | my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/network/private/$network_id", noSignature => 0 ); | ||
| 72 | carp $response->error if $response->error; | ||
| 73 | |||
| 74 | if ( !$response->error ) { | ||
| 75 | |||
| 76 | my $porperties = $response->content; | ||
| 77 | my $self = bless { _available_subnets => [], _subnets => {}, _module => $module, _valid => 1, _api_wrapper => $api, _id => $network_id, _properties => $porperties, _project => $params{project} }, $class; | ||
| 78 | |||
| 79 | return $self; | ||
| 80 | } else { | ||
| 81 | |||
| 82 | 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 | sub _new { | ||
| 104 | |||
| 105 | my ( $class, %params ) = @_; | ||
| 106 | |||
| 107 | my $project_id = $params{project}->id; | ||
| 108 | my $api = $params{wrapper}; | ||
| 109 | my $module = $params{module}; | ||
| 110 | |||
| 111 | my @keys_needed = qw{ project wrapper module vlan_id name }; | ||
| 112 | if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) { | ||
| 113 | |||
| 114 | croak "Missing parameter: @missing_parameters"; | ||
| 115 | } | ||
| 116 | |||
| 117 | my $body = {}; | ||
| 118 | $body->{vlanId} = $params{vlan_id}; | ||
| 119 | $body->{name} = $params{name}; | ||
| 120 | $body->{regions} = $params{region} if exists $params{region}; | ||
| 121 | my $response = $api->rawCall( method => 'post', path => "/cloud/project/$project_id/network/private", body => $body, noSignature => 0 ); | ||
| 122 | croak $response->error if $response->error; | ||
| 123 | |||
| 124 | my $network_id = $response->content->{id}; | ||
| 125 | my $properties = $response->content; | ||
| 126 | |||
| 127 | my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api, _id => $network_id, _properties => $properties, _project => $params{project} }, $class; | ||
| 128 | |||
| 129 | 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 | sub project { | ||
| 147 | |||
| 148 | my ($self) = @_; | ||
| 149 | |||
| 150 | 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 | sub is_valid { | ||
| 168 | |||
| 169 | my ($self) = @_; | ||
| 170 | |||
| 171 | 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 | sub _is_valid { | ||
| 190 | |||
| 191 | my ($self) = @_; | ||
| 192 | |||
| 193 | carp "Network is not valid anymore" unless $self->is_valid; | ||
| 194 | 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 | sub id { | ||
| 212 | |||
| 213 | my ($self) = @_; | ||
| 214 | |||
| 215 | return unless $self->_is_valid; | ||
| 216 | |||
| 217 | 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 | sub properties { | ||
| 236 | |||
| 237 | my ($self) = @_; | ||
| 238 | |||
| 239 | return unless $self->_is_valid; | ||
| 240 | |||
| 241 | my $api = $self->{_api_wrapper}; | ||
| 242 | my $project_id = $self->project->id; | ||
| 243 | my $network_id = $self->id; | ||
| 244 | my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/network/private/$network_id", noSignature => 0 ); | ||
| 245 | croak $response->error if $response->error; | ||
| 246 | $self->{_properties} = $response->content; | ||
| 247 | 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 | sub regions { | ||
| 265 | |||
| 266 | my ($self) = @_; | ||
| 267 | |||
| 268 | return unless $self->_is_valid; | ||
| 269 | |||
| 270 | 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 | sub status { | ||
| 288 | |||
| 289 | my ($self) = @_; | ||
| 290 | |||
| 291 | return unless $self->_is_valid; | ||
| 292 | |||
| 293 | 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 | sub name { | ||
| 311 | |||
| 312 | my ($self) = @_; | ||
| 313 | |||
| 314 | return unless $self->_is_valid; | ||
| 315 | |||
| 316 | 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 | sub type { | ||
| 334 | |||
| 335 | my ($self) = @_; | ||
| 336 | |||
| 337 | return unless $self->_is_valid; | ||
| 338 | |||
| 339 | 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 | sub vlan_id { | ||
| 357 | |||
| 358 | my ($self) = @_; | ||
| 359 | |||
| 360 | return unless $self->_is_valid; | ||
| 361 | |||
| 362 | 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 | sub change { | ||
| 380 | |||
| 381 | my ( $self, $name ) = @_; | ||
| 382 | |||
| 383 | return unless $self->_is_valid; | ||
| 384 | |||
| 385 | croak "Missing name" unless $name; | ||
| 386 | |||
| 387 | my $api = $self->{_api_wrapper}; | ||
| 388 | my $project_id = $self->project->id; | ||
| 389 | my $network_id = $self->id; | ||
| 390 | |||
| 391 | my $response = $api->rawCall( method => 'put', path => "/cloud/project/$project_id/network/private/$network_id", body => { name => $name }, noSignature => 0 ); | ||
| 392 | croak $response->error if $response->error; | ||
| 393 | |||
| 394 | $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 | sub delete { | ||
| 410 | |||
| 411 | my ($self) = @_; | ||
| 412 | |||
| 413 | return unless $self->_is_valid; | ||
| 414 | |||
| 415 | my $api = $self->{_api_wrapper}; | ||
| 416 | my $project_id = $self->project->id; | ||
| 417 | my $network_id = $self->id; | ||
| 418 | |||
| 419 | my $response = $api->rawCall( method => 'delete', path => "/cloud/project/$project_id/network/private/$network_id", noSignature => 0 ); | ||
| 420 | croak $response->error if $response->error; | ||
| 421 | |||
| 422 | $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 | sub region { | ||
| 440 | |||
| 441 | my ( $self, $region ) = @_; | ||
| 442 | |||
| 443 | return unless $self->_is_valid; | ||
| 444 | |||
| 445 | croak "Missing region" unless $region; | ||
| 446 | |||
| 447 | my $api = $self->{_api_wrapper}; | ||
| 448 | my $project_id = $self->project->id; | ||
| 449 | my $network_id = $self->id; | ||
| 450 | |||
| 451 | my $response = $api->rawCall( method => 'post', path => "/cloud/project/$project_id/network/private/$network_id/region", body => { region => $region }, noSignature => 0 ); | ||
| 452 | croak $response->error if $response->error; | ||
| 453 | |||
| 454 | 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 | sub subnets { | ||
| 472 | |||
| 473 | my ($self) = @_; | ||
| 474 | |||
| 475 | my $api = $self->{_api_wrapper}; | ||
| 476 | my $project_id = $self->project->id; | ||
| 477 | my $network_id = $self->id; | ||
| 478 | my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/network/private/$network_id/subnet", noSignature => 0 ); | ||
| 479 | croak $response->error if $response->error; | ||
| 480 | |||
| 481 | my $subnet_array = $response->content; | ||
| 482 | my $subnets = []; | ||
| 483 | $self->{_available_subnets} = $subnet_array; | ||
| 484 | |||
| 485 | foreach my $subnet (@$subnet_array) { | ||
| 486 | |||
| 487 | my $subnet_id = $subnet->{id}; | ||
| 488 | my $subnet = $self->{_subnets}{$subnet_id} = | ||
| 489 | $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 ); | ||
| 490 | push @$subnets, $subnet; | ||
| 491 | } | ||
| 492 | |||
| 493 | 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 | sub subnet { | ||
| 513 | |||
| 514 | my ( $self, $subnet_id ) = @_; | ||
| 515 | |||
| 516 | my $subnets = $self->subnets; | ||
| 517 | |||
| 518 | my @subnet_search = grep { $_->id eq $subnet_id } @$subnet_id; | ||
| 519 | |||
| 520 | return scalar @subnet_search > 0 ? $subnet_search[0] : undef; | ||
| 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 | sub create_subnet { | ||
| 540 | |||
| 541 | my ( $self, %params ) = @_; | ||
| 542 | |||
| 543 | my $api = $self->{_api_wrapper}; | ||
| 544 | my $subnet = Webservice::OVH::Cloud::Project::Network::Private::Subnet->_new( project => $self->project, wrapper => $api, module => $self->{_module}, private => $self, %params ); | ||
| 545 | |||
| 546 | return $subnet; | ||
| 547 | } | ||
| 548 | |||
| 549 | 1; |