| 1 | package Webservice::OVH::Email::Domain::Domain::Account; | ||
| 2 | |||
| 3 | =encoding utf-8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Webservice::OVH::Email::Domain::Domain::Account | ||
| 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 $account = $email_domain->new_account( account_name => 'testaccount', password => $password, description => 'a test account', size => 50000000 ); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Provides access to email accounts. | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | use Carp qw{ carp croak }; | ||
| 30 | |||
| 31 | our $VERSION = 0.47; | ||
| 32 | |||
| 33 | use Webservice::OVH::Helper; | ||
| 34 | |||
| 35 | =head2 _new_existing | ||
| 36 | |||
| 37 | Internal Method to create an Account object. | ||
| 38 | This method should never be called directly. | ||
| 39 | |||
| 40 | =over | ||
| 41 | |||
| 42 | =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object, $domain - parent domain Objekt, $account_name => unique name | ||
| 43 | |||
| 44 | =item * Return: L<Webservice::OVH::Email::Domain::Domain::Account> | ||
| 45 | |||
| 46 | =item * Synopsis: Webservice::OVH::Email::Domain::Domain::Account->_new_existing($ovh_api_wrapper, $domain, $account_name, $module); | ||
| 47 | |||
| 48 | =back | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub _new_existing { | ||
| 53 | |||
| 54 | my ( $class, %params ) = @_; | ||
| 55 | |||
| 56 | die "Missing module" unless $params{module}; | ||
| 57 | die "Missing wrapper" unless $params{wrapper}; | ||
| 58 | die "Missing id" unless $params{id}; | ||
| 59 | die "Missing domain" unless $params{domain}; | ||
| 60 | |||
| 61 | my $module = $params{module}; | ||
| 62 | my $api_wrapper = $params{wrapper}; | ||
| 63 | my $account_name = $params{id}; | ||
| 64 | my $domain = $params{domain}; | ||
| 65 | |||
| 66 | $account_name = lc $account_name; | ||
| 67 | |||
| 68 | my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _name => $account_name, _properties => undef, _domain => $domain }, $class; | ||
| 69 | |||
| 70 | return $self; | ||
| 71 | |||
| 72 | } | ||
| 73 | |||
| 74 | =head2 _new | ||
| 75 | |||
| 76 | Internal Method to create the Account 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::Account> | ||
| 84 | |||
| 85 | =item * Synopsis: Webservice::OVH::Email::Domain::Domain::Account->_new($ovh_api_wrapper, $domain, $module, account_name => $account_name, password => $password, description => $description, size => $size ); | ||
| 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{ account_name password }; | ||
| 104 | if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) { | ||
| 105 | |||
| 106 | croak "Missing parameter: @missing_parameters"; | ||
| 107 | } | ||
| 108 | |||
| 109 | my $domain_name = $domain->name; | ||
| 110 | my $body = {}; | ||
| 111 | $body->{accountName} = Webservice::OVH::Helper->trim($params{account_name}); | ||
| 112 | $body->{password} = $params{password}; | ||
| 113 | $body->{description} = $params{description} if exists $params{description}; | ||
| 114 | $body->{size} = $params{size} if exists $params{size}; | ||
| 115 | my $response = $api_wrapper->rawCall( method => 'post', path => "/email/domain/$domain_name/account", body => $body, noSignature => 0 ); | ||
| 116 | croak $response->error if $response->error; | ||
| 117 | |||
| 118 | my $task_id = $response->content->{id}; | ||
| 119 | my $task = Webservice::OVH::Email::Domain::Domain::Task::Account->_new_existing( wrapper => $api_wrapper, domain => $domain, id => $task_id, module => $module ); | ||
| 120 | |||
| 121 | my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _name => $params{account_name}, _properties => undef, _domain => $domain }, $class; | ||
| 122 | |||
| 123 | return ( $self, $task ); | ||
| 124 | |||
| 125 | } | ||
| 126 | |||
| 127 | =head2 is_valid | ||
| 128 | |||
| 129 | When this account 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 $account->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 name | ||
| 151 | |||
| 152 | Unique identifier. | ||
| 153 | |||
| 154 | =over | ||
| 155 | |||
| 156 | =item * Return: VALUE | ||
| 157 | |||
| 158 | =item * Synopsis: my $name = $account->name; | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub name { | ||
| 165 | |||
| 166 | my ($self) = @_; | ||
| 167 | |||
| 168 | return $self->{_name}; | ||
| 169 | } | ||
| 170 | |||
| 171 | =head2 properties | ||
| 172 | |||
| 173 | Returns the raw properties as a hash. | ||
| 174 | This is the original return value of the web-api. | ||
| 175 | |||
| 176 | =over | ||
| 177 | |||
| 178 | =item * Return: HASH | ||
| 179 | |||
| 180 | =item * Synopsis: my $properties = $account->properties; | ||
| 181 | |||
| 182 | =back | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub properties { | ||
| 187 | |||
| 188 | my ($self) = @_; | ||
| 189 | |||
| 190 | return unless $self->{_valid}; | ||
| 191 | |||
| 192 | my $api = $self->{_api_wrapper}; | ||
| 193 | my $domain_name = $self->domain->name; | ||
| 194 | my $account_name = $self->name; | ||
| 195 | my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/account/$account_name", noSignature => 0 ); | ||
| 196 | carp $response->error if $response->error; | ||
| 197 | |||
| 198 | if ( $response->error ) { | ||
| 199 | |||
| 200 | $self->{_valid} = 0; | ||
| 201 | $self->{_properties} = undef; | ||
| 202 | return; | ||
| 203 | |||
| 204 | } else { | ||
| 205 | |||
| 206 | $self->{_properties} = $response->content; | ||
| 207 | return $self->{_properties}; | ||
| 208 | } | ||
| 209 | |||
| 210 | } | ||
| 211 | |||
| 212 | =head2 is_blocked | ||
| 213 | |||
| 214 | Exposed property value. | ||
| 215 | |||
| 216 | =over | ||
| 217 | |||
| 218 | =item * Return: VALUE | ||
| 219 | |||
| 220 | =item * Synopsis: my $is_blocked = $account->is_blocked; | ||
| 221 | |||
| 222 | =back | ||
| 223 | |||
| 224 | =cut | ||
| 225 | |||
| 226 | sub is_blocked { | ||
| 227 | |||
| 228 | my ($self) = @_; | ||
| 229 | |||
| 230 | $self->properties unless $self->{_properties}; | ||
| 231 | return unless $self->{_valid}; | ||
| 232 | |||
| 233 | return $self->{_properties}->{isBlocked} ? 1 : 0; | ||
| 234 | |||
| 235 | } | ||
| 236 | |||
| 237 | =head2 email | ||
| 238 | |||
| 239 | Exposed property value. | ||
| 240 | |||
| 241 | =over | ||
| 242 | |||
| 243 | =item * Return: VALUE | ||
| 244 | |||
| 245 | =item * Synopsis: my $email = $account->email; | ||
| 246 | |||
| 247 | =back | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | sub email { | ||
| 252 | |||
| 253 | my ($self) = @_; | ||
| 254 | |||
| 255 | $self->properties unless $self->{_properties}; | ||
| 256 | return unless $self->{_valid}; | ||
| 257 | |||
| 258 | return $self->{_properties}->{email}; | ||
| 259 | |||
| 260 | } | ||
| 261 | |||
| 262 | =head2 domain | ||
| 263 | |||
| 264 | Returns the email-domain this account is attached to. | ||
| 265 | |||
| 266 | =over | ||
| 267 | |||
| 268 | =item * Return: L<Webservice::Email::Domain::Domain> | ||
| 269 | |||
| 270 | =item * Synopsis: my $email_domain = $account->domain; | ||
| 271 | |||
| 272 | =back | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | sub domain { | ||
| 277 | |||
| 278 | my ($self) = @_; | ||
| 279 | |||
| 280 | return $self->{_domain}; | ||
| 281 | } | ||
| 282 | |||
| 283 | =head2 description | ||
| 284 | |||
| 285 | Exposed property value. | ||
| 286 | |||
| 287 | =over | ||
| 288 | |||
| 289 | =item * Return: VALUE | ||
| 290 | |||
| 291 | =item * Synopsis: my $description = $account->description; | ||
| 292 | |||
| 293 | =back | ||
| 294 | |||
| 295 | =cut | ||
| 296 | |||
| 297 | sub description { | ||
| 298 | |||
| 299 | my ($self) = @_; | ||
| 300 | |||
| 301 | $self->properties unless $self->{_properties}; | ||
| 302 | return unless $self->{_valid}; | ||
| 303 | |||
| 304 | return $self->{_properties}->{description}; | ||
| 305 | } | ||
| 306 | |||
| 307 | =head2 size | ||
| 308 | |||
| 309 | Exposed property value. | ||
| 310 | |||
| 311 | =over | ||
| 312 | |||
| 313 | =item * Return: VALUE | ||
| 314 | |||
| 315 | =item * Synopsis: my $size = $account->size; | ||
| 316 | |||
| 317 | =back | ||
| 318 | |||
| 319 | =cut | ||
| 320 | |||
| 321 | sub size { | ||
| 322 | |||
| 323 | my ($self) = @_; | ||
| 324 | |||
| 325 | $self->properties unless $self->{_properties}; | ||
| 326 | return unless $self->{_valid}; | ||
| 327 | |||
| 328 | return $self->{_properties}->{size}; | ||
| 329 | } | ||
| 330 | |||
| 331 | =head2 change | ||
| 332 | |||
| 333 | Changes the account | ||
| 334 | |||
| 335 | =over | ||
| 336 | |||
| 337 | =item * Parameter: %params - key => value description size | ||
| 338 | |||
| 339 | =item * Synopsis: $account->change(description => 'authors account', size => 2000000 ); | ||
| 340 | |||
| 341 | =back | ||
| 342 | |||
| 343 | =cut | ||
| 344 | |||
| 345 | sub change { | ||
| 346 | |||
| 347 | my ( $self, %params ) = @_; | ||
| 348 | |||
| 349 | croak "Objet is invalid" unless $self->{_valid}; | ||
| 350 | |||
| 351 | my $api = $self->{_api_wrapper}; | ||
| 352 | my $domain_name = $self->domain->name; | ||
| 353 | my $account_name = $self->name; | ||
| 354 | my $body = {}; | ||
| 355 | $body->{description} = $params{description} if exists $params{description}; | ||
| 356 | $body->{size} = $params{size} if exists $params{size}; | ||
| 357 | my $response = $api->rawCall( method => 'put', path => "/email/domain/$domain_name/account/$account_name", body => $body, noSignature => 0 ); | ||
| 358 | croak $response->error if $response->error; | ||
| 359 | |||
| 360 | $self->properties; | ||
| 361 | |||
| 362 | } | ||
| 363 | |||
| 364 | =head2 delete | ||
| 365 | |||
| 366 | Deletes the account api sided and sets this object invalid. | ||
| 367 | |||
| 368 | =over | ||
| 369 | |||
| 370 | =item * Synopsis: $account->delete; | ||
| 371 | |||
| 372 | =back | ||
| 373 | |||
| 374 | =cut | ||
| 375 | |||
| 376 | sub delete { | ||
| 377 | |||
| 378 | my ( $self, %params ) = @_; | ||
| 379 | |||
| 380 | croak "Objet is invalid" unless $self->{_valid}; | ||
| 381 | |||
| 382 | my $api = $self->{_api_wrapper}; | ||
| 383 | my $domain_name = $self->domain->name; | ||
| 384 | my $account_name = $self->name; | ||
| 385 | my $response = $api->rawCall( method => 'delete', path => "/email/domain/$domain_name/account/$account_name", noSignature => 0 ); | ||
| 386 | croak $response->error if $response->error; | ||
| 387 | |||
| 388 | my $task_id = $response->content->{id}; | ||
| 389 | my $task = Webservice::OVH::Email::Domain::Domain::Task::Account->_new_existing( wrapper => $api, domain => $self->domain, id => $task_id, module => $self->{_module} ); | ||
| 390 | |||
| 391 | $self->{_valid} = 0; | ||
| 392 | |||
| 393 | return $task; | ||
| 394 | } | ||
| 395 | |||
| 396 | =head2 delete | ||
| 397 | |||
| 398 | Deletes the account api sided and sets this object invalid. | ||
| 399 | |||
| 400 | =over | ||
| 401 | |||
| 402 | =item * Parameter: $password - new password | ||
| 403 | |||
| 404 | =item * Synopsis: $account->change_password($password); | ||
| 405 | |||
| 406 | =back | ||
| 407 | |||
| 408 | =cut | ||
| 409 | |||
| 410 | sub change_password { | ||
| 411 | |||
| 412 | my ( $self, $password ) = @_; | ||
| 413 | |||
| 414 | croak "Objet is invalid" unless $self->{_valid}; | ||
| 415 | |||
| 416 | my $api = $self->{_api_wrapper}; | ||
| 417 | my $domain_name = $self->domain->name; | ||
| 418 | my $account_name = $self->name; | ||
| 419 | my $body = { password => $password }; | ||
| 420 | my $response = $api->rawCall( method => 'post', path => "/email/domain/$domain_name/account/$account_name/changePassword", body => $body, noSignature => 0 ); | ||
| 421 | croak $response->error if $response->error; | ||
| 422 | |||
| 423 | my $task_id = $response->content->{id}; | ||
| 424 | my $task = Webservice::OVH::Email::Domain::Domain::Task::Account->_new_existing( wrapper => $api, domain => $self->domain, id => $task_id, module => $self->{_module} ); | ||
| 425 | |||
| 426 | return $task; | ||
| 427 | |||
| 428 | } | ||
| 429 | |||
| 430 | =head2 usage | ||
| 431 | |||
| 432 | Deletes the account api sided and sets this object invalid. | ||
| 433 | |||
| 434 | =over | ||
| 435 | |||
| 436 | =item * Return: HASH | ||
| 437 | |||
| 438 | =item * Synopsis: $account->usage; | ||
| 439 | |||
| 440 | =back | ||
| 441 | |||
| 442 | =cut | ||
| 443 | |||
| 444 | sub usage { | ||
| 445 | |||
| 446 | my ($self) = @_; | ||
| 447 | |||
| 448 | croak "Objet is invalid" unless $self->{_valid}; | ||
| 449 | |||
| 450 | my $api = $self->{_api_wrapper}; | ||
| 451 | my $domain_name = $self->domain->name; | ||
| 452 | my $account_name = $self->name; | ||
| 453 | my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/account/$account_name/usage", noSignature => 0 ); | ||
| 454 | croak $response->error if $response->error; | ||
| 455 | |||
| 456 | return $response->content; | ||
| 457 | |||
| 458 | } | ||
| 459 | |||
| 460 | =head2 tasks | ||
| 461 | |||
| 462 | Get all associated tasks | ||
| 463 | |||
| 464 | =over | ||
| 465 | |||
| 466 | =item * Return: HASH | ||
| 467 | |||
| 468 | =item * Synopsis: $account->tasks; | ||
| 469 | |||
| 470 | =back | ||
| 471 | |||
| 472 | =cut | ||
| 473 | |||
| 474 | sub tasks { | ||
| 475 | |||
| 476 | my ($self) = @_; | ||
| 477 | |||
| 478 | croak "Objet is invalid" unless $self->{_valid}; | ||
| 479 | |||
| 480 | my $domain_name = $self->domain->name; | ||
| 481 | my $api = $self->{_api_wrapper}; | ||
| 482 | my $name = $self->name; | ||
| 483 | |||
| 484 | my $response = $api->rawCall( method => 'get', path => sprintf( "/email/domain/$domain_name/task/account?name=%s", $name ), noSignature => 0 ); | ||
| 485 | croak $response->error if $response->error; | ||
| 486 | |||
| 487 | my $taks = $response->content || []; | ||
| 488 | |||
| 489 | return unless scalar @$taks; | ||
| 490 | |||
| 491 | return $taks; | ||
| 492 | |||
| 493 | } | ||
| 494 | |||
| 495 | 1; |