The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojo::Cloudflare - Talk with the cloudflare API using Mojo::UserAgent

VERSION

0.01

DESCRIPTION

Mojo::Cloudflare is an async client for the CloudFlare API.

SYNOPSIS

  use Mojo::Cloudflare;
  my $cf = Mojo::Cloudflare->new(
             email => 'sample@example.com',
             key => '8afbe6dea02407989af4dd4c97bb6e25',
             zone => 'example.com',
           );

  for my $record (@{ $cf->records("all")->get("/objs") }) {
    warn $record->{zone_name};
    
    $cf->edit_record({
      id => $record->{rec_id},
      type => 'CNAME',
      name => 'home',
      content => 'example.com',
      ttl => 1,
      service_mode => 0,
    });
  }

ATTRIBUTES

api_url

Holds the endpoint where we communicate. Default is https://www.cloudflare.com/api_json.html.

email

  $str = $self->email;
  $self = $self->email($str);

The e-mail address associated with the API key.

key

  $str = $self->key;
  $self = $self->key($str);

This is the API key made available on your Account page.

zone

  $str = $self->zone;
  $self = $self->zone($str);

The zone (domain) to act on.

METHODS

add_record

  $json = $self->add_record(\%args);
  $self = $self->add_record(\%args, sub {
          my($self, $err, $json) = @_;
          # ...
        });

Used to add a new DNS record. $err is true and contains a string on error, while $json is a Mojo::JSON::Pointer object with the "rec" part from the JSON on success:

  {
    "request" => { ... },
    "response" => {
      "rec" => { # <== this structure
        "obj" => {
          ...
        },
      },
    },
    "result": ...,
    "msg": ...
  };

Example usage:

  $rec_tag = $json->get("/obj/rec_tag");

Valid %args:

  • type => {A,CNAME,MX,TXT,SPF,AAAA,NS,SRV,LOC},

    Name of the DNS record.

  • name => $str

    Name of the DNS record

  • content => $str

    The content of the DNS record, will depend on the the type of record being added.

  • ttl => $int

    TTL of record in seconds. 1 (default) = Automatic, otherwise, value must in between 120 and 86400 seconds.

  • priority => $int

    MX record priority.

delete_record

  $json = $self->delete_record($id);
  $self = $self->delete_record($id, sub {
          my($self, $err, $json) = @_;
          # ...
        });

Used to delete a DNS record. $err is true and contains a string on error, while $json is a Mojo::JSON::Pointer object on success.

edit_record

  $json = $self->edit_record(\%args);
  $self = $self->edit_record(\%args, sub {
          my($self, $err, $json) = @_;
          # ...
        });

Used to edit a DNS record. $err is true and contains a string on error, while $json is a Mojo::JSON::Pointer object on success.

See "add_record" for more details on the response.

Valid %args:

  • id => $str

    DNS Record ID. Required argument.

  • type => {A,CNAME,MX,TXT,SPF,AAAA,NS,SRV,LOC},

    Name of the DNS record.

  • name => $str

    Name of the DNS record

  • content => $str

    The content of the DNS record, will depend on the the type of record being added.

  • ttl => $int

    TTL of record in seconds. 1 = Automatic, otherwise, value must in between 120 and 86400 seconds.

  • service_mode => $bool

    Status of CloudFlare Proxy, 1 = orange cloud, 0 = grey cloud.

  • priority => $int

    MX record priority.

records

  $json = $self->records($offset);
  $self = $self->records($offset, sub {
            my($self, $err, $json) = @_;
          });

$offset is optional and defaults to "all", which will retrieve all the DNS records instead of the limit of 180 set by CloudFlare.

COPYRIGHT AND LICENSE

Copyright (C) 2014, Jan Henning Thorsen

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

AUTHOR

Jan Henning Thorsen - jhthorsen@cpan.org