File Coverage

lib/Webservice/OVH/Email/Domain/Domain.pm
Criterion Covered Total %
statement 24 188 12.7
branch 0 62 0.0
condition 0 33 0.0
subroutine 8 34 23.5
pod 17 25 68.0
total 49 342 14.3


line stmt bran cond sub pod time code
1              
2             =encoding utf-8
3              
4             =head1 NAME
5              
6             Webservice::OVH::Email::Domain::Domain
7              
8             =head1 SYNOPSIS
9              
10             use Webservice::OVH;
11            
12             my $ovh = Webservice::OVH->new_from_json("credentials.json");
13            
14             my $email_domain = $ovh->email->domain->domain('testdomain.de');
15              
16             =head1 DESCRIPTION
17              
18             Provides access to api email-domain methods like mailinglists, accounts and redirections.
19              
20             =head1 METHODS
21              
22             =cut
23              
24             use strict;
25 36     36   211 use warnings;
  36         92  
  36         1150  
26 36     36   193 use Carp qw{ carp croak };
  36         72  
  36         1077  
27 36     36   202  
  36         94  
  36         2343  
28             our $VERSION = 0.47;
29              
30             use Webservice::OVH::Email::Domain::Domain::Redirection;
31 36     36   14263 use Webservice::OVH::Email::Domain::Domain::Account;
  36         93  
  36         1063  
32 36     36   14827 use Webservice::OVH::Email::Domain::Domain::MailingList;
  36         118  
  36         2480  
33 36     36   16516 use Webservice::OVH::Email::Domain::Domain::Task;
  36         112  
  36         1206  
34 36     36   301 use Webservice::OVH::Helper;
  36         74  
  36         559  
35 36     36   166  
  36         78  
  36         73761  
