Skip to content

Commit

Permalink
Merge pull request digitalocean#19 from andrewsomething/master
Browse files Browse the repository at this point in the history
Add support for DropletActions.Snapshot.
  • Loading branch information
macb committed Feb 20, 2015
2 parents 3c693f2 + 6a3dd9b commit e68e4a8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
13 changes: 12 additions & 1 deletion droplet_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type DropletActionsService interface {
Restore(int, int) (*Action, *Response, error)
Resize(int, string) (*Action, *Response, error)
Rename(int, string) (*Action, *Response, error)
Snapshot(int, string) (*Action, *Response, error)
doAction(int, *ActionRequest) (*Action, *Response, error)
Get(int, int) (*Action, *Response, error)
GetByURI(string) (*Action, *Response, error)
Expand Down Expand Up @@ -91,6 +92,16 @@ func (s *DropletActionsServiceOp) Rename(id int, name string) (*Action, *Respons
return s.doAction(id, request)
}

// Snapshot a Droplet
func (s *DropletActionsServiceOp) Snapshot(id int, name string) (*Action, *Response, error) {
requestType := "snapshot"
request := &ActionRequest{
"type": requestType,
"name": name,
}
return s.doAction(id, request)
}

func (s *DropletActionsServiceOp) doAction(id int, request *ActionRequest) (*Action, *Response, error) {
path := dropletActionPath(id)

Expand Down Expand Up @@ -143,4 +154,4 @@ func (s *DropletActionsServiceOp) get(path string) (*Action, *Response, error) {

func dropletActionPath(dropletID int) string {
return fmt.Sprintf("v2/droplets/%d/actions", dropletID)
}
}
35 changes: 34 additions & 1 deletion droplet_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,39 @@ func TestDropletAction_PowerCycle(t *testing.T) {
}
}

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

request := &ActionRequest{
"type": "snapshot",
"name": "Image-Name",
}

mux.HandleFunc("/v2/droplets/1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "POST")

if !reflect.DeepEqual(v, request) {
t.Errorf("Request body = %+v, expected %+v", v, request)
}

fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})

action, _, err := client.DropletActions.Snapshot(1, "Image-Name")
if err != nil {
t.Errorf("DropletActions.Snapshot returned error: %v", err)
}

expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.Snapshot returned %+v, expected %+v", action, expected)
}
}

func TestDropletActions_Get(t *testing.T) {
setup()
defer teardown()
Expand All @@ -290,4 +323,4 @@ func TestDropletActions_Get(t *testing.T) {
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.Get returned %+v, expected %+v", action, expected)
}
}
}

0 comments on commit e68e4a8

Please sign in to comment.