A C# client for Go Daddy® REST APIs.
- Json.NET
- An account in a compatible service, such as Cloud Servers™
- Your API Access and Secret key pair
If you haven't already tried it, open up the base URL of the API you want to use in a browser. Enter your Access Key as the username and your Secret Key as the password. This interface will allow you to get familiar with what Resource types are available in the API and what operations and actions you can perform on them. For more information, see the documentation site.
Download the source:
Create a client:
string url = "https://api.cloud.secureserver.net/v1/";
string access_key = "your-access-key";
string secret_key = "your-secret-key";
Client gdapi = new Client(url, access_key, secret_key);
Resources are pulled from the API by the Client.
Collection machines = gdapi.getCollection("virtualmachines");
Console.WriteLine( "There are " + machines.getResources().Count + " machines:" );
foreach ( Resource machine in machines )
{
Console.WriteLine( machine.getProperty( "name" ) );
}
Filters allow you to search a collection for resources matching a set of conditions.
CollectionFilter filter = new CollectionFilter("publicStartPort","gt","80");
Collection networkrules = gdapi.getCollection("networkrules",filter);
Console.WriteLine( "There are " + networkrules.getResources().Count + " networkrules with a publicStartPort greater than 80." );
If you know the ID of the resource you are looking for already, you can also get it directly.
Resource machine = gdapi.getResourceById("virtualmachines","your-machine-id");
Attributes of a Resource are stored in a properties dictionary inside a Resource object. To access a particular resource the getProperty method is available.
Resource machine = gdapi.getResourceById("virtualmachines","your-machine-id");
String privateIp = machine.getProperty("privateIpv4Address"); // e.g. '10.1.1.3'
String size = machine.getProperty("ramSizeMb"); // e.g. 1024
Resource machine = gdapi.getResourceById("virtualmachines","your-machine-id");
machine.setProperty("name","bigger machine");
machine.setProperty("offering","2gb-4cpu");
// Save the changes
Resource result = gdapi.save("virtualmachines",machine);
Resource network = new Resource();
network.setProperty("name","My Network");
network.setProperty("domain","mynetwork.local");
network.setProperty("ipv4Cidr","192.168.0.0/24");
Resource result = gdapi.create("networks",network);
With an instance of the resource:
Resource machine = gdapi.getResourceById("virtualmachines","your-machine-id");
Resource result = gdapi.delete("virtualmachines",machine);
Actions are used to perform operations that go beyond simple create/read/update/delete. Available actions are stored in the actions Dictionary of the Resource object.
Resource machine = gdapi.getResourceById("virtualmachines","your-machine-id");
Resource result = gdapi.doAction(machine.getAction("restart"));
Response collections and resources generally have a "links" attribute containing URLs to related resources and collections. For example a virtual machine belongs to a network and has one or more volumes.
Resource machine = gdapi.getResourceById("virtualmachines","your-machine-id");
Resource network = gdapi.fetchLinkAsResource(machine,"network"); // Network resource
Collection volumes = gdapi.fetchLinkAsCollection(machine,"volumes"); // Collection of Volume resources
By default, any error response will be thrown as an exception. The most general type of exception is APIException.
try
{
Resource machine = gdapi.getResourceById("virtualmachines","your-machine-id");
Console.WriteLine( "I found it" );
}
catch ( NotFoundException e )
{
Console.WriteLine( "I couldn't find that machine" );
}
catch ( APIException e )
{
Console.WriteLine( "Something else went wrong" );
}