| 1 | package Webservice::OVH::Cloud::Project::Network; | ||
| 2 | |||
| 3 | =encoding utf-8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Webservice::OVH::Cloud::Project::Network | ||
| 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 to private and in future public networks. | ||
| 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; | ||
| 40 | |||
| 41 | =head2 _new_existing | ||
| 42 | |||
| 43 | Internal Method to create the Network 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::SSH> | ||
| 51 | |||
| 52 | =item * Synopsis: Webservice::OVH::Cloud::Project::SSH->_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 { _project => $params{project}, _module => $params{module}, _api_wrapper => $params{wrapper}, _private => {}, _available_private => [] }, $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->network->project; | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub project { | ||
| 86 | |||
| 87 | my ($self) = @_; | ||
| 88 | |||
| 89 | return $self->{_project}; | ||
| 90 | } | ||
| 91 | |||
| 92 | =head2 private_exists | ||
| 93 | |||
| 94 | Returns 1 if object is available for the connected account, 0 if not. | ||
| 95 | |||
| 96 | =over | ||
| 97 | |||
| 98 | =item * Parameter: $network_id - api id, $no_recheck - (optional)only for internal usage | ||
| 99 | |||
| 100 | =item * Return: VALUE | ||
| 101 | |||
| 102 | =item * Synopsis: print "network exists" if $project->network->private_exists($id); | ||
| 103 | |||
| 104 | =back | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub private_exists { | ||
| 109 | |||
| 110 | my ( $self, $network_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/network/private", noSignature => 0 ); | ||
| 117 | croak $response->error if $response->error; | ||
| 118 | |||
| 119 | my $list = $response->content; | ||
| 120 | my @net_ids = grep { $_ = $_->{id} } @$list; | ||
| 121 | |||
| 122 | return ( grep { $_ eq $network_id } @net_ids ) ? 1 : 0; | ||
| 123 | |||
| 124 | } else { | ||
| 125 | |||
| 126 | my $list = $self->{_available_private}; | ||
| 127 | |||
| 128 | return ( grep { $_ eq $network_id } @$list ) ? 1 : 0; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | =head2 privates | ||
| 133 | |||
| 134 | Produces an array of all available private networks that are connected to the project. | ||
| 135 | |||
| 136 | =over | ||
| 137 | |||
| 138 | =item * Return: ARRAY | ||
| 139 | |||
| 140 | =item * Synopsis: my $private_networks = $project->privates; | ||
| 141 | |||
| 142 | =back | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub privates { | ||
| 147 | |||
| 148 | my ( $self, %filter ) = @_; | ||
| 149 | |||
| 150 | my $api = $self->{_api_wrapper}; | ||
| 151 | my $project_id = $self->project->id; | ||
| 152 | my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/network/private", noSignature => 0 ); | ||
| 153 | croak $response->error if $response->error; | ||
| 154 | |||
| 155 | my $private_array = $response->content; | ||
| 156 | my $privates = []; | ||
| 157 | my @net_ids = grep { $_ = $_->{id} } @$private_array; | ||
| 158 | $self->{_available_private} = \@net_ids; | ||
| 159 | |||
| 160 | foreach my $network_id (@net_ids) { | ||
| 161 | |||
| 162 | if ( $self->private_exists( $network_id, 1 ) ) { | ||
| 163 | my $private = $self->{_private}{$network_id} = $self->{_private}{$network_id} || Webservice::OVH::Cloud::Project::Network::Private->_new_existing( wrapper => $api, module => $self->{_module}, project => $self->project, id => $network_id ); | ||
| 164 | push @$privates, $private; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | return $privates; | ||
| 169 | } | ||
| 170 | |||
| 171 | =head2 private | ||
| 172 | |||
| 173 | Returns a single object by id | ||
| 174 | |||
| 175 | =over | ||
| 176 | |||
| 177 | =item * Parameter: $network_id - api id | ||
| 178 | |||
| 179 | =item * Return: L<Webservice::OVH::Cloud::Project::Network::Private> | ||
| 180 | |||
| 181 | =item * Synopsis: my $private_network = $project->private($id); | ||
| 182 | |||
| 183 | =back | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | sub private { | ||
| 188 | |||
| 189 | my ( $self, $network_id ) = @_; | ||
| 190 | |||
| 191 | if ( $self->private_exists($network_id) ) { | ||
| 192 | |||
| 193 | my $api = $self->{_api_wrapper}; | ||
| 194 | my $private = $self->{_private}{$network_id} = $self->{_private}{$network_id} || Webservice::OVH::Cloud::Project::Network::Private->_new_existing( wrapper => $api, module => $self->{_module}, project => $self->project, id => $network_id ); | ||
| 195 | |||
| 196 | return $private; | ||
| 197 | } else { | ||
| 198 | |||
| 199 | carp "Network $network_id doesn't exists"; | ||
| 200 | return undef; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 create_private | ||
| 205 | |||
| 206 | Creates a new network. | ||
| 207 | |||
| 208 | =over | ||
| 209 | |||
| 210 | =item * Parameter: %params - key => value (required) vlan_id name (optional) region | ||
| 211 | |||
| 212 | =item * Return: <Webservice::OVH::Cloud::Project::Network::Private> | ||
| 213 | |||
| 214 | =item * Synopsis: my $private_network = $project->create_private( vlan_id => 6, name => 'Test Network' ); | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub create_private { | ||
| 221 | |||
| 222 | my ( $self, %params ) = @_; | ||
| 223 | |||
| 224 | return Webservice::OVH::Cloud::Project::Network::Private->_new( module => $self->{_module}, wrapper => $self->{_api_wrapper}, project => $self->project, %params ); | ||
| 225 | } | ||
| 226 | |||
| 227 | 1; |