File Coverage

lib/Webservice/OVH/Email/Domain/Domain/Account.pm
Criterion Covered Total %
statement 12 134 8.9
branch 0 72 0.0
condition 0 2 0.0
subroutine 4 19 21.0
pod 13 13 100.0
total 29 240 12.0


line stmt bran cond sub pod time code
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 36     36   280 use strict;
  36         82  
  36         1102  
28 36     36   203 use warnings;
  36         85  
  36         1011  
29 36     36   194 use Carp qw{ carp croak };
  36         95  
  36         2355  
30              
31             our $VERSION = 0.48;
32              
33 36     36   271 use Webservice::OVH::Helper;
  36         94  
  36         61275  
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 0     0     my ( $class, %params ) = @_;
55              
56 0 0         die "Missing module" unless $params{module};
57 0 0         die "Missing wrapper" unless $params{wrapper};
58 0 0         die "Missing id" unless $params{id};
59 0 0         die "Missing domain" unless $params{domain};
60              
61 0           my $module = $params{module};
62 0           my $api_wrapper = $params{wrapper};
63 0           my $account_name = $params{id};
64 0           my $domain = $params{domain};
65              
66 0           $account_name = lc $account_name;
67              
68 0           my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _name => $account_name, _properties => undef, _domain => $domain }, $class;
69              
70 0           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 0     0     my ( $class, %params ) = @_;
94              
95 0 0         die "Missing module" unless $params{module};
96 0 0         die "Missing wrapper" unless $params{wrapper};
97 0 0         die "Missing domain" unless $params{domain};
98              
99 0           my $module = $params{module};
100 0           my $api_wrapper = $params{wrapper};
101 0           my $domain = $params{domain};
102              
103 0           my @keys_needed = qw{ account_name password };
104 0 0         if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) {
  0            
105              
106 0           croak "Missing parameter: @missing_parameters";
107             }
108              
109 0           my $domain_name = $domain->name;
110 0           my $body = {};
111 0           $body->{accountName} = Webservice::OVH::Helper->trim($params{account_name});
112 0           $body->{password} = $params{password};
113 0 0         $body->{description} = $params{description} if exists $params{description};
114 0 0         $body->{size} = $params{size} if exists $params{size};
115 0           my $response = $api_wrapper->rawCall( method => 'post', path => "/email/domain/$domain_name/account", body => $body, noSignature => 0 );
116 0 0         croak $response->error if $response->error;
117              
118 0           my $task_id = $response->content->{id};
119 0           my $task = Webservice::OVH::Email::Domain::Domain::Task::Account->_new_existing( wrapper => $api_wrapper, domain => $domain, id => $task_id, module => $module );
120              
121 0           my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _name => $params{account_name}, _properties => undef, _domain => $domain }, $class;
122              
123 0           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 0     0 1   my ($self) = @_;
144              
145 0           $self->properties;
146              
147 0           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 0     0 1   my ($self) = @_;
167              
168 0           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 0     0 1   my ($self) = @_;
189              
190 0 0         return unless $self->{_valid};
191              
192 0           my $api = $self->{_api_wrapper};
193 0           my $domain_name = $self->domain->name;
194 0           my $account_name = $self->name;
195 0           my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/account/$account_name", noSignature => 0 );
196 0 0         carp $response->error if $response->error;
197              
198 0 0         if ( $response->error ) {
199              
200 0           $self->{_valid} = 0;
201 0           $self->{_properties} = undef;
202 0           return;
203              
204             } else {
205              
206 0           $self->{_properties} = $response->content;
207 0           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 0     0 1   my ($self) = @_;
229              
230 0 0         $self->properties unless $self->{_properties};
231 0 0         return unless $self->{_valid};
232              
233 0 0         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 0     0 1   my ($self) = @_;
254              
255 0 0         $self->properties unless $self->{_properties};
256 0 0         return unless $self->{_valid};
257              
258 0           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 0     0 1   my ($self) = @_;
279              
280 0           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 0     0 1   my ($self) = @_;
300              
301 0 0         $self->properties unless $self->{_properties};
302 0 0         return unless $self->{_valid};
303              
304 0           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 0     0 1   my ($self) = @_;
324              
325 0 0         $self->properties unless $self->{_properties};
326 0 0         return unless $self->{_valid};
327              
328 0           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 0     0 1   my ( $self, %params ) = @_;
348              
349 0 0         croak "Objet is invalid" unless $self->{_valid};
350              
351 0           my $api = $self->{_api_wrapper};
352 0           my $domain_name = $self->domain->name;
353 0           my $account_name = $self->name;
354 0           my $body = {};
355 0 0         $body->{description} = $params{description} if exists $params{description};
356 0 0         $body->{size} = $params{size} if exists $params{size};
357 0           my $response = $api->rawCall( method => 'put', path => "/email/domain/$domain_name/account/$account_name", body => $body, noSignature => 0 );
358 0 0         croak $response->error if $response->error;
359              
360 0           $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 0     0 1   my ( $self, %params ) = @_;
379              
380 0 0         croak "Objet is invalid" unless $self->{_valid};
381              
382 0           my $api = $self->{_api_wrapper};
383 0           my $domain_name = $self->domain->name;
384 0           my $account_name = $self->name;
385 0           my $response = $api->rawCall( method => 'delete', path => "/email/domain/$domain_name/account/$account_name", noSignature => 0 );
386 0 0         croak $response->error if $response->error;
387              
388 0           my $task_id = $response->content->{id};
389 0           my $task = Webservice::OVH::Email::Domain::Domain::Task::Account->_new_existing( wrapper => $api, domain => $self->domain, id => $task_id, module => $self->{_module} );
390              
391 0           $self->{_valid} = 0;
392              
393 0           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 0     0 1   my ( $self, $password ) = @_;
413              
414 0 0         croak "Objet is invalid" unless $self->{_valid};
415              
416 0           my $api = $self->{_api_wrapper};
417 0           my $domain_name = $self->domain->name;
418 0           my $account_name = $self->name;
419 0           my $body = { password => $password };
420 0           my $response = $api->rawCall( method => 'post', path => "/email/domain/$domain_name/account/$account_name/changePassword", body => $body, noSignature => 0 );
421 0 0         croak $response->error if $response->error;
422              
423 0           my $task_id = $response->content->{id};
424 0           my $task = Webservice::OVH::Email::Domain::Domain::Task::Account->_new_existing( wrapper => $api, domain => $self->domain, id => $task_id, module => $self->{_module} );
425              
426 0           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 0     0 1   my ($self) = @_;
447              
448 0 0         croak "Objet is invalid" unless $self->{_valid};
449              
450 0           my $api = $self->{_api_wrapper};
451 0           my $domain_name = $self->domain->name;
452 0           my $account_name = $self->name;
453 0           my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/account/$account_name/usage", noSignature => 0 );
454 0 0         croak $response->error if $response->error;
455              
456 0           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 0     0 1   my ($self) = @_;
477              
478 0 0         croak "Objet is invalid" unless $self->{_valid};
479              
480 0           my $domain_name = $self->domain->name;
481 0           my $api = $self->{_api_wrapper};
482 0           my $name = $self->name;
483              
484 0           my $response = $api->rawCall( method => 'get', path => sprintf( "/email/domain/$domain_name/task/account?name=%s", $name ), noSignature => 0 );
485 0 0         croak $response->error if $response->error;
486              
487 0   0       my $taks = $response->content || [];
488              
489 0 0         return unless scalar @$taks;
490              
491 0           return $taks;
492              
493             }
494              
495             1;