GO SDK of the GreenHost API. forked from github.com/digitalocean/godo
Go to file
Bryan Liles 1a08a979da Merge pull request #23 from oliver006/fix_pagination_example
Fix the pagination example
2015-03-04 14:59:28 -05:00
util renamed godo's package 2015-01-23 09:20:31 -05:00
.travis.yml hooking up travis 2015-01-22 17:08:17 -05:00
LICENSE.txt initial commit 2014-09-03 10:20:10 -04:00
README.md fix pagination example 2015-02-20 18:57:30 -05:00
action.go Update to current API spec 2014-09-08 20:36:11 -04:00
action_test.go Update to current API spec 2014-09-08 20:36:11 -04:00
doc.go initial commit 2014-09-03 10:20:10 -04:00
domains.go Update to current API spec 2014-09-08 20:36:11 -04:00
domains_test.go Update to current API spec 2014-09-08 20:36:11 -04:00
droplet_actions.go Add support for DropletActions.Snapshot. 2015-02-19 21:22:03 -05:00
droplet_actions_test.go Move TestDropletAction_Snapshot above TestDropletActions_Get with the other individual actions. 2015-02-19 23:08:08 -05:00
droplets.go New struct NetworkV6, rename struct Network -> NetworkV4. 2015-02-20 14:14:47 -05:00
droplets_test.go New struct NetworkV6, rename struct Network -> NetworkV4. 2015-02-20 14:14:47 -05:00
godo.go Update to current API spec 2014-09-08 20:36:11 -04:00
godo_test.go Add all options to droplet create request 2014-10-16 09:36:50 -04:00
image_actions.go Services are interfaces 2014-09-04 16:22:37 -04:00
image_actions_test.go Services are interfaces 2014-09-04 16:22:37 -04:00
images.go Update to current API spec 2014-09-08 20:36:11 -04:00
images_test.go Update to current API spec 2014-09-08 20:36:11 -04:00
keys.go Update to current API spec 2014-09-08 20:36:11 -04:00
keys_test.go Update to current API spec 2014-09-08 20:36:11 -04:00
links.go If pages are empty, assume we are on last 2014-10-16 09:35:23 -04:00
links_test.go Fix a few issues identified by go vet 2014-12-16 14:24:50 -08:00
regions.go Fix a few issues identified by go vet 2014-12-16 14:24:50 -08:00
regions_test.go Update to current API spec 2014-09-08 20:36:11 -04:00
sizes.go Update to current API spec 2014-09-08 20:36:11 -04:00
sizes_test.go Update to current API spec 2014-09-08 20:36:11 -04:00
strings.go Update to current API spec 2014-09-08 20:36:11 -04:00
timestamp.go initial commit 2014-09-03 10:20:10 -04:00
timestamp_test.go initial commit 2014-09-03 10:20:10 -04:00

README.md

Build Status

Godo

Godo is a Go client library for accessing the DigitalOcean V2 API.

You can view the client API docs here: http://godoc.org/github.com/digitalocean/godo

You can view Digital Ocean API docs here: https://developers.digitalocean.com/v2/

Usage

import "github.com/digitalocean/godo"

Create a new DigitalOcean client, then use the exposed services to access different parts of the DigitalOcean API.

Authentication

Currently, Personal Access Token (PAT) is the only method of authenticating with the API. You can manage your tokens at the Digital Ocean Control Panel Applications Page.

You can then use your token to create a new client:

import "code.google.com/p/goauth2/oauth"

pat := "mytoken"
t := &oauth.Transport{
	Token: &oauth.Token{AccessToken: pat},
}

client := godo.NewClient(t.Client())

Examples

To create a new Droplet:

dropletName := "super-cool-droplet"

createRequest := &godo.DropletCreateRequest{
    Name:   dropletName,
    Region: "nyc3",
    Size:   "512mb",
    Image:  "ubuntu-14-04-x64",
}

newDroplet, _, err := client.Droplets.Create(createRequest)

if err != nil {
    fmt.Printf("Something bad happened: %s\n\n", err)
    return err
}

Pagination

If a list of items is paginated by the API, you must request pages individually. For example, to fetch all Droplets:

func DropletList(client *godo.Client) ([]godo.Droplet, error) {
    // create a list to hold our droplets
    list := []godo.Droplet{}

    // create options. initially, these will be blank
    opt := &godo.ListOptions{}
    for {
        droplets, resp, err := client.Droplets.List(opt)
        if err != nil {
            return nil, err
        }

        // append the current page's droplets to our list
        for _, d := range droplets {
            list = append(list, d)
        }

        // if we are at the last page, break out the for loop
        if resp.Links == nil || resp.Links.IsLastPage() {
            break
        }

        page, err := resp.Links.CurrentPage()
        if err != nil {
            return nil, err
        }

        // set the page we want for the next request
        opt.Page = page + 1
    }

    return list, nil
}