Skip to content

Commit a3b494b

Browse files
authored
feat(clb): [119213292] tencentcloud_clb_listener_rule support domains (#2789)
* add * add * add
1 parent 77e1581 commit a3b494b

File tree

5 files changed

+136
-28
lines changed

5 files changed

+136
-28
lines changed

.changelog/2789.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_clb_listener_rule: support `domains`
3+
```

tencentcloud/services/clb/resource_tc_clb_listener_rule.go

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,22 @@ func ResourceTencentCloudClbListenerRule() *schema.Resource {
3939
Description: "ID of CLB instance.",
4040
},
4141
"domain": {
42-
Type: schema.TypeString,
43-
Required: true,
44-
Description: "Domain name of the listener rule.",
42+
Type: schema.TypeString,
43+
Optional: true,
44+
Computed: true,
45+
ConflictsWith: []string{"domains"},
46+
ExactlyOneOf: []string{"domain", "domains"},
47+
Description: "Domain name of the listener rule. Single domain rules are passed to `domain`, and multi domain rules are passed to `domains`.",
48+
},
49+
"domains": {
50+
Type: schema.TypeList,
51+
Optional: true,
52+
Computed: true,
53+
ForceNew: true,
54+
ConflictsWith: []string{"domain"},
55+
ExactlyOneOf: []string{"domain", "domains"},
56+
Elem: &schema.Schema{Type: schema.TypeString},
57+
Description: "Domain name list of the listener rule. Single domain rules are passed to `domain`, and multi domain rules are passed to `domains`.",
4558
},
4659
"url": {
4760
Type: schema.TypeString,
@@ -224,10 +237,28 @@ func resourceTencentCloudClbListenerRuleCreate(d *schema.ResourceData, meta inte
224237
request.ListenerId = helper.String(listenerId)
225238

226239
//rule set
227-
var rule clb.RuleInput
240+
var (
241+
rule clb.RuleInput
242+
domain string
243+
domains []*string
244+
)
245+
246+
if v, ok := d.GetOk("domain"); ok {
247+
rule.Domain = helper.String(v.(string))
248+
domain = v.(string)
249+
}
250+
251+
if v, ok := d.GetOk("domains"); ok {
252+
tmpDomains := v.([]interface{})
253+
domains = make([]*string, 0, len(tmpDomains))
254+
for _, value := range tmpDomains {
255+
tmpDomain := value.(string)
256+
domains = append(domains, &tmpDomain)
257+
}
258+
259+
rule.Domains = domains
260+
}
228261

229-
domain := d.Get("domain").(string)
230-
rule.Domain = helper.String(domain)
231262
url := d.Get("url").(string)
232263
rule.Url = helper.String(url)
233264
rule.TargetType = helper.String(d.Get("target_type").(string))
@@ -305,7 +336,7 @@ func resourceTencentCloudClbListenerRuleCreate(d *schema.ResourceData, meta inte
305336

306337
locationId := ""
307338
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
308-
ruleInstance, ruleErr := clbService.DescribeRuleByPara(ctx, clbId, listenerId, domain, url)
339+
ruleInstance, ruleErr := clbService.DescribeRuleByPara(ctx, clbId, listenerId, domain, url, domains)
309340
if ruleErr != nil {
310341
return tccommon.RetryError(errors.WithStack(ruleErr))
311342
}
@@ -327,7 +358,12 @@ func resourceTencentCloudClbListenerRuleCreate(d *schema.ResourceData, meta inte
327358
domainRequest.Http2 = &http2Switch
328359
domainRequest.LoadBalancerId = &clbId
329360
domainRequest.ListenerId = &listenerId
330-
domainRequest.Domain = &domain
361+
if domain != "" {
362+
domainRequest.Domain = &domain
363+
} else {
364+
domainRequest.NewDomains = domains
365+
}
366+
331367
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
332368
response, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().ModifyDomainAttributes(domainRequest)
333369
if e != nil {
@@ -408,7 +444,14 @@ func resourceTencentCloudClbListenerRuleRead(d *schema.ResourceData, meta interf
408444
instance := instances[0]
409445
_ = d.Set("clb_id", clbId)
410446
_ = d.Set("listener_id", listenerId)
411-
_ = d.Set("domain", instance.Domain)
447+
if instance.Domain != nil {
448+
_ = d.Set("domain", instance.Domain)
449+
}
450+
451+
if instance.Domains != nil {
452+
_ = d.Set("domains", helper.StringsInterfaces(instance.Domains))
453+
}
454+
412455
_ = d.Set("rule_id", instance.LocationId)
413456
_ = d.Set("url", instance.Url)
414457
_ = d.Set("scheduler", instance.Scheduler)

tencentcloud/services/clb/resource_tc_clb_listener_rule.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ Provides a resource to create a CLB listener rule.
44

55
Example Usage
66

7+
Create a single domain listener rule
8+
79
```hcl
8-
resource "tencentcloud_clb_listener_rule" "foo" {
10+
resource "tencentcloud_clb_listener_rule" "example" {
911
listener_id = "lbl-hh141sn9"
1012
clb_id = "lb-k2zjp9lv"
11-
domain = "foo.net"
12-
url = "/bar"
13+
domain = "example.com"
14+
url = "/"
1315
health_check_switch = true
1416
health_check_interval_time = 5
1517
health_check_health_num = 3
1618
health_check_unhealth_num = 3
1719
health_check_http_code = 2
18-
health_check_http_path = "Default Path"
19-
health_check_http_domain = "Default Domain"
20+
health_check_http_path = "/"
21+
health_check_http_domain = "check.com"
2022
health_check_http_method = "GET"
2123
certificate_ssl_mode = "MUTUAL"
2224
certificate_id = "VjANRdz8"
@@ -25,10 +27,31 @@ resource "tencentcloud_clb_listener_rule" "foo" {
2527
scheduler = "WRR"
2628
}
2729
```
30+
31+
Create a listener rule for domain lists
32+
33+
```hcl
34+
resource "tencentcloud_clb_listener_rule" "example" {
35+
listener_id = "lbl-hh141sn9"
36+
clb_id = "lb-k2zjp9lv"
37+
domains = ["example1.com", "example2.com"]
38+
url = "/"
39+
health_check_switch = true
40+
health_check_interval_time = 5
41+
health_check_health_num = 3
42+
health_check_unhealth_num = 3
43+
health_check_http_code = 2
44+
health_check_http_path = "/"
45+
health_check_http_domain = "check.com"
46+
health_check_http_method = "GET"
47+
scheduler = "WRR"
48+
}
49+
```
50+
2851
Import
2952

3053
CLB listener rule can be imported using the id (version >= 1.47.0), e.g.
3154

3255
```
33-
$ terraform import tencentcloud_clb_listener_rule.foo lb-7a0t6zqb#lbl-hh141sn9#loc-agg236ys
34-
```
56+
$ terraform import tencentcloud_clb_listener_rule.example lb-k2zjp9lv#lbl-hh141sn9#loc-agg236ys
57+
```

tencentcloud/services/clb/service_tencentcloud_clb.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func (me *ClbService) DescribeRulesByFilter(ctx context.Context, params map[stri
341341
return
342342
}
343343

344-
func (me *ClbService) DescribeRuleByPara(ctx context.Context, clbId string, listenerId string, domain string, url string) (clbRule *clb.RuleOutput, errRet error) {
344+
func (me *ClbService) DescribeRuleByPara(ctx context.Context, clbId string, listenerId string, domain string, url string, domains []*string) (clbRule *clb.RuleOutput, errRet error) {
345345
logId := tccommon.GetLogId(ctx)
346346
request := clb.NewDescribeListenersRequest()
347347
request.ListenerIds = []*string{&listenerId}
@@ -369,10 +369,26 @@ func (me *ClbService) DescribeRuleByPara(ctx context.Context, clbId string, list
369369
var ruleOutput clb.RuleOutput
370370
findFlag := false
371371
for _, rule := range clbListener.Rules {
372-
if *rule.Domain == domain && *rule.Url == url {
373-
ruleOutput = *rule
374-
findFlag = true
375-
break
372+
if *rule.Url == url {
373+
if domain != "" && *rule.Domain == domain {
374+
ruleOutput = *rule
375+
findFlag = true
376+
break
377+
} else if len(domains) > 0 {
378+
tmpRef := true
379+
for i := range domains {
380+
if *domains[i] != *rule.Domains[i] {
381+
tmpRef = false
382+
break
383+
}
384+
}
385+
386+
if tmpRef {
387+
ruleOutput = *rule
388+
findFlag = true
389+
break
390+
}
391+
}
376392
}
377393
}
378394
if !findFlag {

website/docs/r/clb_listener_rule.html.markdown

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,21 @@ Provides a resource to create a CLB listener rule.
1515

1616
## Example Usage
1717

18+
### Create a single domain listener rule
19+
1820
```hcl
19-
resource "tencentcloud_clb_listener_rule" "foo" {
21+
resource "tencentcloud_clb_listener_rule" "example" {
2022
listener_id = "lbl-hh141sn9"
2123
clb_id = "lb-k2zjp9lv"
22-
domain = "foo.net"
23-
url = "/bar"
24+
domain = "example.com"
25+
url = "/"
2426
health_check_switch = true
2527
health_check_interval_time = 5
2628
health_check_health_num = 3
2729
health_check_unhealth_num = 3
2830
health_check_http_code = 2
29-
health_check_http_path = "Default Path"
30-
health_check_http_domain = "Default Domain"
31+
health_check_http_path = "/"
32+
health_check_http_domain = "check.com"
3133
health_check_http_method = "GET"
3234
certificate_ssl_mode = "MUTUAL"
3335
certificate_id = "VjANRdz8"
@@ -37,17 +39,38 @@ resource "tencentcloud_clb_listener_rule" "foo" {
3739
}
3840
```
3941

42+
### Create a listener rule for domain lists
43+
44+
```hcl
45+
resource "tencentcloud_clb_listener_rule" "example" {
46+
listener_id = "lbl-hh141sn9"
47+
clb_id = "lb-k2zjp9lv"
48+
domains = ["example1.com", "example2.com"]
49+
url = "/"
50+
health_check_switch = true
51+
health_check_interval_time = 5
52+
health_check_health_num = 3
53+
health_check_unhealth_num = 3
54+
health_check_http_code = 2
55+
health_check_http_path = "/"
56+
health_check_http_domain = "check.com"
57+
health_check_http_method = "GET"
58+
scheduler = "WRR"
59+
}
60+
```
61+
4062
## Argument Reference
4163

4264
The following arguments are supported:
4365

4466
* `clb_id` - (Required, String) ID of CLB instance.
45-
* `domain` - (Required, String) Domain name of the listener rule.
4667
* `listener_id` - (Required, String, ForceNew) ID of CLB listener.
4768
* `url` - (Required, String) Url of the listener rule.
4869
* `certificate_ca_id` - (Optional, String) ID of the client certificate. NOTES: Only supports listeners of HTTPS protocol.
4970
* `certificate_id` - (Optional, String) ID of the server certificate. NOTES: Only supports listeners of HTTPS protocol.
5071
* `certificate_ssl_mode` - (Optional, String, ForceNew) Type of certificate. Valid values: `UNIDIRECTIONAL`, `MUTUAL`. NOTES: Only supports listeners of HTTPS protocol.
72+
* `domain` - (Optional, String) Domain name of the listener rule. Single domain rules are passed to `domain`, and multi domain rules are passed to `domains`.
73+
* `domains` - (Optional, List: [`String`], ForceNew) Domain name list of the listener rule. Single domain rules are passed to `domain`, and multi domain rules are passed to `domains`.
5174
* `forward_type` - (Optional, String) Forwarding protocol between the CLB instance and real server. Valid values: `HTTP`, `HTTPS`, `TRPC`. The default is `HTTP`.
5275
* `health_check_health_num` - (Optional, Int) Health threshold of health check, and the default is `3`. If a success result is returned for the health check 3 consecutive times, indicates that the forwarding is normal. 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`.
5376
* `health_check_http_code` - (Optional, Int) HTTP Status Code. The default is 31. Valid value ranges: [1~31]. `1 means the return value '1xx' is health. `2` means the return value '2xx' is health. `4` means the return value '3xx' is health. `8` means the return value '4xx' is health. 16 means the return value '5xx' is health. If you want multiple return codes to indicate health, need to add the corresponding values. NOTES: The 'HTTP' health check of the 'TCP' listener only supports specifying one health check status code. NOTES: Only supports listeners of 'HTTP' and 'HTTPS' protocol.
@@ -78,6 +101,6 @@ In addition to all arguments above, the following attributes are exported:
78101
CLB listener rule can be imported using the id (version >= 1.47.0), e.g.
79102

80103
```
81-
$ terraform import tencentcloud_clb_listener_rule.foo lb-7a0t6zqb#lbl-hh141sn9#loc-agg236ys
104+
$ terraform import tencentcloud_clb_listener_rule.example lb-k2zjp9lv#lbl-hh141sn9#loc-agg236ys
82105
```
83106

0 commit comments

Comments
 (0)