-
Notifications
You must be signed in to change notification settings - Fork 8
/
backendserver.go
198 lines (176 loc) · 4.88 KB
/
backendserver.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package blb
import (
"bytes"
"encoding/json"
"fmt"
"github.com/baidu/baiducloud-sdk-go/bce"
)
type BackendServer struct {
InstanceId string `json:"instanceId"`
Weight int `json:"weight,omitempty"`
}
type BackendServerStatus struct {
InstanceId string `json:"instanceId"`
Weight int `json:"weight"`
Status string `json:"status"`
}
type AddBackendServersArgs struct {
LoadBalancerId string `json:"-"`
BackendServerList []BackendServer `json:"backendServerList"`
}
func (args *AddBackendServersArgs) validate() error {
if args == nil {
return fmt.Errorf("AddBackendServersArgs need args")
}
if args.LoadBalancerId == "" {
return fmt.Errorf("AddBackendServersArgs need LoadBalancerId")
}
if args.BackendServerList == nil {
return fmt.Errorf("UpdateUDPListener need BackendServerList")
}
return nil
}
func (c *Client) AddBackendServers(args *AddBackendServersArgs, option *bce.SignOption) error {
err := args.validate()
if err != nil {
return err
}
params := map[string]string{
"clientToken": c.GenerateClientToken(),
}
postContent, err := json.Marshal(args)
if err != nil {
return err
}
req, err := bce.NewRequest("POST", c.GetURL("v1/blb"+"/"+args.LoadBalancerId+"/backendserver", params), bytes.NewBuffer(postContent))
if err != nil {
return err
}
_, err = c.SendRequest(req, option)
if err != nil {
return err
}
return nil
}
type DescribeBackendServersArgs struct {
LoadBalancerId string `json:"-"`
}
type DescribeBackendServersResponse struct {
Marker string `json:"marker"`
IsTruncated bool `json:"isTruncated"`
NextMarker string `json:"nextMarker"`
MaxKeys int `json:"maxKeys"`
BackendServerList []BackendServer `json:"backendServerList"`
}
func (args *DescribeBackendServersArgs) validate() error {
if args == nil {
return fmt.Errorf("DescribeBackendServersArgs need args")
}
if args.LoadBalancerId == "" {
return fmt.Errorf("DescribeBackendServersArgs need LoadBalancerId")
}
return nil
}
func (c *Client) DescribeBackendServers(args *DescribeBackendServersArgs, option *bce.SignOption) ([]BackendServer, error) {
err := args.validate()
if err != nil {
return nil, err
}
req, err := bce.NewRequest("GET", c.GetURL("v1/blb"+"/"+args.LoadBalancerId+"/backendserver", nil), nil)
if err != nil {
return nil, err
}
resp, err := c.SendRequest(req, option)
if err != nil {
return nil, err
}
bodyContent, err := resp.GetBodyContent()
if err != nil {
return nil, err
}
var blbsResp *DescribeBackendServersResponse
err = json.Unmarshal(bodyContent, &blbsResp)
if err != nil {
return nil, err
}
return blbsResp.BackendServerList, nil
}
type UpdateBackendServersArgs struct {
LoadBalancerId string `json:"-"`
BackendServerList []BackendServer `json:"backendServerList"`
}
func (args *UpdateBackendServersArgs) validate() error {
if args == nil {
return fmt.Errorf("UpdateBackendServersArgs need args")
}
if args.LoadBalancerId == "" {
return fmt.Errorf("UpdateBackendServersArgs need LoadBalancerId")
}
if len(args.BackendServerList) == 0 {
return fmt.Errorf("UpdateBackendServersArgs need BackendServerList")
}
return nil
}
// UpdateBackendServers update BackendServers
func (c *Client) UpdateBackendServers(args *UpdateBackendServersArgs, option *bce.SignOption) error {
err := args.validate()
if err != nil {
return err
}
params := map[string]string{
"update": "",
"clientToken": c.GenerateClientToken(),
}
postContent, err := json.Marshal(args)
if err != nil {
return err
}
req, err := bce.NewRequest("PUT", c.GetURL("v1/blb"+"/"+args.LoadBalancerId+"/backendserver", params), bytes.NewBuffer(postContent))
if err != nil {
return err
}
_, err = c.SendRequest(req, option)
if err != nil {
return err
}
return nil
}
type RemoveBackendServersArgs struct {
LoadBalancerId string `json:"-"`
BackendServerList []string `json:"backendServerList"`
}
func (args *RemoveBackendServersArgs) validate() error {
if args == nil {
return fmt.Errorf("UpdateBackendServersArgs need args")
}
if args.LoadBalancerId == "" {
return fmt.Errorf("UpdateBackendServersArgs need LoadBalancerId")
}
if len(args.BackendServerList) == 0 {
return fmt.Errorf("UpdateBackendServersArgs need BackendServerList")
}
return nil
}
// RemoveBackendServers remove a BackendServers
func (c *Client) RemoveBackendServers(args *RemoveBackendServersArgs, option *bce.SignOption) error {
err := args.validate()
if err != nil {
return err
}
params := map[string]string{
"clientToken": c.GenerateClientToken(),
}
postContent, err := json.Marshal(args)
if err != nil {
return err
}
req, err := bce.NewRequest("PUT", c.GetURL("v1/blb"+"/"+args.LoadBalancerId+"/backendserver", params), bytes.NewBuffer(postContent))
if err != nil {
return err
}
_, err = c.SendRequest(req, option)
if err != nil {
return err
}
return nil
}