Skip to content

Commit

Permalink
Merge pull request #122 from protochron/add_volume_by_name
Browse files Browse the repository at this point in the history
Add GetVolumeByName to Storage
  • Loading branch information
protochron authored Feb 17, 2017
2 parents 7679760 + 595953f commit 1b60bc0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
27 changes: 22 additions & 5 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
// endpoints of the Digital Ocean API.
// See: https://developers.digitalocean.com/documentation/v2#storage
type StorageService interface {
ListVolumes(*ListOptions) ([]Volume, *Response, error)
ListVolumes(*ListVolumeParams) ([]Volume, *Response, error)
GetVolume(string) (*Volume, *Response, error)
CreateVolume(*VolumeCreateRequest) (*Volume, *Response, error)
DeleteVolume(string) (*Response, error)
Expand All @@ -31,6 +31,13 @@ type StorageServiceOp struct {
client *Client
}

// ListVolumeParams stores the options you can set for a ListVolumeCall
type ListVolumeParams struct {
Region string `json:"region"`
Name string `json:"name"`
ListOptions *ListOptions `json:"list_options,omitempty"`
}

var _ StorageService = &StorageServiceOp{}

// Volume represents a Digital Ocean block store volume.
Expand Down Expand Up @@ -68,10 +75,20 @@ type VolumeCreateRequest struct {
}

// ListVolumes lists all storage volumes.
func (svc *StorageServiceOp) ListVolumes(opt *ListOptions) ([]Volume, *Response, error) {
path, err := addOptions(storageAllocPath, opt)
if err != nil {
return nil, nil, err
func (svc *StorageServiceOp) ListVolumes(params *ListVolumeParams) ([]Volume, *Response, error) {
path := storageAllocPath
if params != nil {
if params.Region != "" && params.Name != "" {
path = fmt.Sprintf("%s?name=%s&region=%s", path, params.Name, params.Region)
}

if params.ListOptions != nil {
var err error
path, err = addOptions(path, params.ListOptions)
if err != nil {
return nil, nil, err
}
}
}

req, err := svc.client.NewRequest("GET", path, nil)
Expand Down
57 changes: 57 additions & 0 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,63 @@ func TestStorageVolumes_Get(t *testing.T) {
}
}

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

jBlob :=
`{
"volumes": [
{
"region": {"slug": "nyc3"},
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
"name": "myvolume",
"description": "my description",
"size_gigabytes": 100,
"droplet_ids": [10],
"created_at": "2002-10-02T15:00:00.05Z"
}
],
"links": {},
"meta": {
"total": 1
}
}`

expected := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Name: "myvolume",
Description: "my description",
SizeGigaBytes: 100,
DropletIDs: []int{10},
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
},
}

mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("name") != "myvolume" || r.URL.Query().Get("region") != "nyc3" {
t.Errorf("Storage.GetVolumeByName did not request the correct name or region")
}
testMethod(t, r, "GET")
fmt.Fprint(w, jBlob)
})

options := &ListVolumeParams{
Name: "myvolume",
Region: "nyc3",
}
volumes, _, err := client.Storage.ListVolumes(options)
if err != nil {
t.Errorf("Storage.GetVolumeByName returned error: %v", err)
}

if !reflect.DeepEqual(volumes, expected) {
t.Errorf("Storage.GetVolumeByName returned %+v, expected %+v", volumes, expected)
}
}

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

0 comments on commit 1b60bc0

Please sign in to comment.