Skip to content

Commit cfa8434

Browse files
stbenjamjtopjian
authored andcommitted
Baremetal API v1: Node Power State (#1479)
1 parent e8cb60d commit cfa8434

5 files changed

Lines changed: 90 additions & 1 deletion

File tree

openstack/baremetal/v1/nodes/requests.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,49 @@ func ChangeProvisionState(client *gophercloud.ServiceClient, id string, opts Pro
438438
})
439439
return
440440
}
441+
442+
type TargetPowerState string
443+
444+
// TargetPowerState is used when changing the power state of a node.
445+
const (
446+
PowerOn TargetPowerState = "power on"
447+
PowerOff = "power off"
448+
Rebooting = "rebooting"
449+
SoftPowerOff = "soft power off"
450+
SoftRebooting = "soft rebooting"
451+
)
452+
453+
// PowerStateOptsBuilder allows extensions to add additional parameters to the ChangePowerState request.
454+
type PowerStateOptsBuilder interface {
455+
ToPowerStateMap() (map[string]interface{}, error)
456+
}
457+
458+
// PowerStateOpts for a request to change a node's power state.
459+
type PowerStateOpts struct {
460+
Target TargetPowerState `json:"target" required:"true"`
461+
Timeout int `json:"timeout,omitempty"`
462+
}
463+
464+
// ToPowerStateMap assembles a request body based on the contents of a PowerStateOpts.
465+
func (opts PowerStateOpts) ToPowerStateMap() (map[string]interface{}, error) {
466+
body, err := gophercloud.BuildRequestBody(opts, "")
467+
if err != nil {
468+
return nil, err
469+
}
470+
471+
return body, nil
472+
}
473+
474+
// Request to change a Node's power state.
475+
func ChangePowerState(client *gophercloud.ServiceClient, id string, opts PowerStateOptsBuilder) (r ChangePowerStateResult) {
476+
reqBody, err := opts.ToPowerStateMap()
477+
if err != nil {
478+
r.Err = err
479+
return
480+
}
481+
482+
_, r.Err = client.Put(powerStateURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
483+
OkCodes: []int{202},
484+
})
485+
return
486+
}

openstack/baremetal/v1/nodes/results.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ type SupportedBootDeviceResult struct {
278278
gophercloud.Result
279279
}
280280

281+
// ChangePowerStateResult is the response from a ChangePowerState operation. Call its ExtractErr
282+
// method to determine if the call succeeded or failed.
283+
type ChangePowerStateResult struct {
284+
gophercloud.ErrResult
285+
}
286+
281287
// Each element in the response will contain a “result” variable, which will have a value of “true” or “false”, and
282288
// also potentially a reason. A value of nil indicates that the Node’s driver does not support that interface.
283289
type DriverValidation struct {

openstack/baremetal/v1/nodes/testing/fixtures.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,3 +909,17 @@ func HandleNodeChangeProvisionStateClean(t *testing.T) {
909909
w.WriteHeader(http.StatusAccepted)
910910
})
911911
}
912+
913+
// HandleChangePowerStateSuccessfully sets up the test server to respond to a change power state request for a node
914+
func HandleChangePowerStateSuccessfully(t *testing.T) {
915+
th.Mux.HandleFunc("/nodes/1234asdf/states/power", func(w http.ResponseWriter, r *http.Request) {
916+
th.TestMethod(t, r, "PUT")
917+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
918+
th.TestJSONRequest(t, r, `{
919+
"target": "power on",
920+
"timeout": 100
921+
}`)
922+
923+
w.WriteHeader(http.StatusAccepted)
924+
})
925+
}

openstack/baremetal/v1/nodes/testing/requests_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,18 @@ func TestCleanStepRequiresStep(t *testing.T) {
311311
t.Fatal("ErrMissingInput was expected to occur")
312312
}
313313
}
314+
315+
func TestChangePowerState(t *testing.T) {
316+
th.SetupHTTP()
317+
defer th.TeardownHTTP()
318+
HandleChangePowerStateSuccessfully(t)
319+
320+
opts := nodes.PowerStateOpts{
321+
Target: nodes.PowerOn,
322+
Timeout: 100,
323+
}
324+
325+
c := client.ServiceClient()
326+
err := nodes.ChangePowerState(c, "1234asdf", opts).ExtractErr()
327+
th.AssertNoErr(t, err)
328+
}

openstack/baremetal/v1/nodes/urls.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ func supportedBootDeviceURL(client *gophercloud.ServiceClient, id string) string
4242
return client.ServiceURL("nodes", id, "management", "boot_device", "supported")
4343
}
4444

45+
func statesResourceURL(client *gophercloud.ServiceClient, id string, state string) string {
46+
return client.ServiceURL("nodes", id, "states", state)
47+
}
48+
49+
func powerStateURL(client *gophercloud.ServiceClient, id string) string {
50+
return statesResourceURL(client, id, "power")
51+
}
52+
4553
func provisionStateURL(client *gophercloud.ServiceClient, id string) string {
46-
return client.ServiceURL("nodes", id, "states", "provision")
54+
return statesResourceURL(client, id, "provision")
4755
}

0 commit comments

Comments
 (0)