Update blockstorage v2 API
diff --git a/openstack/blockstorage/v2/extensions/volumeactions/doc.go b/openstack/blockstorage/v2/extensions/volumeactions/doc.go
new file mode 100644
index 0000000..0935fdb
--- /dev/null
+++ b/openstack/blockstorage/v2/extensions/volumeactions/doc.go
@@ -0,0 +1,5 @@
+// Package volumeactions provides information and interaction with volumes in the
+// OpenStack Block Storage service. A volume is a detachable block storage
+// device, akin to a USB hard drive. It can only be attached to one instance at
+// a time.
+package volumeactions
diff --git a/openstack/blockstorage/v2/extensions/volumeactions/fixtures.go b/openstack/blockstorage/v2/extensions/volumeactions/fixtures.go
new file mode 100644
index 0000000..905d6df
--- /dev/null
+++ b/openstack/blockstorage/v2/extensions/volumeactions/fixtures.go
@@ -0,0 +1,183 @@
+package volumeactions
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func MockAttachResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
+ func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "os-attach":
+ {
+ "mountpoint": "/mnt",
+ "mode": "rw",
+ "instance_uuid": "50902f4f-a974-46a0-85e9-7efc5e22dfdd"
+ }
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusAccepted)
+
+ fmt.Fprintf(w, `{}`)
+ })
+}
+
+func MockDetachResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
+ func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "os-detach": {}
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusAccepted)
+
+ fmt.Fprintf(w, `{}`)
+ })
+}
+
+func MockReserveResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
+ func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "os-reserve": {}
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusAccepted)
+
+ fmt.Fprintf(w, `{}`)
+ })
+}
+
+func MockUnreserveResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
+ func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "os-unreserve": {}
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusAccepted)
+
+ fmt.Fprintf(w, `{}`)
+ })
+}
+
+func MockInitializeConnectionResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
+ func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "os-initialize_connection":
+ {
+ "connector":
+ {
+ "ip":"127.0.0.1",
+ "host":"stack",
+ "initiator":"iqn.1994-05.com.redhat:17cf566367d2",
+ "multipath": false,
+ "platform": "x86_64",
+ "os_type": "linux2"
+ }
+ }
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusAccepted)
+
+ fmt.Fprintf(w, `{
+"connection_info": {
+ "data": {
+ "target_portals": [
+ "172.31.17.48:3260"
+ ],
+ "auth_method": "CHAP",
+ "auth_username": "5MLtcsTEmNN5jFVcT6ui",
+ "access_mode": "rw",
+ "target_lun": 0,
+ "volume_id": "cd281d77-8217-4830-be95-9528227c105c",
+ "target_luns": [
+ 0
+ ],
+ "target_iqns": [
+ "iqn.2010-10.org.openstack:volume-cd281d77-8217-4830-be95-9528227c105c"
+ ],
+ "auth_password": "x854ZY5Re3aCkdNL",
+ "target_discovered": false,
+ "encrypted": false,
+ "qos_specs": null,
+ "target_iqn": "iqn.2010-10.org.openstack:volume-cd281d77-8217-4830-be95-9528227c105c",
+ "target_portal": "172.31.17.48:3260"
+ },
+ "driver_volume_type": "iscsi"
+ }
+ }`)
+ })
+}
+
+func MockTerminateConnectionResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action",
+ func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "os-terminate_connection":
+ {
+ "connector":
+ {
+ "ip":"127.0.0.1",
+ "host":"stack",
+ "initiator":"iqn.1994-05.com.redhat:17cf566367d2",
+ "multipath": false,
+ "platform": "x86_64",
+ "os_type": "linux2"
+ }
+ }
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusAccepted)
+
+ fmt.Fprintf(w, `{}`)
+ })
+}
diff --git a/openstack/blockstorage/v2/extensions/volumeactions/requests.go b/openstack/blockstorage/v2/extensions/volumeactions/requests.go
new file mode 100644
index 0000000..5f37f3c
--- /dev/null
+++ b/openstack/blockstorage/v2/extensions/volumeactions/requests.go
@@ -0,0 +1,176 @@
+package volumeactions
+
+import (
+ "github.com/rackspace/gophercloud"
+)
+
+type AttachOptsBuilder interface {
+ ToVolumeAttachMap() (map[string]interface{}, error)
+}
+
+type AttachOpts struct {
+ // The mountpoint of this volume
+ MountPoint string
+ // The nova instance ID, can't set simultaneously with HostName
+ InstanceUUID string
+ // The hostname of baremetal host, can't set simultaneously with InstanceUUID
+ HostName string
+ // Mount mode of this volume
+ Mode string
+}
+
+func (opts AttachOpts) ToVolumeAttachMap() (map[string]interface{}, error) {
+ v := make(map[string]interface{})
+
+ if opts.MountPoint != "" {
+ v["mountpoint"] = opts.MountPoint
+ }
+ if opts.Mode != "" {
+ v["mode"] = opts.Mode
+ }
+ if opts.InstanceUUID != "" {
+ v["instance_uuid"] = opts.InstanceUUID
+ }
+ if opts.HostName != "" {
+ v["host_name"] = opts.HostName
+ }
+
+ return map[string]interface{}{"os-attach": v}, nil
+}
+
+func Attach(client *gophercloud.ServiceClient, id string, opts AttachOptsBuilder) AttachResult {
+ var res AttachResult
+
+ reqBody, err := opts.ToVolumeAttachMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ _, res.Err = client.Post(attachURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
+ OkCodes: []int{200, 201, 202},
+ })
+
+ return res
+}
+
+func Detach(client *gophercloud.ServiceClient, id string) DetachResult {
+ var res DetachResult
+
+ v := make(map[string]interface{})
+ reqBody := map[string]interface{}{"os-detach": v}
+
+ _, res.Err = client.Post(detachURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
+ OkCodes: []int{200, 201, 202},
+ })
+
+ return res
+}
+
+func Reserve(client *gophercloud.ServiceClient, id string) ReserveResult {
+ var res ReserveResult
+
+ v := make(map[string]interface{})
+ reqBody := map[string]interface{}{"os-reserve": v}
+
+ _, res.Err = client.Post(reserveURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
+ OkCodes: []int{200, 201, 202},
+ })
+
+ return res
+}
+
+func Unreserve(client *gophercloud.ServiceClient, id string) UnreserveResult {
+ var res UnreserveResult
+
+ v := make(map[string]interface{})
+ reqBody := map[string]interface{}{"os-unreserve": v}
+
+ _, res.Err = client.Post(unreserveURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
+ OkCodes: []int{200, 201, 202},
+ })
+
+ return res
+}
+
+type ConnectorOptsBuilder interface {
+ ToConnectorMap() (map[string]interface{}, error)
+}
+
+type ConnectorOpts struct {
+ IP string
+ Host string
+ Initiator string
+ Wwpns string
+ Wwnns string
+ Multipath bool
+ Platform string
+ OSType string
+}
+
+func (opts ConnectorOpts) ToConnectorMap() (map[string]interface{}, error) {
+ v := make(map[string]interface{})
+
+ if opts.IP != "" {
+ v["ip"] = opts.IP
+ }
+ if opts.Host != "" {
+ v["host"] = opts.Host
+ }
+ if opts.Initiator != "" {
+ v["initiator"] = opts.Initiator
+ }
+ if opts.Wwpns != "" {
+ v["wwpns"] = opts.Wwpns
+ }
+ if opts.Wwnns != "" {
+ v["wwnns"] = opts.Wwnns
+ }
+
+ v["multipath"] = opts.Multipath
+
+ if opts.Platform != "" {
+ v["platform"] = opts.Platform
+ }
+ if opts.OSType != "" {
+ v["os_type"] = opts.OSType
+ }
+
+ return map[string]interface{}{"connector": v}, nil
+}
+
+func InitializeConnection(client *gophercloud.ServiceClient, id string, opts *ConnectorOpts) InitializeConnectionResult {
+ var res InitializeConnectionResult
+
+ connctorMap, err := opts.ToConnectorMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ reqBody := map[string]interface{}{"os-initialize_connection": connctorMap}
+
+ _, res.Err = client.Post(initializeConnectionURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
+ OkCodes: []int{200, 201, 202},
+ })
+
+ return res
+}
+
+func TerminateConnection(client *gophercloud.ServiceClient, id string, opts *ConnectorOpts) TerminateConnectionResult {
+ var res TerminateConnectionResult
+
+ connctorMap, err := opts.ToConnectorMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ reqBody := map[string]interface{}{"os-terminate_connection": connctorMap}
+
+ _, res.Err = client.Post(teminateConnectionURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
+ OkCodes: []int{200, 201, 202},
+ })
+
+ return res
+}
diff --git a/openstack/blockstorage/v2/extensions/volumeactions/requests_test.go b/openstack/blockstorage/v2/extensions/volumeactions/requests_test.go
new file mode 100644
index 0000000..57e2b59
--- /dev/null
+++ b/openstack/blockstorage/v2/extensions/volumeactions/requests_test.go
@@ -0,0 +1,89 @@
+package volumeactions
+
+import (
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestAttach(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ MockAttachResponse(t)
+
+ options := &AttachOpts{
+ MountPoint: "/mnt",
+ Mode: "rw",
+ InstanceUUID: "50902f4f-a974-46a0-85e9-7efc5e22dfdd",
+ }
+ _, err := Attach(client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).Extract()
+ th.AssertNoErr(t, err)
+}
+
+func TestDetach(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ MockDetachResponse(t)
+
+ _, err := Detach(client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c").Extract()
+ th.AssertNoErr(t, err)
+}
+
+func TestReserve(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ MockReserveResponse(t)
+
+ _, err := Reserve(client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c").Extract()
+ th.AssertNoErr(t, err)
+}
+
+func TestUnreserve(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ MockUnreserveResponse(t)
+
+ _, err := Unreserve(client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c").Extract()
+ th.AssertNoErr(t, err)
+}
+
+func TestInitializeConnection(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ MockInitializeConnectionResponse(t)
+
+ options := &ConnectorOpts{
+ IP: "127.0.0.1",
+ Host: "stack",
+ Initiator: "iqn.1994-05.com.redhat:17cf566367d2",
+ Multipath: false,
+ Platform: "x86_64",
+ OSType: "linux2",
+ }
+ _, err := InitializeConnection(client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).Extract()
+ th.AssertNoErr(t, err)
+}
+
+func TestTerminateConnection(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ MockTerminateConnectionResponse(t)
+
+ options := &ConnectorOpts{
+ IP: "127.0.0.1",
+ Host: "stack",
+ Initiator: "iqn.1994-05.com.redhat:17cf566367d2",
+ Multipath: false,
+ Platform: "x86_64",
+ OSType: "linux2",
+ }
+ _, err := TerminateConnection(client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).Extract()
+ th.AssertNoErr(t, err)
+}
diff --git a/openstack/blockstorage/v2/extensions/volumeactions/results.go b/openstack/blockstorage/v2/extensions/volumeactions/results.go
new file mode 100644
index 0000000..c767307
--- /dev/null
+++ b/openstack/blockstorage/v2/extensions/volumeactions/results.go
@@ -0,0 +1,53 @@
+package volumeactions
+
+import (
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
+)
+
+type commonResult struct {
+ gophercloud.Result
+}
+
+// AttachResult contains the response body and error from a Get request.
+type AttachResult struct {
+ commonResult
+}
+
+// DetachResult contains the response body and error from a Get request.
+type DetachResult struct {
+ commonResult
+}
+
+// ReserveResult contains the response body and error from a Get request.
+type ReserveResult struct {
+ commonResult
+}
+
+// UnreserveResult contains the response body and error from a Get request.
+type UnreserveResult struct {
+ commonResult
+}
+
+// InitializeConnectionResult contains the response body and error from a Get request.
+type InitializeConnectionResult struct {
+ commonResult
+}
+
+// TerminateConnectionResult contains the response body and error from a Get request.
+type TerminateConnectionResult struct {
+ commonResult
+}
+
+// Extract will get the Volume object out of the commonResult object.
+func (r commonResult) Extract() (map[string]interface{}, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res map[string]interface{}
+
+ err := mapstructure.Decode(r.Body, &res)
+
+ return res, err
+}
diff --git a/openstack/blockstorage/v2/extensions/volumeactions/urls.go b/openstack/blockstorage/v2/extensions/volumeactions/urls.go
new file mode 100644
index 0000000..4b3c5f2
--- /dev/null
+++ b/openstack/blockstorage/v2/extensions/volumeactions/urls.go
@@ -0,0 +1,27 @@
+package volumeactions
+
+import "github.com/rackspace/gophercloud"
+
+func attachURL(c *gophercloud.ServiceClient, id string) string {
+ return c.ServiceURL("volumes", id, "action")
+}
+
+func detachURL(c *gophercloud.ServiceClient, id string) string {
+ return attachURL(c, id)
+}
+
+func reserveURL(c *gophercloud.ServiceClient, id string) string {
+ return attachURL(c, id)
+}
+
+func unreserveURL(c *gophercloud.ServiceClient, id string) string {
+ return attachURL(c, id)
+}
+
+func initializeConnectionURL(c *gophercloud.ServiceClient, id string) string {
+ return attachURL(c, id)
+}
+
+func teminateConnectionURL(c *gophercloud.ServiceClient, id string) string {
+ return attachURL(c, id)
+}
diff --git a/openstack/blockstorage/v2/extensions/volumeactions/urls_test.go b/openstack/blockstorage/v2/extensions/volumeactions/urls_test.go
new file mode 100644
index 0000000..ff4a5d3
--- /dev/null
+++ b/openstack/blockstorage/v2/extensions/volumeactions/urls_test.go
@@ -0,0 +1,50 @@
+package volumeactions
+
+import (
+ "testing"
+
+ "github.com/rackspace/gophercloud"
+ th "github.com/rackspace/gophercloud/testhelper"
+)
+
+const endpoint = "http://localhost:57909"
+
+func endpointClient() *gophercloud.ServiceClient {
+ return &gophercloud.ServiceClient{Endpoint: endpoint}
+}
+
+func TestAttachURL(t *testing.T) {
+ actual := attachURL(endpointClient(), "foo")
+ expected := endpoint + "volumes/foo/action"
+ th.AssertEquals(t, expected, actual)
+}
+
+func TestDettachURL(t *testing.T) {
+ actual := detachURL(endpointClient(), "foo")
+ expected := endpoint + "volumes/foo/action"
+ th.AssertEquals(t, expected, actual)
+}
+
+func TestReserveURL(t *testing.T) {
+ actual := reserveURL(endpointClient(), "foo")
+ expected := endpoint + "volumes/foo/action"
+ th.AssertEquals(t, expected, actual)
+}
+
+func TestUnreserveURL(t *testing.T) {
+ actual := unreserveURL(endpointClient(), "foo")
+ expected := endpoint + "volumes/foo/action"
+ th.AssertEquals(t, expected, actual)
+}
+
+func TestInitializeConnectionURL(t *testing.T) {
+ actual := initializeConnectionURL(endpointClient(), "foo")
+ expected := endpoint + "volumes/foo/action"
+ th.AssertEquals(t, expected, actual)
+}
+
+func TestTeminateConnectionURL(t *testing.T) {
+ actual := teminateConnectionURL(endpointClient(), "foo")
+ expected := endpoint + "volumes/foo/action"
+ th.AssertEquals(t, expected, actual)
+}
diff --git a/openstack/blockstorage/v2/volumes/fixtures.go b/openstack/blockstorage/v2/volumes/fixtures.go
new file mode 100644
index 0000000..b70e298
--- /dev/null
+++ b/openstack/blockstorage/v2/volumes/fixtures.go
@@ -0,0 +1,204 @@
+package volumes
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ fake "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func MockListResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes/detail", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+
+ fmt.Fprintf(w, `
+ {
+ "volumes": [
+ {
+ "volume_type": "lvmdriver-1",
+ "created_at": "2015-09-17T03:35:03.000000",
+ "bootable": "false",
+ "name": "vol-001",
+ "os-vol-mig-status-attr:name_id": null,
+ "consistencygroup_id": null,
+ "source_volid": null,
+ "os-volume-replication:driver_data": null,
+ "multiattach": false,
+ "snapshot_id": null,
+ "replication_status": "disabled",
+ "os-volume-replication:extended_status": null,
+ "encrypted": false,
+ "os-vol-host-attr:host": null,
+ "availability_zone": "nova",
+ "attachments": [
+ {
+ "attachment_id": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
+ "id": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
+ "volume_id": "289da7f8-6440-407c-9fb4-7db01ec49164",
+ "server_id": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
+ "host_name": "stack",
+ "device": "/dev/vdc"
+ }
+ ],
+ "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
+ "size": 75,
+ "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
+ "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459",
+ "os-vol-mig-status-attr:migstat": null,
+ "metadata": {"foo": "bar"},
+ "status": "available",
+ "description": null
+ },
+ {
+ "volume_type": "lvmdriver-1",
+ "created_at": "2015-09-17T03:32:29.000000",
+ "bootable": "false",
+ "name": "vol-002",
+ "os-vol-mig-status-attr:name_id": null,
+ "consistencygroup_id": null,
+ "source_volid": null,
+ "os-volume-replication:driver_data": null,
+ "multiattach": false,
+ "snapshot_id": null,
+ "replication_status": "disabled",
+ "os-volume-replication:extended_status": null,
+ "encrypted": false,
+ "os-vol-host-attr:host": null,
+ "availability_zone": "nova",
+ "attachments": [],
+ "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
+ "size": 75,
+ "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
+ "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459",
+ "os-vol-mig-status-attr:migstat": null,
+ "metadata": {},
+ "status": "available",
+ "description": null
+ }
+ ]
+}
+
+ `)
+ })
+}
+
+func MockGetResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ fmt.Fprintf(w, `
+{
+ "volume": {
+ "volume_type": "lvmdriver-1",
+ "created_at": "2015-09-17T03:32:29.000000",
+ "bootable": "false",
+ "name": "vol-001",
+ "os-vol-mig-status-attr:name_id": null,
+ "consistencygroup_id": null,
+ "source_volid": null,
+ "os-volume-replication:driver_data": null,
+ "multiattach": false,
+ "snapshot_id": null,
+ "replication_status": "disabled",
+ "os-volume-replication:extended_status": null,
+ "encrypted": false,
+ "os-vol-host-attr:host": null,
+ "availability_zone": "nova",
+ "attachments": [{
+ "attachment_id": "dbce64e3-f3b9-4423-a44f-a2b15deffa1b",
+ "id": "3eafc6f5-ed74-456d-90fb-f253f594dbae",
+ "volume_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
+ "server_id": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
+ "host_name": "stack",
+ "device": "/dev/vdd"
+ }],
+ "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
+ "size": 75,
+ "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
+ "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459",
+ "os-vol-mig-status-attr:migstat": null,
+ "metadata": {},
+ "status": "available",
+ "description": null
+ }
+}
+ `)
+ })
+}
+
+func MockCreateResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ th.TestHeader(t, r, "Content-Type", "application/json")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestJSONRequest(t, r, `
+{
+ "volume": {
+ "name": "vol-001",
+ "size": 75
+ }
+}
+ `)
+
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusAccepted)
+
+ fmt.Fprintf(w, `
+{
+ "volume": {
+ "size": 75,
+ "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
+ "metadata": {},
+ "created_at": "2015-09-17T03:32:29.044216",
+ "encrypted": false,
+ "bootable": "false",
+ "availability_zone": "nova",
+ "attachments": [],
+ "user_id": "ff1ce52c03ab433aaba9108c2e3ef541",
+ "status": "creating",
+ "description": null,
+ "volume_type": "lvmdriver-1",
+ "name": "vol-001",
+ "replication_status": "disabled",
+ "consistencygroup_id": null,
+ "source_volid": null,
+ "snapshot_id": null,
+ "multiattach": false
+ }
+}
+ `)
+ })
+}
+
+func MockDeleteResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "DELETE")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ w.WriteHeader(http.StatusAccepted)
+ })
+}
+
+func MockUpdateResponse(t *testing.T) {
+ th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "PUT")
+ th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
+ w.WriteHeader(http.StatusOK)
+ fmt.Fprintf(w, `
+{
+ "volume": {
+ "name": "vol-002"
+ }
+}
+ `)
+ })
+}
diff --git a/openstack/blockstorage/v2/volumes/requests.go b/openstack/blockstorage/v2/volumes/requests.go
index 3e9243a..4c60936 100644
--- a/openstack/blockstorage/v2/volumes/requests.go
+++ b/openstack/blockstorage/v2/volumes/requests.go
@@ -17,19 +17,28 @@
// the volumes.Create function. For more information about these parameters,
// see the Volume object.
type CreateOpts struct {
- // OPTIONAL
- Availability string
- // OPTIONAL
+ // The availability zone [OPTIONAL]
+ AvailabilityZone string
+ // ConsistencyGroupID is the ID of a consistency group [OPTINAL]
+ ConsistencyGroupID string
+ // The volume description [OPTIONAL]
Description string
- // OPTIONAL
+ // One or more metadata key and value pairs to associate with the volume [OPTIONAL]
Metadata map[string]string
- // OPTIONAL
+ // The volume name [OPTIONAL]
Name string
- // REQUIRED
+ // The size of the volume, in gibibytes (GiB) [REQUIRED]
Size int
- // OPTIONAL
- SnapshotID, SourceVolID, ImageID string
- // OPTIONAL
+ // the ID of the existing volume snapshot [OPTIONAL]
+ SnapshotID string
+ // SourceReplica is a UUID of an existing volume to replicate with [OPTIONAL]
+ SourceReplica string
+ // the ID of the existing volume [OPTIONAL]
+ SourceVolID string
+ // The ID of the image from which you want to create the volume.
+ // Required to create a bootable volume.
+ ImageID string
+ // The associated volume type [OPTIONAL]
VolumeType string
}
@@ -43,11 +52,14 @@
}
v["size"] = opts.Size
- if opts.Availability != "" {
- v["availability_zone"] = opts.Availability
+ if opts.AvailabilityZone != "" {
+ v["availability_zone"] = opts.AvailabilityZone
+ }
+ if opts.ConsistencyGroupID != "" {
+ v["consistencygroup_id"] = opts.ConsistencyGroupID
}
if opts.Description != "" {
- v["display_description"] = opts.Description
+ v["description"] = opts.Description
}
if opts.ImageID != "" {
v["imageRef"] = opts.ImageID
@@ -56,7 +68,10 @@
v["metadata"] = opts.Metadata
}
if opts.Name != "" {
- v["display_name"] = opts.Name
+ v["name"] = opts.Name
+ }
+ if opts.SourceReplica != "" {
+ v["source_replica"] = opts.SourceReplica
}
if opts.SourceVolID != "" {
v["source_volid"] = opts.SourceVolID
@@ -84,7 +99,7 @@
}
_, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
- OkCodes: []int{200, 201},
+ OkCodes: []int{202},
})
return res
}
@@ -173,13 +188,13 @@
v := make(map[string]interface{})
if opts.Description != "" {
- v["display_description"] = opts.Description
+ v["description"] = opts.Description
}
if opts.Metadata != nil {
v["metadata"] = opts.Metadata
}
if opts.Name != "" {
- v["display_name"] = opts.Name
+ v["name"] = opts.Name
}
return map[string]interface{}{"volume": v}, nil
diff --git a/openstack/blockstorage/v2/volumes/requests_test.go b/openstack/blockstorage/v2/volumes/requests_test.go
index 75c2bbc..962d94b 100644
--- a/openstack/blockstorage/v2/volumes/requests_test.go
+++ b/openstack/blockstorage/v2/volumes/requests_test.go
@@ -3,7 +3,6 @@
import (
"testing"
- fixtures "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes/testing"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
"github.com/rackspace/gophercloud/testhelper/client"
@@ -13,7 +12,7 @@
th.SetupHTTP()
defer th.TeardownHTTP()
- fixtures.MockListResponse(t)
+ MockListResponse(t)
count := 0
@@ -26,13 +25,58 @@
}
expected := []Volume{
- Volume{
+ {
ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
Name: "vol-001",
+ Attachments: []map[string]interface{}{{
+ "AttachmentID": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
+ "ID": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
+ "VolumeID": "289da7f8-6440-407c-9fb4-7db01ec49164",
+ "ServerID": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
+ "HostName": "stack",
+ "Device": "/dev/vdc",
+ }},
+ AvailabilityZone: "nova",
+ Bootable: "false",
+ ConsistencyGroupID: "",
+ CreatedAt: "2015-09-17T03:35:03.000000",
+ Description: "",
+ Encrypted: false,
+ Metadata: map[string]string{"foo": "bar"},
+ Multiattach: false,
+ TenantID: "304dc00909ac4d0da6c62d816bcb3459",
+ ReplicationDriverData: "",
+ ReplicationExtendedStatus: "",
+ ReplicationStatus: "disabled",
+ Size: 75,
+ SnapshotID: "",
+ SourceVolID: "",
+ Status: "available",
+ UserID: "ff1ce52c03ab433aaba9108c2e3ef541",
+ VolumeType: "lvmdriver-1",
},
- Volume{
- ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
- Name: "vol-002",
+ {
+ ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
+ Name: "vol-002",
+ Attachments: []map[string]interface{}{},
+ AvailabilityZone: "nova",
+ Bootable: "false",
+ ConsistencyGroupID: "",
+ CreatedAt: "2015-09-17T03:32:29.000000",
+ Description: "",
+ Encrypted: false,
+ Metadata: map[string]string{},
+ Multiattach: false,
+ TenantID: "304dc00909ac4d0da6c62d816bcb3459",
+ ReplicationDriverData: "",
+ ReplicationExtendedStatus: "",
+ ReplicationStatus: "disabled",
+ Size: 75,
+ SnapshotID: "",
+ SourceVolID: "",
+ Status: "available",
+ UserID: "ff1ce52c03ab433aaba9108c2e3ef541",
+ VolumeType: "lvmdriver-1",
},
}
@@ -50,7 +94,7 @@
th.SetupHTTP()
defer th.TeardownHTTP()
- fixtures.MockListResponse(t)
+ MockListResponse(t)
allPages, err := List(client.ServiceClient(), &ListOpts{}).AllPages()
th.AssertNoErr(t, err)
@@ -58,13 +102,58 @@
th.AssertNoErr(t, err)
expected := []Volume{
- Volume{
+ {
ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
Name: "vol-001",
+ Attachments: []map[string]interface{}{{
+ "AttachmentID": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
+ "ID": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
+ "VolumeID": "289da7f8-6440-407c-9fb4-7db01ec49164",
+ "ServerID": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
+ "HostName": "stack",
+ "Device": "/dev/vdc",
+ }},
+ AvailabilityZone: "nova",
+ Bootable: "false",
+ ConsistencyGroupID: "",
+ CreatedAt: "2015-09-17T03:35:03.000000",
+ Description: "",
+ Encrypted: false,
+ Metadata: map[string]string{"foo": "bar"},
+ Multiattach: false,
+ TenantID: "304dc00909ac4d0da6c62d816bcb3459",
+ ReplicationDriverData: "",
+ ReplicationExtendedStatus: "",
+ ReplicationStatus: "disabled",
+ Size: 75,
+ SnapshotID: "",
+ SourceVolID: "",
+ Status: "available",
+ UserID: "ff1ce52c03ab433aaba9108c2e3ef541",
+ VolumeType: "lvmdriver-1",
},
- Volume{
- ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
- Name: "vol-002",
+ {
+ ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
+ Name: "vol-002",
+ Attachments: []map[string]interface{}{},
+ AvailabilityZone: "nova",
+ Bootable: "false",
+ ConsistencyGroupID: "",
+ CreatedAt: "2015-09-17T03:32:29.000000",
+ Description: "",
+ Encrypted: false,
+ Metadata: map[string]string{},
+ Multiattach: false,
+ TenantID: "304dc00909ac4d0da6c62d816bcb3459",
+ ReplicationDriverData: "",
+ ReplicationExtendedStatus: "",
+ ReplicationStatus: "disabled",
+ Size: 75,
+ SnapshotID: "",
+ SourceVolID: "",
+ Status: "available",
+ UserID: "ff1ce52c03ab433aaba9108c2e3ef541",
+ VolumeType: "lvmdriver-1",
},
}
@@ -76,27 +165,26 @@
th.SetupHTTP()
defer th.TeardownHTTP()
- fixtures.MockGetResponse(t)
+ MockGetResponse(t)
v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, v.Name, "vol-001")
th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
- th.AssertEquals(t, v.Attachments[0]["device"], "/dev/vde")
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- fixtures.MockCreateResponse(t)
+ MockCreateResponse(t)
- options := &CreateOpts{Size: 75}
+ options := &CreateOpts{Size: 75, Name: "vol-001"}
n, err := Create(client.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
- th.AssertEquals(t, n.Size, 4)
+ th.AssertEquals(t, n.Size, 75)
th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
}
@@ -104,7 +192,7 @@
th.SetupHTTP()
defer th.TeardownHTTP()
- fixtures.MockDeleteResponse(t)
+ MockDeleteResponse(t)
res := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
th.AssertNoErr(t, res.Err)
@@ -114,7 +202,7 @@
th.SetupHTTP()
defer th.TeardownHTTP()
- fixtures.MockUpdateResponse(t)
+ MockUpdateResponse(t)
options := UpdateOpts{Name: "vol-002"}
v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
diff --git a/openstack/blockstorage/v2/volumes/results.go b/openstack/blockstorage/v2/volumes/results.go
index 2fd4ef1..59fa530 100644
--- a/openstack/blockstorage/v2/volumes/results.go
+++ b/openstack/blockstorage/v2/volumes/results.go
@@ -9,44 +9,68 @@
// Volume contains all the information associated with an OpenStack Volume.
type Volume struct {
- // Current status of the volume.
- Status string `mapstructure:"status"`
-
- // Human-readable display name for the volume.
- Name string `mapstructure:"display_name"`
-
// Instances onto which the volume is attached.
Attachments []map[string]interface{} `mapstructure:"attachments"`
- // This parameter is no longer used.
+ // AvailabilityZone is which availability zone the volume is in.
AvailabilityZone string `mapstructure:"availability_zone"`
// Indicates whether this is a bootable volume.
Bootable string `mapstructure:"bootable"`
+ // ConsistencyGroupID is the consistency group ID.
+ ConsistencyGroupID string `mapstructure:"consistencygroup_id"`
+
// The date when this volume was created.
CreatedAt string `mapstructure:"created_at"`
// Human-readable description for the volume.
- Description string `mapstructure:"display_description"`
+ Description string `mapstructure:"description"`
+
+ // Encrypted denotes if the volume is encrypted.
+ Encrypted bool `mapstructure:"encrypted"`
+
+ // Human-readable display name for the volume.
+ Name string `mapstructure:"name"`
// The type of volume to create, either SATA or SSD.
VolumeType string `mapstructure:"volume_type"`
+ // ReplicationDriverData contains data about the replication driver.
+ ReplicationDriverData string `mapstructure:"os-volume-replication:driver_data"`
+
+ // ReplicationExtendedStatus contains extended status about replication.
+ ReplicationExtendedStatus string `mapstructure:"os-volume-replication:extended_status"`
+
+ // ReplicationStatus is the status of replication.
+ ReplicationStatus string `mapstructure:"replication_status"`
+
// The ID of the snapshot from which the volume was created
SnapshotID string `mapstructure:"snapshot_id"`
// The ID of another block storage volume from which the current volume was created
SourceVolID string `mapstructure:"source_volid"`
+ // Current status of the volume.
+ Status string `mapstructure:"status"`
+
+ // TenantID is the id of the project that owns the volume.
+ TenantID string `mapstructure:"os-vol-tenant-attr:tenant_id"`
+
// Arbitrary key-value pairs defined by the user.
Metadata map[string]string `mapstructure:"metadata"`
+ // Multiattach denotes if the volume is multi-attach capable.
+ Multiattach bool `mapstructure:"multiattach"`
+
// Unique identifier for the volume.
ID string `mapstructure:"id"`
// Size of the volume in GB.
Size int `mapstructure:"size"`
+
+ // UserID is the id of the user who created the volume.
+ UserID string `mapstructure:"user_id"`
}
// CreateResult contains the response body and error from a Create request.
diff --git a/openstack/blockstorage/v2/volumes/testing/doc.go b/openstack/blockstorage/v2/volumes/testing/doc.go
deleted file mode 100644
index 2f66ba5..0000000
--- a/openstack/blockstorage/v2/volumes/testing/doc.go
+++ /dev/null
@@ -1,7 +0,0 @@
-/*
-This is package created is to hold fixtures (which imports testing),
-so that importing volumes package does not inadvertently import testing into production code
-More information here:
-https://github.com/rackspace/gophercloud/issues/473
-*/
-package testing
diff --git a/openstack/blockstorage/v2/volumes/testing/fixtures.go b/openstack/blockstorage/v2/volumes/testing/fixtures.go
deleted file mode 100644
index 3df7653..0000000
--- a/openstack/blockstorage/v2/volumes/testing/fixtures.go
+++ /dev/null
@@ -1,113 +0,0 @@
-package testing
-
-import (
- "fmt"
- "net/http"
- "testing"
-
- th "github.com/rackspace/gophercloud/testhelper"
- fake "github.com/rackspace/gophercloud/testhelper/client"
-)
-
-func MockListResponse(t *testing.T) {
- th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
-
- fmt.Fprintf(w, `
- {
- "volumes": [
- {
- "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
- "display_name": "vol-001"
- },
- {
- "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
- "display_name": "vol-002"
- }
- ]
- }
- `)
- })
-}
-
-func MockGetResponse(t *testing.T) {
- th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
-
- w.Header().Add("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `
-{
- "volume": {
- "display_name": "vol-001",
- "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
- "attachments": [
- {
- "device": "/dev/vde",
- "server_id": "a740d24b-dc5b-4d59-ac75-53971c2920ba",
- "id": "d6da11e5-2ed3-413e-88d8-b772ba62193d",
- "volume_id": "d6da11e5-2ed3-413e-88d8-b772ba62193d"
- }
- ]
- }
-}
- `)
- })
-}
-
-func MockCreateResponse(t *testing.T) {
- th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- th.TestHeader(t, r, "Content-Type", "application/json")
- th.TestHeader(t, r, "Accept", "application/json")
- th.TestJSONRequest(t, r, `
-{
- "volume": {
- "size": 75
- }
-}
- `)
-
- w.Header().Add("Content-Type", "application/json")
- w.WriteHeader(http.StatusCreated)
-
- fmt.Fprintf(w, `
-{
- "volume": {
- "size": 4,
- "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
- }
-}
- `)
- })
-}
-
-func MockDeleteResponse(t *testing.T) {
- th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "DELETE")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- w.WriteHeader(http.StatusNoContent)
- })
-}
-
-func MockUpdateResponse(t *testing.T) {
- th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "PUT")
- th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
- w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, `
- {
- "volume": {
- "display_name": "vol-002",
- "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
- }
- }
- `)
- })
-}
diff --git a/openstack/blockstorage/v2/volumes/urls.go b/openstack/blockstorage/v2/volumes/urls.go
index 29629a1..2523ec6 100644
--- a/openstack/blockstorage/v2/volumes/urls.go
+++ b/openstack/blockstorage/v2/volumes/urls.go
@@ -7,7 +7,7 @@
}
func listURL(c *gophercloud.ServiceClient) string {
- return createURL(c)
+ return c.ServiceURL("volumes", "detail")
}
func deleteURL(c *gophercloud.ServiceClient, id string) string {
diff --git a/openstack/blockstorage/v2/volumes/urls_test.go b/openstack/blockstorage/v2/volumes/urls_test.go
index a95270e..792b19b 100644
--- a/openstack/blockstorage/v2/volumes/urls_test.go
+++ b/openstack/blockstorage/v2/volumes/urls_test.go
@@ -21,7 +21,7 @@
func TestListURL(t *testing.T) {
actual := listURL(endpointClient())
- expected := endpoint + "volumes"
+ expected := endpoint + "volumes/detail"
th.AssertEquals(t, expected, actual)
}