Merge pull request #157 from andrewsykim/storage/list-volumes-param-bug
storage: list volumes should also handle only name or only region params
This commit is contained in:
commit
7249498ac0
|
@ -85,6 +85,10 @@ func (svc *StorageServiceOp) ListVolumes(ctx context.Context, params *ListVolume
|
|||
if params != nil {
|
||||
if params.Region != "" && params.Name != "" {
|
||||
path = fmt.Sprintf("%s?name=%s®ion=%s", path, params.Name, params.Region)
|
||||
} else if params.Region != "" {
|
||||
path = fmt.Sprintf("%s?region=%s", path, params.Region)
|
||||
} else if params.Name != "" {
|
||||
path = fmt.Sprintf("%s?name=%s", path, params.Name)
|
||||
}
|
||||
|
||||
if params.ListOptions != nil {
|
||||
|
|
122
storage_test.go
122
storage_test.go
|
@ -163,24 +163,136 @@ func TestStorageVolumes_ListVolumesByName(t *testing.T) {
|
|||
}
|
||||
|
||||
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")
|
||||
if r.URL.Query().Get("name") != "myvolume" || r.URL.Query().Get("region") != "" {
|
||||
t.Errorf("Storage.ListVolumeByName did not request the correct name or region")
|
||||
}
|
||||
testMethod(t, r, http.MethodGet)
|
||||
fmt.Fprint(w, jBlob)
|
||||
})
|
||||
|
||||
options := &ListVolumeParams{
|
||||
Name: "myvolume",
|
||||
}
|
||||
volumes, _, err := client.Storage.ListVolumes(ctx, options)
|
||||
if err != nil {
|
||||
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(volumes, expected) {
|
||||
t.Errorf("Storage.ListVolumeByName returned %+v, expected %+v", volumes, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorageVolumes_ListVolumesByRegion(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("region") != "nyc3" || r.URL.Query().Get("name") != "" {
|
||||
t.Errorf("Storage.ListVolumeByName did not request the correct name or region")
|
||||
}
|
||||
testMethod(t, r, http.MethodGet)
|
||||
fmt.Fprint(w, jBlob)
|
||||
})
|
||||
|
||||
options := &ListVolumeParams{
|
||||
Name: "myvolume",
|
||||
Region: "nyc3",
|
||||
}
|
||||
volumes, _, err := client.Storage.ListVolumes(ctx, options)
|
||||
if err != nil {
|
||||
t.Errorf("Storage.GetVolumeByName returned error: %v", err)
|
||||
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(volumes, expected) {
|
||||
t.Errorf("Storage.GetVolumeByName returned %+v, expected %+v", volumes, expected)
|
||||
t.Errorf("Storage.ListVolumeByName returned %+v, expected %+v", volumes, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorageVolumes_ListVolumesByNameAndRegion(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("region") != "nyc3" || r.URL.Query().Get("name") != "myvolume" {
|
||||
t.Errorf("Storage.ListVolumeByName did not request the correct name or region")
|
||||
}
|
||||
testMethod(t, r, http.MethodGet)
|
||||
fmt.Fprint(w, jBlob)
|
||||
})
|
||||
|
||||
options := &ListVolumeParams{
|
||||
Region: "nyc3",
|
||||
Name: "myvolume",
|
||||
}
|
||||
volumes, _, err := client.Storage.ListVolumes(ctx, options)
|
||||
if err != nil {
|
||||
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(volumes, expected) {
|
||||
t.Errorf("Storage.ListVolumeByName returned %+v, expected %+v", volumes, expected)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue