This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
New ECS powers sdk supported #446
Merged
ShiChangkuo
merged 1 commit into
huaweicloud:master
from
Lance52259:br_ecs_power_active
May 15, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
openstack/compute/v2/extensions/startstop/testing/fixtures.go
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
openstack/compute/v2/extensions/startstop/testing/requests_test.go
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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