36             =head2 _new
37              
38             Internal Method to create the domain object.
39             This method is not ment to be called external.
40              
41             =over
42              
43             =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object
44              
45             =item * Return: L<Webservice::OVH::Email::Domain>
46              
47             =item * Synopsis: Webservice::OVH::Email::Domain->_new($ovh_api_wrapper, $zone_name, $module);
48              
49             =back
50              
51             =cut
52              
53              
54             my ( $class, %params ) = @_;
55              
56 0     0     die "Missing module" unless $params{module};
57             die "Missing wrapper" unless $params{wrapper};
58 0 0         die "Missing id" unless $params{id};
59 0 0          
60 0 0         my $module = $params{module};
61             my $api_wrapper = $params{wrapper};
62 0           my $domain = $params{id};
63 0            
64 0           croak "Missing domain name" unless $domain;
65              
66 0 0         my $self = bless {
67             _module => $module,
68 0           _api_wrapper => $api_wrapper,
69             _name => $domain,
70             _service_infos => undef,
71             _properties => undef,
72             _redirections => {},
73             _accounts => {},
74             _mailing_lists => {},
75             }, $class;
76              
77             my $task = Webservice::OVH::Email::Domain::Domain::Task->_new( module => $module, wrapper => $api_wrapper, domain => $self );
78             $self->{_task} = $task;
79 0            
80 0           return $self;
81             }
82 0            
83             =head2 service_infos
84              
85             Retrieves additional infos about the email-domain.
86             Not part of the properties
87              
88             =over
89              
90             =item * Return: HASH
91              
92             =item * Synopsis: my $info = $email_domain->service_infos;
93              
94             =back
95              
96             =cut
97              
98              
99             my ($self) = @_;
100              
101             my $api = $self->{_api_wrapper};
102 0     0 1   my $domain = $self->name;
103             my $response_service_info = $api->rawCall( method => 'get', path => "/email/domain/$domain/serviceInfos", noSignature => 0 );
104 0            
105 0           croak $response_service_info->error if $response_service_info->error;
106 0            
107             $self->{_service_infos} = $response_service_info->content;
108 0 0          
109             return $self->{_service_infos};
110 0           }
111              
112 0           =head2 quota
113              
114             Retrieves info about quotas.
115             Not part of the properties
116              
117             =over
118              
119             =item * Return: HASH
120              
121             =item * Synopsis: my $info = $email_domain->quota;
122              
123             =back
124              
125             =cut
126              
127              
128             my ($self) = @_;
129              
130             my $api = $self->{_api_wrapper};
131             my $domain = $self->name;
132 0     0 1   my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain/quota", noSignature => 0 );
133              
134 0           croak $response->error if $response->error;
135 0           return $response->content;
136 0           }
137              
138 0 0         =head2 name
139 0            
140             Name is the unique identifier.
141              
142             =over
143              
144             =item * Return: VALUE
145              
146             =item * Synopsis: my $name = $email_domain->name;
147              
148             =back
149              
150             =cut
151              
152              
153             my ($self) = @_;
154              
155             return $self->{_name};
156             }
157              
158 0     0 1   =head2 properties
159              
160 0           Retrieves properties of the email-domain.
161             This method updates the intern property variable.
162              
163             =over
164              
165             =item * Return: HASH
166              
167             =item * Synopsis: my $properties = $email_domain->properties;
168              
169             =back
170              
171             =cut
172              
173              
174             my ($self) = @_;
175              
176             my $api = $self->{_api_wrapper};
177             my $domain = $self->name;
178             my $response_properties = $api->rawCall( method => 'get', path => "/email/domain/$domain", noSignature => 0 );
179             croak $response_properties->error if $response_properties->error;
180 0     0 1    
181             $self->{_properties} = $response_properties->content;
182 0            
183 0           return $self->{_properties};
184 0           }
185 0 0          
186             =head2 allowed_account_size
187 0            
188             Exposed Property Value. Readonly.
189 0            
190             =over
191              
192             =item * Return: VALUE
193              
194             =item * Synopsis: my $allowed_account_size = $email_domain->allowed_account_size;
195              
196             =back
197              
198             =cut
199              
200              
201             my ($self) = @_;
202              
203             return $self->{_properties}->{allowedAccountSize};
204             }
205              
206             =head2 creation_date
207              
208 0     0 1   Exposed Property Value. Readonly.
209              
210 0           =over
211              
212             =item * Return: DateTime
213              
214             =item * Synopsis: my $creation_date = $email_domain->creation_date;
215              
216             =back
217              
218             =cut
219              
220              
221             my ($self) = @_;
222              
223             my $str_datetime = $self->{_properties}->{creationDate};
224             my $datetime = Webservice::OVH::Helper->parse_datetime($str_datetime);
225             return $datetime;
226             }
227              
228             =head2 filerz
229 0     0 1    
230             Exposed Property Value. Readonly.
231 0            
232 0           =over
233 0            
234             =item * Return: VALUE
235              
236             =item * Synopsis: my $filerz = $email_domain->filerz;
237              
238             =back
239              
240             =cut
241              
242              
243             my ($self) = @_;
244              
245             return $self->{_properties}->{filerz};
246             }
247              
248             =head2 status
249              
250             Exposed Property Value. Readonly.
251              
252 0     0 1   =over
253              
254 0           =item * Return: VALUE
255              
256             =item * Synopsis: my $status = $email_domain->status;
257              
258             =back
259              
260             =cut
261              
262              
263             my ($self) = @_;
264              
265             return $self->{_properties}->{status};
266             }
267              
268              
269             my ($self) = @_;
270              
271             my $api = $self->{_api_wrapper};
272             my $domain_name = $self->name;
273 0     0 1   my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/redirection", noSignature => 0 );
274             croak $response->error if $response->error;
275 0            
276             return scalar @{ $response->content };
277             }
278              
279             =head2 redirections
280 0     0 0    
281             Produces an array of all available redirections that are connected to the email-domain.
282 0            
283 0           =over
284 0            
285 0 0         =item * Return: L<ARRAY>
286              
287 0           =item * Synopsis: my $redirections = $email_domain->redirections();
  0            
