-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
39 lines (33 loc) · 1.54 KB
/
requests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package powers
import (
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/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
}