File Coverage

lib/Webservice/OVH/Domain/Zone/Record.pm
Criterion Covered Total %
statement 12 106 11.3
branch 0 56 0.0
condition 0 6 0.0
subroutine 4 17 23.5
pod 10 10 100.0
total 26 195 13.3


line stmt bran cond sub pod time code
1             package Webservice::OVH::Domain::Zone::Record;
2              
3             =encoding utf-8
4              
5             =head1 NAME
6              
7             Webservice::OVH::Domain::Zone::Record
8              
9             =head1 SYNOPSIS
10              
11             use Webservice::OVH;
12            
13             my $ovh = Webservice::OVH->new_from_json("credentials.json");
14            
15             my $zone = $ovh->domain->zone("myzone.de");
16            
17             my $a_record = $zone->new_record(field_type => 'A', target => '0.0.0.0', ttl => 1000 );
18             my $mx_record = $zone->new_record(field_type => 'MX', target => '1 my.mail.server.de.');
19            
20             my $records = $zone->records(filed_type => 'A', sub_domain => 'www');
21            
22             foreach my $record (@$records) {
23            
24             $record->change( target => '0.0.0.0' );
25             $record->zone->refresh;
26             $record->change( sub_domain => 'www', refresh => 'true' );
27             }
28            
29             $record->delete('true');
30            
31             print "Not Valid anymore" unless $record->is_valid;
32              
33             =head1 DESCRIPTION
34              
35             Provides all api Record Methods available in the api.
36             Delete deletes the record object in the api and makes the object invalid.
37             No actions be done with it, when it is invalid.
38              
39             =head1 METHODS
40              
41             =cut
42              
43 36     36   275 use strict;
  36         93  
  36         1029  
44 36     36   215 use warnings;
  36         125  
  36         976  
45 36     36   194 use Carp qw{ carp croak };
  36         99  
  36         2670  
46              
47             our $VERSION = 0.48;
48              
49 36     36   311 use Webservice::OVH::Me::Contact;
  36         115  
  36         48446  
