Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanl committed Sep 3, 2014
0 parents commit b4bec8f
Show file tree
Hide file tree
Showing 30 changed files with 3,119 additions and 0 deletions.
55 changes: 55 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Copyright (c) 2014 The godo AUTHORS. All rights reserved.

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

======================
Portions of the client are based on code at:
https://github.com/google/go-github/

Copyright (c) 2013 The go-github AUTHORS. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
OPEN = $(shell which xdg-open || which gnome-open || which open)

cov:
@@gocov test | gocov-html > /tmp/coverage.html
@@${OPEN} /tmp/coverage.html

ci:
go get -d -v -t ./...
go build ./...
go test -v ./...
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# GODO

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

## Usage

```go
import "github.com/digitaloceancloud/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](https://cloud.digitalocean.com/settings/applications).

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

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

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

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

## Examples

[Digital Ocean API Documentation](https://developers.digitalocean.com/v2/)


To list all Droplets your account has access to:

```go
droplets, _, err := client.Droplet.List()
if err != nil {
fmt.Printf("error: %v\n\n", err)
return err
} else {
fmt.Printf("%v\n\n", godo.Stringify(droplets))
}
```

To create a new Droplet:

```go
dropletName := "super-cool-droplet"

createRequest := &godo.DropletCreateRequest{
Name: godo.String(dropletName),
Region: godo.String("nyc2"),
Size: godo.String("512mb"),
Image: godo.Int(3240036), // ubuntu 14.04 64bit
}

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

if err != nil {
fmt.Printf("Something bad happened: %s\n\n", err)
return err
}
```
76 changes: 76 additions & 0 deletions action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package godo

import "fmt"

const (
actionsBasePath = "v2/actions"

// ActionInProgress is an in progress action status
ActionInProgress = "in-progress"

//ActionCompleted is a completed action status
ActionCompleted = "completed"
)

// ImageActionsService handles communition with the image action related methods of the
// DigitalOcean API.
type ActionsService struct {
client *Client
}

type actionsRoot struct {
Actions []Action `json:"actions"`
}

type actionRoot struct {
Event Action `json:"action"`
}

// Action represents a DigitalOcean Action
type Action struct {
ID int `json:"id"`
Status string `json:"status"`
Type string `json:"type"`
StartedAt *Timestamp `json:"started_at"`
CompletedAt *Timestamp `json:"completed_at"`
ResourceID int `json:"resource_id"`
ResourceType string `json:"resource_type"`
}

// List all actions
func (s *ActionsService) List() ([]Action, *Response, error) {
path := actionsBasePath

req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}

root := new(actionsRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}

return root.Actions, resp, err
}

func (s *ActionsService) Get(id int) (*Action, *Response, error) {
path := fmt.Sprintf("%s/%d", actionsBasePath, id)
req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}

root := new(actionRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}

return &root.Event, resp, err
}

func (a Action) String() string {
return Stringify(a)
}
12 changes: 12 additions & 0 deletions action_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package godo

// ActionRequest reprents DigitalOcean Action Request
type ActionRequest struct {
Type string `json:"type"`
Params map[string]interface{} `json:"params,omitempty"`
}

// Converts an ActionRequest to a string.
func (d ActionRequest) String() string {
return Stringify(d)
}
16 changes: 16 additions & 0 deletions action_request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package godo

import "testing"

func TestActionRequest_String(t *testing.T) {
action := &ActionRequest{
Type: "transfer",
Params: map[string]interface{}{"key-1": "value-1"},
}

stringified := action.String()
expected := `godo.ActionRequest{Type:"transfer", Params:map[key-1:value-1]}`
if expected != stringified {
t.Errorf("Action.Stringify returned %+v, expected %+v", stringified, expected)
}
}
67 changes: 67 additions & 0 deletions action_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package godo

import (
"fmt"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestAction_List(t *testing.T) {
setup()
defer teardown()

assert := assert.New(t)

mux.HandleFunc("/v2/actions", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"actions": [{"id":1},{"id":2}]}`)
testMethod(t, r, "GET")
})

actions, _, err := client.Actions.List()
assert.NoError(err)
expected := []Action{{ID: 1}, {ID: 2}}
assert.Equal(expected, actions)
}

func TestAction_Get(t *testing.T) {
setup()
defer teardown()

assert := assert.New(t)

mux.HandleFunc("/v2/actions/12345", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"action": {"id":12345}}`)
testMethod(t, r, "GET")
})

action, _, err := client.Actions.Get(12345)
assert.NoError(err)
assert.Equal(12345, action.ID)
}

func TestAction_String(t *testing.T) {
assert := assert.New(t)
pt, err := time.Parse(time.RFC3339, "2014-05-08T20:36:47Z")
assert.NoError(err)

startedAt := &Timestamp{
Time: pt,
}
action := &Action{
ID: 1,
Status: "in-progress",
Type: "transfer",
StartedAt: startedAt,
}

stringified := action.String()
expected := `godo.Action{ID:1, Status:"in-progress", Type:"transfer", ` +
`StartedAt:godo.Timestamp{2014-05-08 20:36:47 +0000 UTC}, ` +
`ResourceID:0, ResourceType:""}`
if expected != stringified {
t.Errorf("Action.Stringify returned %+v, expected %+v", stringified, expected)
}
}
Loading

0 comments on commit b4bec8f

Please sign in to comment.