Skip to content

Commit

Permalink
Adds DetachByDropletID
Browse files Browse the repository at this point in the history
  • Loading branch information
wardviaene committed Nov 7, 2016
1 parent 2ff8a02 commit 950397c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions storage_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "fmt"
type StorageActionsService interface {
Attach(volumeID string, dropletID int) (*Action, *Response, error)
Detach(volumeID string) (*Action, *Response, error)
DetachByDropletID(volumeID string, dropletID int) (*Action, *Response, error)
Get(volumeID string, actionID int) (*Action, *Response, error)
List(volumeID string, opt *ListOptions) ([]Action, *Response, error)
Resize(volumeID string, sizeGigabytes int, regionSlug string) (*Action, *Response, error)
Expand Down Expand Up @@ -42,6 +43,15 @@ func (s *StorageActionsServiceOp) Detach(volumeID string) (*Action, *Response, e
return s.doAction(volumeID, request)
}

// Detach a storage volume from a droplet by droplet ID.
func (s *StorageActionsServiceOp) DetachByDropletID(volumeID string, dropletID int) (*Action, *Response, error) {
request := &ActionRequest{
"type": "detach",
"droplet_id": dropletID,
}
return s.doAction(volumeID, request)
}

// Get an action for a particular storage volume by id.
func (s *StorageActionsServiceOp) Get(volumeID string, actionID int) (*Action, *Response, error) {
path := fmt.Sprintf("%s/%d", storageAllocationActionPath(volumeID), actionID)
Expand Down
32 changes: 32 additions & 0 deletions storage_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,38 @@ func TestStoragesActions_Detach(t *testing.T) {
}
}

func TestStoragesActions_DetachByDropletID(t *testing.T) {
setup()
defer teardown()
volumeID := "98d414c6-295e-4e3a-ac58-eb9456c1e1d1"
dropletID := 123456

detachByDropletIDRequest := &ActionRequest{
"type": "detach",
"droplet_id": float64(dropletID), // encoding/json decodes numbers as floats
}

mux.HandleFunc("/v2/volumes/"+volumeID+"/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, detachByDropletIDRequest) {
t.Errorf("want=%#v", detachByDropletIDRequest)
t.Errorf("got=%#v", v)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})

_, _, err := client.StorageActions.DetachByDropletID(volumeID, dropletID)
if err != nil {
t.Errorf("StoragesActions.DetachByDropletID returned error: %v", err)
}
}

func TestStorageActions_Get(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 950397c

Please sign in to comment.