288              
289             =back
290              
291             =cut
292              
293              
294             my ( $self, %filter ) = @_;
295              
296             my $filter_from = ( exists $filter{from} && !$filter{from} ) ? "_empty_" : $filter{from};
297             my $filter_to = ( exists $filter{to} && !$filter{to} ) ? "_empty_" : $filter{to};
298             my $filter = Webservice::OVH::Helper->construct_filter( "from" => $filter_from, "to" => $filter_to );
299              
300             my $api = $self->{_api_wrapper};
301             my $domain_name = $self->name;
302             my $response = $api->rawCall( method => 'get', path => sprintf( "/email/domain/$domain_name/redirection%s", $filter ), noSignature => 0 );
303             croak $response->error if $response->error;
304              
305             my $redirection_ids = $response->content;
306 0     0 1   my $redirections = [];
307              
308 0 0 0       foreach my $redirection_id (@$redirection_ids) {
309 0 0 0        
310 0           my $redirection = $self->{_redirections}{$redirection_id} = $self->{_redirections}{$redirection_id} || Webservice::OVH::Email::Domain::Domain::Redirection->_new_existing( wrapper => $api, domain => $self, id => $redirection_id, module => $self->{_module} );
311             push @$redirections, $redirection;
312 0           }
313 0            
314 0           return $redirections;
315 0 0         }
316              
317 0            
318 0           my ( $self, $redirection_id ) = @_;
319              
320 0           croak "Missing redirection_id" unless $redirection_id;
321              
322 0   0       my $api = $self->{_api_wrapper};
323 0           my $domain_name = $self->name;
324              
325             my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/redirection/$redirection_id", noSignature => 0 );
326 0            
327             return $response->error ? 0 : 1;
328             }
329              
330             =head2 redirection
331 0     0 0    
332             Returns a single redirection by id
333 0 0          
334             =over
335 0            
336 0           =item * Parameter: $redirection_id - id
337              
338 0           =item * Return: L<Webservice::OVH::Email::Domain::Domain::Redirection>
339              
340 0 0         =item * Synopsis: my $service = $email_domain->redirection(12345);
341              
342             =back
343              
344             =cut
345              
346              
347             my ( $self, $redirection_id ) = @_;
348              
349             croak "Missing redirection_id" unless $redirection_id;
350              
351             if ( $self->redirection_exists($redirection_id) ) {
352              
353             my $api = $self->{_api_wrapper};
354             my $from_array_redirection = $self->{_redirections}{$redirection_id} if $self->{_redirections}{$redirection_id} && $self->{_redirections}{$redirection_id}->is_valid;
355             my $redirection = $self->{_redirections}{$redirection_id} = $from_array_redirection || Webservice::OVH::Email::Domain::Domain::Redirection->_new_existing( wrapper => $api, domain => $self, id => $redirection_id, module => $self->{_module} );
356              
357             return $redirection;
358              
359             } else {
360              
361 0     0 1   return undef;
362             }
363 0 0         }
364              
365 0 0         =head2 new_redirection
366              
367 0           Creates a new redirection.
368 0 0 0        
369 0   0       =over
370              
371 0           =item * Parameter: %params - key => value from to local_copy
372              
373             =item * Return: L<Webservice::Email::Domain::Domain::Redirection>
374              
375 0           =item * Synopsis: my $redirection = $email_domain->new_redirection(from => 'test@test.de', to => 'test2@test.de', local_copy => 'false');
376              
377             =back
378              
379             =cut
380              
381              
382             my ( $self, %params ) = @_;
383              
384             my $api = $self->{_api_wrapper};
385             my $redirection = Webservice::OVH::Email::Domain::Domain::Redirection->_new( wrapper => $api, domain => $self, module => $self->{_module}, %params );
386              
387             return $redirection;
388             }
389              
390              
391             my ($self) = @_;
392              
393             my $api = $self->{_api_wrapper};
394             my $domain_name = $self->name;
395             my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/account", noSignature => 0 );
396             croak $response->error if $response->error;
397 0     0 1    
398             return scalar @{ $response->content };
399 0           }
400 0            
401             =head2 accounts
402 0            
403             Produces an array of all available accounts that are connected to the email-domain.
404              
405             =over
406              
407 0     0 0   =item * Return: L<ARRAY>
408              
409 0           =item * Synopsis: my $accounts = $email_domain->accounts();
410 0            
411 0           =back
412 0 0          
413             =cut
414 0            
  0            
