Go Daddy API, Example use

Very recently, Go Daddy released it’s public API that can be used with Go Daddy Cloud Servers and Easy Database. Since I’m a Go Daddy Cloud Servers customer (and employee of Go Daddy), I wanted to provide you with an example use of the API.

I had an extra server that I wanted to delete. Of course, I could do this with the GUI, but for practice, I decided to delete it with a script.

The first thing I did was grabbed a copy of the Go Daddy API for PHP, which can be found here:
https://github.com/godaddy/gdapi-php

and gave the documentation a once over:
http://docs.cloud.secureserver.net/

I then went to the Go Daddy Cloud Servers GUI and clicked the API tab, accepted the TOS, and generated an API key. It was easy, one click and my access key and secret key were provided.

I then created a file called csremove.php:

<?php

$name = $argv[1];

if (!$name)
	die("USAGE: php csremove.php 'server name'\n");

require_once('init.php');
$url        = 'https://api.cloud.secureserver.net/v1/schemas';
$access_key = 'use your access key';
$secret_key = 'use your secret key';
$client = new \GDAPI\Client($url, $access_key, $secret_key);


$machines = $client->virtualmachine->query();
$id = null;
foreach ( $machines as $machine )
{
  if ( preg_match("/^".preg_quote($name)."$/", $machine->getName()) )
  	$id = $machine->getId();
}

if (!$id)
	die("Did not find machine you were looking for, $name\n");
	
$machine = $client->virtualmachine->getById($id);
echo $machine->getName() . " selected\n";

$result = $machine->remove();

Now I can remove a machine by name from the command line very easily like this:

php csremove.php 'machine name'

Pretty cool! Love it.

Thought I’d mention, I did get an error on first run:

Fatal error: Uncaught GDAPI\HTTPRequestException 'SSL certificate problem, verify that the CA cert is OK.
Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed' in C:\Users\dave\git\gdapi-php\class\Client.php(518) 
#0 C:\Users\dave\git\gdapi-php\class\CurlRequest.php(249): GDAPI\Client->error('SSL certificate...', 'request', Array)
 #1 C:\Users\dave\git\gdapi-php\class\Client.php(311): GDAPI\CurlRequest->request('GET', '/') 
#2 C:\Users\dave\git\gdapi-php\class\Client.php(225): GDAPI\Client->loadSchemas() 
#3 C:\Users\dave\git\gdapi-php\try1.php(8): GDAPI\Client->__construct('https://api.clo...', 'XXXXXXXXXXXXXXX...', 'XXXXXXXXXXXXXXX...') 
#4 {main} thrown in C:\Users\dave\git\gdapi-php\class\Client.php on line 518 

This will be addressed soon, I’m sure of it. The problem is that PHP/Curl on my laptop doesn’t have a valid CA bundle available to it. If you run into this problem, you can work around this by disabling the ssl verification in the Client.php file, like this:

'verify_ssl'          => false,

Or better, download a CA bundle from http://curl.haxx.se/docs/caextract.html and hack CurlRequest.php to include it by adding this to the setOptions function:

CURLOPT_CAINFO          =>  'cacert.pem',

Or, wait for Go Daddy to address this issue. They are aware and will publish a solution soon.

UPDATE 7/18/12:
Go Daddy addressed the SSL issue, check it out: https://github.com/godaddy/gdapi-php#ssl-problems

And I’ve forked the repo and stated to add some CLI commands similar to this example
https://github.com/gddk/gdapi-php

Example uses:

php cs-vm-list.php
php cs-vm-create.php serverName01 networkId templateId
php cs-vm-remote.php serverName01
php cs-nw-list.php # to list your networks, useful to get their IDs
php cs-tp-list.php # to list your templates, useful to get their IDs

UPDATE 7/23/2012:
https://github.com/gddk/gdapi-php
…my fork is now rebased from godaddy master, and has just the cli-cs.php and cs-config.template.php files added.

The cli-cs.php file is fun to use. Can run commands like:

php cli-cs.php vmls
php cli-cs.php vmls -v name=web01
php cli-cs.php vmcreate name=web02 networkId=1234 templateId=1234
php cli-cs.php vmdelete name=web02
php cli-cs.php vmdelete id=1234
php cli-cs.php nwls
php cli-cs.php tpls
php cli-cs.php lbls -v
php cli-cs.php ipls -v

48 thoughts on “Go Daddy API, Example use

  1. I’m day dreaming about turning the CLI into a single file for easier maintenance and less clutter.

    php cs.php vm-list
    php cs.php vm-create serverName01 networkId templateID

    like that.

Comments are closed.