File Coverage

lib/Webservice/OVH/Domain/Zone.pm
Criterion Covered Total %
statement 15 106 14.1
branch 0 48 0.0
condition 0 17 0.0
subroutine 5 19 26.3
pod 13 13 100.0
total 33 203 16.2


line stmt bran cond sub pod time code
1              
2             =encoding utf-8
3              
4             =head1 NAME
5              
6             Webservice::OVH::Domain::Zone
7              
8             =head1 SYNOPSIS
9              
10             use Webservice::OVH;
11            
12             my $ovh = Webservice::OVH->new_from_json("credentials.json");
13            
14             my $zone = $ovh->domain->zone("myzone.de");
15            
16             my $a_record = $zone->new_record(field_type => 'A', target => '0.0.0.0', ttl => 1000 );
17             my $mx_record = $zone->new_record(field_type => 'MX', target => '1 my.mail.server.de.');
18            
19             my $records = $zone->records(filed_type => 'A', sub_domain => 'www');
20            
21             foreach my $record (@$records) {
22            
23             $record->change( target => '0.0.0.0' );
24             }
25            
26             $zone->refresh;
27             $zone->reset;
28            
29             $zone->change_contact(contact_billing => 'account-ovh', contact_tech => 'account-ovh', contact_admin => 'account-ovh');
30              
31             =head1 DESCRIPTION
32              
33             Provieds basic functionality for Zones. Records can be created and fetched.
34             Records can be fetched through a filter.
35             A zone contact_change can be initialized.
36              
37             =head1 METHODS
38              
39             =cut
40              
41             use strict;
42 36     36   246 use warnings;
  36         73  
  36         1012  
43 36     36   177 use Carp qw{ carp croak };
  36         77  
  36         982  
44 36     36   239  
  36         80  
  36         2378  
45             our $VERSION = 0.47;
46              
47             use Webservice::OVH::Helper;
48 36     36   232 use Webservice::OVH::Domain::Zone::Record;
  36         72  
  36         933  
49 36     36   14530  
  36         99  
  36         40537  
