Initial attempt at volumeattach extension
diff --git a/openstack/compute/v2/extensions/volumeattach/doc.go b/openstack/compute/v2/extensions/volumeattach/doc.go
new file mode 100644
index 0000000..22f68d8
--- /dev/null
+++ b/openstack/compute/v2/extensions/volumeattach/doc.go
@@ -0,0 +1,3 @@
+// Package volumeattach provides the ability to attach and detach volumes
+// to instances
+package volumeattach
diff --git a/openstack/compute/v2/extensions/volumeattach/fixtures.go b/openstack/compute/v2/extensions/volumeattach/fixtures.go
new file mode 100644
index 0000000..6b08e18
--- /dev/null
+++ b/openstack/compute/v2/extensions/volumeattach/fixtures.go
@@ -0,0 +1,139 @@
+// +build fixtures
+
+package volumeattach
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+// ListOutput is a sample response to a List call.
+const ListOutput = `
+{
+ "volumeAttachments": [
+ {
+ "device": "/dev/vdd",
+ "id": "a26887c6-c47b-4654-abb5-dfadf7d3f803",
+ "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f803"
+ },
+ {
+ "device": "/dev/vdc",
+ "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
+ "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804"
+ }
+ ]
+}
+`
+
+// GetOutput is a sample response to a Get call.
+const GetOutput = `
+{
+ "volumeAttachment": {
+ "device": "/dev/vdc",
+ "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
+ "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804"
+ }
+}
+`
+
+// CreateOutput is a sample response to a Create call.
+const CreateOutput = `
+{
+ "volumeAttachment": {
+ "device": "/dev/vdc",
+ "id": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
+ "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804"
+ }
+}
+`
+
+// FirstVolumeAttachment is the first result in ListOutput.
+var FirstVolumeAttachment = VolumeAttachment{
+ Device: "/dev/vdd",
+ ID: "a26887c6-c47b-4654-abb5-dfadf7d3f803",
+ ServerID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ VolumeID: "a26887c6-c47b-4654-abb5-dfadf7d3f803",
+}
+
+// SecondVolumeAttachment is the first result in ListOutput.
+var SecondVolumeAttachment = VolumeAttachment{
+ Device: "/dev/vdc",
+ ID: "a26887c6-c47b-4654-abb5-dfadf7d3f804",
+ ServerID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ VolumeID: "a26887c6-c47b-4654-abb5-dfadf7d3f804",
+}
+
+// ExpectedVolumeAttachmentSlide is the slice of results that should be parsed
+// from ListOutput, in the expected order.
+var ExpectedVolumeAttachmentSlice = []VolumeAttachment{FirstVolumeAttachment, SecondVolumeAttachment}
+
+// CreatedVolumeAttachment is the parsed result from CreatedOutput.
+var CreatedVolumeAttachment = VolumeAttachment{
+ Device: "/dev/vdc",
+ ID: "a26887c6-c47b-4654-abb5-dfadf7d3f804",
+ ServerID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ VolumeID: "a26887c6-c47b-4654-abb5-dfadf7d3f804",
+}
+
+// HandleListSuccessfully configures the test server to respond to a List request.
+func HandleListSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, ListOutput)
+ })
+}
+
+// HandleGetSuccessfully configures the test server to respond to a Get request
+// for an existing attachment
+func HandleGetSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments/a26887c6-c47b-4654-abb5-dfadf7d3f804", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, GetOutput)
+ })
+}
+
+// HandleCreateSuccessfully configures the test server to respond to a Create request
+// for a new attachment
+func HandleCreateSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "POST")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+ th.TestJSONRequest(t, r, `
+{
+ "volumeAttachment": {
+ "volumeId": "a26887c6-c47b-4654-abb5-dfadf7d3f804",
+ "serverId": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
+ "device": "/dev/vdc"
+ }
+}
+`)
+
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, CreateOutput)
+ })
+}
+
+// HandleDeleteSuccessfully configures the test server to respond to a Delete request for a
+// an existing attachment
+func HandleDeleteSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/os-volume_attachments/a26887c6-c47b-4654-abb5-dfadf7d3f804", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "DELETE")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ w.WriteHeader(http.StatusAccepted)
+ })
+}
diff --git a/openstack/compute/v2/extensions/volumeattach/requests.go b/openstack/compute/v2/extensions/volumeattach/requests.go
new file mode 100644
index 0000000..ba1b7d4
--- /dev/null
+++ b/openstack/compute/v2/extensions/volumeattach/requests.go
@@ -0,0 +1,119 @@
+package volumeattach
+
+import (
+ "errors"
+
+ "github.com/racker/perigee"
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// CreateOptsExt adds a VolumeAttachment option to the base CreateOpts.
+type CreateOptsExt struct {
+ servers.CreateOptsBuilder
+ Device string `json:"device"`
+ VolumeID string `json:"volumeId"`
+ ServerID string `json:"serverId"`
+}
+
+// ToServerCreateMap adds the volume_id, device, and optionally server_id to
+// the base server creation options.
+func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
+ base, err := opts.CreateOptsBuilder.ToServerCreateMap()
+ if err != nil {
+ return nil, err
+ }
+
+ serverMap := base["server"].(map[string]interface{})
+ serverMap["device"] = opts.Device
+ serverMap["volume_id"] = opts.VolumeID
+ serverMap["server_id"] = opts.ServerID
+
+ return base, nil
+}
+
+// List returns a Pager that allows you to iterate over a collection of VolumeAttachments.
+func List(client *gophercloud.ServiceClient, serverId string) pagination.Pager {
+ return pagination.NewPager(client, listURL(client, serverId), func(r pagination.PageResult) pagination.Page {
+ return VolumeAttachmentsPage{pagination.SinglePageBase(r)}
+ })
+}
+
+// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
+// CreateOpts struct in this package does.
+type CreateOptsBuilder interface {
+ ToVolumeAttachmentCreateMap() (map[string]interface{}, error)
+}
+
+// CreateOpts specifies volume attachment creation or import parameters.
+type CreateOpts struct {
+ // Device is the device that the volume will attach to the instance as. Omit for "auto"
+ Device string
+
+ // VolumeID is the ID of the volume to attach to the instance
+ VolumeID string
+
+ // ServerID is the ID of the server that the volume will be attached to
+ ServerID string
+}
+
+// ToVolumeAttachmentCreateMap constructs a request body from CreateOpts.
+func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]interface{}, error) {
+ if opts.VolumeID == "" {
+ return nil, errors.New("Missing field required for volume attachment creation: VolumeID")
+ }
+
+ if opts.ServerID == "" {
+ return nil, errors.New("Missing field required for volume attachment creation: ServerID")
+ }
+
+ volumeAttachment := make(map[string]interface{})
+ volumeAttachment["volumeId"] = opts.VolumeID
+ volumeAttachment["serverId"] = opts.ServerID
+ if opts.Device != "" {
+ volumeAttachment["device"] = opts.Device
+ }
+
+ return map[string]interface{}{"volumeAttachment": volumeAttachment}, nil
+}
+
+// Create requests the creation of a new volume attachment on the server
+func Create(client *gophercloud.ServiceClient, serverId string, opts CreateOptsBuilder) CreateResult {
+ var res CreateResult
+
+ reqBody, err := opts.ToVolumeAttachmentCreateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
+ _, res.Err = perigee.Request("POST", createURL(client, serverId), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ ReqBody: reqBody,
+ Results: &res.Body,
+ OkCodes: []int{200},
+ })
+ return res
+}
+
+// Get returns public data about a previously created VolumeAttachment.
+func Get(client *gophercloud.ServiceClient, serverId, aId string) GetResult {
+ var res GetResult
+ _, res.Err = perigee.Request("GET", getURL(client, serverId, aId), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ Results: &res.Body,
+ OkCodes: []int{200},
+ })
+ return res
+}
+
+// Delete requests the deletion of a previous stored VolumeAttachment from the server.
+func Delete(client *gophercloud.ServiceClient, serverId, aId string) DeleteResult {
+ var res DeleteResult
+ _, res.Err = perigee.Request("DELETE", deleteURL(client, serverId, aId), perigee.Options{
+ MoreHeaders: client.AuthenticatedHeaders(),
+ OkCodes: []int{202},
+ })
+ return res
+}
diff --git a/openstack/compute/v2/extensions/volumeattach/requests_test.go b/openstack/compute/v2/extensions/volumeattach/requests_test.go
new file mode 100644
index 0000000..e0d5fdf
--- /dev/null
+++ b/openstack/compute/v2/extensions/volumeattach/requests_test.go
@@ -0,0 +1,66 @@
+package volumeattach
+
+import (
+ "testing"
+
+ "github.com/rackspace/gophercloud/pagination"
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestList(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleListSuccessfully(t)
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+
+ count := 0
+ err := List(client.ServiceClient(), serverId).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+ actual, err := ExtractVolumeAttachments(page)
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, ExpectedVolumeAttachmentSlice, actual)
+
+ return true, nil
+ })
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, 1, count)
+}
+
+func TestCreate(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleCreateSuccessfully(t)
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+
+ actual, err := Create(client.ServiceClient(), serverId, CreateOpts{
+ Device: "/dev/vdc",
+ VolumeID: "a26887c6-c47b-4654-abb5-dfadf7d3f804",
+ ServerID: serverId,
+ }).Extract()
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, &CreatedVolumeAttachment, actual)
+}
+
+func TestGet(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleGetSuccessfully(t)
+ aId := "a26887c6-c47b-4654-abb5-dfadf7d3f804"
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+
+ actual, err := Get(client.ServiceClient(), serverId, aId).Extract()
+ th.AssertNoErr(t, err)
+ th.CheckDeepEquals(t, &SecondVolumeAttachment, actual)
+}
+
+func TestDelete(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleDeleteSuccessfully(t)
+ aId := "a26887c6-c47b-4654-abb5-dfadf7d3f804"
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+
+ err := Delete(client.ServiceClient(), serverId, aId).ExtractErr()
+ th.AssertNoErr(t, err)
+}
diff --git a/openstack/compute/v2/extensions/volumeattach/results.go b/openstack/compute/v2/extensions/volumeattach/results.go
new file mode 100644
index 0000000..26be39e
--- /dev/null
+++ b/openstack/compute/v2/extensions/volumeattach/results.go
@@ -0,0 +1,84 @@
+package volumeattach
+
+import (
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
+)
+
+// VolumeAttach controls the attachment of a volume to an instance.
+type VolumeAttachment struct {
+ // ID is a unique id of the attachment
+ ID string `mapstructure:"id"`
+
+ // Device is what device the volume is attached as
+ Device string `mapstructure:"device"`
+
+ // VolumeID is the ID of the attached volume
+ VolumeID string `mapstructure:"volumeId"`
+
+ // ServerID is the ID of the instance that has the volume attached
+ ServerID string `mapstructure:"serverId"`
+}
+
+// VolumeAttachmentsPage stores a single, only page of VolumeAttachments
+// results from a List call.
+type VolumeAttachmentsPage struct {
+ pagination.SinglePageBase
+}
+
+// IsEmpty determines whether or not a VolumeAttachmentsPage is empty.
+func (page VolumeAttachmentsPage) IsEmpty() (bool, error) {
+ va, err := ExtractVolumeAttachments(page)
+ return len(va) == 0, err
+}
+
+// ExtractVolumeAttachments interprets a page of results as a slice of
+// VolumeAttachments.
+func ExtractVolumeAttachments(page pagination.Page) ([]VolumeAttachment, error) {
+ casted := page.(VolumeAttachmentsPage).Body
+ var response struct {
+ VolumeAttachments []VolumeAttachment `mapstructure:"volumeAttachments"`
+ }
+
+ err := mapstructure.WeakDecode(casted, &response)
+
+ return response.VolumeAttachments, err
+}
+
+type VolumeAttachmentResult struct {
+ gophercloud.Result
+}
+
+// Extract is a method that attempts to interpret any VolumeAttachment resource
+// response as a VolumeAttachment struct.
+func (r VolumeAttachmentResult) Extract() (*VolumeAttachment, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res struct {
+ VolumeAttachment *VolumeAttachment `json:"volumeAttachment" mapstructure:"volumeAttachment"`
+ }
+
+ err := mapstructure.Decode(r.Body, &res)
+ return res.VolumeAttachment, err
+}
+
+// CreateResult is the response from a Create operation. Call its Extract method to interpret it
+// as a VolumeAttachment.
+type CreateResult struct {
+ VolumeAttachmentResult
+}
+
+// GetResult is the response from a Get operation. Call its Extract method to interpret it
+// as a VolumeAttachment.
+type GetResult struct {
+ VolumeAttachmentResult
+}
+
+// DeleteResult is the response from a Delete operation. Call its Extract method to determine if
+// the call succeeded or failed.
+type DeleteResult struct {
+ gophercloud.ErrResult
+}
diff --git a/openstack/compute/v2/extensions/volumeattach/urls.go b/openstack/compute/v2/extensions/volumeattach/urls.go
new file mode 100644
index 0000000..9d9d178
--- /dev/null
+++ b/openstack/compute/v2/extensions/volumeattach/urls.go
@@ -0,0 +1,25 @@
+package volumeattach
+
+import "github.com/rackspace/gophercloud"
+
+const resourcePath = "os-volume_attachments"
+
+func resourceURL(c *gophercloud.ServiceClient, serverId string) string {
+ return c.ServiceURL("servers", serverId, resourcePath)
+}
+
+func listURL(c *gophercloud.ServiceClient, serverId string) string {
+ return resourceURL(c, serverId)
+}
+
+func createURL(c *gophercloud.ServiceClient, serverId string) string {
+ return resourceURL(c, serverId)
+}
+
+func getURL(c *gophercloud.ServiceClient, serverId, aId string) string {
+ return c.ServiceURL("servers", serverId, resourcePath, aId)
+}
+
+func deleteURL(c *gophercloud.ServiceClient, serverId, aId string) string {
+ return getURL(c, serverId, aId)
+}
diff --git a/openstack/compute/v2/extensions/volumeattach/urls_test.go b/openstack/compute/v2/extensions/volumeattach/urls_test.go
new file mode 100644
index 0000000..8ee0e42
--- /dev/null
+++ b/openstack/compute/v2/extensions/volumeattach/urls_test.go
@@ -0,0 +1,46 @@
+package volumeattach
+
+import (
+ "testing"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestListURL(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ c := client.ServiceClient()
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+
+ th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/os-volume_attachments", listURL(c, serverId))
+}
+
+func TestCreateURL(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ c := client.ServiceClient()
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+
+ th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/os-volume_attachments", createURL(c, serverId))
+}
+
+func TestGetURL(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ c := client.ServiceClient()
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+ aId := "a26887c6-c47b-4654-abb5-dfadf7d3f804"
+
+ th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/os-volume_attachments/"+aId, getURL(c, serverId, aId))
+}
+
+func TestDeleteURL(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ c := client.ServiceClient()
+ serverId := "4d8c3732-a248-40ed-bebc-539a6ffd25c0"
+ aId := "a26887c6-c47b-4654-abb5-dfadf7d3f804"
+
+ th.CheckEquals(t, c.Endpoint+"servers/"+serverId+"/os-volume_attachments/"+aId, deleteURL(c, serverId, aId))
+}