tenantattr extension for cinder; ExtractInto method for handling custom Volume objects
diff --git a/openstack/blockstorage/extensions/tenantattr/results.go b/openstack/blockstorage/extensions/tenantattr/results.go
new file mode 100644
index 0000000..5e9ef23
--- /dev/null
+++ b/openstack/blockstorage/extensions/tenantattr/results.go
@@ -0,0 +1,5 @@
+package tenantattr
+
+type VolumeExt struct {
+ TenantID string `json:"os-vol-tenant-attr:tenant_id"`
+}
diff --git a/openstack/blockstorage/v2/volumes/results.go b/openstack/blockstorage/v2/volumes/results.go
index 2ad94cd..0f39fdb 100644
--- a/openstack/blockstorage/v2/volumes/results.go
+++ b/openstack/blockstorage/v2/volumes/results.go
@@ -1,6 +1,8 @@
package volumes
import (
+ "encoding/json"
+
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
@@ -100,6 +102,22 @@
return s.Volume, err
}
+func (r commonResult) ExtractInto(v interface{}) error {
+ var vol map[string]map[string]interface{}
+ err := r.ExtractInto(&vol)
+ if err != nil {
+ return err
+ }
+
+ b, err := json.Marshal(vol["volume"])
+ if err != nil {
+ return err
+ }
+
+ err = json.Unmarshal(b, &v)
+ return err
+}
+
// CreateResult contains the response body and error from a Create request.
type CreateResult struct {
commonResult
diff --git a/openstack/blockstorage/v2/volumes/testing/requests_test.go b/openstack/blockstorage/v2/volumes/testing/requests_test.go
index 789072a..ec3afc4 100644
--- a/openstack/blockstorage/v2/volumes/testing/requests_test.go
+++ b/openstack/blockstorage/v2/volumes/testing/requests_test.go
@@ -5,6 +5,7 @@
"time"
"github.com/gophercloud/gophercloud"
+ "github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/tenantattr"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
"github.com/gophercloud/gophercloud/pagination"
th "github.com/gophercloud/gophercloud/testhelper"
@@ -214,3 +215,19 @@
th.AssertNoErr(t, err)
th.CheckEquals(t, "vol-002", v.Name)
}
+
+func TestGetWithExtensions(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ MockGetResponse(t)
+
+ var s struct {
+ volumes.Volume
+ tenantattr.VolumeExt
+ }
+ err := volumes.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(&s)
+ th.AssertNoErr(t, err)
+
+ th.AssertEquals(t, "304dc00909ac4d0da6c62d816bcb3459", s.TenantID)
+}