Skip to content

Commit fe1f99b

Browse files
author
mikatong
committed
udp listener support enable health_check
1 parent cb77acf commit fe1f99b

File tree

4 files changed

+346
-47
lines changed

4 files changed

+346
-47
lines changed

tencentcloud/services/gaap/resource_tc_gaap_layer4_listener.go

Lines changed: 170 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ func ResourceTencentCloudGaapLayer4Listener() *schema.Resource {
6767
Type: schema.TypeBool,
6868
Optional: true,
6969
Default: false,
70-
Description: "Indicates whether health check is enable, default value is `false`. NOTES: Only supports listeners of `TCP` protocol.",
70+
Description: "Indicates whether health check is enable, default value is `false`.",
7171
},
7272
"interval": {
7373
Type: schema.TypeInt,
7474
Optional: true,
7575
Default: 5,
7676
ValidateFunc: tccommon.ValidateIntegerInRange(5, 300),
77-
Description: "Interval of the health check, default value is 5s. NOTES: Only supports listeners of `TCP` protocol.",
77+
Description: "Interval of the health check, default value is 5s.",
7878
},
7979
"connect_timeout": {
8080
Type: schema.TypeInt,
@@ -83,6 +83,52 @@ func ResourceTencentCloudGaapLayer4Listener() *schema.Resource {
8383
ValidateFunc: tccommon.ValidateIntegerInRange(2, 60),
8484
Description: "Timeout of the health check response, should less than interval, default value is 2s. NOTES: Only supports listeners of `TCP` protocol and require less than `interval`.",
8585
},
86+
"healthy_threshold": {
87+
Type: schema.TypeInt,
88+
Optional: true,
89+
Default: 1,
90+
ValidateFunc: tccommon.ValidateIntegerInRange(1, 10),
91+
Description: "Health threshold, which indicates how many consecutive inspections are successful, the source station is determined to be healthy. Range from 1 to 10. Default value is 1.",
92+
},
93+
"unhealthy_threshold": {
94+
Type: schema.TypeInt,
95+
Optional: true,
96+
Default: 1,
97+
ValidateFunc: tccommon.ValidateIntegerInRange(1, 10),
98+
Description: "Unhealthy threshold, which indicates how many consecutive check failures the source station is considered unhealthy. Range from 1 to 10. Default value is 1.",
99+
},
100+
"check_type": {
101+
Type: schema.TypeString,
102+
Optional: true,
103+
Computed: true,
104+
ValidateFunc: tccommon.ValidateAllowedStringValue([]string{"PORT", "PING"}),
105+
Description: "UDP origin server health type. PORT means check port, and PING means PING.",
106+
},
107+
"check_port": {
108+
Type: schema.TypeInt,
109+
Optional: true,
110+
Computed: true,
111+
Description: "UDP origin station health check probe port.",
112+
},
113+
"context_type": {
114+
Type: schema.TypeString,
115+
Optional: true,
116+
Computed: true,
117+
ValidateFunc: tccommon.ValidateAllowedStringValue([]string{"TEXT"}),
118+
Description: "UDP source station health check port probe message type: TEXT represents text. Only used when the health check type is PORT.",
119+
},
120+
"send_context": {
121+
Type: schema.TypeString,
122+
Optional: true,
123+
Computed: true,
124+
Description: "UDP source server health check port detection sends messages. Only used when health check type is PORT.",
125+
},
126+
"recv_context": {
127+
Type: schema.TypeString,
128+
Optional: true,
129+
Computed: true,
130+
Description: "UDP source server health check port detects received messages. Only used when the health check type is PORT.",
131+
},
86132
"client_ip_method": {
87133
Type: schema.TypeInt,
88134
Optional: true,
@@ -164,12 +210,10 @@ func resourceTencentCloudGaapLayer4ListenerCreate(d *schema.ResourceData, m inte
164210
proxyId := d.Get("proxy_id").(string)
165211
healthCheck := d.Get("health_check").(bool)
166212

167-
if protocol == "UDP" && healthCheck {
168-
return errors.New("UDP listener can't use health check")
169-
}
170-
171213
interval := d.Get("interval").(int)
172214
connectTimeout := d.Get("connect_timeout").(int)
215+
healthyThreshold := d.Get("healthy_threshold").(int)
216+
unhealthyThreshold := d.Get("unhealthy_threshold").(int)
173217

174218
// only check for TCP listener
175219
if protocol == "TCP" && connectTimeout >= interval {
@@ -206,13 +250,33 @@ func resourceTencentCloudGaapLayer4ListenerCreate(d *schema.ResourceData, m inte
206250

207251
switch protocol {
208252
case "TCP":
209-
id, err = service.CreateTCPListener(ctx, name, scheduler, realserverType, proxyId, port, interval, connectTimeout, clientIPMethod, healthCheck)
253+
id, err = service.CreateTCPListener(ctx, name, scheduler, realserverType, proxyId, port, interval, connectTimeout, clientIPMethod, healthyThreshold, unhealthyThreshold, healthCheck)
210254
if err != nil {
211255
return err
212256
}
213257

214258
case "UDP":
215-
id, err = service.CreateUDPListener(ctx, name, scheduler, realserverType, proxyId, port)
259+
optionalParams := make(map[string]interface{})
260+
if v, ok := d.GetOk("check_type"); ok {
261+
optionalParams["check_type"] = v.(string)
262+
} else {
263+
if healthCheck {
264+
return errors.New("Must set check_type when enable health_check.")
265+
}
266+
}
267+
if v, ok := d.GetOk("check_port"); ok {
268+
optionalParams["check_port"] = v.(int)
269+
}
270+
if v, ok := d.GetOk("context_type"); ok {
271+
optionalParams["context_type"] = v.(string)
272+
}
273+
if v, ok := d.GetOk("send_context"); ok {
274+
optionalParams["send_context"] = v.(string)
275+
}
276+
if v, ok := d.GetOk("recv_context"); ok {
277+
optionalParams["recv_context"] = v.(string)
278+
}
279+
id, err = service.CreateUDPListener(ctx, name, scheduler, realserverType, proxyId, port, interval, connectTimeout, healthyThreshold, unhealthyThreshold, healthCheck, optionalParams)
216280
if err != nil {
217281
return err
218282
}
@@ -239,19 +303,26 @@ func resourceTencentCloudGaapLayer4ListenerRead(d *schema.ResourceData, m interf
239303
id := d.Id()
240304

241305
var (
242-
protocol string
243-
name *string
244-
port *uint64
245-
scheduler *string
246-
realServerType *string
247-
healthCheck *bool
248-
interval *uint64
249-
connectTimeout *uint64
250-
status *uint64
251-
createTime string
252-
realservers []map[string]interface{}
253-
clientIpMethod *uint64
254-
proxyId *string
306+
protocol string
307+
name *string
308+
port *uint64
309+
scheduler *string
310+
realServerType *string
311+
healthCheck *bool
312+
interval *uint64
313+
connectTimeout *uint64
314+
status *uint64
315+
createTime string
316+
realservers []map[string]interface{}
317+
clientIpMethod *uint64
318+
proxyId *string
319+
healthyThreshold *uint64
320+
unhealthyThreshold *uint64
321+
checkType *string
322+
checkPort *int64
323+
contextType *string
324+
sendContext *string
325+
recvContext *string
255326
)
256327

257328
service := GaapService{client: m.(tccommon.ProviderMeta).GetAPIV3Conn()}
@@ -285,6 +356,8 @@ func resourceTencentCloudGaapLayer4ListenerRead(d *schema.ResourceData, m interf
285356

286357
interval = listener.DelayLoop
287358
connectTimeout = listener.ConnectTimeout
359+
healthyThreshold = listener.HealthyThreshold
360+
unhealthyThreshold = listener.UnhealthyThreshold
288361

289362
if len(listener.RealServerSet) > 0 {
290363
realservers = make([]map[string]interface{}, 0, len(listener.RealServerSet))
@@ -316,11 +389,16 @@ func resourceTencentCloudGaapLayer4ListenerRead(d *schema.ResourceData, m interf
316389
realServerType = listener.RealServerType
317390
proxyId = listener.ProxyId
318391

319-
healthCheck = helper.Bool(false)
320-
connectTimeout = helper.IntUint64(2)
321-
interval = helper.IntUint64(5)
322-
clientIpMethod = helper.IntUint64(0)
323-
392+
healthCheck = helper.Bool(*listener.HealthCheck == 1)
393+
connectTimeout = listener.ConnectTimeout
394+
interval = listener.DelayLoop
395+
healthyThreshold = listener.HealthyThreshold
396+
unhealthyThreshold = listener.UnhealthyThreshold
397+
checkType = listener.CheckType
398+
checkPort = listener.CheckPort
399+
contextType = listener.ContextType
400+
sendContext = listener.SendContext
401+
recvContext = listener.RecvContext
324402
if len(listener.RealServerSet) > 0 {
325403
realservers = make([]map[string]interface{}, 0, len(listener.RealServerSet))
326404
for _, rs := range listener.RealServerSet {
@@ -353,12 +431,22 @@ func resourceTencentCloudGaapLayer4ListenerRead(d *schema.ResourceData, m interf
353431
_ = d.Set("health_check", healthCheck)
354432
_ = d.Set("interval", interval)
355433
_ = d.Set("connect_timeout", connectTimeout)
356-
_ = d.Set("client_ip_method", clientIpMethod)
357434
_ = d.Set("realserver_bind_set", realservers)
358435
_ = d.Set("status", status)
359436
_ = d.Set("create_time", createTime)
360437
_ = d.Set("proxy_id", proxyId)
361-
438+
_ = d.Set("healthy_threshold", healthyThreshold)
439+
_ = d.Set("unhealthy_threshold", unhealthyThreshold)
440+
if protocol == "TCP" {
441+
_ = d.Set("client_ip_method", clientIpMethod)
442+
}
443+
if protocol == "UDP" {
444+
_ = d.Set("check_type", checkType)
445+
_ = d.Set("check_port", checkPort)
446+
_ = d.Set("context_type", contextType)
447+
_ = d.Set("send_context", sendContext)
448+
_ = d.Set("recv_context", recvContext)
449+
}
362450
return nil
363451
}
364452

@@ -374,12 +462,14 @@ func resourceTencentCloudGaapLayer4ListenerUpdate(d *schema.ResourceData, m inte
374462
protocol := d.Get("protocol").(string)
375463
proxyId := d.Get("proxy_id").(string)
376464
var (
377-
name *string
378-
scheduler *string
379-
healthCheck *bool
380-
interval int
381-
connectTimeout int
382-
attrChange []string
465+
name *string
466+
scheduler *string
467+
healthCheck *bool
468+
interval int
469+
connectTimeout int
470+
attrChange []string
471+
healthyThreshold int
472+
unhealthyThreshold int
383473
)
384474

385475
service := GaapService{client: m.(tccommon.ProviderMeta).GetAPIV3Conn()}
@@ -388,21 +478,18 @@ func resourceTencentCloudGaapLayer4ListenerUpdate(d *schema.ResourceData, m inte
388478

389479
if d.HasChange("name") {
390480
attrChange = append(attrChange, "name")
391-
name = helper.String(d.Get("name").(string))
392481
}
482+
name = helper.String(d.Get("name").(string))
393483

394484
if d.HasChange("scheduler") {
395485
attrChange = append(attrChange, "scheduler")
396-
scheduler = helper.String(d.Get("scheduler").(string))
397486
}
487+
scheduler = helper.String(d.Get("scheduler").(string))
398488

399489
if d.HasChange("health_check") {
400490
attrChange = append(attrChange, "health_check")
401-
healthCheck = helper.Bool(d.Get("health_check").(bool))
402-
}
403-
if protocol == "UDP" && healthCheck != nil && *healthCheck {
404-
return errors.New("UDP listener can't enable health check")
405491
}
492+
healthCheck = helper.Bool(d.Get("health_check").(bool))
406493

407494
if d.HasChange("interval") {
408495
attrChange = append(attrChange, "interval")
@@ -414,6 +501,47 @@ func resourceTencentCloudGaapLayer4ListenerUpdate(d *schema.ResourceData, m inte
414501
}
415502
connectTimeout = d.Get("connect_timeout").(int)
416503

504+
if d.HasChange("healthy_threshold") {
505+
attrChange = append(attrChange, "healthy_threshold")
506+
}
507+
healthyThreshold = d.Get("healthy_threshold").(int)
508+
509+
if d.HasChange("unhealthy_threshold") {
510+
attrChange = append(attrChange, "unhealthy_threshold")
511+
}
512+
unhealthyThreshold = d.Get("unhealthy_threshold").(int)
513+
514+
optionalParams := make(map[string]interface{})
515+
if d.HasChange("check_type") {
516+
attrChange = append(attrChange, "check_type")
517+
}
518+
if d.HasChange("check_port") {
519+
attrChange = append(attrChange, "check_port")
520+
}
521+
if d.HasChange("context_type") {
522+
attrChange = append(attrChange, "context_type")
523+
}
524+
if d.HasChange("send_context") {
525+
attrChange = append(attrChange, "send_context")
526+
}
527+
if d.HasChange("recv_context") {
528+
attrChange = append(attrChange, "recv_context")
529+
}
530+
if v, ok := d.GetOk("check_type"); ok {
531+
optionalParams["check_type"] = v.(string)
532+
}
533+
if v, ok := d.GetOk("check_port"); ok {
534+
optionalParams["check_port"] = v.(int)
535+
}
536+
if v, ok := d.GetOk("context_type"); ok {
537+
optionalParams["context_type"] = v.(string)
538+
}
539+
if v, ok := d.GetOk("send_context"); ok {
540+
optionalParams["send_context"] = v.(string)
541+
}
542+
if v, ok := d.GetOk("recv_context"); ok {
543+
optionalParams["recv_context"] = v.(string)
544+
}
417545
// only check for TCP listener
418546
if protocol == "TCP" && connectTimeout >= interval {
419547
return errors.New("connect_timeout must be less than interval")
@@ -422,12 +550,12 @@ func resourceTencentCloudGaapLayer4ListenerUpdate(d *schema.ResourceData, m inte
422550
if len(attrChange) > 0 {
423551
switch protocol {
424552
case "TCP":
425-
if err := service.ModifyTCPListenerAttribute(ctx, proxyId, id, name, scheduler, healthCheck, interval, connectTimeout); err != nil {
553+
if err := service.ModifyTCPListenerAttribute(ctx, proxyId, id, name, scheduler, healthCheck, interval, connectTimeout, healthyThreshold, unhealthyThreshold); err != nil {
426554
return err
427555
}
428556

429557
case "UDP":
430-
if err := service.ModifyUDPListenerAttribute(ctx, proxyId, id, name, scheduler); err != nil {
558+
if err := service.ModifyUDPListenerAttribute(ctx, proxyId, id, name, scheduler, connectTimeout, interval, healthyThreshold, unhealthyThreshold, healthCheck, optionalParams); err != nil {
431559
return err
432560
}
433561
}

0 commit comments

Comments
 (0)