415              
416             my ($self) = @_;
417              
418             my $api = $self->{_api_wrapper};
419             my $domain_name = $self->name;
420             my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/account", noSignature => 0 );
421             croak $response->error if $response->error;
422              
423             my $account_names = $response->content;
424             my $accounts = [];
425              
426             foreach my $account_name (@$account_names) {
427             my $account = $self->{_accounts}{$account_name} = $self->{_accounts}{$account_name} || Webservice::OVH::Email::Domain::Domain::Account->_new_existing( wrapper => $api, domain => $self, id => $account_name, module => $self->{_module} );
428             push @$accounts, $account;
429             }
430              
431             return $accounts;
432             }
433 0     0 1    
434              
435 0           my ( $self, $account_name ) = @_;
436 0            
437 0           croak "Missing account_name" unless $account_name;
438 0 0          
439             my $api = $self->{_api_wrapper};
440 0           my $domain_name = $self->name;
441 0            
442             my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/account/$account_name", noSignature => 0 );
443 0            
444 0   0       return $response->error ? 0 : 1;
445 0           }
446              
447             =head2 account
448 0            
449             Returns a single account by name
450              
451             =over
452              
453 0     0 0   =item * Parameter: $account_name - name
454              
455 0 0         =item * Return: L<Webservice::OVH::Email::Domain::Domain::Account>
456              
457 0           =item * Synopsis: my $account = $email_domain->account('testaccount');
458 0            
459             =back
460 0            
461             =cut
462 0 0          
463              
464             my ( $self, $account_name ) = @_;
465              
466             croak "Missing account_name" unless $account_name;
467              
468             $account_name = lc $account_name;
469              
470             if ( $self->account_exists($account_name) ) {
471              
472             my $api = $self->{_api_wrapper};
473             my $from_array_account = $self->{_accounts}{$account_name} if $self->{_accounts}{$account_name} && $self->{_accounts}{$account_name}->is_valid;
474             my $account = $self->{_accounts}{$account_name} = $from_array_account || Webservice::OVH::Email::Domain::Domain::Account->_new_existing( wrapper => $api, domain => $self, id => $account_name, module => $self->{_module} );
475              
476             return $account;
477              
478             } else {
479              
480             return undef;
481             }
482             }
483 0     0 1    
484             =head2 new_account
485 0 0          
486             Creates a new account.
487 0            
488             =over
489 0 0          
490             =item * Parameter: %params - key => value account_name password description size
491 0            
492 0 0 0       =item * Return: L<Webservice::Email::Domain::Domain::Account>
493 0   0        
494             =item * Synopsis: my $account = $email_domain->new_account(account_name => 'testaccount', password => $password, description => 'a test account', size => 5000000 );
495 0            
496             =back
497              
498             =cut
499 0            
500              
501             my ( $self, %params ) = @_;
502              
503             my $api = $self->{_api_wrapper};
504             my ( $account, $task ) = Webservice::OVH::Email::Domain::Domain::Account->_new( wrapper => $api, domain => $self, module => $self->{_module}, %params );
505              
506             return ( $account, $task );
507             }
508              
509              
510             my ($self) = @_;
511              
512             my $api = $self->{_api_wrapper};
513             my $domain_name = $self->name;
514             my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/mailingList", noSignature => 0 );
515             croak $response->error if $response->error;
516              
517             return scalar @{ $response->content };
518             }
519              
520              
521 0     0 1   my ($self) = @_;
522              
523 0           my $api = $self->{_api_wrapper};
524 0           my $domain_name = $self->name;
525             my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/mailingList", noSignature => 0 );
526 0           croak $response->error if $response->error;
527              
528             return $response->content;
529             }
530              
531 0     0 0   =head2 mailing_lists
532              
533 0           Produces an array of all available mailing_lists that are connected to the email-domain.
534 0            
535 0           =over
536 0 0          
537             =item * Return: L<ARRAY>
538 0            
  0            
