« blib/lib/Webservice/OVH/Cloud/Project/Network/Private.pm
↑ Summary
lib/Webservice/OVH/Domain/Zone.pm »
| 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 | use strict; | ||
| 44 | use warnings; | ||
| 45 | use Carp qw{ carp croak }; | ||
| 46 | |||
| 47 | our $VERSION = 0.47; | ||
| 48 | |||
| 49 | use Webservice::OVH::Me::Contact; | ||
| 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 | my ( $class, %params ) = @_; | ||
| 71 | |||
| 72 | die "Missing module" unless $params{module}; | ||
| 73 | die "Missing wrapper" unless $params{wrapper}; | ||
| 74 | die "Missing id" unless $params{id}; | ||
| 75 | die "Missing zone" unless $params{zone}; | ||
| 76 | |||
| 77 | my $module = $params{module}; | ||
| 78 | my $api_wrapper = $params{wrapper}; | ||
| 79 | my $record_id = $params{id}; | ||
| 80 | my $zone = $params{zone}; | ||
| 81 | |||
| 82 | my $zone_name = $zone->name; | ||
| 83 | my $response = $api_wrapper->rawCall( method => 'get', path => "/domain/zone/$zone_name/record/$record_id", noSignature => 0 ); | ||
| 84 | carp $response->error if $response->error; | ||
| 85 | |||
| 86 | if ( !$response->error ) { | ||
| 87 | |||
| 88 | my $porperties = $response->content; | ||
| 89 | my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $record_id, _properties => $porperties, _zone => $zone }, $class; | ||
| 90 | |||
| 91 | return $self; | ||
| 92 | } else { | ||
| 93 | |||
| 94 | 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 | my ( $class, %params ) = @_; | ||
| 118 | |||
| 119 | die "Missing module" unless $params{module}; | ||
| 120 | die "Missing wrapper" unless $params{wrapper}; | ||
| 121 | die "Missing zone" unless $params{zone}; | ||
| 122 | |||
| 123 | my $module = $params{module}; | ||
| 124 | my $api_wrapper = $params{wrapper}; | ||
| 125 | my $zone = $params{zone}; | ||
| 126 | |||
| 127 | my @keys_needed = qw{ field_type target }; | ||
| 128 | if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) { | ||
| 129 | |||
| 130 | croak "Missing parameter: @missing_parameters"; | ||
| 131 | } | ||
| 132 | |||
| 133 | my $zone_name = $zone->name; | ||
| 134 | my $body = {}; | ||
| 135 | $body->{subDomain} = $params{sub_domain} if exists $params{sub_domain}; | ||
| 136 | $body->{target} = $params{target}; | ||
| 137 | $body->{ttl} = $params{ttl} if exists $params{ttl}; | ||
| 138 | $body->{fieldType} = $params{field_type} if exists $params{field_type}; | ||
| 139 | my $response = $api_wrapper->rawCall( method => 'post', path => "/domain/zone/$zone_name/record", body => $body, noSignature => 0 ); | ||
| 140 | croak $response->error if $response->error; | ||
| 141 | |||
| 142 | my $record_id = $response->content->{id}; | ||
| 143 | my $properties = $response->content; | ||
| 144 | |||
| 145 | my $refresh = $params{'refresh'} || 'false'; | ||
| 146 | $zone->refresh if $refresh eq 'true'; | ||
| 147 | |||
| 148 | my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $record_id, _properties => $properties, _zone => $zone }, $class; | ||
| 149 | |||
| 150 | 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 | my ($self) = @_; | ||
| 170 | |||
| 171 | 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 | my ($self) = @_; | ||
| 192 | |||
| 193 | my $record_id = $self->id; | ||
| 194 | carp "Record $record_id is not valid anymore" unless $self->is_valid; | ||
| 195 | 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 | my ($self) = @_; | ||
| 215 | |||
| 216 | 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 | my ($self) = @_; | ||
| 236 | |||
| 237 | 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 | my ($self) = @_; | ||
| 258 | |||
| 259 | return unless $self->_is_valid; | ||
| 260 | |||
| 261 | my $api = $self->{_api_wrapper}; | ||
| 262 | my $zone_name = $self->zone->name; | ||
| 263 | my $record_id = $self->id; | ||
| 264 | my $response = $api->rawCall( method => 'get', path => "/domain/zone/$zone_name/record/$record_id", noSignature => 0 ); | ||
| 265 | croak $response->error if $response->error; | ||
| 266 | $self->{_properties} = $response->content; | ||
| 267 | 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 | my ($self) = @_; | ||
| 287 | |||
| 288 | 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 | my ($self) = @_; | ||
| 308 | |||
| 309 | 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 | my ($self) = @_; | ||
| 329 | |||
| 330 | 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 | my ($self) = @_; | ||
| 350 | |||
| 351 | 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 | my ( $self, $refresh ) = @_; | ||
| 372 | |||
| 373 | return unless $self->_is_valid; | ||
| 374 | |||
| 375 | my $api = $self->{_api_wrapper}; | ||
| 376 | my $zone_name = $self->{_zone}->name; | ||
| 377 | my $record_id = $self->id; | ||
| 378 | my $response = $api->rawCall( method => 'delete', path => "/domain/zone/$zone_name/record/$record_id", noSignature => 0 ); | ||
| 379 | croak $response->error if $response->error; | ||
| 380 | |||
| 381 | $refresh ||= 'false'; | ||
| 382 | $self->zone->refresh if $refresh eq 'true'; | ||
| 383 | $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 | my ( $self, %params ) = @_; | ||
| 404 | |||
| 405 | return unless $self->_is_valid; | ||
| 406 | |||
| 407 | if ( scalar keys %params != 0 ) { | ||
| 408 | |||
| 409 | my $api = $self->{_api_wrapper}; | ||
| 410 | my $zone_name = $self->{_zone}->name; | ||
| 411 | my $record_id = $self->id; | ||
| 412 | my $body = {}; | ||
| 413 | $body->{subDomain} = $params{sub_domain} if exists $params{sub_domain}; | ||
| 414 | $body->{target} = $params{target} if exists $params{target}; | ||
| 415 | $body->{ttl} = $params{ttl} if exists $params{ttl}; | ||
| 416 | my $response = $api->rawCall( method => 'put', path => "/domain/zone/$zone_name/record/$record_id", body => $body, noSignature => 0 ); | ||
| 417 | croak $response->error if $response->error; | ||
| 418 | |||
| 419 | my $refresh = $params{refresh} || 'false'; | ||
| 420 | $self->zone->refresh if $refresh eq 'true'; | ||
| 421 | $self->properties; | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | 1; |