| 1 | package Webservice::OVH::Domain; | ||
| 2 | |||
| 3 | =encoding utf-8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Webservice::OVH::Domain | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Webservice::OVH; | ||
| 12 | |||
| 13 | my $ovh = Webservice::OVH->new_from_json("credentials.json"); | ||
| 14 | |||
| 15 | my $services = $ovh->domain->services; | ||
| 16 | foreach my $service (@$services) { | ||
| 17 | |||
| 18 | print $service->name; | ||
| 19 | } | ||
| 20 | |||
| 21 | my $ = $ovh->domain->zones; | ||
| 22 | foreach my $zone (@$zones) { | ||
| 23 | |||
| 24 | print $zone->name; | ||
| 25 | } | ||
| 26 | |||
| 27 | print "I have a zone" if $ovh->domain->zone_exists("myaddress.de"); | ||
| 28 | print "I have a service" if $ovh->domain->service_exists("myaddress.de"); | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | Gives access to services and zones connected to the uses account. | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | use strict; | ||
| 39 | use warnings; | ||
| 40 | use Carp qw{ carp croak }; | ||
| 41 | |||
| 42 | our $VERSION = 0.47; | ||
| 43 | |||
| 44 | use Webservice::OVH::Domain::Service; | ||
| 45 | use Webservice::OVH::Domain::Zone; | ||
| 46 | |||
| 47 | =head2 _new | ||
| 48 | |||
| 49 | Internal Method to create the domain object. | ||
| 50 | This method is not ment to be called external. | ||
| 51 | |||
| 52 | =over | ||
| 53 | |||
| 54 | =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object | ||
| 55 | |||
| 56 | =item * Return: L<Webservice::OVH::Order> | ||
| 57 | |||
| 58 | =item * Synopsis: Webservice::OVH::Order->_new($ovh_api_wrapper, $self); | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub _new { | ||
| 65 | |||
| 66 | my ( $class, %params ) = @_; | ||
| 67 | |||
| 68 | die "Missing module" unless $params{module}; | ||
| 69 | die "Missing wrapper" unless $params{wrapper}; | ||
| 70 | |||
| 71 | my $module = $params{module}; | ||
| 72 | my $api_wrapper = $params{wrapper}; | ||
| 73 | |||
| 74 | my $self = bless { _module => $module, _api_wrapper => $api_wrapper, _services => {}, _zones => {}, _aviable_services => [], _aviable_zones => [] }, $class; | ||
| 75 | |||
| 76 | return $self; | ||
| 77 | } | ||
| 78 | |||
| 79 | =head2 service_exists | ||
| 80 | |||
| 81 | Returns 1 if service is available for the connected account, 0 if not. | ||
| 82 | |||
| 83 | =over | ||
| 84 | |||
| 85 | =item * Parameter: $service_name - Domain name, $no_recheck - (optional)only for internal usage | ||
| 86 | |||
| 87 | =item * Return: VALUE | ||
| 88 | |||
| 89 | =item * Synopsis: print "mydomain.com exists" if $ovh->domain->service_exists("mydomain.com"); | ||
| 90 | |||
| 91 | =back | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub service_exists { | ||
| 96 | |||
| 97 | my ( $self, $service_name, $no_recheck ) = @_; | ||
| 98 | |||
| 99 | if ( !$no_recheck ) { | ||
| 100 | |||
| 101 | my $api = $self->{_api_wrapper}; | ||
| 102 | my $response = $api->rawCall( method => 'get', path => "/domain", noSignature => 0 ); | ||
| 103 | croak $response->error if $response->error; | ||
| 104 | |||
| 105 | my $list = $response->content; | ||
| 106 | |||
| 107 | return ( grep { $_ eq $service_name } @$list ) ? 1 : 0; | ||
| 108 | |||
| 109 | } else { | ||
| 110 | |||
| 111 | my $list = $self->{_aviable_services}; | ||
| 112 | |||
| 113 | return ( grep { $_ eq $service_name } @$list ) ? 1 : 0; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | =head2 zone_exists | ||
| 118 | |||
| 119 | Returns 1 if zone is available for the connected account, 0 if not. | ||
| 120 | |||
| 121 | =over | ||
| 122 | |||
| 123 | =item * Parameter: $zone_name - Domain name, $no_recheck - (optional)only for internal usage | ||
| 124 | |||
| 125 | =item * Return: VALUE | ||
| 126 | |||
| 127 | =item * Synopsis: print "zone mydomain.com exists" if $ovh->domain->zone_exists("mydomain.com"); | ||
| 128 | |||
| 129 | =back | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | sub zone_exists { | ||
| 134 | |||
| 135 | my ( $self, $zone_name, $no_recheck ) = @_; | ||
| 136 | |||
| 137 | if ( !$no_recheck ) { | ||
| 138 | |||
| 139 | my $api = $self->{_api_wrapper}; | ||
| 140 | my $response = $api->rawCall( method => 'get', path => "/domain/zone", noSignature => 0 ); | ||
| 141 | croak $response->error if $response->error; | ||
| 142 | |||
| 143 | my $list = $response->content; | ||
| 144 | |||
| 145 | return ( grep { $_ eq $zone_name } @$list ) ? 1 : 0; | ||
| 146 | |||
| 147 | } else { | ||
| 148 | |||
| 149 | my $list = $self->{_aviable_zones}; | ||
| 150 | |||
| 151 | return ( grep { $_ eq $zone_name } @$list ) ? 1 : 0; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | =head2 services | ||
| 156 | |||
| 157 | Produces an array of all available services that are connected to the used account. | ||
| 158 | |||
| 159 | =over | ||
| 160 | |||
| 161 | =item * Return: ARRAY | ||
| 162 | |||
| 163 | =item * Synopsis: my $services = $ovh->order->services(); | ||
| 164 | |||
| 165 | =back | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub services { | ||
| 170 | |||
| 171 | my ($self) = @_; | ||
| 172 | |||
| 173 | my $api = $self->{_api_wrapper}; | ||
| 174 | my $response = $api->rawCall( method => 'get', path => "/domain", noSignature => 0 ); | ||
| 175 | croak $response->error if $response->error; | ||
| 176 | |||
| 177 | my $service_array = $response->content; | ||
| 178 | my $services = []; | ||
| 179 | $self->{_aviable_services} = $service_array; | ||
| 180 | |||
| 181 | foreach my $service_name (@$service_array) { | ||
| 182 | if ( $self->service_exists( $service_name, 1 ) ) { | ||
| 183 | my $service = $self->{_services}{$service_name} = $self->{_services}{$service_name} || Webservice::OVH::Domain::Service->_new( wrapper => $api, id => $service_name, module => $self->{_module} ); | ||
| 184 | push @$services, $service; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | return $services; | ||
| 189 | } | ||
| 190 | |||
| 191 | =head2 zones | ||
| 192 | |||
| 193 | Produces an array of all available zones that are connected to the used account. | ||
| 194 | |||
| 195 | =over | ||
| 196 | |||
| 197 | =item * Return: ARRAY | ||
| 198 | |||
| 199 | =item * Synopsis: my $zones = $ovh->order->zones(); | ||
| 200 | |||
| 201 | =back | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub zones { | ||
| 206 | |||
| 207 | my ($self) = @_; | ||
| 208 | |||
| 209 | my $api = $self->{_api_wrapper}; | ||
| 210 | my $response = $api->rawCall( method => 'get', path => "/domain/zone", noSignature => 0 ); | ||
| 211 | croak $response->error if $response->error; | ||
| 212 | |||
| 213 | my $zone_names = $response->content; | ||
| 214 | my $zones = []; | ||
| 215 | $self->{_aviable_zones} = $zone_names; | ||
| 216 | |||
| 217 | foreach my $zone_name (@$zone_names) { | ||
| 218 | |||
| 219 | if ( $self->zone_exists( $zone_name, 1 ) ) { | ||
| 220 | my $zone = $self->{_zones}{$zone_name} = $self->{_zones}{$zone_name} || Webservice::OVH::Domain::Zone->_new( wrapper => $api, id => $zone_name, module => $self->{_module} ); | ||
| 221 | push @$zones, $zone; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | return $zones; | ||
| 226 | } | ||
| 227 | |||
| 228 | =head2 service | ||
| 229 | |||
| 230 | Returns a single service by name | ||
| 231 | |||
| 232 | =over | ||
| 233 | |||
| 234 | =item * Parameter: $service_name - domain name | ||
| 235 | |||
| 236 | =item * Return: L<Webservice::OVH::Domain::Service> | ||
| 237 | |||
| 238 | =item * Synopsis: my $service = $ovh->domain->service("mydomain.com"); | ||
| 239 | |||
| 240 | =back | ||
| 241 | |||
| 242 | =cut | ||
| 243 | |||
| 244 | sub service { | ||
| 245 | |||
| 246 | my ( $self, $service_name ) = @_; | ||
| 247 | |||
| 248 | if ( $self->service_exists($service_name) ) { | ||
| 249 | |||
| 250 | my $api = $self->{_api_wrapper}; | ||
| 251 | my $service = $self->{_services}{$service_name} = $self->{_services}{$service_name} || Webservice::OVH::Domain::Service->_new( wrapper => $api, id => $service_name, module => $self->{_module} ); | ||
| 252 | |||
| 253 | return $service; | ||
| 254 | } else { | ||
| 255 | |||
| 256 | carp "Service $service_name doesn't exists"; | ||
| 257 | return undef; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | =head2 zone | ||
| 262 | |||
| 263 | Returns a single zone by name | ||
| 264 | |||
| 265 | =over | ||
| 266 | |||
| 267 | =item * Parameter: $zone_name - domain name | ||
| 268 | |||
| 269 | =item * Return: L<Webservice::OVH::Domain::Zone> | ||
| 270 | |||
| 271 | =item * Synopsis: my $zone = $ovh->domain->zone("mydomain.com"); | ||
| 272 | |||
| 273 | =back | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub zone { | ||
| 278 | |||
| 279 | my ( $self, $zone_name ) = @_; | ||
| 280 | |||
| 281 | if ( $self->zone_exists($zone_name) ) { | ||
| 282 | my $api = $self->{_api_wrapper}; | ||
| 283 | my $zone = $self->{_zones}{$zone_name} = $self->{_zones}{$zone_name} || Webservice::OVH::Domain::Zone->_new( wrapper => $api, id => $zone_name, module => $self->{_module} ); | ||
| 284 | |||
| 285 | return $zone; | ||
| 286 | |||
| 287 | } else { | ||
| 288 | |||
| 289 | carp "Zone $zone_name doesn't exists"; | ||
| 290 | return undef; | ||
| 291 | } | ||
| 292 | |||
| 293 | } | ||
| 294 | |||
| 295 | 1; |