Skip to content

feat(clb): [119213292] tencentcloud_clb_listener_rule support domains #2789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2789.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_clb_listener_rule: support `domains`
```
61 changes: 52 additions & 9 deletions tencentcloud/services/clb/resource_tc_clb_listener_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,22 @@ func ResourceTencentCloudClbListenerRule() *schema.Resource {
Description: "ID of CLB instance.",
},
"domain": {
Type: schema.TypeString,
Required: true,
Description: "Domain name of the listener rule.",
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"domains"},
ExactlyOneOf: []string{"domain", "domains"},
Description: "Domain name of the listener rule. Single domain rules are passed to `domain`, and multi domain rules are passed to `domains`.",
},
"domains": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"domain"},
ExactlyOneOf: []string{"domain", "domains"},
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Domain name list of the listener rule. Single domain rules are passed to `domain`, and multi domain rules are passed to `domains`.",
},
"url": {
Type: schema.TypeString,
Expand Down Expand Up @@ -224,10 +237,28 @@ func resourceTencentCloudClbListenerRuleCreate(d *schema.ResourceData, meta inte
request.ListenerId = helper.String(listenerId)

//rule set
var rule clb.RuleInput
var (
rule clb.RuleInput
domain string
domains []*string
)

if v, ok := d.GetOk("domain"); ok {
rule.Domain = helper.String(v.(string))
domain = v.(string)
}

if v, ok := d.GetOk("domains"); ok {
tmpDomains := v.([]interface{})
domains = make([]*string, 0, len(tmpDomains))
for _, value := range tmpDomains {
tmpDomain := value.(string)
domains = append(domains, &tmpDomain)
}

rule.Domains = domains
}

domain := d.Get("domain").(string)
rule.Domain = helper.String(domain)
url := d.Get("url").(string)
rule.Url = helper.String(url)
rule.TargetType = helper.String(d.Get("target_type").(string))
Expand Down Expand Up @@ -305,7 +336,7 @@ func resourceTencentCloudClbListenerRuleCreate(d *schema.ResourceData, meta inte

locationId := ""
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
ruleInstance, ruleErr := clbService.DescribeRuleByPara(ctx, clbId, listenerId, domain, url)
ruleInstance, ruleErr := clbService.DescribeRuleByPara(ctx, clbId, listenerId, domain, url, domains)
if ruleErr != nil {
return tccommon.RetryError(errors.WithStack(ruleErr))
}
Expand All @@ -327,7 +358,12 @@ func resourceTencentCloudClbListenerRuleCreate(d *schema.ResourceData, meta inte
domainRequest.Http2 = &http2Switch
domainRequest.LoadBalancerId = &clbId
domainRequest.ListenerId = &listenerId
domainRequest.Domain = &domain
if domain != "" {
domainRequest.Domain = &domain
} else {
domainRequest.NewDomains = domains
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
response, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().ModifyDomainAttributes(domainRequest)
if e != nil {
Expand Down Expand Up @@ -408,7 +444,14 @@ func resourceTencentCloudClbListenerRuleRead(d *schema.ResourceData, meta interf
instance := instances[0]
_ = d.Set("clb_id", clbId)
_ = d.Set("listener_id", listenerId)
_ = d.Set("domain", instance.Domain)
if instance.Domain != nil {
_ = d.Set("domain", instance.Domain)
}

if instance.Domains != nil {
_ = d.Set("domains", helper.StringsInterfaces(instance.Domains))
}

_ = d.Set("rule_id", instance.LocationId)
_ = d.Set("url", instance.Url)
_ = d.Set("scheduler", instance.Scheduler)
Expand Down
37 changes: 30 additions & 7 deletions tencentcloud/services/clb/resource_tc_clb_listener_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ Provides a resource to create a CLB listener rule.

Example Usage

Create a single domain listener rule

```hcl
resource "tencentcloud_clb_listener_rule" "foo" {
resource "tencentcloud_clb_listener_rule" "example" {
listener_id = "lbl-hh141sn9"
clb_id = "lb-k2zjp9lv"
domain = "foo.net"
url = "/bar"
domain = "example.com"
url = "/"
health_check_switch = true
health_check_interval_time = 5
health_check_health_num = 3
health_check_unhealth_num = 3
health_check_http_code = 2
health_check_http_path = "Default Path"
health_check_http_domain = "Default Domain"
health_check_http_path = "/"
health_check_http_domain = "check.com"
health_check_http_method = "GET"
certificate_ssl_mode = "MUTUAL"
certificate_id = "VjANRdz8"
Expand All @@ -25,10 +27,31 @@ resource "tencentcloud_clb_listener_rule" "foo" {
scheduler = "WRR"
}
```

Create a listener rule for domain lists

```hcl
resource "tencentcloud_clb_listener_rule" "example" {
listener_id = "lbl-hh141sn9"
clb_id = "lb-k2zjp9lv"
domains = ["example1.com", "example2.com"]
url = "/"
health_check_switch = true
health_check_interval_time = 5
health_check_health_num = 3
health_check_unhealth_num = 3
health_check_http_code = 2
health_check_http_path = "/"
health_check_http_domain = "check.com"
health_check_http_method = "GET"
scheduler = "WRR"
}
```

Import

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

```
$ terraform import tencentcloud_clb_listener_rule.foo lb-7a0t6zqb#lbl-hh141sn9#loc-agg236ys
```
$ terraform import tencentcloud_clb_listener_rule.example lb-k2zjp9lv#lbl-hh141sn9#loc-agg236ys
```
26 changes: 21 additions & 5 deletions tencentcloud/services/clb/service_tencentcloud_clb.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func (me *ClbService) DescribeRulesByFilter(ctx context.Context, params map[stri
return
}

func (me *ClbService) DescribeRuleByPara(ctx context.Context, clbId string, listenerId string, domain string, url string) (clbRule *clb.RuleOutput, errRet error) {
func (me *ClbService) DescribeRuleByPara(ctx context.Context, clbId string, listenerId string, domain string, url string, domains []*string) (clbRule *clb.RuleOutput, errRet error) {
logId := tccommon.GetLogId(ctx)
request := clb.NewDescribeListenersRequest()
request.ListenerIds = []*string{&listenerId}
Expand Down Expand Up @@ -369,10 +369,26 @@ func (me *ClbService) DescribeRuleByPara(ctx context.Context, clbId string, list
var ruleOutput clb.RuleOutput
findFlag := false
for _, rule := range clbListener.Rules {
if *rule.Domain == domain && *rule.Url == url {
ruleOutput = *rule
findFlag = true
break
if *rule.Url == url {
if domain != "" && *rule.Domain == domain {
ruleOutput = *rule
findFlag = true
break
} else if len(domains) > 0 {
tmpRef := true
for i := range domains {
if *domains[i] != *rule.Domains[i] {
tmpRef = false
break
}
}

if tmpRef {
ruleOutput = *rule
findFlag = true
break
}
}
}
}
if !findFlag {
Expand Down
37 changes: 30 additions & 7 deletions website/docs/r/clb_listener_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ Provides a resource to create a CLB listener rule.

## Example Usage

### Create a single domain listener rule

```hcl
resource "tencentcloud_clb_listener_rule" "foo" {
resource "tencentcloud_clb_listener_rule" "example" {
listener_id = "lbl-hh141sn9"
clb_id = "lb-k2zjp9lv"
domain = "foo.net"
url = "/bar"
domain = "example.com"
url = "/"
health_check_switch = true
health_check_interval_time = 5
health_check_health_num = 3
health_check_unhealth_num = 3
health_check_http_code = 2
health_check_http_path = "Default Path"
health_check_http_domain = "Default Domain"
health_check_http_path = "/"
health_check_http_domain = "check.com"
health_check_http_method = "GET"
certificate_ssl_mode = "MUTUAL"
certificate_id = "VjANRdz8"
Expand All @@ -37,17 +39,38 @@ resource "tencentcloud_clb_listener_rule" "foo" {
}
```

### Create a listener rule for domain lists

```hcl
resource "tencentcloud_clb_listener_rule" "example" {
listener_id = "lbl-hh141sn9"
clb_id = "lb-k2zjp9lv"
domains = ["example1.com", "example2.com"]
url = "/"
health_check_switch = true
health_check_interval_time = 5
health_check_health_num = 3
health_check_unhealth_num = 3
health_check_http_code = 2
health_check_http_path = "/"
health_check_http_domain = "check.com"
health_check_http_method = "GET"
scheduler = "WRR"
}
```

## Argument Reference

The following arguments are supported:

* `clb_id` - (Required, String) ID of CLB instance.
* `domain` - (Required, String) Domain name of the listener rule.
* `listener_id` - (Required, String, ForceNew) ID of CLB listener.
* `url` - (Required, String) Url of the listener rule.
* `certificate_ca_id` - (Optional, String) ID of the client certificate. NOTES: Only supports listeners of HTTPS protocol.
* `certificate_id` - (Optional, String) ID of the server certificate. NOTES: Only supports listeners of HTTPS protocol.
* `certificate_ssl_mode` - (Optional, String, ForceNew) Type of certificate. Valid values: `UNIDIRECTIONAL`, `MUTUAL`. NOTES: Only supports listeners of HTTPS protocol.
* `domain` - (Optional, String) Domain name of the listener rule. Single domain rules are passed to `domain`, and multi domain rules are passed to `domains`.
* `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`.
* `forward_type` - (Optional, String) Forwarding protocol between the CLB instance and real server. Valid values: `HTTP`, `HTTPS`, `TRPC`. The default is `HTTP`.
* `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`.
* `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.
Expand Down Expand Up @@ -78,6 +101,6 @@ In addition to all arguments above, the following attributes are exported:
CLB listener rule can be imported using the id (version >= 1.47.0), e.g.

```
$ terraform import tencentcloud_clb_listener_rule.foo lb-7a0t6zqb#lbl-hh141sn9#loc-agg236ys
$ terraform import tencentcloud_clb_listener_rule.example lb-k2zjp9lv#lbl-hh141sn9#loc-agg236ys
```

Loading