WebService::DigitalOcean

Visit site

An API wrapper for the DigitalOcean RESTful API v2 in the Perl programing language.

See main documentation on MetaCPAN.

Patches welcome!

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};
Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Publish your Tool on Community

Have you created an Integration, API Wrapper, Service, or other Tool that helps developers build on DigitalOcean? Help users find it by listing it in Community Tools.

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more