« blib/lib/Webservice/OVH/Cloud/Project/Network.pm
↑ Summary
blib/lib/Webservice/OVH/Hosting/Web/Service.pm »
| 1 | package Webservice::OVH::Email::Domain; | ||
| 2 | |||
| 3 | =encoding utf-8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Webservice::OVH::Email::Domain | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Webservice::OVH; | ||
| 12 | |||
| 13 | my $ovh = Webservice::OVH->new_from_json("credentials.json"); | ||
| 14 | |||
| 15 | my $email_domains = $ovh->email->domain->domains; | ||
| 16 | |||
| 17 | foreach $email_domain (@$email_domains) { | ||
| 18 | |||
| 19 | $email_domain->name; | ||
| 20 | } | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | Provides access to email domain objects. | ||
| 25 | |||
| 26 | =head1 METHODS | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | use Carp qw{ carp croak }; | ||
| 33 | |||
| 34 | our $VERSION = 0.47; | ||
| 35 | |||
| 36 | use Webservice::OVH::Email::Domain::Domain; | ||
| 37 | |||
| 38 | =head2 _new | ||
| 39 | |||
| 40 | Internal Method to create the domain object. | ||
| 41 | This method is not ment to be called external. | ||
| 42 | |||
| 43 | =over | ||
| 44 | |||
| 45 | =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object | ||
| 46 | |||
| 47 | =item * Return: L<Webservice::OVH::Email::Domain> | ||
| 48 | |||
| 49 | =item * Synopsis: Webservice::OVH::Email::Domain->_new($ovh_api_wrapper, $zone_name, $module); | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub _new { | ||
| 56 | |||
| 57 | my ( $class, %params ) = @_; | ||
| 58 | |||
| 59 | die "Missing module" unless $params{module}; | ||
| 60 | die "Missing wrapper" unless $params{wrapper}; | ||
| 61 | |||
| 62 | my $module = $params{module}; | ||
| 63 | my $api_wrapper = $params{wrapper}; | ||
| 64 | |||
| 65 | my $self = bless { _module => $module, _api_wrapper => $api_wrapper, _domains => {}, _aviable_domains => [] }, $class; | ||
| 66 | |||
| 67 | my $api = $self->{_api_wrapper}; | ||
| 68 | my $response = $api->rawCall( method => 'get', path => "/email/domain/", noSignature => 0 ); | ||
| 69 | croak $response->error if $response->error; | ||
| 70 | |||
| 71 | $self->{_aviable_domains} = $response->content; | ||
| 72 | |||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | =head2 domain_exists | ||
| 77 | |||
| 78 | Returns 1 if email-domain is available for the connected account, 0 if not. | ||
| 79 | |||
| 80 | =over | ||
| 81 | |||
| 82 | =item * Parameter: $domain - (required)Domain name, $no_recheck - (optional)only for internal usage | ||
| 83 | |||
| 84 | =item * Return: VALUE | ||
| 85 | |||
| 86 | =item * Synopsis: print "mydomain.com exists" if $ovh->email->domain->domain_exists("mydomain.com"); | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub domain_exists { | ||
| 93 | |||
| 94 | my ( $self, $domain, $no_recheck ) = @_; | ||
| 95 | |||
| 96 | if ( !$no_recheck ) { | ||
| 97 | |||
| 98 | my $api = $self->{_api_wrapper}; | ||
| 99 | my $response = $api->rawCall( method => 'get', path => "/email/domain/", noSignature => 0 ); | ||
| 100 | croak $response->error if $response->error; | ||
| 101 | |||
| 102 | $self->{_aviable_domains} = $response->content; | ||
| 103 | |||
| 104 | my $list = $response->content; | ||
| 105 | |||
| 106 | return ( grep { $_ eq $domain } @$list ) ? 1 : 0; | ||
| 107 | |||
| 108 | } else { | ||
| 109 | |||
| 110 | my $list = $self->{_aviable_domains}; | ||
| 111 | |||
| 112 | return ( grep { $_ eq $domain } @$list ) ? 1 : 0; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | =head2 domains | ||
| 117 | |||
| 118 | Produces an array of all available email-domains that are connected to the used account. | ||
| 119 | |||
| 120 | =over | ||
| 121 | |||
| 122 | =item * Return: L<ARRAY> | ||
| 123 | |||
| 124 | =item * Synopsis: my $domains = $ovh->email->domain->domains(); | ||
| 125 | |||
| 126 | =back | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub domains { | ||
| 131 | |||
| 132 | my ($self) = @_; | ||
| 133 | |||
| 134 | my $api = $self->{_api_wrapper}; | ||
| 135 | my $response = $api->rawCall( method => 'get', path => "/email/domain/", noSignature => 0 ); | ||
| 136 | croak $response->error if $response->error; | ||
| 137 | |||
| 138 | my $domain_array = $response->content; | ||
| 139 | my $domains = []; | ||
| 140 | $self->{_aviable_domains} = $domain_array; | ||
| 141 | |||
| 142 | foreach my $domain (@$domain_array) { | ||
| 143 | if ( $self->domain_exists( $domain, 1 ) ) { | ||
| 144 | my $domain = $self->{_domains}{$domain} = $self->{_domains}{$domain} || Webservice::OVH::Email::Domain::Domain->_new( wrapper => $api, id => $domain, module => $self->{_module} ); | ||
| 145 | push @$domains, $domain; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | return $domains; | ||
| 150 | |||
| 151 | } | ||
| 152 | |||
| 153 | =head2 domain | ||
| 154 | |||
| 155 | Returns a single email-domains by name | ||
| 156 | |||
| 157 | =over | ||
| 158 | |||
| 159 | =item * Parameter: $domain - domain name | ||
| 160 | |||
| 161 | =item * Return: L<Webservice::OVH::Email::Domains::Domain> | ||
| 162 | |||
| 163 | =item * Synopsis: my $email_domain = $ovh->email->domain->domain("mydomain.com"); | ||
| 164 | |||
| 165 | =back | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub domain { | ||
| 170 | |||
| 171 | my ( $self, $domain ) = @_; | ||
| 172 | |||
| 173 | if ( $self->domain_exists($domain) ) { | ||
| 174 | |||
| 175 | my $api = $self->{_api_wrapper}; | ||
| 176 | my $domain = $self->{_domains}{$domain} = $self->{_domains}{$domain} || Webservice::OVH::Email::Domain::Domain->_new( wrapper => $api, id => $domain, module => $self->{_module} ); | ||
| 177 | |||
| 178 | return $domain; | ||
| 179 | } else { | ||
| 180 | |||
| 181 | carp "Domain $domain doesn't exists"; | ||
| 182 | return undef; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | 1; |