539             =item * Synopsis: my $mailing_lists = $email_domain->mailing_lists();
540              
541             =back
542              
543 0     0 0   =cut
544              
545 0            
546 0           my ($self) = @_;
547 0            
548 0 0         my $api = $self->{_api_wrapper};
549             my $domain_name = $self->name;
550 0           my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/mailingList", noSignature => 0 );
551             croak $response->error if $response->error;
552              
553             my $mailing_list_names = $response->content;
554             my $mailing_lists = [];
555              
556             foreach my $mailing_list_name (@$mailing_list_names) {
557              
558             my $mailing_list = $self->{_mailing_lists}{$mailing_list_name} = $self->{_mailing_lists}{$mailing_list_name} || Webservice::OVH::Email::Domain::Domain::MailingList->_new_existing( wrapper => $api, domain => $self, id => $mailing_list_name, module => $self->{_module} );
559             push @$mailing_lists, $mailing_list;
560             }
561              
562             return $mailing_lists;
563             }
564              
565              
566             my ( $self, $mailinglist_name ) = @_;
567              
568             croak "Missing mailinglist_name" unless $mailinglist_name;
569 0     0 1    
570             my $api = $self->{_api_wrapper};
571 0           my $domain_name = $self->name;
572 0            
573 0           my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/mailingList/$mailinglist_name", noSignature => 0 );
574 0 0          
575             return $response->error ? 0 : 1;
576 0           }
577 0            
578             =head2 mailing_list
579 0            
580             Returns a single account by name
581 0   0        
582 0           =over
583              
584             =item * Parameter: $mailing_list_name - name
585 0            
586             =item * Return: L<Webservice::OVH::Email::Domain::Domain::MailingList>
587              
588             =item * Synopsis: my $mailing_list = $email_domain->mailing_list('subscriber_list');
589              
590 0     0 0   =back
591              
592 0 0         =cut
593              
594 0            
595 0           my ( $self, $mailing_list_name ) = @_;
596              
597 0           croak "Missing mailing_list_name" unless $mailing_list_name;
598              
599 0 0         if ( $self->mailinglist_exists($mailing_list_name) ) {
600              
601             my $api = $self->{_api_wrapper};
602             my $from_array_mailing_list = $self->{_mailing_lists}{$mailing_list_name} if $self->{_mailing_lists}{$mailing_list_name} && $self->{_mailing_lists}{$mailing_list_name}->is_valid;
603             my $mailing_list = $self->{_mailing_lists}{$mailing_list_name} = $from_array_mailing_list || Webservice::OVH::Email::Domain::Domain::MailingList->_new_existing( wrapper => $api, domain => $self, id => $mailing_list_name, module => $self->{_module} );
604              
605             return $mailing_list;
606              
607             } else {
608              
609             return undef;
610             }
611             }
612              
613             =head2 new_mailing_list
614              
615             Creates a new mailing list.
616              
617             =over
618              
619             =item * Parameter: %params - key => value language name options owner_email reply_to
620 0     0 1    
621             =item * Return: L<Webservice::Email::Domain::Domain::MailingList>
622 0 0          
623             =item * Synopsis: my $mailing_list = $email_domain->new_mailing_list(language 'DE', name => 'infos', options => {}, owner_email => 'owner@test.de', reply_to => 'test@test.de' );
624 0 0          
625             =back
626 0            
627 0 0 0       =cut
628 0   0        
629              
630 0           my ( $self, %params ) = @_;
631              
632             my $api = $self->{_api_wrapper};
633             my ( $mailing_list, $task ) = Webservice::OVH::Email::Domain::Domain::MailingList->_new( wrapper => $api, domain => $self, module => $self->{_module}, %params );
634 0            
635             return ( $mailing_list, $task );
636             }
637              
638              
639             my ($self) = @_;
640              
641             return $self->{_task};
642             }
643              
644             1;