This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
requests.go
116 lines (101 loc) · 3.31 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package instances
import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
//ListOptsBuilder is an interface by which can be able to build the query string
//of the list function
type ListOptsBuilder interface {
ToInstancesListQuery() (string, error)
}
type ListOpts struct {
LifeCycleStatus string `q:"life_cycle_state"`
HealthStatus string `q:"health_status"`
}
func (opts ListOpts) ToInstancesListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
return q.String(), err
}
//List is a method by which can be able to access the list function that can get
//instances of a group
func List(client *golangsdk.ServiceClient, groupID string, opts ListOptsBuilder) pagination.Pager {
url := listURL(client, groupID)
if opts != nil {
q, err := opts.ToInstancesListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += q
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return InstancePage{pagination.SinglePageBase(r)}
})
}
//DeleteOptsBuilder is an interface by whick can be able to build the query string
//of instance deletion
type DeleteOptsBuilder interface {
ToInstanceDeleteQuery() (string, error)
}
type DeleteOpts struct {
DeleteInstance bool `q:"instance_delete"`
}
func (opts DeleteOpts) ToInstanceDeleteQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
return q.String(), err
}
//Delete is a method by which can be able to delete an instance from a group
func Delete(client *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) {
url := deleteURL(client, id)
if opts != nil {
q, err := opts.ToInstanceDeleteQuery()
if err != nil {
r.Err = err
return
}
url += q
}
_, r.Err = client.Delete(url, nil)
return
}
//BatchOptsBuilder is an interface which can build the query body of batch operation
type BatchOptsBuilder interface {
ToInstanceBatchMap() (map[string]interface{}, error)
}
//BatchOpts is a struct which represents parameters of batch operations
type BatchOpts struct {
Instances []string `json:"instances_id" required:"true"`
IsDeleteEcs string `json:"instance_delete,omitempty"`
Action string `json:"action,omitempty"`
}
func (opts BatchOpts) ToInstanceBatchMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
//batch is method which can be able to add/delete numbers instances
func batch(client *golangsdk.ServiceClient, groupID string, opts BatchOptsBuilder) (r BatchResult) {
b, err := opts.ToInstanceBatchMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(batchURL(client, groupID), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{204},
})
return
}
//BatchAdd is a method by which can add numbers of instances into a group
func BatchAdd(client *golangsdk.ServiceClient, groupID string, instances []string) (r BatchResult) {
var opts = BatchOpts{
Instances: instances,
Action: "ADD",
}
return batch(client, groupID, opts)
}
//BatchDelete is a method by which can delete numbers of instances from a group
func BatchDelete(client *golangsdk.ServiceClient, groupID string, instances []string, deleteEcs string) (r BatchResult) {
var opts = BatchOpts{
Instances: instances,
IsDeleteEcs: deleteEcs,
Action: "REMOVE",
}
return batch(client, groupID, opts)
}