forked from denverdino/aliyungo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listeners.go
554 lines (482 loc) · 18.5 KB
/
listeners.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package slb
import (
"fmt"
"strings"
"time"
"github.com/denverdino/aliyungo/common"
)
type ListenerStatus string
const (
Starting = ListenerStatus("starting")
Running = ListenerStatus("running")
Configuring = ListenerStatus("configuring")
Stopping = ListenerStatus("stopping")
Stopped = ListenerStatus("stopped")
)
type SchedulerType string
const (
WRRScheduler = SchedulerType("wrr")
WLCScheduler = SchedulerType("wlc")
)
type FlagType string
const (
OnFlag = FlagType("on")
OffFlag = FlagType("off")
)
type StickySessionType string
const (
InsertStickySessionType = StickySessionType("insert")
ServerStickySessionType = StickySessionType("server")
)
const BackendServerPort = -520
type HealthCheckHttpCodeType string
const (
HTTP_2XX = HealthCheckHttpCodeType("http_2xx")
HTTP_3XX = HealthCheckHttpCodeType("http_3xx")
HTTP_4XX = HealthCheckHttpCodeType("http_4xx")
HTTP_5XX = HealthCheckHttpCodeType("http_5xx")
)
func EncodeHealthCheckHttpCodeType(healthCheckHttpCodes []HealthCheckHttpCodeType) (HealthCheckHttpCodeType, error) {
code := ""
if nil == healthCheckHttpCodes || len(healthCheckHttpCodes) < 1 {
return "", fmt.Errorf("Invalid size of healthCheckHttpCodes")
}
for _, healthCheckHttpCode := range healthCheckHttpCodes {
if strings.EqualFold(string(HTTP_2XX), string(healthCheckHttpCode)) ||
strings.EqualFold(string(HTTP_3XX), string(healthCheckHttpCode)) ||
strings.EqualFold(string(HTTP_4XX), string(healthCheckHttpCode)) ||
strings.EqualFold(string(HTTP_5XX), string(healthCheckHttpCode)) {
if "" == code {
code = string(healthCheckHttpCode)
} else {
if strings.Contains(code, string(healthCheckHttpCode)) {
return "", fmt.Errorf("Duplicates healthCheckHttpCode(%v in %v)", healthCheckHttpCode, healthCheckHttpCodes)
}
code += code + "," + string(healthCheckHttpCode)
}
} else {
return "", fmt.Errorf("Invalid healthCheckHttpCode(%v in %v)", healthCheckHttpCode, healthCheckHttpCodes)
}
}
return HealthCheckHttpCodeType(code), nil
}
type CommonLoadBalancerListenerResponse struct {
common.Response
}
type HTTPListenerType struct {
LoadBalancerId string
ListenerPort int
BackendServerPort int
Bandwidth int
Scheduler SchedulerType
StickySession FlagType
StickySessionType StickySessionType
CookieTimeout int
Cookie string
HealthCheck FlagType
HealthCheckDomain string
HealthCheckURI string
HealthCheckConnectPort int
HealthyThreshold int
UnhealthyThreshold int
HealthCheckTimeout int
HealthCheckInterval int
ForwardPort int
ListenerForward FlagType
HealthCheckHttpCode HealthCheckHttpCodeType
VServerGroup FlagType
VServerGroupId string
Gzip FlagType
XForwardedFor_SLBID FlagType
XForwardedFor_SLBIP FlagType
XForwardedFor_proto FlagType
Description string
AclId string
AclStatus string
AclType string
}
type CreateLoadBalancerHTTPListenerArgs HTTPListenerType
// CreateLoadBalancerHTTPListener create HTTP listener on loadbalancer
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&CreateLoadBalancerHTTPListener
func (client *Client) CreateLoadBalancerHTTPListener(args *CreateLoadBalancerHTTPListenerArgs) (err error) {
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("CreateLoadBalancerHTTPListener", args, response)
return err
}
type HTTPSListenerType struct {
HTTPListenerType
ServerCertificateId string
}
type CreateLoadBalancerHTTPSListenerArgs HTTPSListenerType
// CreateLoadBalancerHTTPSListener create HTTPS listener on loadbalancer
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&CreateLoadBalancerHTTPSListener
func (client *Client) CreateLoadBalancerHTTPSListener(args *CreateLoadBalancerHTTPSListenerArgs) (err error) {
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("CreateLoadBalancerHTTPSListener", args, response)
return err
}
type HealthCheckType string
const (
TCPHealthCheckType = HealthCheckType("tcp")
HTTPHealthCheckType = HealthCheckType("http")
)
type TCPListenerType struct {
LoadBalancerId string
ListenerPort int
BackendServerPort int
Bandwidth int
Scheduler SchedulerType
PersistenceTimeout *int
HealthCheck FlagType
HealthCheckType HealthCheckType
HealthCheckDomain string
HealthCheckURI string
HealthCheckConnectPort int
HealthyThreshold int
UnhealthyThreshold int
HealthCheckConnectTimeout int
HealthCheckInterval int
HealthCheckHttpCode HealthCheckHttpCodeType
VServerGroup FlagType
VServerGroupId string
Description string
AclId string
AclStatus string
AclType string
}
type CreateLoadBalancerTCPListenerArgs TCPListenerType
// CreateLoadBalancerTCPListener create TCP listener on loadbalancer
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&CreateLoadBalancerTCPListener
func (client *Client) CreateLoadBalancerTCPListener(args *CreateLoadBalancerTCPListenerArgs) (err error) {
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("CreateLoadBalancerTCPListener", args, response)
return err
}
type UDPListenerType struct {
LoadBalancerId string
ListenerPort int
BackendServerPort int
Bandwidth int
Scheduler SchedulerType
PersistenceTimeout *int
HealthCheck FlagType
HealthCheckConnectPort int
HealthyThreshold int
UnhealthyThreshold int
HealthCheckConnectTimeout int
HealthCheckInterval int
VServerGroup FlagType
VServerGroupId string
Description string
AclId string
AclStatus string
AclType string
}
type CreateLoadBalancerUDPListenerArgs UDPListenerType
// CreateLoadBalancerUDPListener create UDP listener on loadbalancer
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&CreateLoadBalancerUDPListener
func (client *Client) CreateLoadBalancerUDPListener(args *CreateLoadBalancerUDPListenerArgs) (err error) {
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("CreateLoadBalancerUDPListener", args, response)
return err
}
type CommonLoadBalancerListenerArgs struct {
LoadBalancerId string
ListenerPort int
}
// DeleteLoadBalancerListener Delete listener
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&DeleteLoadBalancerListener
func (client *Client) DeleteLoadBalancerListener(loadBalancerId string, port int) (err error) {
args := &CommonLoadBalancerListenerArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
}
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("DeleteLoadBalancerListener", args, response)
return err
}
// StartLoadBalancerListener Start listener
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&StartLoadBalancerListener
func (client *Client) StartLoadBalancerListener(loadBalancerId string, port int) (err error) {
args := &CommonLoadBalancerListenerArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
}
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("StartLoadBalancerListener", args, response)
return err
}
// StopLoadBalancerListener Stop listener
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&StopLoadBalancerListener
func (client *Client) StopLoadBalancerListener(loadBalancerId string, port int) (err error) {
args := &CommonLoadBalancerListenerArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
}
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("StopLoadBalancerListener", args, response)
return err
}
type AccessControlStatus string
const (
OpenWhileList = AccessControlStatus("open_white_list")
Close = AccessControlStatus("close")
)
type SetListenerAccessControlStatusArgs struct {
LoadBalancerId string
ListenerPort int
AccessControlStatus AccessControlStatus
}
// SetListenerAccessControlStatus Set listener access control status
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&SetListenerAccessControlStatus
func (client *Client) SetListenerAccessControlStatus(loadBalancerId string, port int, status AccessControlStatus) (err error) {
args := &SetListenerAccessControlStatusArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
AccessControlStatus: status,
}
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("SetListenerAccessControlStatus", args, response)
return err
}
type CommonListenerWhiteListItemArgs struct {
LoadBalancerId string
ListenerPort int
SourceItems string
}
// AddListenerWhiteListItem Add listener white-list item
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&AddListenerWhiteListItem
func (client *Client) AddListenerWhiteListItem(loadBalancerId string, port int, sourceItems string) (err error) {
args := &CommonListenerWhiteListItemArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
SourceItems: sourceItems,
}
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("AddListenerWhiteListItem", args, response)
return err
}
// RemoveListenerWhiteListItem Remove listener white-list item
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&RemoveListenerWhiteListItem
func (client *Client) RemoveListenerWhiteListItem(loadBalancerId string, port int, sourceItems string) (err error) {
args := &CommonListenerWhiteListItemArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
SourceItems: sourceItems,
}
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("RemoveListenerWhiteListItem", args, response)
return err
}
type SetLoadBalancerHTTPListenerAttributeArgs CreateLoadBalancerHTTPListenerArgs
// SetLoadBalancerHTTPListenerAttribute Set HTTP listener attribute
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&SetLoadBalancerHTTPListenerAttribute
func (client *Client) SetLoadBalancerHTTPListenerAttribute(args *SetLoadBalancerHTTPListenerAttributeArgs) (err error) {
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("SetLoadBalancerHTTPListenerAttribute", args, response)
return err
}
type SetLoadBalancerHTTPSListenerAttributeArgs CreateLoadBalancerHTTPSListenerArgs
// SetLoadBalancerHTTPSListenerAttribute Set HTTPS listener attribute
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&SetLoadBalancerHTTPSListenerAttribute
func (client *Client) SetLoadBalancerHTTPSListenerAttribute(args *SetLoadBalancerHTTPSListenerAttributeArgs) (err error) {
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("SetLoadBalancerHTTPSListenerAttribute", args, response)
return err
}
type SetLoadBalancerTCPListenerAttributeArgs CreateLoadBalancerTCPListenerArgs
// SetLoadBalancerTCPListenerAttribute Set TCP listener attribute
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&SetLoadBalancerTCPListenerAttribute
func (client *Client) SetLoadBalancerTCPListenerAttribute(args *SetLoadBalancerTCPListenerAttributeArgs) (err error) {
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("SetLoadBalancerTCPListenerAttribute", args, response)
return err
}
type SetLoadBalancerUDPListenerAttributeArgs CreateLoadBalancerUDPListenerArgs
// SetLoadBalancerUDPListenerAttribute Set UDP listener attribute
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&SetLoadBalancerUDPListenerAttribute
func (client *Client) SetLoadBalancerUDPListenerAttribute(args *SetLoadBalancerUDPListenerAttributeArgs) (err error) {
response := &CommonLoadBalancerListenerResponse{}
err = client.Invoke("SetLoadBalancerUDPListenerAttribute", args, response)
return err
}
type DescribeLoadBalancerListenerAttributeResponse struct {
common.Response
Status ListenerStatus
}
type DescribeLoadBalancerHTTPListenerAttributeResponse struct {
DescribeLoadBalancerListenerAttributeResponse
HTTPListenerType
}
// DescribeLoadBalancerHTTPListenerAttribute Describe HTTP listener attribute
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&DescribeLoadBalancerHTTPListenerAttribute
func (client *Client) DescribeLoadBalancerHTTPListenerAttribute(loadBalancerId string, port int) (response *DescribeLoadBalancerHTTPListenerAttributeResponse, err error) {
args := &CommonLoadBalancerListenerArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
}
response = &DescribeLoadBalancerHTTPListenerAttributeResponse{}
err = client.Invoke("DescribeLoadBalancerHTTPListenerAttribute", args, response)
if err != nil {
return nil, err
}
return response, err
}
type DescribeLoadBalancerHTTPSListenerAttributeResponse struct {
DescribeLoadBalancerListenerAttributeResponse
HTTPSListenerType
}
// DescribeLoadBalancerHTTPSListenerAttribute Describe HTTPS listener attribute
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&DescribeLoadBalancerHTTPSListenerAttribute
func (client *Client) DescribeLoadBalancerHTTPSListenerAttribute(loadBalancerId string, port int) (response *DescribeLoadBalancerHTTPSListenerAttributeResponse, err error) {
args := &CommonLoadBalancerListenerArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
}
response = &DescribeLoadBalancerHTTPSListenerAttributeResponse{}
err = client.Invoke("DescribeLoadBalancerHTTPSListenerAttribute", args, response)
if err != nil {
return nil, err
}
return response, err
}
type DescribeLoadBalancerTCPListenerAttributeResponse struct {
DescribeLoadBalancerListenerAttributeResponse
TCPListenerType
}
// DescribeLoadBalancerTCPListenerAttribute Describe TCP listener attribute
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&DescribeLoadBalancerTCPListenerAttribute
func (client *Client) DescribeLoadBalancerTCPListenerAttribute(loadBalancerId string, port int) (response *DescribeLoadBalancerTCPListenerAttributeResponse, err error) {
args := &CommonLoadBalancerListenerArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
}
response = &DescribeLoadBalancerTCPListenerAttributeResponse{}
err = client.Invoke("DescribeLoadBalancerTCPListenerAttribute", args, response)
if err != nil {
return nil, err
}
return response, err
}
type DescribeLoadBalancerUDPListenerAttributeResponse struct {
DescribeLoadBalancerListenerAttributeResponse
UDPListenerType
}
// DescribeLoadBalancerUDPListenerAttribute Describe UDP listener attribute
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&DescribeLoadBalancerUDPListenerAttribute
func (client *Client) DescribeLoadBalancerUDPListenerAttribute(loadBalancerId string, port int) (response *DescribeLoadBalancerUDPListenerAttributeResponse, err error) {
args := &CommonLoadBalancerListenerArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
}
response = &DescribeLoadBalancerUDPListenerAttributeResponse{}
err = client.Invoke("DescribeLoadBalancerUDPListenerAttribute", args, response)
if err != nil {
return nil, err
}
return response, err
}
type ListenerType string
const (
UDP = ListenerType("UDP")
TCP = ListenerType("TCP")
HTTP = ListenerType("HTTP")
HTTPS = ListenerType("HTTPS")
)
const DefaultWaitForInterval = 5 //5 seconds
const DefaultTimeout = 60 //60 seconds
// WaitForListener waits for listener to given status
func (client *Client) WaitForListener(loadBalancerId string, port int, listenerType ListenerType) (status ListenerStatus, err error) {
timeout := DefaultTimeout
args := &CommonLoadBalancerListenerArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
}
method := fmt.Sprintf("DescribeLoadBalancer%sListenerAttribute", listenerType)
response := &DescribeLoadBalancerListenerAttributeResponse{}
for {
timeout = timeout - DefaultWaitForInterval
if timeout <= 0 {
return response.Status, common.GetClientErrorFromString("Timeout")
}
time.Sleep(DefaultWaitForInterval * time.Second)
//Sleep first to ensure the previous request is sent
err = client.Invoke(method, args, response)
if err != nil {
return "", err
}
if response.Status == Running || response.Status == Stopped {
break
}
}
return response.Status, nil
}
// WaitForListener waits for listener to given status
func (client *Client) WaitForListenerAsyn(loadBalancerId string, port int, listenerType ListenerType, status ListenerStatus, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
args := &CommonLoadBalancerListenerArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
}
method := fmt.Sprintf("DescribeLoadBalancer%sListenerAttribute", listenerType)
response := &DescribeLoadBalancerListenerAttributeResponse{}
for {
err := client.Invoke(method, args, response)
e, _ := err.(*common.Error)
if e != nil {
if e.StatusCode == 404 || e.Code == "InvalidLoadBalancerId.NotFound" {
continue
}
return err
} else if response != nil && response.Status == status {
//TODO
break
}
timeout = timeout - DefaultWaitForInterval
if timeout <= 0 {
return common.GetClientErrorFromString("Timeout")
}
time.Sleep(DefaultWaitForInterval * time.Second)
}
return nil
}
type DescribeListenerAccessControlAttributeResponse struct {
common.Response
AccessControlStatus AccessControlStatus
SourceItems string
}
// DescribeListenerAccessControlAttribute Describe listener access control attribute
//
// You can read doc at https://docs.aliyun.com/#/pub/slb/api-reference/api-related-listener&DescribeListenerAccessControlAttribute
func (client *Client) DescribeListenerAccessControlAttribute(loadBalancerId string, port int) (response *DescribeListenerAccessControlAttributeResponse, err error) {
args := &CommonLoadBalancerListenerArgs{
LoadBalancerId: loadBalancerId,
ListenerPort: port,
}
response = &DescribeListenerAccessControlAttributeResponse{}
err = client.Invoke("DescribeListenerAccessControlAttribute", args, response)
if err != nil {
return nil, err
}
return response, err
}