list volume types
diff --git a/acceptance/openstack/blockstorage/v1/volumeTypes_test.go b/acceptance/openstack/blockstorage/v1/volumeTypes_test.go
index 3801f16..d49d2eb 100644
--- a/acceptance/openstack/blockstorage/v1/volumeTypes_test.go
+++ b/acceptance/openstack/blockstorage/v1/volumeTypes_test.go
@@ -8,6 +8,7 @@
"time"
"github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumeTypes"
+ "github.com/rackspace/gophercloud/pagination"
)
var numVolTypes = 1
@@ -54,4 +55,18 @@
}
t.Logf("Got volume type: %+v\n", v)
+ pager := volumeTypes.List(client, volumeTypes.ListOpts{})
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ err = pager.EachPage(func(page pagination.Page) (bool, error) {
+ volTypes, err := volumeTypes.ExtractVolumeTypes(page)
+ if len(volTypes) != numVolTypes {
+ t.Errorf("Expected %d volume types, got %d", numVolTypes, len(volTypes))
+ }
+ t.Logf("Listing volume types: %+v\n", volTypes)
+ return true, err
+ })
+
}
diff --git a/openstack/blockstorage/v1/volumeTypes/requests.go b/openstack/blockstorage/v1/volumeTypes/requests.go
index 326ea78..89ee2c5 100644
--- a/openstack/blockstorage/v1/volumeTypes/requests.go
+++ b/openstack/blockstorage/v1/volumeTypes/requests.go
@@ -4,6 +4,7 @@
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/utils"
+ "github.com/rackspace/gophercloud/pagination"
)
type CreateOpts struct {
@@ -64,3 +65,11 @@
})
return gr, err
}
+
+func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
+ createPage := func(r pagination.LastHTTPResponse) pagination.Page {
+ return ListResult{pagination.SinglePageBase(r)}
+ }
+
+ return pagination.NewPager(client, volumeTypesURL(client), createPage)
+}
diff --git a/openstack/blockstorage/v1/volumeTypes/results.go b/openstack/blockstorage/v1/volumeTypes/results.go
index cf2356f..2207eec 100644
--- a/openstack/blockstorage/v1/volumeTypes/results.go
+++ b/openstack/blockstorage/v1/volumeTypes/results.go
@@ -3,7 +3,7 @@
import (
"fmt"
- //"github.com/rackspace/gophercloud/pagination"
+ "github.com/rackspace/gophercloud/pagination"
"github.com/mitchellh/mapstructure"
)
@@ -14,6 +14,34 @@
Name string `json:"name" mapstructure:"name"`
}
+// ListOpts holds options for listing volumes. It is passed to the volumes.List function.
+type ListOpts struct {
+}
+
+// ListResult is a *http.Response that is returned from a call to the List function.
+type ListResult struct {
+ pagination.SinglePageBase
+}
+
+// IsEmpty returns true if a ListResult contains no container names.
+func (r ListResult) IsEmpty() (bool, error) {
+ volumeTypes, err := ExtractVolumeTypes(r)
+ if err != nil {
+ return true, err
+ }
+ return len(volumeTypes) == 0, nil
+}
+
+// ExtractVolumeTypes extracts and returns the Volumes from a 'List' request.
+func ExtractVolumeTypes(page pagination.Page) ([]VolumeType, error) {
+ var response struct {
+ VolumeTypes []VolumeType `mapstructure:"volume_types"`
+ }
+
+ err := mapstructure.Decode(page.(ListResult).Body, &response)
+ return response.VolumeTypes, err
+}
+
type GetResult map[string]interface{}
func ExtractVolumeType(gr GetResult) (*VolumeType, error) {