Merge pull request #44 from rightscale/feature/complete_droplet_actions

Add missing droplet actions.
This commit is contained in:
Bryan Liles 2015-04-22 11:56:24 -04:00
commit 0017aa5e88
2 changed files with 339 additions and 0 deletions

View File

@ -21,6 +21,14 @@ type DropletActionsService interface {
Resize(int, string, bool) (*Action, *Response, error)
Rename(int, string) (*Action, *Response, error)
Snapshot(int, string) (*Action, *Response, error)
DisableBackups(int) (*Action, *Response, error)
PasswordReset(int) (*Action, *Response, error)
RebuildByImageID(int, int) (*Action, *Response, error)
RebuildByImageSlug(int, string) (*Action, *Response, error)
ChangeKernel(int, int) (*Action, *Response, error)
EnableIPv6(int) (*Action, *Response, error)
EnablePrivateNetworking(int) (*Action, *Response, error)
Upgrade(int) (*Action, *Response, error)
doAction(int, *ActionRequest) (*Action, *Response, error)
Get(int, int) (*Action, *Response, error)
GetByURI(string) (*Action, *Response, error)
@ -105,6 +113,54 @@ func (s *DropletActionsServiceOp) Snapshot(id int, name string) (*Action, *Respo
return s.doAction(id, request)
}
// Disable backups for a droplet
func (s *DropletActionsServiceOp) DisableBackups(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "disable_backups"}
return s.doAction(id, request)
}
// Reset password for a droplet
func (s *DropletActionsServiceOp) PasswordReset(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "password_reset"}
return s.doAction(id, request)
}
// Rebuild droplet from an image with a given id
func (s *DropletActionsServiceOp) RebuildByImageID(id, imageID int) (*Action, *Response, error) {
request := &ActionRequest{"type": "rebuild", "image": imageID}
return s.doAction(id, request)
}
// Rebuild a droplet from an image with a given slug
func (s *DropletActionsServiceOp) RebuildByImageSlug(id int, slug string) (*Action, *Response, error) {
request := &ActionRequest{"type": "rebuild", "image": slug}
return s.doAction(id, request)
}
// Change a droplet kernel
func (s *DropletActionsServiceOp) ChangeKernel(id, kernelID int) (*Action, *Response, error) {
request := &ActionRequest{"type": "change_kernel", "kernel": kernelID}
return s.doAction(id, request)
}
// Enable IPv6 on a droplet
func (s *DropletActionsServiceOp) EnableIPv6(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_ipv6"}
return s.doAction(id, request)
}
// Enable private networking on a droplet
func (s *DropletActionsServiceOp) EnablePrivateNetworking(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_private_networking"}
return s.doAction(id, request)
}
// Upgrade a droplet
func (s *DropletActionsServiceOp) Upgrade(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "upgrade"}
return s.doAction(id, request)
}
func (s *DropletActionsServiceOp) doAction(id int, request *ActionRequest) (*Action, *Response, error) {
path := dropletActionPath(id)

View File

@ -327,6 +327,289 @@ func TestDropletAction_Snapshot(t *testing.T) {
}
}
func TestDropletAction_DisableBackups(t *testing.T) {
setup()
defer teardown()
request := &ActionRequest{
"type": "disable_backups",
}
mux.HandleFunc("/v2/droplets/1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
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.DisableBackups(1)
if err != nil {
t.Errorf("DropletActions.DisableBackups returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.DisableBackups returned %+v, expected %+v", action, expected)
}
}
func TestDropletAction_PasswordReset(t *testing.T) {
setup()
defer teardown()
request := &ActionRequest{
"type": "password_reset",
}
mux.HandleFunc("/v2/droplets/1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
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.PasswordReset(1)
if err != nil {
t.Errorf("DropletActions.PasswordReset returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.PasswordReset returned %+v, expected %+v", action, expected)
}
}
func TestDropletAction_RebuildByImageID(t *testing.T) {
setup()
defer teardown()
request := &ActionRequest{
"type": "rebuild",
"image": float64(2),
}
mux.HandleFunc("/v2/droplets/1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, "POST")
if !reflect.DeepEqual(v, request) {
t.Errorf("Request body = \n%#v, expected \n%#v", v, request)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.DropletActions.RebuildByImageID(1, 2)
if err != nil {
t.Errorf("DropletActions.RebuildByImageID returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.RebuildByImageID returned %+v, expected %+v", action, expected)
}
}
func TestDropletAction_RebuildByImageSlug(t *testing.T) {
setup()
defer teardown()
request := &ActionRequest{
"type": "rebuild",
"image": "Image-Name",
}
mux.HandleFunc("/v2/droplets/1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
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.RebuildByImageSlug(1, "Image-Name")
if err != nil {
t.Errorf("DropletActions.RebuildByImageSlug returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.RebuildByImageSlug returned %+v, expected %+v", action, expected)
}
}
func TestDropletAction_ChangeKernel(t *testing.T) {
setup()
defer teardown()
request := &ActionRequest{
"type": "change_kernel",
"kernel": float64(2),
}
mux.HandleFunc("/v2/droplets/1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
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.ChangeKernel(1, 2)
if err != nil {
t.Errorf("DropletActions.ChangeKernel returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.ChangeKernel returned %+v, expected %+v", action, expected)
}
}
func TestDropletAction_EnableIPv6(t *testing.T) {
setup()
defer teardown()
request := &ActionRequest{
"type": "enable_ipv6",
}
mux.HandleFunc("/v2/droplets/1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
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.EnableIPv6(1)
if err != nil {
t.Errorf("DropletActions.EnableIPv6 returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.EnableIPv6 returned %+v, expected %+v", action, expected)
}
}
func TestDropletAction_EnablePrivateNetworking(t *testing.T) {
setup()
defer teardown()
request := &ActionRequest{
"type": "enable_private_networking",
}
mux.HandleFunc("/v2/droplets/1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
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.EnablePrivateNetworking(1)
if err != nil {
t.Errorf("DropletActions.EnablePrivateNetworking returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.EnablePrivateNetworking returned %+v, expected %+v", action, expected)
}
}
func TestDropletAction_Upgrade(t *testing.T) {
setup()
defer teardown()
request := &ActionRequest{
"type": "upgrade",
}
mux.HandleFunc("/v2/droplets/1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
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.Upgrade(1)
if err != nil {
t.Errorf("DropletActions.Upgrade returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.Upgrade returned %+v, expected %+v", action, expected)
}
}
func TestDropletActions_Get(t *testing.T) {
setup()
defer teardown()