« lib/Webservice/OVH/Cloud/Project/Network/Private.pm
↑ Summary
lib/Webservice/OVH/Email/Domain/Domain/Redirection.pm »
| 1 | package Webservice::OVH::Email::Domain::Domain::Redirection; | ||
| 2 | |||
| 3 | =encoding utf-8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Webservice::OVH::Email::Domain::Domain::Redirection | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Webservice::OVH; | ||
| 12 | |||
| 13 | my $ovh = Webservice::OVH->new_from_json("credentials.json"); | ||
| 14 | |||
| 15 | my $email_domain = $ovh->email->domain->domain('testdomain.de'); | ||
| 16 | |||
| 17 | my $redirection = $email_domain->new_redirection(from => 'test@test.de', to => 'test2@test.de', local_copy => 'false'); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Provides the ability to create, delete and change redirections for an email-domain. | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | use Carp qw{ carp croak }; | ||
| 30 | use JSON; | ||
| 31 | |||
| 32 | our $VERSION = 0.47; | ||
| 33 | |||
| 34 | use Webservice::OVH::Helper; | ||
| 35 | |||
| 36 | =head2 _new_existing | ||
| 37 | |||
| 38 | Internal Method to create a Redirection object. | ||
| 39 | This method should never be called directly. | ||
| 40 | |||
| 41 | =over | ||
| 42 | |||
| 43 | =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object, $domain - parent domain Objekt, $redirection_id => api intern id | ||
| 44 | |||
| 45 | =item * Return: L<Webservice::OVH::Email::Domain::Domain::Redirection> | ||
| 46 | |||
| 47 | =item * Synopsis: Webservice::OVH::Email::Domain::Domain::Redirection->_new_existing($ovh_api_wrapper, $domain, $redirection_id, $module); | ||
| 48 | |||
| 49 | =back | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub _new_existing { | ||
| 54 | |||
| 55 | my ( $class, %params ) = @_; | ||
| 56 | |||
| 57 | die "Missing module" unless $params{module}; | ||
| 58 | die "Missing wrapper" unless $params{wrapper}; | ||
| 59 | die "Missing id" unless $params{id}; | ||
| 60 | die "Missing domain" unless $params{domain}; | ||
| 61 | |||
| 62 | my $module = $params{module}; | ||
| 63 | my $api_wrapper = $params{wrapper}; | ||
| 64 | my $redirection_id = $params{id}; | ||
| 65 | my $domain = $params{domain}; | ||
| 66 | |||
| 67 | die "Missing redirection_id" unless $redirection_id; | ||
| 68 | my $domain_name = $domain->name; | ||
| 69 | |||
| 70 | my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $redirection_id, _properties => undef, _domain => $domain }, $class; | ||
| 71 | return $self; | ||
| 72 | } | ||
| 73 | |||
| 74 | =head2 _new | ||
| 75 | |||
| 76 | Internal Method to create the Redirection object. | ||
| 77 | This method should never be called directly. | ||
| 78 | |||
| 79 | =over | ||
| 80 | |||
| 81 | =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object, $domain - parent domain, %params - key => value | ||
| 82 | |||
| 83 | =item * Return: L<Webservice::OVH::Email::Domain::Domain::Redirection> | ||
| 84 | |||
| 85 | =item * Synopsis: Webservice::OVH::Email::Domain::Domain::Redirection->_new($ovh_api_wrapper, $domain, $module, from => 'from@test.com', to => 'to@test.com', local_copy => 'false'); | ||
| 86 | |||
| 87 | =back | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub _new { | ||
| 92 | |||
| 93 | my ( $class, %params ) = @_; | ||
| 94 | |||
| 95 | die "Missing module" unless $params{module}; | ||
| 96 | die "Missing wrapper" unless $params{wrapper}; | ||
| 97 | die "Missing domain" unless $params{domain}; | ||
| 98 | |||
| 99 | my $module = $params{module}; | ||
| 100 | my $api_wrapper = $params{wrapper}; | ||
| 101 | my $domain = $params{domain}; | ||
| 102 | |||
| 103 | my @keys_needed = qw{ from local_copy to }; | ||
| 104 | |||
| 105 | if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) { | ||
| 106 | |||
| 107 | croak "Missing parameter: @missing_parameters"; | ||
| 108 | } | ||
| 109 | |||
| 110 | my $local_copy = $params{local_copy} eq 'true' || $params{local_copy} eq '1' || $params{local_copy} eq 'yes' ? JSON::true : JSON::false; | ||
| 111 | |||
| 112 | my $domain_name = $domain->name; | ||
| 113 | my $body = {}; | ||
| 114 | $body->{from} = Webservice::OVH::Helper->trim($params{from}); | ||
| 115 | $body->{to} = Webservice::OVH::Helper->trim($params{to}); | ||
| 116 | $body->{localCopy} = $local_copy; | ||
| 117 | my $response = $api_wrapper->rawCall( method => 'post', path => "/email/domain/$domain_name/redirection/", body => $body, noSignature => 0 ); | ||
| 118 | croak $response->error if $response->error; | ||
| 119 | |||
| 120 | my $redirection_id = $response->{id}; | ||
| 121 | |||
| 122 | my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $redirection_id, _properties => undef, _domain => $domain }, $class; | ||
| 123 | |||
| 124 | return $self; | ||
| 125 | } | ||
| 126 | |||
| 127 | =head2 is_valid | ||
| 128 | |||
| 129 | When this redirection is deleted on the api side, this method returns 0. | ||
| 130 | |||
| 131 | =over | ||
| 132 | |||
| 133 | =item * Return: VALUE | ||
| 134 | |||
| 135 | =item * Synopsis: print "Valid" if $redirection->is_valid; | ||
| 136 | |||
| 137 | =back | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub is_valid { | ||
| 142 | |||
| 143 | my ($self) = @_; | ||
| 144 | |||
| 145 | $self->properties; | ||
| 146 | |||
| 147 | return $self->{_valid}; | ||
| 148 | } | ||
| 149 | |||
| 150 | =head2 id | ||
| 151 | |||
| 152 | Returns the api id. | ||
| 153 | |||
| 154 | =over | ||
| 155 | |||
| 156 | =item * Return: VALUE | ||
| 157 | |||
| 158 | =item * Synopsis: my $id = $redirection->id; | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub id { | ||
| 165 | |||
| 166 | my ($self) = @_; | ||
| 167 | |||
| 168 | return $self->{_id}; | ||
| 169 | } | ||
| 170 | |||
| 171 | =head2 domain | ||
| 172 | |||
| 173 | Returns the email-domain this redirection is attached to. | ||
| 174 | |||
| 175 | =over | ||
| 176 | |||
| 177 | =item * Return: L<Webservice::Email::Domain::Domain> | ||
| 178 | |||
| 179 | =item * Synopsis: my $email_domain = $redirection->domain; | ||
| 180 | |||
| 181 | =back | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | sub domain { | ||
| 186 | |||
| 187 | my ($self) = @_; | ||
| 188 | |||
| 189 | return $self->{_domain}; | ||
| 190 | } | ||
| 191 | |||
| 192 | =head2 properties | ||
| 193 | |||
| 194 | Returns the raw properties as a hash. | ||
| 195 | This is the original return value of the web-api. | ||
| 196 | |||
| 197 | =over | ||
| 198 | |||
| 199 | =item * Return: HASH | ||
| 200 | |||
| 201 | =item * Synopsis: my $properties = $record->properties; | ||
| 202 | |||
| 203 | =back | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | sub properties { | ||
| 208 | |||
| 209 | my ($self) = @_; | ||
| 210 | |||
| 211 | return unless $self->{_valid}; | ||
| 212 | |||
| 213 | my $api = $self->{_api_wrapper}; | ||
| 214 | my $domain_name = $self->domain->name; | ||
| 215 | my $redirection_id = $self->id; | ||
| 216 | my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/redirection/$redirection_id", noSignature => 0 ); | ||
| 217 | carp $response->error if $response->error; | ||
| 218 | |||
| 219 | if ( $response->error ) { | ||
| 220 | |||
| 221 | $self->{_valid} = 0; | ||
| 222 | $self->{_properties} = undef; | ||
| 223 | return; | ||
| 224 | |||
| 225 | } else { | ||
| 226 | |||
| 227 | $self->{_properties} = $response->content; | ||
| 228 | return $self->{_properties}; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | =head2 field_type | ||
| 233 | |||
| 234 | Exposed property value. | ||
| 235 | |||
| 236 | =over | ||
| 237 | |||
| 238 | =item * Return: VALUE | ||
| 239 | |||
| 240 | =item * Synopsis: my $from = $record->from; | ||
| 241 | |||
| 242 | =back | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub from { | ||
| 247 | |||
| 248 | my ($self) = @_; | ||
| 249 | |||
| 250 | $self->properties unless $self->{_properties}; | ||
| 251 | return unless $self->{_valid}; | ||
| 252 | |||
| 253 | return $self->{_properties}->{from}; | ||
| 254 | } | ||
| 255 | |||
| 256 | =head2 to | ||
| 257 | |||
| 258 | Exposed property value. | ||
| 259 | |||
| 260 | =over | ||
| 261 | |||
| 262 | =item * Return: VALUE | ||
| 263 | |||
| 264 | =item * Synopsis: my $to = $record->to; | ||
| 265 | |||
| 266 | =back | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | sub to { | ||
| 271 | |||
| 272 | my ($self) = @_; | ||
| 273 | |||
| 274 | $self->properties unless $self->{_properties}; | ||
| 275 | return unless $self->{_valid}; | ||
| 276 | |||
| 277 | return $self->{_properties}->{to}; | ||
| 278 | } | ||
| 279 | |||
| 280 | =head2 delete | ||
| 281 | |||
| 282 | Deletes the redirection api sided and sets this object invalid. | ||
| 283 | |||
| 284 | =over | ||
| 285 | |||
| 286 | =item * Synopsis: $redirection->delete; | ||
| 287 | |||
| 288 | =back | ||
| 289 | |||
| 290 | =cut | ||
| 291 | |||
| 292 | sub delete { | ||
| 293 | |||
| 294 | my ($self) = @_; | ||
| 295 | |||
| 296 | return unless $self->{_valid}; | ||
| 297 | |||
| 298 | my $api = $self->{_api_wrapper}; | ||
| 299 | my $domain_name = $self->domain->name; | ||
| 300 | my $redirection_id = $self->id; | ||
| 301 | my $response = $api->rawCall( method => 'delete', path => "/email/domain/$domain_name/redirection/$redirection_id", noSignature => 0 ); | ||
| 302 | croak $response->error if $response->error; | ||
| 303 | |||
| 304 | $self->{_valid} = 0; | ||
| 305 | } | ||
| 306 | |||
| 307 | =head2 change | ||
| 308 | |||
| 309 | Changes the redirection | ||
| 310 | |||
| 311 | =over | ||
| 312 | |||
| 313 | =item * Parameter: %params - key => value to | ||
| 314 | |||
| 315 | =item * Synopsis: $redirection->change(to => 'test@test.de'); | ||
| 316 | |||
| 317 | =back | ||
| 318 | |||
| 319 | =cut | ||
| 320 | |||
| 321 | sub change { | ||
| 322 | |||
| 323 | my ( $self, $to ) = @_; | ||
| 324 | |||
| 325 | return unless $self->{_valid}; | ||
| 326 | |||
| 327 | croak "Missing to as parameter" unless $to; | ||
| 328 | |||
| 329 | my $api = $self->{_api_wrapper}; | ||
| 330 | my $domain_name = $self->domain->name; | ||
| 331 | my $redirection_id = $self->id; | ||
| 332 | my $body = {}; | ||
| 333 | $body->{to} = Webservice::OVH::Helper->trim($to); | ||
| 334 | my $response = $api->rawCall( method => 'post', path => "/email/domain/$domain_name/redirection/$redirection_id/changeRedirection", body => $body, noSignature => 0 ); | ||
| 335 | croak $response->error if $response->error; | ||
| 336 | |||
| 337 | my $task_id = $response->content->{id}; | ||
| 338 | my $task = Webservice::OVH::Email::Domain::Domain::Task::Redirection->_new_existing( wrapper => $api, domain => $self->domain, id => $task_id, module => $self->{_module} ); | ||
| 339 | |||
| 340 | return $task; | ||
| 341 | } | ||
| 342 | |||
| 343 | =head2 tasks | ||
| 344 | |||
| 345 | Get all associated tasks | ||
| 346 | |||
| 347 | =over | ||
| 348 | |||
| 349 | =item * Return: HASH | ||
| 350 | |||
| 351 | =item * Synopsis: $mailinglist->tasks; | ||
| 352 | |||
| 353 | =back | ||
| 354 | |||
| 355 | =cut | ||
| 356 | |||
| 357 | sub tasks { | ||
| 358 | |||
| 359 | my ($self) = @_; | ||
| 360 | |||
| 361 | return unless $self->{_valid}; | ||
| 362 | |||
| 363 | my $domain_name = $self->domain->name; | ||
| 364 | my $api = $self->{_api_wrapper}; | ||
| 365 | my $id = $self->id; | ||
| 366 | |||
| 367 | my $response = $api->rawCall( method => 'get', path => sprintf( "/email/domain/$domain_name/task/redirection?account=%s", $id ), noSignature => 0 ); | ||
| 368 | croak $response->error if $response->error; | ||
| 369 | |||
| 370 | my $taks = $response->content || []; | ||
| 371 | |||
| 372 | return unless scalar @$taks; | ||
| 373 | |||
| 374 | return $taks; | ||
| 375 | |||
| 376 | } | ||
| 377 | |||
| 378 | 1; |