get volume rewrite
diff --git a/acceptance/openstack/blockstorage/v1/volumes_test.go b/acceptance/openstack/blockstorage/v1/volumes_test.go
index b6f6335..145fde0 100644
--- a/acceptance/openstack/blockstorage/v1/volumes_test.go
+++ b/acceptance/openstack/blockstorage/v1/volumes_test.go
@@ -3,6 +3,7 @@
package v1
import (
+ "fmt"
"os"
"strconv"
"testing"
@@ -39,8 +40,9 @@
t.Fatalf("Failed to create Block Storage v1 client: %v", err)
}
+ var cv *volumes.Volume
for i := 0; i < numVols; i++ {
- cv, err := volumes.Create(client, volumes.VolumeOpts{
+ cv, err = volumes.Create(client, volumes.VolumeOpts{
Size: 1,
Name: "gophercloud-test-volume-" + strconv.Itoa(i),
})
@@ -59,6 +61,18 @@
}
+ gr, err := volumes.Get(client, cv.ID)
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ v, err := volumes.ExtractVolume(gr)
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ fmt.Printf("Got volume: %+v\n", v)
+
pager := volumes.List(client, volumes.ListOpts{})
if err != nil {
t.Error(err)
diff --git a/openstack/blockstorage/v1/volumes/requests.go b/openstack/blockstorage/v1/volumes/requests.go
index f32e061..7de272f 100644
--- a/openstack/blockstorage/v1/volumes/requests.go
+++ b/openstack/blockstorage/v1/volumes/requests.go
@@ -76,23 +76,14 @@
return pagination.NewPager(client, volumesURL(client), createPage)
}
-/*
-func Get(c *blockstorage.Client, opts GetOpts) (Volume, error) {
- var v Volume
- h, err := c.GetHeaders()
- if err != nil {
- return v, err
- }
- url := c.GetVolumeURL(opts["id"])
- _, err = perigee.Request("GET", url, perigee.Options{
- Results: &struct {
- Volume *Volume `json:"volume"`
- }{&v},
- MoreHeaders: h,
+func Get(client *gophercloud.ServiceClient, id string) (GetResult, error) {
+ var gr GetResult
+ _, err := perigee.Request("GET", volumeURL(client, id), perigee.Options{
+ Results: &gr,
+ MoreHeaders: client.Provider.AuthenticatedHeaders(),
})
- return v, err
+ return gr, err
}
-*/
func Delete(client *gophercloud.ServiceClient, id string) error {
_, err := perigee.Request("DELETE", volumeURL(client, id), perigee.Options{
diff --git a/openstack/blockstorage/v1/volumes/results.go b/openstack/blockstorage/v1/volumes/results.go
index 429ab8c..ebf788e 100644
--- a/openstack/blockstorage/v1/volumes/results.go
+++ b/openstack/blockstorage/v1/volumes/results.go
@@ -1,27 +1,30 @@
package volumes
import (
+ "fmt"
+
"github.com/rackspace/gophercloud/pagination"
"github.com/mitchellh/mapstructure"
)
type Volume struct {
- Status string
- Name string
- Attachments []string
- AvailabilityZone string
- Bootable bool
- CreatedAt string
- Description string
- VolumeType string
- SnapshotID string
- SourceVolID string
- Metadata map[string]string
- ID string
- Size int
+ Status string `mapstructure:"status"`
+ Name string `mapstructure:"display_name"`
+ Attachments []string `mapstructure:"attachments"`
+ AvailabilityZone string `mapstructure:"availability_zone"`
+ Bootable string `mapstructure:"bootable"`
+ CreatedAt string `mapstructure:"created_at"`
+ Description string `mapstructure:"display_discription"`
+ VolumeType string `mapstructure:"volume_type"`
+ SnapshotID string `mapstructure:"snapshot_id"`
+ SourceVolID string `mapstructure:"source_volid"`
+ Metadata map[string]string `mapstructure:"metadata"`
+ ID string `mapstructure:"id"`
+ Size int `mapstructure:"size"`
}
+// ListOpts holds options for listing volumes. It is passed to the volumes.List function.
type ListOpts struct {
// AllTenants is an admin-only option. Set it to true to see a tenant volumes.
AllTenants bool
@@ -47,6 +50,7 @@
return len(volumes) == 0, nil
}
+// ExtractVolumes extracts and returns the Volumes from a 'List' request.
func ExtractVolumes(page pagination.Page) ([]Volume, error) {
var response struct {
Volumes []Volume `json:"volumes"`
@@ -55,3 +59,18 @@
err := mapstructure.Decode(page.(ListResult).Body, &response)
return response.Volumes, err
}
+
+type GetResult map[string]interface{}
+
+// ExtractVolume extracts and returns the Volume from a 'Get' request.
+func ExtractVolume(gr GetResult) (*Volume, error) {
+ var response struct {
+ Volume *Volume `json:"volume"`
+ }
+
+ err := mapstructure.Decode(gr, &response)
+ if err != nil {
+ return nil, fmt.Errorf("volumes: Error decoding volumes.GetResult: %v", err)
+ }
+ return response.Volume, nil
+}