-
Notifications
You must be signed in to change notification settings - Fork 26
/
loadbalancer.go
337 lines (296 loc) · 11.2 KB
/
loadbalancer.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shell
import (
"yunion.io/x/cloudmux/pkg/cloudprovider"
huawei "yunion.io/x/cloudmux/pkg/multicloud/hcso"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type ElbListOptions struct {
}
shellutils.R(&ElbListOptions{}, "elb-list", "List loadbalancers", func(cli *huawei.SRegion, args *ElbListOptions) error {
elbs, err := cli.GetILoadBalancers()
if err != nil {
return err
}
printList(elbs, len(elbs), 0, 0, []string{})
return nil
})
type ElbCreateOptions struct {
Name string `help:"loadblancer name"`
SUBNET string `help:"subnet id"`
PrivateIP string `help:"loadbalancer private ip address"`
EipID string `help:"loadbalancer public ip id"`
}
shellutils.R(&ElbCreateOptions{}, "elb-create", "create loadbalancer", func(cli *huawei.SRegion, args *ElbCreateOptions) error {
loadbalancer := &cloudprovider.SLoadbalancer{
Name: args.Name,
NetworkIDs: []string{args.SUBNET},
EipID: args.EipID,
Address: args.PrivateIP,
}
elb, err := cli.CreateLoadBalancer(loadbalancer)
if err != nil {
return err
}
printObject(elb)
return nil
})
type ElbDeleteOptions struct {
ID string `help:"loadblancer id"`
}
shellutils.R(&ElbDeleteOptions{}, "elb-delete", "delete loadbalancer", func(cli *huawei.SRegion, args *ElbDeleteOptions) error {
err := cli.DeleteLoadBalancer(args.ID)
if err != nil {
return err
}
return nil
})
type ElbListenerListOptions struct {
ElbID string `help:"loadblancer id"`
}
shellutils.R(&ElbListenerListOptions{}, "elb-listener-list", "list loadbalancer listener", func(cli *huawei.SRegion, args *ElbListenerListOptions) error {
listeners, err := cli.GetLoadBalancerListeners(args.ElbID)
if err != nil {
return err
}
printList(listeners, len(listeners), 0, 0, []string{})
return nil
})
type ElbListenerCreateOptions struct {
Name string `help:"listener name"`
Desc string `help:"listener Description"`
Http2 bool `help:"http2 enable status"`
PoolID string `help:"default backend group id"`
CertId string `help:"default certification id"`
XForwardedFor bool `help:"XForwardedFor enable status"`
LISTENER_TYPE string `help:"listener type" choices:"tcp|udp|http|https"`
LISTENER_PORT int `help:"listener port"`
ELB_ID string `help:"loadbalancer id"`
}
shellutils.R(&ElbListenerCreateOptions{}, "elb-listener-create", "create loadbalancer listener", func(cli *huawei.SRegion, args *ElbListenerCreateOptions) error {
input := &cloudprovider.SLoadbalancerListener{
Name: args.Name,
LoadbalancerID: args.ELB_ID,
ListenerType: args.LISTENER_TYPE,
ListenerPort: args.LISTENER_PORT,
BackendGroupID: args.PoolID,
EnableHTTP2: args.Http2,
CertificateID: args.CertId,
Description: args.Desc,
XForwardedFor: args.XForwardedFor,
}
listener, err := cli.CreateLoadBalancerListener(input)
if err != nil {
return err
}
printObject(listener)
return nil
})
type ElbListenerUpdateOptions struct {
Name string `help:"listener name"`
Desc string `help:"listener Description"`
Http2 bool `help:"http2 enable status"`
PoolID string `help:"default backend group id"`
CertId string `help:"default certification id"`
XForwardedFor bool `help:"XForwardedFor enable status"`
LISTENER_ID string `help:"listener id"`
}
shellutils.R(&ElbListenerUpdateOptions{}, "elb-listener-update", "update loadbalancer listener", func(cli *huawei.SRegion, args *ElbListenerUpdateOptions) error {
input := &cloudprovider.SLoadbalancerListener{
Name: args.Name,
BackendGroupID: args.PoolID,
EnableHTTP2: args.Http2,
CertificateID: args.CertId,
Description: args.Desc,
XForwardedFor: args.XForwardedFor,
}
err := cli.UpdateLoadBalancerListener(args.LISTENER_ID, input)
if err != nil {
return err
}
return nil
})
type ElbBackendGroupListOptions struct {
ElbID string `help:"loadbalancer id"`
}
shellutils.R(&ElbBackendGroupListOptions{}, "elb-backend-group-list", "List backend groups", func(cli *huawei.SRegion, args *ElbBackendGroupListOptions) error {
elbbg, err := cli.GetLoadBalancerBackendGroups(args.ElbID)
if err != nil {
return err
}
printList(elbbg, len(elbbg), 0, 0, []string{})
return nil
})
type ElbBackendGroupCreateOptions struct {
Name string `help:"backend group name"`
Desc string `help:"backend group description"`
PROTOCOL string `help:"backend group protocol" choices:"tcp|udp|http"`
ALGORITHM string `help:"backend group algorithm" choices:"rr|wlc|sch"`
ListenerID string `help:"listener id to binding"`
ElbID string `help:"loadbalancer id belong to"`
StickySessionType string `help:"sticky session type" choices:"insert|server"`
StickySessionCookieName string `help:"sticky session cookie name"`
StickySessionTimeout int `help:"sticky session timeout. udp/tcp 1~60. http 1~1440"`
HealthCheck bool `help:"enable health check"`
HealthCheckType string `help:"health check type protocol" choices:"tcp|udp|http"`
HealthCheckTimeout int `help:"health check timeout"`
HealthCheckDomain string `help:"health check domain"`
HealthCheckURI string `help:"health check uri path"`
HealthCheckInterval int `help:"health check interval"`
HealthCheckRise int `help:"health check max retries"`
}
shellutils.R(&ElbBackendGroupCreateOptions{}, "elb-backend-group-create", "Create backend groups", func(cli *huawei.SRegion, args *ElbBackendGroupCreateOptions) error {
var health *cloudprovider.SLoadbalancerHealthCheck
if args.HealthCheck {
health = &cloudprovider.SLoadbalancerHealthCheck{
HealthCheckType: args.HealthCheckType,
HealthCheckTimeout: args.HealthCheckTimeout,
HealthCheckDomain: args.HealthCheckDomain,
HealthCheckURI: args.HealthCheckURI,
HealthCheckInterval: args.HealthCheckInterval,
HealthCheckRise: args.HealthCheckRise,
}
}
var sticky *cloudprovider.SLoadbalancerStickySession
if len(args.StickySessionType) > 0 {
sticky = &cloudprovider.SLoadbalancerStickySession{
StickySessionCookie: args.StickySessionCookieName,
StickySessionType: args.StickySessionType,
StickySessionCookieTimeout: args.StickySessionTimeout,
}
}
group := &cloudprovider.SLoadbalancerBackendGroup{
Name: args.Name,
LoadbalancerID: args.ElbID,
ListenerID: args.ListenerID,
ListenType: args.PROTOCOL,
Scheduler: args.ALGORITHM,
StickySession: sticky,
HealthCheck: health,
}
elbbg, err := cli.CreateLoadBalancerBackendGroup(group)
if err != nil {
return err
}
printObject(elbbg)
return nil
})
type ElbBackendGroupUpdateOptions struct {
POOL_ID string `help:"backend group id"`
Name string `help:"backend group name"`
}
shellutils.R(&ElbBackendGroupUpdateOptions{}, "elb-backend-group-update", "Update backend groups", func(cli *huawei.SRegion, args *ElbBackendGroupUpdateOptions) error {
group := &cloudprovider.SLoadbalancerBackendGroup{
Name: args.Name,
}
elbbg, err := cli.UpdateLoadBalancerBackendGroup(args.POOL_ID, group)
if err != nil {
return err
}
printObject(elbbg)
return nil
})
type ElbBackendGroupDeleteOptions struct {
POOL_ID string `help:"backend group id"`
}
shellutils.R(&ElbBackendGroupDeleteOptions{}, "elb-backend-group-delete", "Delete backend group", func(cli *huawei.SRegion, args *ElbBackendGroupDeleteOptions) error {
err := cli.DeleteLoadBalancerBackendGroup(args.POOL_ID)
if err != nil {
return err
}
return nil
})
type ElbBackendAddOptions struct {
Name string `help:"backend name"`
POOL_ID string `help:"backend group id"`
SUBNET_ID string `help:"instance subnet id"`
ADDRESS string `help:"instance ip address"`
PORT int `help:"backend protocol port [1,65535]"`
Weight int `help:"backend weight [0,100]" default:"1"`
}
shellutils.R(&ElbBackendAddOptions{}, "elb-backend-add", "Add backend to backendgroup", func(cli *huawei.SRegion, args *ElbBackendAddOptions) error {
elbb, err := cli.AddLoadBalancerBackend(args.POOL_ID, args.SUBNET_ID, args.ADDRESS, args.PORT, args.Weight)
if err != nil {
return err
}
printObject(elbb)
return nil
})
type ElbBackendListOptions struct {
POOL_ID string `help:"backend group id"`
}
shellutils.R(&ElbBackendListOptions{}, "elb-backend-list", "list backend", func(cli *huawei.SRegion, args *ElbBackendListOptions) error {
elbb, err := cli.GetLoadBalancerBackends(args.POOL_ID)
if err != nil {
return err
}
printList(elbb, len(elbb), 0, 0, []string{})
return nil
})
type ElbListenerPolicyListOptions struct {
ListenerID string `help:"listener id"`
}
shellutils.R(&ElbListenerPolicyListOptions{}, "elb-listener-policy-list", "List listener policies", func(cli *huawei.SRegion, args *ElbListenerPolicyListOptions) error {
elblp, err := cli.GetLoadBalancerPolicies(args.ListenerID)
if err != nil {
return err
}
printList(elblp, len(elblp), 0, 0, []string{})
return nil
})
type ElbListenerPolicyCreateOptions struct {
LISTENER_ID string `help:"listener id"`
Name string `help:"policy name"`
Domain string `help:"policy domain"`
Path string `help:"policy path"`
PoolID string `help:"backend group name"`
}
shellutils.R(&ElbListenerPolicyCreateOptions{}, "elb-listener-policy-create", "Create listener policy", func(cli *huawei.SRegion, args *ElbListenerPolicyCreateOptions) error {
rule := &cloudprovider.SLoadbalancerListenerRule{
Name: args.Name,
Domain: args.Domain,
Path: args.Path,
BackendGroupID: args.PoolID,
}
elblp, err := cli.CreateLoadBalancerPolicy(args.LISTENER_ID, rule)
if err != nil {
return err
}
printObject(elblp)
return nil
})
type ElbListenerPolicyDeleteOptions struct {
POLICY_ID string `help:"policy id"`
}
shellutils.R(&ElbListenerPolicyDeleteOptions{}, "elb-listener-policy-delete", "Delete listener policy", func(cli *huawei.SRegion, args *ElbListenerPolicyDeleteOptions) error {
err := cli.DeleteLoadBalancerPolicy(args.POLICY_ID)
if err != nil {
return err
}
return nil
})
type ElbListenerPolicyRuleListOptions struct {
POLICY_ID string `help:"policy id"`
}
shellutils.R(&ElbListenerPolicyRuleListOptions{}, "elb-listener-policyrule-list", "List listener policy rules", func(cli *huawei.SRegion, args *ElbListenerPolicyRuleListOptions) error {
elblpr, err := cli.GetLoadBalancerPolicyRules(args.POLICY_ID)
if err != nil {
return err
}
printList(elblpr, len(elblpr), 0, 0, []string{})
return nil
})
}