Skip to content

Commit

Permalink
Cinder V3: Update a volume’s bootable status
Browse files Browse the repository at this point in the history
  • Loading branch information
kayrus committed Mar 11, 2020
1 parent 965816e commit dc7939a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
25 changes: 25 additions & 0 deletions openstack/blockstorage/extensions/volumeactions/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions openstack/blockstorage/extensions/volumeactions/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, `{}`)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit dc7939a

Please sign in to comment.