Merge pull request #122 from protochron/add_volume_by_name
Add GetVolumeByName to Storage
This commit is contained in:
commit
1b60bc0c6d
27
storage.go
27
storage.go
|
@ -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)
|
||||
|
@ -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.
|
||||
|
@ -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®ion=%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)
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue