« lib/Webservice/OVH/Email/Domain/Domain/Task/Account.pm
↑ Summary
blib/lib/Webservice/OVH/Cloud/Project/Network.pm »
| 1 | package Webservice::OVH::Cloud::Project::IP; | ||
| 2 | |||
| 3 | =encoding utf-8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Webservice::OVH::Cloud::Project::IP; | ||
| 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 = $project->network->privates; | ||
| 19 | |||
| 20 | foreach my $network (@$networks) { | ||
| 21 | |||
| 22 | print @$networks->status; | ||
| 23 | } | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | Bridge Object failover ips | ||
| 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::Failover; | ||
| 40 | |||
| 41 | =head2 _new_existing | ||
| 42 | |||
| 43 | Internal Method to create the 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::IP> | ||
| 51 | |||
| 52 | =item * Synopsis: Webservice::OVH::Cloud::Project::IP->_new( wrapper => $ovh_api_wrapper, project => $project, module => $module ); | ||
| 53 | |||
| 54 | =back | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub _new { | ||
| 59 | |||
| 60 | my ( $class, %params ) = @_; | ||
| 61 | |||
| 62 | die "Missing project" unless $params{project}; | ||
| 63 | die "Missing module" unless $params{module}; | ||
| 64 | die "Missing wrapper" unless $params{wrapper}; | ||
| 65 | |||
| 66 | my $self = bless { module => $params{module}, _api_wrapper => $params{wrapper}, _project => $params{project}, _available_failovers => [], _failovers => {} }, $class; | ||
| 67 | |||
| 68 | return $self; | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 project | ||
| 72 | |||
| 73 | Shorthand to call $self->project directly for internal usage. | ||
| 74 | |||
| 75 | =over | ||
| 76 | |||
| 77 | =item * Return: L<Webservice::OVH::Cloud::Project> | ||
| 78 | |||
| 79 | =item * Synopsis: my $project = $project->ip->project; | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub project { | ||
| 86 | |||
| 87 | my ($self) = @_; | ||
| 88 | |||
| 89 | return $self->{_project}; | ||
| 90 | } | ||
| 91 | |||
| 92 | =head2 failover_exists | ||
| 93 | |||
| 94 | Returns 1 if failover is available for the connected account, 0 if not. | ||
| 95 | |||
| 96 | =over | ||
| 97 | |||
| 98 | =item * Parameter: $failover_id - api id, $no_recheck - (optional)only for internal usage | ||
| 99 | |||
| 100 | =item * Return: VALUE | ||
| 101 | |||
| 102 | =item * Synopsis: print "failover exists" if $project->ip->failover_exists(1234); | ||
| 103 | |||
| 104 | =back | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub failover_exists { | ||
| 109 | |||
| 110 | my ( $self, $failover_id, $no_recheck ) = @_; | ||
| 111 | |||
| 112 | if ( !$no_recheck ) { | ||
| 113 | |||
| 114 | my $api = $self->{_api_wrapper}; | ||
| 115 | my $project_id = $self->project->id; | ||
| 116 | my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/ip/failover", noSignature => 0 ); | ||
| 117 | croak $response->error if $response->error; | ||
| 118 | |||
| 119 | my $list = $response->content; | ||
| 120 | |||
| 121 | return ( grep { $_ eq $failover_id } @$list ) ? 1 : 0; | ||
| 122 | |||
| 123 | } else { | ||
| 124 | |||
| 125 | my $list = $self->{_avaiable_projects}; | ||
| 126 | |||
| 127 | return ( grep { $_ eq $failover_id } @$list ) ? 1 : 0; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | =head2 failovers | ||
| 132 | |||
| 133 | Produces an array of all available failovers that are connected to the project. | ||
| 134 | |||
| 135 | =over | ||
| 136 | |||
| 137 | =item * Return: ARRAY | ||
| 138 | |||
| 139 | =item * Synopsis: my $ips = $project->ip->failovers; | ||
| 140 | |||
| 141 | =back | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub failovers { | ||
| 146 | |||
| 147 | my ($self) = @_; | ||
| 148 | |||
| 149 | my $api = $self->{_api_wrapper}; | ||
| 150 | my $project_id = $self->project->id; | ||
| 151 | my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/ip/failover", noSignature => 0 ); | ||
| 152 | croak $response->error if $response->error; | ||
| 153 | |||
| 154 | my $failover_array = $response->content; | ||
| 155 | my $failovers = []; | ||
| 156 | $self->{_available_failovers} = $failover_array; | ||
| 157 | |||
| 158 | foreach my $failover_hash (@$failover_array) { | ||
| 159 | |||
| 160 | my $failover_id = $failover_hash->{id}; | ||
| 161 | if ( $self->failover_exists( $failover_id, 1 ) ) { | ||
| 162 | my $failover = $self->{_failovers}{$failover_id} = $self->{_failovers}{$failover_id} || Webservice::OVH::Cloud::Project::IP::Failover->_new( wrapper => $api, project => $self->project, id => $failover_id, module => $self->{_module} ); | ||
| 163 | push @$failovers, $failover; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | return $failovers; | ||
| 168 | } | ||
| 169 | |||
| 170 | =head2 failover | ||
| 171 | |||
| 172 | Returns a single failover by id | ||
| 173 | |||
| 174 | =over | ||
| 175 | |||
| 176 | =item * Parameter: $failover_id - api id | ||
| 177 | |||
| 178 | =item * Return: L<Webservice::OVH::Cloud::Project::IP::Failover> | ||
| 179 | |||
| 180 | =item * Synopsis: my $failover = $project->ip->failover(1234); | ||
| 181 | |||
| 182 | =back | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub failover { | ||
| 187 | |||
| 188 | my ( $self, $failover_id ) = @_; | ||
| 189 | |||
| 190 | if ( $self->failover_exists($failover_id) ) { | ||
| 191 | |||
| 192 | my $api = $self->{_api_wrapper}; | ||
| 193 | my $failover = $self->{_failovers}{$failover_id} = $self->{_failovers}{$failover_id} || Webservice::OVH::Cloud::Project::IP::Failover->_new( wrapper => $api, project => $self->project, id => $failover_id, module => $self->{_module} ); | ||
| 194 | |||
| 195 | return $failover; | ||
| 196 | } else { | ||
| 197 | |||
| 198 | carp "Failover $failover_id doesn't exists"; | ||
| 199 | return undef; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; |