diff --git a/.changelog/2789.txt b/.changelog/2789.txt new file mode 100644 index 0000000000..aa517981dc --- /dev/null +++ b/.changelog/2789.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_clb_listener_rule: support `domains` +``` \ No newline at end of file diff --git a/tencentcloud/services/clb/resource_tc_clb_listener_rule.go b/tencentcloud/services/clb/resource_tc_clb_listener_rule.go index f5f16850a5..d2e4e97452 100644 --- a/tencentcloud/services/clb/resource_tc_clb_listener_rule.go +++ b/tencentcloud/services/clb/resource_tc_clb_listener_rule.go @@ -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, @@ -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)) @@ -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)) } @@ -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 { @@ -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) diff --git a/tencentcloud/services/clb/resource_tc_clb_listener_rule.md b/tencentcloud/services/clb/resource_tc_clb_listener_rule.md index a9886d9a6e..cc6370f6f7 100644 --- a/tencentcloud/services/clb/resource_tc_clb_listener_rule.md +++ b/tencentcloud/services/clb/resource_tc_clb_listener_rule.md @@ -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" @@ -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 -``` \ No newline at end of file +$ terraform import tencentcloud_clb_listener_rule.example lb-k2zjp9lv#lbl-hh141sn9#loc-agg236ys +``` diff --git a/tencentcloud/services/clb/service_tencentcloud_clb.go b/tencentcloud/services/clb/service_tencentcloud_clb.go index 7ca9af79c2..86034ecf7e 100644 --- a/tencentcloud/services/clb/service_tencentcloud_clb.go +++ b/tencentcloud/services/clb/service_tencentcloud_clb.go @@ -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} @@ -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 { diff --git a/website/docs/r/clb_listener_rule.html.markdown b/website/docs/r/clb_listener_rule.html.markdown index 57bbcc5e57..8cd1fc4670 100644 --- a/website/docs/r/clb_listener_rule.html.markdown +++ b/website/docs/r/clb_listener_rule.html.markdown @@ -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" @@ -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. @@ -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 ```