50              
51             =head2 _new_existing
52              
53             Internal Method to create a Record object.
54             This method should never be called directly.
55              
56             =over
57              
58             =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object, $zone - parent zone Objekt, $record_id => api intern id
59              
60             =item * Return: L<Webservice::OVH::Domain::Zone::Record>
61              
62             =item * Synopsis: Webservice::OVH::Domain::Zone::Record->_new_existing($ovh_api_wrapper, $module, $zone, $record_id);
63              
64             =back
65              
66             =cut
67              
68             sub _new_existing {
69              
70 0     0     my ( $class, %params ) = @_;
71              
72 0 0         die "Missing module" unless $params{module};
73 0 0         die "Missing wrapper" unless $params{wrapper};
74 0 0         die "Missing id" unless $params{id};
75 0 0         die "Missing zone" unless $params{zone};
76              
77 0           my $module = $params{module};
78 0           my $api_wrapper = $params{wrapper};
79 0           my $record_id = $params{id};
80 0           my $zone = $params{zone};
81              
82 0           my $zone_name = $zone->name;
83 0           my $response = $api_wrapper->rawCall( method => 'get', path => "/domain/zone/$zone_name/record/$record_id", noSignature => 0 );
84 0 0         carp $response->error if $response->error;
85              
86 0 0         if ( !$response->error ) {
87              
88 0           my $porperties = $response->content;
89 0           my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $record_id, _properties => $porperties, _zone => $zone }, $class;
90              
91 0           return $self;
92             } else {
93              
94 0           return undef;
95             }
96             }
97              
98             =head2 _new
99              
100             Internal Method to create the zone object.
101             This method should never be called directly.
102              
103             =over
104              
105             =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object, $zone - parent zone, %params - key => value
106              
107             =item * Return: L<Webservice::OVH::Domain::Zone::Record>
108              
109             =item * Synopsis: Webservice::OVH::Domain::Zone::Record->_new($ovh_api_wrapper, $module, $zone_name, target => '0.0.0.0', field_type => 'A', sub_domain => 'www');
110              
111             =back
112              
113             =cut
114              
115             sub _new {
116              
117 0     0     my ( $class, %params ) = @_;
118              
119 0 0         die "Missing module" unless $params{module};
120 0 0         die "Missing wrapper" unless $params{wrapper};
121 0 0         die "Missing zone" unless $params{zone};
122              
123 0           my $module = $params{module};
124 0           my $api_wrapper = $params{wrapper};
125 0           my $zone = $params{zone};
126              
127 0           my @keys_needed = qw{ field_type target };
128 0 0         if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) {
  0            
129              
130 0           croak "Missing parameter: @missing_parameters";
131             }
132              
133 0           my $zone_name = $zone->name;
134 0           my $body = {};
135 0 0         $body->{subDomain} = $params{sub_domain} if exists $params{sub_domain};
136 0           $body->{target} = $params{target};
137 0 0         $body->{ttl} = $params{ttl} if exists $params{ttl};
138 0 0         $body->{fieldType} = $params{field_type} if exists $params{field_type};
139 0           my $response = $api_wrapper->rawCall( method => 'post', path => "/domain/zone/$zone_name/record", body => $body, noSignature => 0 );
140 0 0         croak $response->error if $response->error;
141              
142 0           my $record_id = $response->content->{id};
143 0           my $properties = $response->content;
144              
145 0   0       my $refresh = $params{'refresh'} || 'false';
146 0 0         $zone->refresh if $refresh eq 'true';
147              
148 0           my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $record_id, _properties => $properties, _zone => $zone }, $class;
149              
150 0           return $self;
151             }
152              
153             =head2 is_valid
154              
155             When this record is deleted on the api side, this method returns 0.
156              
157             =over
158              
159             =item * Return: VALUE
160              
161             =item * Synopsis: print "Valid" if $record->is_valid;
162              
163             =back
164              
165             =cut
166              
167             sub is_valid {
168              
169 0     0 1   my ($self) = @_;
170              
171 0           return $self->{_valid};
172             }
173              
174             =head2 _is_valid
175              
176             Intern method to check validity.
177             Difference is that this method carps an error.
178              
179             =over
180              
181             =item * Return: VALUE
182              
183             =item * Synopsis: $record->_is_valid;
184              
185             =back
186              
187             =cut
188              
189             sub _is_valid {
190              
191 0     0     my ($self) = @_;
192              
193 0           my $record_id = $self->id;
194 0 0         carp "Record $record_id is not valid anymore" unless $self->is_valid;
195 0           return $self->is_valid;
196             }
197              
198             =head2 id
199              
200             Returns the api id of this record
201              
202             =over
203              
204             =item * Return: VALUE
205              
206             =item * Synopsis: my $id = $record->id;
207              
208             =back
209              
210             =cut
211              
212             sub id {
213              
214 0     0 1   my ($self) = @_;
215              
216 0           return $self->{_id};
217             }
218              
219             =head2 zone
220              
221             Returns the zone this record is attached to.
222              
223             =over
224              
225             =item * Return: L<Webservice::Domain::Zone>
226              
227             =item * Synopsis: my $zone = $record->zone;
228              
229             =back
230              
231             =cut
232              
233             sub zone {
234              
235 0     0 1   my ($self) = @_;
236              
237 0           return $self->{_zone};
238             }
239              
240             =head2 properties
241              
242             Returns the raw properties as a hash.
243             This is the original return value of the web-api.
244              
245             =over
246              
247             =item * Return: HASH
248              
249             =item * Synopsis: my $properties = $record->properties;
250              
251             =back
252              
253             =cut
254              
255             sub properties {
256              
257 0     0 1   my ($self) = @_;
258              
259 0 0         return unless $self->_is_valid;
260              
261 0           my $api = $self->{_api_wrapper};
262 0           my $zone_name = $self->zone->name;
263 0           my $record_id = $self->id;
264 0           my $response = $api->rawCall( method => 'get', path => "/domain/zone/$zone_name/record/$record_id", noSignature => 0 );
265 0 0         croak $response->error if $response->error;
266 0           $self->{_properties} = $response->content;
267 0           return $self->{_properties};
268             }
269              
270             =head2 field_type
271              
272             Exposed property value.
273              
274             =over
275              
276             =item * Return: VALUE
277              
278             =item * Synopsis: my $field_type = $record->field_type;
279              
280             =back
281              
282             =cut
283              
284             sub field_type {
285              
286 0     0 1   my ($self) = @_;
287              
288 0           return $self->{_properties}->{fieldType};
289             }
290              
291             =head2 sub_domain
292              
293             Exposed property value.
294              
295             =over
296              
297             =item * Return: VALUE
298              
299             =item * Synopsis: my $sub_domain = $record->sub_domain;
300              
301             =back
302              
303             =cut
304              
305             sub sub_domain {
306              
307 0     0 1   my ($self) = @_;
308              
309 0           return $self->{_properties}->{subDomain};
310             }
311              
312             =head2 target
313              
314             Exposed property value.
315              
316             =over
317              
318             =item * Return: VALUE
319              
320             =item * Synopsis: my $target = $record->target;
321              
322             =back
323              
324             =cut
325              
326             sub target {
327              
328 0     0 1   my ($self) = @_;
329              
330 0           return $self->{_properties}->{target};
331             }
332              
333             =head2 ttl
334              
335             Exposed property value.
336              
337             =over
338              
339             =item * Return: VALUE
340              
341             =item * Synopsis: my $ttl = $record->ttl;
342              
343             =back
344              
345             =cut
346              
347             sub ttl {
348              
349 0     0 1   my ($self) = @_;
350              
351 0           return $self->{_properties}->{ttl};
352             }
353              
354             =head2 delete
355              
356             Deletes the record api sided and sets this object invalid.
357             After deleting, the zone must be refreshed, if the refresh parameter is not set.
358              
359             =over
360              
361             =item * Parameter: $refresh 'true' 'false' undef - imidiate refreshing of the domain zone
362              
363             =item * Synopsis: $record->delete('true');
364              
365             =back
366              
367             =cut
368              
369             sub delete {
370              
371 0     0 1   my ( $self, $refresh ) = @_;
372              
373 0 0         return unless $self->_is_valid;
374              
375 0           my $api = $self->{_api_wrapper};
376 0           my $zone_name = $self->{_zone}->name;
377 0           my $record_id = $self->id;
378 0           my $response = $api->rawCall( method => 'delete', path => "/domain/zone/$zone_name/record/$record_id", noSignature => 0 );
379 0 0         croak $response->error if $response->error;
380              
381 0   0       $refresh ||= 'false';
382 0 0         $self->zone->refresh if $refresh eq 'true';
383 0           $self->{_valid} = 0;
384             }
385              
386             =head2 change
387              
388             Changes the record
389             After changing the zone must be refreshed, if the refresh parameter is not set.
390              
391             =over
392              
393             =item * Parameter: %params - key => value sub_domain target ttl refresh
394              
395             =item * Synopsis: $record->change(sub_domain => 'www', refresh => 'true');
396              
397             =back
398              
399             =cut
400              
401             sub change {
402              
403 0     0 1   my ( $self, %params ) = @_;
404              
405 0 0         return unless $self->_is_valid;
406              
407 0 0         if ( scalar keys %params != 0 ) {
408              
409 0           my $api = $self->{_api_wrapper};
410 0           my $zone_name = $self->{_zone}->name;
411 0           my $record_id = $self->id;
412 0           my $body = {};
413 0 0         $body->{subDomain} = $params{sub_domain} if exists $params{sub_domain};
414 0 0         $body->{target} = $params{target} if exists $params{target};
415 0 0         $body->{ttl} = $params{ttl} if exists $params{ttl};
416 0           my $response = $api->rawCall( method => 'put', path => "/domain/zone/$zone_name/record/$record_id", body => $body, noSignature => 0 );
417 0 0         croak $response->error if $response->error;
418              
419 0   0       my $refresh = $params{refresh} || 'false';
420 0 0         $self->zone->refresh if $refresh eq 'true';
421 0           $self->properties;
422             }
423             }
424              
425             1;