From 9d58f6714f77a9f4f55541c874c97423071f2dc8 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Tue, 20 Aug 2024 16:32:58 +0800 Subject: [PATCH 1/3] add --- .../clb/resource_tc_clb_listener_rule.go | 58 ++++++++++++++++--- .../services/clb/service_tencentcloud_clb.go | 22 +++++-- 2 files changed, 66 insertions(+), 14 deletions(-) diff --git a/tencentcloud/services/clb/resource_tc_clb_listener_rule.go b/tencentcloud/services/clb/resource_tc_clb_listener_rule.go index f5f16850a5..05feffda8e 100644 --- a/tencentcloud/services/clb/resource_tc_clb_listener_rule.go +++ b/tencentcloud/services/clb/resource_tc_clb_listener_rule.go @@ -39,9 +39,19 @@ 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, + ConflictsWith: []string{"domains"}, + AtLeastOneOf: []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, + ConflictsWith: []string{"domain"}, + AtLeastOneOf: []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 +234,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 +333,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 +355,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 +441,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/service_tencentcloud_clb.go b/tencentcloud/services/clb/service_tencentcloud_clb.go index 7ca9af79c2..33ecd198c3 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,22 @@ 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 { + for i := range domains { + if *domains[i] != *rule.Domains[i] { + break + } + } + + ruleOutput = *rule + findFlag = true + break + } } } if !findFlag { From e582219652a0ef377bae9d932d33fcd6af6b9f5d Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Tue, 27 Aug 2024 11:44:10 +0800 Subject: [PATCH 2/3] add --- .../clb/resource_tc_clb_listener_rule.go | 7 +++- .../clb/resource_tc_clb_listener_rule.md | 37 +++++++++++++++---- .../services/clb/service_tencentcloud_clb.go | 10 +++-- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/tencentcloud/services/clb/resource_tc_clb_listener_rule.go b/tencentcloud/services/clb/resource_tc_clb_listener_rule.go index 05feffda8e..d2e4e97452 100644 --- a/tencentcloud/services/clb/resource_tc_clb_listener_rule.go +++ b/tencentcloud/services/clb/resource_tc_clb_listener_rule.go @@ -41,15 +41,18 @@ func ResourceTencentCloudClbListenerRule() *schema.Resource { "domain": { Type: schema.TypeString, Optional: true, + Computed: true, ConflictsWith: []string{"domains"}, - AtLeastOneOf: []string{"domain", "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"}, - AtLeastOneOf: []string{"domain", "domains"}, + 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`.", }, 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 33ecd198c3..86034ecf7e 100644 --- a/tencentcloud/services/clb/service_tencentcloud_clb.go +++ b/tencentcloud/services/clb/service_tencentcloud_clb.go @@ -375,15 +375,19 @@ func (me *ClbService) DescribeRuleByPara(ctx context.Context, clbId string, list findFlag = true break } else if len(domains) > 0 { + tmpRef := true for i := range domains { if *domains[i] != *rule.Domains[i] { + tmpRef = false break } } - ruleOutput = *rule - findFlag = true - break + if tmpRef { + ruleOutput = *rule + findFlag = true + break + } } } } From 99e53b6b117a6fba0479072a3f8fc62fee29d6e0 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Tue, 27 Aug 2024 11:45:52 +0800 Subject: [PATCH 3/3] add --- .changelog/2789.txt | 3 ++ .../docs/r/clb_listener_rule.html.markdown | 37 +++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 .changelog/2789.txt 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/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 ```