Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions openstack/compute/v2/extensions/startstop/doc.go

This file was deleted.

19 changes: 0 additions & 19 deletions openstack/compute/v2/extensions/startstop/requests.go

This file was deleted.

15 changes: 0 additions & 15 deletions openstack/compute/v2/extensions/startstop/results.go

This file was deleted.

2 changes: 0 additions & 2 deletions openstack/compute/v2/extensions/startstop/testing/doc.go

This file was deleted.

27 changes: 0 additions & 27 deletions openstack/compute/v2/extensions/startstop/testing/fixtures.go

This file was deleted.

31 changes: 0 additions & 31 deletions openstack/compute/v2/extensions/startstop/testing/requests_test.go

This file was deleted.

39 changes: 39 additions & 0 deletions openstack/ecs/v1/powers/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package powers

import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/ecs/v1/cloudservers"
)

// PowerOpts allows batch update of the ECS instances power state through the API.
// Parameter 'Type' supports two methods of 'SOFT' and 'HARD' to shut down and reboot the machine's power.
type PowerOpts struct {
Servers []ServerInfo `json:"servers" required:"true"`
Type string `json:"type,omitempty"`
}

type ServerInfo struct {
ID string `json:"id" required:"true"`
}

// PowerOptsBuilder allows extensions to add additional parameters to the PowerAction(power on/off and reboot) request.
type PowerOptsBuilder interface {
ToPowerActionMap(option string) (map[string]interface{}, error)
}

// ToPowerActionMap assembles a request body based on the contents of a PowerOpts and the power option parameter.
func (opts PowerOpts) ToPowerActionMap(option string) (map[string]interface{}, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to add more comments about the valid vaules of option

return golangsdk.BuildRequestBody(opts, option)
}

// PowerAction uses a option parameter to control the power state of the ECS instance.
// The option only supports 'os-start' (power on), 'os-stop' (power off) and 'reboot'.
func PowerAction(client *golangsdk.ServiceClient, opts PowerOptsBuilder, option string) (r cloudservers.JobResult) {
reqBody, err := opts.ToPowerActionMap(option)
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(actionURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
return
}
84 changes: 84 additions & 0 deletions openstack/ecs/v1/powers/testing/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package testing

import (
"fmt"
"net/http"
"testing"

"github.com/huaweicloud/golangsdk/openstack/ecs/v1/cloudservers"
"github.com/huaweicloud/golangsdk/openstack/ecs/v1/powers"
th "github.com/huaweicloud/golangsdk/testhelper"
"github.com/huaweicloud/golangsdk/testhelper/client"
)

const (
expectedPowerOnRequest = `
{
"os-start": {
"servers": [
{
"id": "42ecde1c-d97d-40c6-ac84-39ed9c35dc90"
}
]
}
}`

expectedPowerOffRequest = `
{
"os-stop": {
"servers": [
{
"id": "42ecde1c-d97d-40c6-ac84-39ed9c35dc90"
}
]
}
}`

expectedPowerOnResponse = `
{
"job_id": "ff808081787e9f100179693fd6f92e39"
}`

expectedPowerOffResponse = `
{
"job_id": "ff808081787ea4b5017969510b0543e6"
}`
)

var (
powerOpts = &powers.PowerOpts{
Servers: []powers.ServerInfo{
{
ID: "42ecde1c-d97d-40c6-ac84-39ed9c35dc90",
},
},
}

expectedPowerOnResponseData = &cloudservers.JobResponse{
JobID: "ff808081787e9f100179693fd6f92e39",
}

expectedPowerOffResponseData = &cloudservers.JobResponse{
JobID: "ff808081787ea4b5017969510b0543e6",
}
)

func handlePowerOn(t *testing.T) {
th.Mux.HandleFunc("/cloudservers/action", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, expectedPowerOnResponse)
})
}

func handlePowerOff(t *testing.T) {
th.Mux.HandleFunc("/cloudservers/action", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, expectedPowerOffResponse)
})
}
29 changes: 29 additions & 0 deletions openstack/ecs/v1/powers/testing/requests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package testing

import (
"testing"

"github.com/huaweicloud/golangsdk/openstack/ecs/v1/powers"
th "github.com/huaweicloud/golangsdk/testhelper"
"github.com/huaweicloud/golangsdk/testhelper/client"
)

func TestPowerOnV1(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
handlePowerOn(t)

actual, err := powers.PowerAction(client.ServiceClient(), powerOpts, "on-start").ExtractJobResponse()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, expectedPowerOnResponseData, actual)
}

func TestPowerOffV1(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
handlePowerOff(t)

actual, err := powers.PowerAction(client.ServiceClient(), powerOpts, "on-stop").ExtractJobResponse()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, expectedPowerOffResponseData, actual)
}
9 changes: 9 additions & 0 deletions openstack/ecs/v1/powers/urls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package powers

import "github.com/huaweicloud/golangsdk"

const rootURL = "cloudservers"

func actionURL(client *golangsdk.ServiceClient) string {
return client.ServiceURL(rootURL, "action")
}