From dc7939a9bdc559ae31d116c7caab18635b65944a Mon Sep 17 00:00:00 2001 From: kayrus Date: Wed, 11 Mar 2020 15:18:07 +0100 Subject: [PATCH] =?UTF-8?q?Cinder=20V3:=20Update=20a=20volume=E2=80=99s=20?= =?UTF-8?q?bootable=20status?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../extensions/volumeactions/requests.go | 25 +++++++++++++++++++ .../extensions/volumeactions/results.go | 6 +++++ .../volumeactions/testing/fixtures.go | 20 +++++++++++++++ .../volumeactions/testing/requests_test.go | 14 +++++++++++ 4 files changed, 65 insertions(+) diff --git a/openstack/blockstorage/extensions/volumeactions/requests.go b/openstack/blockstorage/extensions/volumeactions/requests.go index c2d86498a8..e579e61256 100644 --- a/openstack/blockstorage/extensions/volumeactions/requests.go +++ b/openstack/blockstorage/extensions/volumeactions/requests.go @@ -306,3 +306,28 @@ func SetImageMetadata(client *gophercloud.ServiceClient, id string, opts ImageMe }) return } + +// BootableOpts contains options for setting bootable status to a volume. +type BootableOpts struct { + // Enables or disables the bootable attribute. You can boot an instance from a bootable volume. + Bootable bool `json:"bootable"` +} + +// ToBootableMap assembles a request body based on the contents of a +// BootableOpts. +func (opts BootableOpts) ToBootableMap() (map[string]interface{}, error) { + return gophercloud.BuildRequestBody(opts, "os-set_bootable") +} + +// SetBootable will set bootable status on a volume based on the values in BootableOpts +func SetBootable(client *gophercloud.ServiceClient, id string, opts BootableOpts) (r SetBootableResult) { + b, err := opts.ToBootableMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ + OkCodes: []int{200}, + }) + return +} diff --git a/openstack/blockstorage/extensions/volumeactions/results.go b/openstack/blockstorage/extensions/volumeactions/results.go index 8587327cf6..691ecc8950 100644 --- a/openstack/blockstorage/extensions/volumeactions/results.go +++ b/openstack/blockstorage/extensions/volumeactions/results.go @@ -35,6 +35,12 @@ type SetImageMetadataResult struct { gophercloud.ErrResult } +// SetBootableResult contains the response body and error from a SetBootable +// request. +type SetBootableResult struct { + gophercloud.ErrResult +} + // ReserveResult contains the response body and error from a Reserve request. type ReserveResult struct { gophercloud.ErrResult diff --git a/openstack/blockstorage/extensions/volumeactions/testing/fixtures.go b/openstack/blockstorage/extensions/volumeactions/testing/fixtures.go index 032b5809f8..9099abd099 100644 --- a/openstack/blockstorage/extensions/volumeactions/testing/fixtures.go +++ b/openstack/blockstorage/extensions/volumeactions/testing/fixtures.go @@ -307,3 +307,23 @@ func MockSetImageMetadataResponse(t *testing.T) { fmt.Fprintf(w, `{}`) }) } + +func MockSetBootableResponse(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-set_bootable": { + "bootable": true + } +} + `) + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + + fmt.Fprintf(w, `{}`) + }) +} diff --git a/openstack/blockstorage/extensions/volumeactions/testing/requests_test.go b/openstack/blockstorage/extensions/volumeactions/testing/requests_test.go index c0919bb5ea..f792b8c2d8 100644 --- a/openstack/blockstorage/extensions/volumeactions/testing/requests_test.go +++ b/openstack/blockstorage/extensions/volumeactions/testing/requests_test.go @@ -180,3 +180,17 @@ func TestSetImageMetadata(t *testing.T) { err := volumeactions.SetImageMetadata(client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr() th.AssertNoErr(t, err) } + +func TestSetBootable(t *testing.T) { + th.SetupHTTP() + defer th.TeardownHTTP() + + MockSetBootableResponse(t) + + options := volumeactions.BootableOpts{ + Bootable: true, + } + + err := volumeactions.SetBootable(client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr() + th.AssertNoErr(t, err) +}