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)
+}