Skip to content

Commit 1b60bc0

Browse files
authored
Merge pull request #122 from protochron/add_volume_by_name
Add GetVolumeByName to Storage
2 parents 7679760 + 595953f commit 1b60bc0

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

storage.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const (
1515
// endpoints of the Digital Ocean API.
1616
// See: https://developers.digitalocean.com/documentation/v2#storage
1717
type StorageService interface {
18-
ListVolumes(*ListOptions) ([]Volume, *Response, error)
18+
ListVolumes(*ListVolumeParams) ([]Volume, *Response, error)
1919
GetVolume(string) (*Volume, *Response, error)
2020
CreateVolume(*VolumeCreateRequest) (*Volume, *Response, error)
2121
DeleteVolume(string) (*Response, error)
@@ -31,6 +31,13 @@ type StorageServiceOp struct {
3131
client *Client
3232
}
3333

34+
// ListVolumeParams stores the options you can set for a ListVolumeCall
35+
type ListVolumeParams struct {
36+
Region string `json:"region"`
37+
Name string `json:"name"`
38+
ListOptions *ListOptions `json:"list_options,omitempty"`
39+
}
40+
3441
var _ StorageService = &StorageServiceOp{}
3542

3643
// Volume represents a Digital Ocean block store volume.
@@ -68,10 +75,20 @@ type VolumeCreateRequest struct {
6875
}
6976

7077
// ListVolumes lists all storage volumes.
71-
func (svc *StorageServiceOp) ListVolumes(opt *ListOptions) ([]Volume, *Response, error) {
72-
path, err := addOptions(storageAllocPath, opt)
73-
if err != nil {
74-
return nil, nil, err
78+
func (svc *StorageServiceOp) ListVolumes(params *ListVolumeParams) ([]Volume, *Response, error) {
79+
path := storageAllocPath
80+
if params != nil {
81+
if params.Region != "" && params.Name != "" {
82+
path = fmt.Sprintf("%s?name=%s&region=%s", path, params.Name, params.Region)
83+
}
84+
85+
if params.ListOptions != nil {
86+
var err error
87+
path, err = addOptions(path, params.ListOptions)
88+
if err != nil {
89+
return nil, nil, err
90+
}
91+
}
7592
}
7693

7794
req, err := svc.client.NewRequest("GET", path, nil)

storage_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,63 @@ func TestStorageVolumes_Get(t *testing.T) {
127127
}
128128
}
129129

130+
func TestStorageVolumes_ListVolumesByName(t *testing.T) {
131+
setup()
132+
defer teardown()
133+
134+
jBlob :=
135+
`{
136+
"volumes": [
137+
{
138+
"region": {"slug": "nyc3"},
139+
"id": "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
140+
"name": "myvolume",
141+
"description": "my description",
142+
"size_gigabytes": 100,
143+
"droplet_ids": [10],
144+
"created_at": "2002-10-02T15:00:00.05Z"
145+
}
146+
],
147+
"links": {},
148+
"meta": {
149+
"total": 1
150+
}
151+
}`
152+
153+
expected := []Volume{
154+
{
155+
Region: &Region{Slug: "nyc3"},
156+
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
157+
Name: "myvolume",
158+
Description: "my description",
159+
SizeGigaBytes: 100,
160+
DropletIDs: []int{10},
161+
CreatedAt: time.Date(2002, 10, 02, 15, 00, 00, 50000000, time.UTC),
162+
},
163+
}
164+
165+
mux.HandleFunc("/v2/volumes", func(w http.ResponseWriter, r *http.Request) {
166+
if r.URL.Query().Get("name") != "myvolume" || r.URL.Query().Get("region") != "nyc3" {
167+
t.Errorf("Storage.GetVolumeByName did not request the correct name or region")
168+
}
169+
testMethod(t, r, "GET")
170+
fmt.Fprint(w, jBlob)
171+
})
172+
173+
options := &ListVolumeParams{
174+
Name: "myvolume",
175+
Region: "nyc3",
176+
}
177+
volumes, _, err := client.Storage.ListVolumes(options)
178+
if err != nil {
179+
t.Errorf("Storage.GetVolumeByName returned error: %v", err)
180+
}
181+
182+
if !reflect.DeepEqual(volumes, expected) {
183+
t.Errorf("Storage.GetVolumeByName returned %+v, expected %+v", volumes, expected)
184+
}
185+
}
186+
130187
func TestStorageVolumes_Create(t *testing.T) {
131188
setup()
132189
defer teardown()

0 commit comments

Comments
 (0)