From e580287d3a79b76b9152cf650d09c6a5e7ba9e63 Mon Sep 17 00:00:00 2001 From: Lance52259 Date: Fri, 14 May 2021 14:08:38 +0800 Subject: [PATCH] replace old power sdk with new batch power sdk - remove invalid sdk and related functions - new functions of batch power on and off --- .../compute/v2/extensions/startstop/doc.go | 19 ----- .../v2/extensions/startstop/requests.go | 19 ----- .../v2/extensions/startstop/results.go | 15 ---- .../v2/extensions/startstop/testing/doc.go | 2 - .../extensions/startstop/testing/fixtures.go | 27 ------ .../startstop/testing/requests_test.go | 31 ------- openstack/ecs/v1/powers/requests.go | 39 +++++++++ openstack/ecs/v1/powers/testing/fixtures.go | 84 +++++++++++++++++++ .../ecs/v1/powers/testing/requests_test.go | 29 +++++++ openstack/ecs/v1/powers/urls.go | 9 ++ 10 files changed, 161 insertions(+), 113 deletions(-) delete mode 100644 openstack/compute/v2/extensions/startstop/doc.go delete mode 100644 openstack/compute/v2/extensions/startstop/requests.go delete mode 100644 openstack/compute/v2/extensions/startstop/results.go delete mode 100644 openstack/compute/v2/extensions/startstop/testing/doc.go delete mode 100644 openstack/compute/v2/extensions/startstop/testing/fixtures.go delete mode 100644 openstack/compute/v2/extensions/startstop/testing/requests_test.go create mode 100644 openstack/ecs/v1/powers/requests.go create mode 100644 openstack/ecs/v1/powers/testing/fixtures.go create mode 100644 openstack/ecs/v1/powers/testing/requests_test.go create mode 100644 openstack/ecs/v1/powers/urls.go diff --git a/openstack/compute/v2/extensions/startstop/doc.go b/openstack/compute/v2/extensions/startstop/doc.go deleted file mode 100644 index ab97edb77..000000000 --- a/openstack/compute/v2/extensions/startstop/doc.go +++ /dev/null @@ -1,19 +0,0 @@ -/* -Package startstop provides functionality to start and stop servers that have -been provisioned by the OpenStack Compute service. - -Example to Stop and Start a Server - - serverID := "47b6b7b7-568d-40e4-868c-d5c41735532e" - - err := startstop.Stop(computeClient, serverID).ExtractErr() - if err != nil { - panic(err) - } - - err := startstop.Start(computeClient, serverID).ExtractErr() - if err != nil { - panic(err) - } -*/ -package startstop diff --git a/openstack/compute/v2/extensions/startstop/requests.go b/openstack/compute/v2/extensions/startstop/requests.go deleted file mode 100644 index df4aba08f..000000000 --- a/openstack/compute/v2/extensions/startstop/requests.go +++ /dev/null @@ -1,19 +0,0 @@ -package startstop - -import "github.com/huaweicloud/golangsdk" - -func actionURL(client *golangsdk.ServiceClient, id string) string { - return client.ServiceURL("servers", id, "action") -} - -// Start is the operation responsible for starting a Compute server. -func Start(client *golangsdk.ServiceClient, id string) (r StartResult) { - _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"os-start": nil}, nil, nil) - return -} - -// Stop is the operation responsible for stopping a Compute server. -func Stop(client *golangsdk.ServiceClient, id string) (r StopResult) { - _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"os-stop": nil}, nil, nil) - return -} diff --git a/openstack/compute/v2/extensions/startstop/results.go b/openstack/compute/v2/extensions/startstop/results.go deleted file mode 100644 index a1f54f2f4..000000000 --- a/openstack/compute/v2/extensions/startstop/results.go +++ /dev/null @@ -1,15 +0,0 @@ -package startstop - -import "github.com/huaweicloud/golangsdk" - -// StartResult is the response from a Start operation. Call its ExtractErr -// method to determine if the request succeeded or failed. -type StartResult struct { - golangsdk.ErrResult -} - -// StopResult is the response from Stop operation. Call its ExtractErr -// method to determine if the request succeeded or failed. -type StopResult struct { - golangsdk.ErrResult -} diff --git a/openstack/compute/v2/extensions/startstop/testing/doc.go b/openstack/compute/v2/extensions/startstop/testing/doc.go deleted file mode 100644 index b6c5b8c14..000000000 --- a/openstack/compute/v2/extensions/startstop/testing/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// startstop unit tests -package testing diff --git a/openstack/compute/v2/extensions/startstop/testing/fixtures.go b/openstack/compute/v2/extensions/startstop/testing/fixtures.go deleted file mode 100644 index 0e1253f5a..000000000 --- a/openstack/compute/v2/extensions/startstop/testing/fixtures.go +++ /dev/null @@ -1,27 +0,0 @@ -package testing - -import ( - "net/http" - "testing" - - th "github.com/huaweicloud/golangsdk/testhelper" - "github.com/huaweicloud/golangsdk/testhelper/client" -) - -func mockStartServerResponse(t *testing.T, id string) { - th.Mux.HandleFunc("/servers/"+id+"/action", 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, `{"os-start": null}`) - w.WriteHeader(http.StatusAccepted) - }) -} - -func mockStopServerResponse(t *testing.T, id string) { - th.Mux.HandleFunc("/servers/"+id+"/action", 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, `{"os-stop": null}`) - w.WriteHeader(http.StatusAccepted) - }) -} diff --git a/openstack/compute/v2/extensions/startstop/testing/requests_test.go b/openstack/compute/v2/extensions/startstop/testing/requests_test.go deleted file mode 100644 index 3ceaaaf7d..000000000 --- a/openstack/compute/v2/extensions/startstop/testing/requests_test.go +++ /dev/null @@ -1,31 +0,0 @@ -package testing - -import ( - "testing" - - "github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/startstop" - th "github.com/huaweicloud/golangsdk/testhelper" - "github.com/huaweicloud/golangsdk/testhelper/client" -) - -const serverID = "{serverId}" - -func TestStart(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - - mockStartServerResponse(t, serverID) - - err := startstop.Start(client.ServiceClient(), serverID).ExtractErr() - th.AssertNoErr(t, err) -} - -func TestStop(t *testing.T) { - th.SetupHTTP() - defer th.TeardownHTTP() - - mockStopServerResponse(t, serverID) - - err := startstop.Stop(client.ServiceClient(), serverID).ExtractErr() - th.AssertNoErr(t, err) -} diff --git a/openstack/ecs/v1/powers/requests.go b/openstack/ecs/v1/powers/requests.go new file mode 100644 index 000000000..30d8d1e2d --- /dev/null +++ b/openstack/ecs/v1/powers/requests.go @@ -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) { + 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 +} diff --git a/openstack/ecs/v1/powers/testing/fixtures.go b/openstack/ecs/v1/powers/testing/fixtures.go new file mode 100644 index 000000000..e24544802 --- /dev/null +++ b/openstack/ecs/v1/powers/testing/fixtures.go @@ -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) + }) +} diff --git a/openstack/ecs/v1/powers/testing/requests_test.go b/openstack/ecs/v1/powers/testing/requests_test.go new file mode 100644 index 000000000..5f1be329c --- /dev/null +++ b/openstack/ecs/v1/powers/testing/requests_test.go @@ -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) +} diff --git a/openstack/ecs/v1/powers/urls.go b/openstack/ecs/v1/powers/urls.go new file mode 100644 index 000000000..08aa01f17 --- /dev/null +++ b/openstack/ecs/v1/powers/urls.go @@ -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") +}