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