-
Notifications
You must be signed in to change notification settings - Fork 134
/
resource_tc_clb_listener.go
668 lines (619 loc) · 26.4 KB
/
resource_tc_clb_listener.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package clb
import (
"context"
"fmt"
"log"
"strings"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudClbListener() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudClbListenerCreate,
Read: resourceTencentCloudClbListenerRead,
Update: resourceTencentCloudClbListenerUpdate,
Delete: resourceTencentCloudClbListenerDelete,
Importer: &schema.ResourceImporter{
State: helper.ImportWithDefaultValue(map[string]interface{}{
"scheduler": CLB_LISTENER_SCHEDULER_WRR,
}),
},
Schema: map[string]*schema.Schema{
"clb_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: tccommon.ValidateStringLengthInRange(1, 60),
Description: "ID of the CLB.",
},
"listener_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: tccommon.ValidateStringLengthInRange(1, 60),
Description: "Name of the CLB listener, and available values can only be Chinese characters, English letters, numbers, underscore and hyphen '-'.",
},
"port": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: tccommon.ValidateIntegerInRange(1, 65535),
Description: "Port of the CLB listener.",
},
"protocol": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: tccommon.ValidateAllowedStringValue(CLB_LISTENER_PROTOCOL),
Description: "Type of protocol within the listener. Valid values: `TCP`, `UDP`, `HTTP`, `HTTPS`, `TCP_SSL` and `QUIC`.",
},
"health_check_switch": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "Indicates whether health check is enabled.",
},
"health_check_time_out": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateIntegerInRange(2, 60),
Description: "Response timeout of health check. Valid value ranges: [2~60] sec. Default is 2 sec. Response timeout needs to be less than check interval. NOTES: Only supports listeners of `TCP`,`UDP`,`TCP_SSL` protocol.",
},
"health_check_interval_time": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateIntegerInRange(2, 300),
Description: "Interval time of health check. Valid value ranges: [2~300] sec. and the default is 5 sec. NOTES: TCP/UDP/TCP_SSL listener allows direct configuration, HTTP/HTTPS listener needs to be configured in `tencentcloud_clb_listener_rule`.",
},
"health_check_health_num": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateIntegerInRange(2, 10),
Description: "Health threshold of health check, and the default is `3`. If a success result is returned for the health check for 3 consecutive times, the backend CVM is identified as healthy. The value range is 2-10. NOTES: TCP/UDP/TCP_SSL listener allows direct configuration, HTTP/HTTPS listener needs to be configured in tencentcloud_clb_listener_rule.",
},
"health_check_unhealth_num": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateIntegerInRange(2, 10),
Description: "Unhealthy threshold of health check, and the default is `3`. " +
"If a success result is returned for the health check 3 consecutive times, " +
"the CVM is identified as unhealthy. The value range is [2-10]. " +
"NOTES: TCP/UDP/TCP_SSL listener allows direct configuration, " +
"HTTP/HTTPS listener needs to be configured in `tencentcloud_clb_listener_rule`.",
},
"health_check_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateAllowedStringValue(HEALTH_CHECK_TYPE),
Description: "Protocol used for health check. Valid values: `CUSTOM`, `TCP`, `HTTP`,`HTTPS`, `PING`, `GRPC`.",
},
"health_check_port": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: tccommon.ValidateIntegerInRange(1, 65535),
Description: "The health check port is the port of the backend service by default. " +
"Unless you want to specify a specific port, it is recommended to leave it blank. " +
"Only applicable to TCP/UDP listener.",
},
"health_check_http_version": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: tccommon.ValidateAllowedStringValue(HTTP_VERSION),
Description: "The HTTP version of the backend service. When the value of `health_check_type` of " +
"the health check protocol is `HTTP`, this field is required. " +
"Valid values: `HTTP/1.0`, `HTTP/1.1`.",
},
"health_check_http_code": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: tccommon.ValidateIntegerInRange(1, 31),
Description: "HTTP health check code of TCP listener, Valid value ranges: [1~31]. When the value of `health_check_type` of " +
"the health check protocol is `HTTP`, this field is required. Valid values: `1`, `2`, `4`, `8`, `16`. " +
"`1` means http_1xx, `2` means http_2xx, `4` means http_3xx, `8` means http_4xx, `16` means http_5xx." +
"If you want multiple return codes to indicate health, need to add the corresponding values.",
},
"health_check_http_path": {
Type: schema.TypeString,
Optional: true,
Description: "HTTP health check path of TCP listener.",
},
"health_check_http_domain": {
Type: schema.TypeString,
Optional: true,
Description: "HTTP health check domain of TCP listener.",
},
"health_check_http_method": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateAllowedStringValue(CLB_HTTP_METHOD),
Description: "HTTP health check method of TCP listener. Valid values: `HEAD`, `GET`.",
},
"health_check_context_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateAllowedStringValue(CONTEX_TYPE),
Description: "Health check protocol. When the value of `health_check_type` of the health check protocol is `CUSTOM`, " +
"this field is required, which represents the input format of the health check. " +
"Valid values: `HEX`, `TEXT`.",
},
"health_check_send_context": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: tccommon.ValidateStringLengthInRange(0, 500),
Description: "It represents the content of the request sent by the health check. " +
"When the value of `health_check_type` of the health check protocol is `CUSTOM`, " +
"this field is required. Only visible ASCII characters are allowed and the maximum length is 500. " +
"When `health_check_context_type` value is `HEX`, " +
"the characters of SendContext and RecvContext can only be selected in `0123456789ABCDEF` " +
"and the length must be even digits.",
},
"health_check_recv_context": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: tccommon.ValidateStringLengthInRange(0, 500),
Description: "It represents the result returned by the health check. " +
"When the value of `health_check_type` of the health check protocol is `CUSTOM`, " +
"this field is required. Only ASCII visible characters are allowed and the maximum length is 500. " +
"When `health_check_context_type` value is `HEX`, " +
"the characters of SendContext and RecvContext can only be selected in `0123456789ABCDEF` " +
"and the length must be even digits.",
},
"health_source_ip_type": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{0, 1}),
Description: "Specifies the type of health check source IP. `0` (default): CLB VIP. `1`: 100.64 IP range.",
},
"certificate_ssl_mode": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: tccommon.ValidateAllowedStringValue(CERT_SSL_MODE),
Description: "Type of certificate. Valid values: `UNIDIRECTIONAL`, `MUTUAL`. NOTES: Only supports listeners of `HTTPS` and `TCP_SSL` protocol and must be set when it is available.",
},
"certificate_id": {
Type: schema.TypeString,
Optional: true,
Description: "ID of the server certificate. NOTES: Only supports listeners of `HTTPS` and `TCP_SSL` protocol and must be set when it is available.",
},
"certificate_ca_id": {
Type: schema.TypeString,
Optional: true,
Description: "ID of the client certificate. NOTES: Only supports listeners of `HTTPS` and `TCP_SSL` protocol and must be set when the ssl mode is `MUTUAL`.",
},
"session_expire_time": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: tccommon.ValidateIntegerInRange(30, 3600),
Description: "Time of session persistence within the CLB listener. NOTES: Available when scheduler is specified as `WRR`, and not available when listener protocol is `TCP_SSL`. NOTES: TCP/UDP/TCP_SSL listener allows direct configuration, HTTP/HTTPS listener needs to be configured in `tencentcloud_clb_listener_rule`.",
},
"scheduler": {
Type: schema.TypeString,
Default: CLB_LISTENER_SCHEDULER_WRR,
Optional: true,
ValidateFunc: tccommon.ValidateAllowedStringValue(CLB_LISTENER_SCHEDULER),
Description: "Scheduling method of the CLB listener, and available values are 'WRR' and 'LEAST_CONN'. The default is 'WRR'. NOTES: The listener of `HTTP` and `HTTPS` protocol additionally supports the `IP Hash` method. NOTES: TCP/UDP/TCP_SSL listener allows direct configuration, HTTP/HTTPS listener needs to be configured in `tencentcloud_clb_listener_rule`.",
},
"sni_switch": {
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Description: "Indicates whether SNI is enabled, and only supported with protocol `HTTPS`. If enabled, you can set a certificate for each rule in `tencentcloud_clb_listener_rule`, otherwise all rules have a certificate.",
},
"target_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateAllowedStringValue([]string{CLB_TARGET_TYPE_NODE, CLB_TARGET_TYPE_TARGETGROUP}),
Description: "Backend target type. Valid values: `NODE`, `TARGETGROUP`. `NODE` means to bind ordinary nodes, `TARGETGROUP` means to bind target group. NOTES: TCP/UDP/TCP_SSL listener must configuration, HTTP/HTTPS listener needs to be configured in tencentcloud_clb_listener_rule.",
},
"session_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateAllowedStringValue([]string{CLB_SESSION_TYPE_NORMAL, CLB_SESSION_TYPE_QUIC}),
Description: "Session persistence type. Valid values: `NORMAL`: the default session persistence type; `QUIC_CID`: session persistence by QUIC connection ID. The `QUIC_CID` value can only be configured in UDP listeners. If this field is not specified, the default session persistence type will be used.",
},
"keepalive_enable": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "Whether to enable a persistent connection. This parameter is applicable only to HTTP and HTTPS listeners. Valid values: 0 (disable; default value) and 1 (enable).",
},
"end_port": {
Type: schema.TypeInt,
ForceNew: true,
Computed: true,
Optional: true,
Description: "This parameter is used to specify the end port and is required when creating a port range listener. Only one member can be passed in when inputting the `Ports` parameter, which is used to specify the start port. If you want to try the port range feature, please [submit a ticket](https://console.cloud.tencent.com/workorder/category).",
},
//computed
"listener_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of this CLB listener.",
},
},
}
}
func resourceTencentCloudClbListenerCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_listener.create")()
clbActionMu.Lock()
defer clbActionMu.Unlock()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
clbId := d.Get("clb_id").(string)
listenerName := d.Get("listener_name").(string)
request := clb.NewCreateListenerRequest()
request.LoadBalancerId = helper.String(clbId)
request.ListenerNames = []*string{&listenerName}
port := int64(d.Get("port").(int))
ports := []*int64{&port}
request.Ports = ports
protocol := d.Get("protocol").(string)
request.Protocol = helper.String(protocol)
healthSetFlag, healthCheck, healthErr := checkHealthCheckPara(ctx, d, protocol, HEALTH_APPLY_TYPE_LISTENER)
if healthErr != nil {
return healthErr
}
if healthSetFlag {
request.HealthCheck = healthCheck
}
certificateSetFlag, certificateInput, certErr := checkCertificateInputPara(ctx, d, meta)
if certErr != nil {
return certErr
}
if certificateSetFlag {
request.Certificate = certificateInput
} else {
if protocol == CLB_LISTENER_PROTOCOL_TCPSSL {
return fmt.Errorf("[CHECK][CLB listener][Create] check: certificated need to be set when protocol is TCPSSL")
}
}
scheduler := ""
if v, ok := d.GetOk("scheduler"); ok {
if v == CLB_LISTENER_SCHEDULER_IP_HASH {
return fmt.Errorf("[CHECK][CLB listener][Create] check: Scheduler 'IP_HASH' can only be set with rule of listener HTTP/HTTPS")
}
scheduler = v.(string)
request.Scheduler = helper.String(scheduler)
}
if v, ok := d.GetOk("target_type"); ok {
targetType := v.(string)
request.TargetType = &targetType
} else if protocol == CLB_LISTENER_PROTOCOL_TCP || protocol == CLB_LISTENER_PROTOCOL_UDP ||
protocol == CLB_LISTENER_PROTOCOL_TCPSSL || protocol == CLB_LISTENER_PROTOCOL_QUIC {
targetType := CLB_TARGET_TYPE_NODE
request.TargetType = &targetType
}
if v, ok := d.GetOk("session_expire_time"); ok {
if !(protocol == CLB_LISTENER_PROTOCOL_TCP || protocol == CLB_LISTENER_PROTOCOL_UDP) {
return fmt.Errorf("[CHECK][CLB listener][Create] check: session_expire_time can only be set with protocol TCP/UDP or rule of listener HTTP/HTTPS")
}
if scheduler != CLB_LISTENER_SCHEDULER_WRR && scheduler != "" {
return fmt.Errorf("[CHECK][CLB listener][Create] check: session_expire_time can only be set when scheduler is WRR ")
}
vv := int64(v.(int))
request.SessionExpireTime = &vv
}
if v, ok := d.GetOkExists("sni_switch"); ok {
if protocol != CLB_LISTENER_PROTOCOL_HTTPS {
return fmt.Errorf("[CHECK][CLB listener][Create] check: sni_switch can only be set with protocol HTTPS ")
} else {
vv := v.(bool)
vvv := int64(0)
if vv {
vvv = 1
} else {
if !certificateSetFlag {
return fmt.Errorf("[CHECK][CLB listener][Create] check: certificated need to be set when protocol is HTTPS")
}
}
request.SniSwitch = &vvv
}
}
if v, ok := d.GetOk("session_type"); ok {
request.SessionType = helper.String(v.(string))
}
if v, ok := d.GetOkExists("keepalive_enable"); ok {
request.KeepaliveEnable = helper.IntInt64(v.(int))
}
if v, ok := d.GetOkExists("end_port"); ok {
request.EndPort = helper.IntUint64(v.(int))
}
var response *clb.CreateListenerResponse
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().CreateListener(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
requestId := *result.Response.RequestId
retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient())
if retryErr != nil {
return resource.NonRetryableError(errors.WithStack(retryErr))
}
}
response = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create CLB listener failed, reason:%+v", logId, err)
return err
}
if len(response.Response.ListenerIds) < 1 {
return fmt.Errorf("[CHECK][CLB listener][Create] check: Response error, listener id is null")
}
listenerId := *response.Response.ListenerIds[0]
//this ID style changes since terraform 1.47.0
d.SetId(clbId + tccommon.FILED_SP + listenerId)
return resourceTencentCloudClbListenerRead(d, meta)
}
func resourceTencentCloudClbListenerRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_listener.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
clbService := ClbService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
resourceId := d.Id()
var listenerId = resourceId
items := strings.Split(resourceId, tccommon.FILED_SP)
itemLength := len(items)
clbId := d.Get("clb_id").(string)
if itemLength == 1 && clbId == "" {
return fmt.Errorf("the old style listenerId %s does not support import, please use clbId#listenerId style", resourceId)
} else if itemLength == 2 && clbId == "" {
listenerId = items[1]
clbId = items[0]
} else if itemLength == 2 && clbId != "" {
listenerId = items[1]
} else if itemLength != 1 && itemLength != 2 {
return fmt.Errorf("broken ID %s", resourceId)
}
var instance *clb.Listener
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := clbService.DescribeListenerById(ctx, listenerId, clbId)
if e != nil {
return tccommon.RetryError(e)
}
instance = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read CLB listener failed, reason:%s\n ", logId, err.Error())
return err
}
if instance == nil {
d.SetId("")
return nil
}
_ = d.Set("clb_id", clbId)
_ = d.Set("listener_id", instance.ListenerId)
_ = d.Set("port", instance.Port)
_ = d.Set("protocol", instance.Protocol)
if instance.ListenerName != nil {
_ = d.Set("listener_name", instance.ListenerName)
}
if instance.TargetType != nil {
_ = d.Set("target_type", instance.TargetType)
}
if instance.SessionExpireTime != nil {
_ = d.Set("session_expire_time", instance.SessionExpireTime)
}
if *instance.Protocol == CLB_LISTENER_PROTOCOL_TCP || *instance.Protocol == CLB_LISTENER_PROTOCOL_TCPSSL ||
*instance.Protocol == CLB_LISTENER_PROTOCOL_UDP || *instance.Protocol == CLB_LISTENER_PROTOCOL_QUIC {
_ = d.Set("scheduler", instance.Scheduler)
}
_ = d.Set("sni_switch", *instance.SniSwitch > 0)
//health check
if instance.HealthCheck != nil {
healthCheckSwitch := false
if *instance.HealthCheck.HealthSwitch == int64(1) {
healthCheckSwitch = true
}
_ = d.Set("health_check_switch", healthCheckSwitch)
if instance.HealthCheck.IntervalTime != nil {
_ = d.Set("health_check_interval_time", instance.HealthCheck.IntervalTime)
}
if instance.HealthCheck.TimeOut != nil {
_ = d.Set("health_check_time_out", instance.HealthCheck.TimeOut)
}
if instance.HealthCheck.HealthNum != nil {
_ = d.Set("health_check_health_num", instance.HealthCheck.HealthNum)
}
if instance.HealthCheck.UnHealthNum != nil {
_ = d.Set("health_check_unhealth_num", instance.HealthCheck.UnHealthNum)
}
if instance.HealthCheck.CheckPort != nil {
_ = d.Set("health_check_port", instance.HealthCheck.CheckPort)
}
if instance.HealthCheck.CheckType != nil {
_ = d.Set("health_check_type", instance.HealthCheck.CheckType)
}
if instance.HealthCheck.HttpCode != nil {
_ = d.Set("health_check_http_code", instance.HealthCheck.HttpCode)
}
if instance.HealthCheck.HttpCheckPath != nil {
_ = d.Set("health_check_http_path", instance.HealthCheck.HttpCheckPath)
}
if instance.HealthCheck.HttpCheckDomain != nil {
_ = d.Set("health_check_http_domain", instance.HealthCheck.HttpCheckDomain)
}
if instance.HealthCheck.HttpCheckMethod != nil {
_ = d.Set("health_check_http_method", instance.HealthCheck.HttpCheckMethod)
}
if instance.HealthCheck.HttpVersion != nil {
_ = d.Set("health_check_http_version", instance.HealthCheck.HttpVersion)
}
if instance.HealthCheck.ContextType != nil {
_ = d.Set("health_check_context_type", instance.HealthCheck.ContextType)
}
if instance.HealthCheck.SendContext != nil {
_ = d.Set("health_check_send_context", instance.HealthCheck.SendContext)
}
if instance.HealthCheck.RecvContext != nil {
_ = d.Set("health_check_recv_context", instance.HealthCheck.RecvContext)
}
if instance.HealthCheck.SourceIpType != nil {
_ = d.Set("health_source_ip_type", instance.HealthCheck.SourceIpType)
}
}
if instance.Certificate != nil {
_ = d.Set("certificate_ssl_mode", instance.Certificate.SSLMode)
_ = d.Set("certificate_id", instance.Certificate.CertId)
if instance.Certificate.CertCaId != nil {
_ = d.Set("certificate_ca_id", instance.Certificate.CertCaId)
}
}
if instance.SessionType != nil {
_ = d.Set("session_type", instance.SessionType)
}
if instance.KeepaliveEnable != nil {
_ = d.Set("keepalive_enable", instance.KeepaliveEnable)
}
if instance.EndPort != nil {
_ = d.Set("end_port", instance.EndPort)
}
return nil
}
func resourceTencentCloudClbListenerUpdate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_listener.update")()
clbActionMu.Lock()
defer clbActionMu.Unlock()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
resourceId := d.Id()
items := strings.Split(resourceId, tccommon.FILED_SP)
itemLength := len(items)
listenerId := items[itemLength-1]
clbId := d.Get("clb_id").(string)
changed := false
scheduler := ""
listenerName := ""
sessionExpireTime := 0
protocol := d.Get("protocol").(string)
request := clb.NewModifyListenerRequest()
request.ListenerId = helper.String(listenerId)
request.LoadBalancerId = helper.String(clbId)
if d.HasChange("listener_name") {
listenerName = d.Get("listener_name").(string)
request.ListenerName = helper.String(listenerName)
}
if d.HasChange("scheduler") {
changed = true
scheduler = d.Get("scheduler").(string)
if !(protocol == CLB_LISTENER_PROTOCOL_TCP || protocol == CLB_LISTENER_PROTOCOL_UDP ||
protocol == CLB_LISTENER_PROTOCOL_TCPSSL || protocol == CLB_LISTENER_PROTOCOL_QUIC) {
return fmt.Errorf("[CHECK][CLB listener %s][Update] check: Scheduler can only be set with listener protocol TCP/UDP/TCP_SSL/QUIC or rule of listener HTTP/HTTPS", listenerId)
}
if scheduler == CLB_LISTENER_SCHEDULER_IP_HASH {
return fmt.Errorf("[CHECK][CLB listener %s][Update] check: Scheduler 'IP_HASH' can only be set with rule of listener HTTP/HTTPS", listenerId)
}
request.Scheduler = helper.String(scheduler)
}
if d.HasChange("session_expire_time") {
changed = true
sessionExpireTime = d.Get("session_expire_time").(int)
if !(protocol == CLB_LISTENER_PROTOCOL_TCP || protocol == CLB_LISTENER_PROTOCOL_UDP) {
return fmt.Errorf("[CHECK][CLB listener %s][Update] check: session_expire_time can only be set with protocol TCP/UDP or rule of listener HTTP/HTTPS", listenerId)
}
if scheduler != CLB_LISTENER_SCHEDULER_WRR && scheduler != "" {
return fmt.Errorf("[CHECK][CLB listener %s][Update] check: session_expire_time can only be set when scheduler is WRR", listenerId)
}
sessionExpireTime64 := int64(sessionExpireTime)
request.SessionExpireTime = &sessionExpireTime64
}
healthSetFlag, healthCheck, healthErr := checkHealthCheckPara(ctx, d, protocol, HEALTH_APPLY_TYPE_LISTENER)
if healthErr != nil {
return healthErr
}
if healthSetFlag {
changed = true
request.HealthCheck = healthCheck
}
certificateSetFlag, certificateInput, certErr := checkCertificateInputPara(ctx, d, meta)
if certErr != nil {
return certErr
}
if certificateSetFlag {
changed = true
request.Certificate = certificateInput
}
if d.HasChange("target_type") {
changed = true
targetType := d.Get("target_type").(string)
request.TargetType = helper.String(targetType)
}
if d.HasChange("session_type") {
changed = true
sessionType := d.Get("session_type").(string)
request.SessionType = helper.String(sessionType)
}
if d.HasChange("keepalive_enable") {
changed = true
keepaliveEnable := d.Get("keepalive_enable").(int)
request.KeepaliveEnable = helper.IntInt64(keepaliveEnable)
}
if changed {
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
response, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().ModifyListener(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
requestId := *response.Response.RequestId
retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient())
if retryErr != nil {
return resource.NonRetryableError(errors.WithStack(retryErr))
}
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s update CLB listener failed, reason:%+v", logId, err)
return err
}
}
return resourceTencentCloudClbListenerRead(d, meta)
}
func resourceTencentCloudClbListenerDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_listener.delete")()
clbActionMu.Lock()
defer clbActionMu.Unlock()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
resourceId := d.Id()
items := strings.Split(resourceId, tccommon.FILED_SP)
itemLength := len(items)
listenerId := items[itemLength-1]
clbId := d.Get("clb_id").(string)
clbService := ClbService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
e := clbService.DeleteListenerById(ctx, clbId, listenerId)
if e != nil {
return tccommon.RetryError(e)
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s delete CLB listener failed, reason:%+v", logId, err)
return err
}
return nil
}