WebService::DigitalOcean - Access the DigitalOcean RESTful API (v2)
version 0.026
use WebService::DigitalOcean; my $do = WebService::DigitalOcean->new({ token => $TOKEN }); ### ## Upload your public ssh key ### open my $fh, '<', $ENV{HOME} . '/.ssh/id_rsa.pub'; my $key = $do->key_create({ name => 'Andre Walker', public_key => do { local $/ = <$fh> }, }); close $fh; ### ## Select a random available region to create a droplet ### my @regions = grep { $_->{available} } @{ $do->region_list->{content} }; my $random_region = $regions[rand @regions]; ### ## Create droplets! ### my $droplet1_res = $do->droplet_create({ name => 'server1.example.com', region => $random_region->{slug}, size => '1gb', image => 'ubuntu-14-04-x64', ssh_keys => [ $key->{content}{fingerprint} ], }); die "Could not create droplet 1" unless $droplet1_res->{is_success}; my $droplet2_res = $do->droplet_create({ name => 'server2.example.com', region => $random_region->{slug}, size => '1gb', image => 'ubuntu-14-04-x64', ssh_keys => [ $key->{content}{fingerprint} ], }); die "Could not create droplet 2" unless $droplet2_res->{is_success}; ### ## Create domains ### my $subdomain1_res = $do->domain_record_create({ domain => 'example.com', type => 'A', name => 'server1', data => $droplet1_res->{content}{networks}{v4}{ip_address}, }); die "Could not create subdomain server1" unless $subdomain1_res->{is_success}; my $subdomain2_res = $do->domain_create({ domain => 'example.com', type => 'A', name => 'server2', data => $droplet2_res->{content}{networks}{v4}{ip_address}, }); die "Could not create subdomain server2" unless $subdomain2_res->{is_success};
This module implements DigitalOceans new RESTful API.
A string prepended to all API endpoints. By default, it's https://api.digitalocean.com/v2. This can be adjusted to facilitate tests.
The authorization token. It can be retrieved by logging into one's DigitalOcean account, and generating a personal token here: https://cloud.digitalocean.com/settings/applications.
my $res = $do->domain_create({ name => 'example.com', ip_address => '12.34.56.78', });
Arguments:
Str
The domain name.
The IP address the domain will point to.
Creates a new top level domain.
More info: https://developers.digitalocean.com/documentation/v2/#create-a-new-domain.
$do->domain_delete('example.com');
Deletes the specified domain.
More info: https://developers.digitalocean.com/documentation/v2/#delete-a-domain.
my $response = $do->domain_get('example.com');
Retrieves the specified domain.
More info: https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-domain.
my $response = $do->domain_list(); for (@{ $response->{content} }) { say $_->{id}; }
Lists all domains for this account.
More info: https://developers.digitalocean.com/documentation/v2/#list-all-domains.
my $response = $do->domain_record_create({ domain => 'example.com', type => 'A', name => 'www2', data => '12.34.56.78', }); my $id = $response->{content}{id};
The domain under which the record will be created.
The type of the record (eg MX, CNAME, A, etc).
The name of the record.
The data (such as the IP address) of the record.
Int
Priority, for MX or SRV records.
The port, for SRV records.
The weight, for SRV records.
Creates a new record for a domain.
More info: https://developers.digitalocean.com/documentation/v2/#create-a-new-domain-record.
$do->domain_record_delete({ domain => 'example.com', id => 1215, });
The domain to which the record belongs.
The id of the record.
Deletes the specified record.
More info: https://developers.digitalocean.com/documentation/v2/#delete-a-domain-record.
my $response = $do->domain_record_get({ domain => 'example.com', id => 1215, }); my $ip = $response->{content}{data};
Retrieves details about a particular record, identified by id.
More info: https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-domain-record.
my $response = $do->domain_record_list('example.com'); for (@{ $response->{content} }) { print "$_->{name} => $_->{data}\n"; }
The domain to which the records belong.
Retrieves all the records for a particular domain.
More info: https://developers.digitalocean.com/documentation/v2/#list-all-domain-records.
$do->droplet_create( name => "My-Droplet", region => "nyc1", size => "512mb", image => 449676389, ssh_keys => [ 52341234, 215124, 64325534 ], backups => 0, ipv6 => 1, private_networking => 0, );
ArrayRef
Bool
Creates a new droplet.
More info: https://developers.digitalocean.com/documentation/v2/#create-a-new-droplet.
$do->droplet_delete(1250928);
Deletes the specified droplet.
More info: https://developers.digitalocean.com/documentation/v2/#delete-a-droplet.
my $response = $do->droplet_get(15314123);
Retrieves the specified droplet.
More info: https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-droplet-by-id.
my $response = $do->droplet_list(); for (@{ $response->{content} }) { print $_->{id}; }
Lists all droplets for this account.
More info: https://developers.digitalocean.com/documentation/v2/#list-all-droplets.
$do->droplet_resize({ droplet => 123456, disk => 1, size => '1gb', });
Resizes a droplet.
More info: https://developers.digitalocean.com/documentation/v2/#resize-a-droplet.
$do->droplet_change_kernel({ droplet => 123456, kernel => 654321, });
Changes the kernel of a droplet.
More info: https://developers.digitalocean.com/documentation/v2/#change-the-kernel.
$do->droplet_rebuild({ droplet => 123456, image => 654321, });
Rebuilds a droplet.
More info: https://developers.digitalocean.com/documentation/v2/#rebuild-a-droplet.
Restores a droplet to an image backup.
More info: https://developers.digitalocean.com/documentation/v2/#restore-a-droplet.
$do->droplet_rename({ droplet => 123456, name => 'new-name', });
Renames a droplet, thus setting the reverse DNS.
More info: https://developers.digitalocean.com/documentation/v2/#rename-a-droplet.
$do->droplet_rebuild({ droplet => 123456, name => 'snapshot-name', });
Saves a snapshopt of the droplet.
$do->droplet_reboot(123456);
Reboots droplet.
More info: https://developers.digitalocean.com/documentation/v2/#reboot-a-droplet.
$do->droplet_power_cycle(123456);
Power cycles droplet.
More info: https://developers.digitalocean.com/documentation/v2/#power-cycle-a-droplet.
$do->droplet_power_on(123456);
Powers on droplet.
More info: https://developers.digitalocean.com/documentation/v2/#power-on-a-droplet.
$do->droplet_power_off(123456);
Powers off droplet.
More info: https://developers.digitalocean.com/documentation/v2/#power-off-a-droplet.
$do->droplet_password_reset(123456);
Resets the root password of the droplet.
More info: https://developers.digitalocean.com/documentation/v2/#password-reset-a-droplet.
$do->droplet_shutdown(123456);
Shuts down a droplet
More info: https://developers.digitalocean.com/documentation/v2/#shutdown-a-droplet.
$do->droplet_enable_ipv6(123456);
Enables IPv6 in a droplet.
More info: https://developers.digitalocean.com/documentation/v2/#enable-ipv6.
$do->droplet_enable_private_networking(123456);
Enables private networking for a droplet.
More info: https://developers.digitalocean.com/documentation/v2/#enable-private-networking.
$do->droplet_disable_backups(123456);
Disables backups for the droplet.
More info: https://developers.digitalocean.com/documentation/v2/#disable-backups.
$do->droplet_action_get({ droplet => 123456, action => 53, });
Retrieve details from a specific action.
More info: https://developers.digitalocean.com/documentation/v2/#retrieve-a-droplet-action.
my $response = $do->key_create({ name => 'my public key', public_key => <$public_key_fh>, });
Creates a new ssh key for this account.
More info: https://developers.digitalocean.com/documentation/v2/#create-a-new-key.
$do->key_delete({ id => 146432 });
Deletes the specified ssh key.
More info: https://developers.digitalocean.com/documentation/v2/#destroy-a-key.
my $response = $do->key_get({ id => 1215 });
Retrieves details about a particular ssh key, identified by id or fingerprint (pick one).
More info: https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-key.
Retrieves all the keys for this account.
More info: https://developers.digitalocean.com/documentation/v2/#list-all-keys.
my $regions = $do->region_list(); for my $r (@{ $regions->{content} }) { if ($r->{available}) { say "$r->{name} is available"; } }
Retrieves all the regions available in Digital Ocean.
More info: https://developers.digitalocean.com/documentation/v2/#list-all-regions.
Retrieves all the sizes available in Digital Ocean.
my $sizes = $do->size_list(); for my $s (@{ $sizes->{content} }) { say "Size $s->{slug} costs $s->{price_hourly} per hour."; }
More info: https://developers.digitalocean.com/documentation/v2/#list-all-sizes.
Retrieves all the images available in Digital Ocean.
my $images = $do->image_list(); for my $i (@{ $images->{content} }) { say join "\t", $i->{distribution}, $i->{id}, $i->{name}; }
More info: https://developers.digitalocean.com/documentation/v2/#list-all-images.
First DigitalOcean module, uses v1 API. It has a more OO approach than this module, and might have a more stable interface at the moment.
Documentation for API v2, in DigitalOcean's website.
André Walker <andre@cpan.org>
This software is Copyright (c) 2015 by André Walker.
This is free software, licensed under:
The GNU General Public License, Version 2, June 1991
To install WebService::DigitalOcean, copy and paste the appropriate command in to your terminal.
cpanm
cpanm WebService::DigitalOcean
CPAN shell
perl -MCPAN -e shell install WebService::DigitalOcean
For more information on module installation, please visit the detailed CPAN module installation guide.