50             =head2 _new
51              
52             Internal Method to create the zone object.
53             This method is not ment to be called external.
54              
55             =over
56              
57             =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object
58              
59             =item * Return: L<Webservice::OVH::Domain::Zone>
60              
61             =item * Synopsis: Webservice::OVH::Domain::Zone->_new($ovh_api_wrapper, $zone_name, $module);
62              
63             =back
64              
65             =cut
66              
67              
68             my ( $class, %params ) = @_;
69              
70 0     0     die "Missing module" unless $params{module};
71             die "Missing wrapper" unless $params{wrapper};
72 0 0         die "Missing id" unless $params{id};
73 0 0          
74 0 0         my $module = $params{module};
75             my $api_wrapper = $params{wrapper};
76 0           my $zone_name = $params{id};
77 0            
78 0           croak "Missing zone_name" unless $zone_name;
79              
80 0 0         my $self = bless { _module => $module, _api_wrapper => $api_wrapper, _name => $zone_name, _service_info => undef, _properties => undef, _records => {} }, $class;
81              
82 0           return $self;
83             }
84 0            
85             =head2 service_infos
86              
87             Retrieves additional infos about the zone.
88             Infos that are not part of the properties
89              
90             =over
91              
92             =item * Return: HASH
93              
94             =item * Synopsis: my $info = $zone->service_info;
95              
96             =back
97              
98             =cut
99              
100              
101             my ($self) = @_;
102              
103             my $api = $self->{_api_wrapper};
104 0     0 1   my $zone_name = $self->name;
105             my $response = $api->rawCall( method => 'get', path => "/domain/zone/$zone_name/serviceInfos", noSignature => 0 );
106 0            
107 0           croak $response->error if $response->error;
108 0            
109             $self->{_service_info} = $response->content;
110 0 0          
111             return $self->{_service_info};
112 0           }
113              
114 0           =head2 properties
115              
116             Retrieves properties of the zone.
117             This method updates the intern property variable.
118              
119             =over
120              
121             =item * Return: HASH
122              
123             =item * Synopsis: my $properties = $zone->properties;
124              
125             =back
126              
127             =cut
128              
129              
130             my ($self) = @_;
131              
132             my $api = $self->{_api_wrapper};
133             my $zone_name = $self->name;
134 0     0 1   my $response = $api->rawCall( method => 'get', path => "/domain/zone/$zone_name", noSignature => 0 );
135             croak $response->error if $response->error;
136 0            
137 0           $self->{_properties} = $response->content;
138 0            
139 0 0         return $self->{_properties};
140             }
141 0            
142             =head2 dnssec_supported
143 0            
144             Exposed Property Value. Readonly.
145              
146             =over
147              
148             =item * Return: VALUE
149              
150             =item * Synopsis: my $value = $zone->dnssec_supported;
151              
152             =back
153              
154             =cut
155              
156              
157             my ($self) = @_;
158              
159             $self->properties unless $self->{_properties};
160              
161             return $self->{_properties}->{dnssecSupported} ? 1 : 0;
162 0     0 1   }
163              
164 0 0         =head2 has_dns_anycast
165              
166 0 0         Exposed Property Value. Readonly.
167              
168             =over
169              
170             =item * Return: VALUE
171              
172             =item * Synopsis: my $value = $zone->has_dns_anycast;
173              
174             =back
175              
176             =cut
177              
178              
179             my ($self) = @_;
180              
181             $self->properties unless $self->{_properties};
182              
183             return $self->{_properties}->{hasDnsAnycast} ? 1 : 0;
184             }
185 0     0 1    
186             =head2 last_update
187 0 0          
188             Exposed Property Value. Readonly.
189 0 0          
190             =over
191              
192             =item * Return: DateTime
193              
194             =item * Synopsis: my $value = $zone->last_update;
195              
196             =back
197              
198             =cut
199              
200              
201             my ($self) = @_;
202              
203             $self->properties unless $self->{_properties};
204              
205             my $str_datetime = $self->{_properties}->{lastUpdate};
206             my $datetime = Webservice::OVH::Helper->parse_datetime($str_datetime);
207             return $datetime;
208 0     0 1   }
209              
210 0 0         =head2 name_servers
211              
212 0           Exposed Property Value. Readonly.
213 0            
214 0           =over
215              
216             =item * Return: L<ARRAY>
217              
218             =item * Synopsis: my $value = $zone->name_servers;
219              
220             =back
221              
222             =cut
223              
224              
225             my ($self) = @_;
226              
227             $self->properties unless $self->{_properties};
228              
229             return $self->{_properties}->{nameServers};
230             }
231              
232             =head2 records
233 0     0 1    
234             Produces an Array of record Objects.
235 0 0         Can be filtered by field_type and sub_domain.
236              
237 0           =over
238              
239             =item * Parameter: %filter - (optional) - field_type => record type sub_domain => subdomain string
240              
241             =item * Return: L<ARRAY>
242              
243             =item * Synopsis: my $records = $zone->records(field_type => 'A', sub_domain => 'www');
244              
245             =back
246              
247             =cut
248              
249              
250             my ( $self, %filter ) = @_;
251              
252             my $filter_type = ( exists $filter{field_type} && !$filter{field_type} ) ? "_empty_" : $filter{field_type};
253             my $filter_subdomain = ( exists $filter{subdomain} && !$filter{subdomain} ) ? "_empty_" : $filter{subdomain};
254             my $filter = Webservice::OVH::Helper->construct_filter( "fieldType" => $filter_type, "subDomain" => $filter_subdomain );
255              
256             my $api = $self->{_api_wrapper};
257             my $zone_name = $self->name;
258             my $response = $api->rawCall( method => 'get', path => sprintf( "/domain/zone/$zone_name/record%s", $filter ), noSignature => 0 );
259 0     0 1   croak $response->error if $response->error;
260              
261 0 0 0       my $record_ids = $response->content;
262 0 0 0       my $records = [];
263 0            
264             foreach my $record_id (@$record_ids) {
265 0            
266 0           my $record = $self->{_records}{$record_id} = $self->{_records}{$record_id} || Webservice::OVH::Domain::Zone::Record->_new_existing( wrapper => $api, module => $self->{_module}, zone => $self, id => $record_id );
267 0           push @$records, $record;
268 0 0         }
269              
270 0           return $records;
271 0           }
272              
273 0           =head2 record
274              
275 0   0       Returns a single record by id
276 0            
277             =over
278              
279 0           =item * Parameter: $record_id - id
280              
281             =item * Return: L<Webservice::OVH::Domain::Zone::Record>
282              
283             =item * Synopsis: my $record = $ovh->domain->zone->record(123456);
284              
285             =back
286              
287             =cut
288              
289              
290             my ( $self, $record_id ) = @_;
291              
292             croak "Missing record_id" unless $record_id;
293              
294             my $api = $self->{_api_wrapper};
295             my $from_array_record = $self->{_records}{$record_id} if $self->{_records}{$record_id} && $self->{_records}{$record_id}->is_valid;
296             my $record = $self->{_records}{$record_id} = $from_array_record || Webservice::OVH::Domain::Zone::Record->_new_existing( wrapper => $api, module => $self->{_module}, zone => $self, id => $record_id );
297              
298             return $record;
299             }
300 0     0 1    
301             =head2 new_record
302 0 0          
303             Creates a new record.
304 0            
305 0 0 0       =over
306 0   0        
307             =item * Parameter: %params - refresh => 'true', 'false' - directly refreshes the zone target (required) => '0.0.0.0' ttl (optional) => 3000 sub_domain (optional) => 'www' field_type (required) => 'A'
308 0            
309             =item * Return: L<Webservice::OVH::Domain::Zone::Record>
310              
311             =item * Synopsis: my $record = $zone->new_record(field_type => 'MX', target => '1 my.mailserver.de.');
312              
313             =back
314              
315             =cut
316              
317              
318             my ( $self, %params ) = @_;
319              
320             my $api = $self->{_api_wrapper};
321             my $record = Webservice::OVH::Domain::Zone::Record->_new( wrapper => $api, module => $self->{_module}, zone => $self, %params );
322              
323             return $record;
324             }
325              
326             =head2 name
327              
328             Name is the unique identifier.
329 0     0 1    
330             =over
331 0            
332 0           =item * Return: VALUE
333              
334 0           =item * Synopsis: my $name = $zone->name;
335              
336             =back
337              
338             =cut
339              
340              
341             my ($self) = @_;
342              
343             return $self->{_name};
344             }
345              
346             =head2 change_contact
347              
348             Changes contact information for this zone.
349             Contact must be another ovh account name.
350              
351             =over
352              
353 0     0 1   =item * Parameter: %params - contactBilling (optional) => 'account-ovh' contact_admin (optional) => 'account-ovh' contact_tech (optional) => 'account-ovh'
354              
355 0           =item * Return: L<Webservice::OVH::Me::Task>
356              
357             =item * Synopsis: my $task = $zone->change_contact(contact_billing => 'another-ovh');
358              
359             =back
360              
361             =cut
362              
363              
364             my ( $self, %params ) = @_;
365             croak "at least one parameter needed: contact_billing contact_admin contact_tech" unless %params;
366              
367             my $api = $self->{_api_wrapper};
368             my $zone_name = $self->name;
369             my $body = {};
370             $body->{contactBilling} = $params{contact_billing} if exists $params{contact_billing};
371             $body->{contactAdmin} = $params{contact_admin} if exists $params{contact_admin};
372             $body->{contactTech} = $params{contact_tech} if exists $params{contact_tech};
373             my $response = $api->rawCall( method => 'post', path => "/domain/zone/$zone_name/changeContact", body => $body, noSignature => 0 );
374             croak $response->error if $response->error;
375              
376             my $tasks = [];
377 0     0 1   my $task_ids = $response->content;
378 0 0         foreach my $task_id (@$task_ids) {
379              
380 0           my $task = $api->me->task_contact_change($task_id);
381 0           push @$tasks, $task;
382 0           }
383 0 0          
384 0 0         return $tasks;
385 0 0         }
386 0            
387 0 0         =head2 refresh
388              
389 0           Refreshes the domain zone and applies changes.
390 0            
391 0           =over
392              
393 0           =item * Synopsis:$zone->refresh;
394 0            
395             =back
396              
397 0           =cut
398              
399              
400             my ($self) = @_;
401             my $api = $self->{_api_wrapper};
402             my $zone_name = $self->name;
403              
404             my $response = $api->rawCall( method => 'post', path => "/domain/zone/$zone_name/refresh", body => {}, noSignature => 0 );
405             croak $response->error if $response->error;
406             }
407              
408             =head2 reset
409              
410             Deletes all custom records and resetzt to default.
411              
412             =over
413              
414 0     0 1   =item * Parameter: $minimal - only creates nesseccary dns records
415 0            
416 0           =item * Synopsis: $zone->reset;
417              
418 0           =back
419 0 0          
420             =cut
421              
422              
423             my ( $self, $minimal ) = @_;
424              
425             $minimal ||= 'false';
426              
427             my $api = $self->{_api_wrapper};
428             my $zone_name = $self->name;
429              
430             my $response = $api->rawCall( method => 'post', path => "/domain/zone/$zone_name/reset", body => {}, noSignature => 0 );
431             croak $response->error if $response->error;
432             }
433              
434             1;