From 378309d489d1bafc23c1c7e17ccf2e56f90a1829 Mon Sep 17 00:00:00 2001 From: arunma Date: Thu, 27 Mar 2025 20:10:47 +0800 Subject: [PATCH 1/5] fix(teo): [122612321] modify l7 acc rule --- .../teo/resource_tc_teo_l7_acc_rule.go | 252 +++++++----------- .../teo/resource_tc_teo_l7_acc_rule.md | 8 +- .../resource_tc_teo_l7_acc_rule_extension.go | 18 ++ .../teo/resource_tc_teo_l7_acc_rule_test.go | 137 ++++------ .../services/teo/service_tencentcloud_teo.go | 28 ++ website/docs/r/teo_l7_acc_rule.html.markdown | 10 +- 6 files changed, 195 insertions(+), 258 deletions(-) diff --git a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go index 7b31497981..5aa09e4e0f 100644 --- a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go +++ b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go @@ -3,8 +3,8 @@ package teo import ( "context" - "errors" "log" + "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -45,7 +45,9 @@ func ResourceTencentCloudTeoL7AccRule() *schema.Resource { "status": { Type: schema.TypeString, Optional: true, + Computed: true, Description: "Rule status. The possible values are: `enable`: enabled; `disable`: disabled.", + Deprecated: "This field is deprecated and will be removed in the future. No longer valid. If the rule is empty, delete the rule.", }, "rule_name": { Type: schema.TypeString, @@ -85,70 +87,17 @@ func resourceTencentCloudTeoL7AccRuleCreate(d *schema.ResourceData, meta interfa defer tccommon.LogElapsed("resource.tencentcloud_teo_l7_acc_rule.create")() defer tccommon.InconsistentCheck(d, meta)() - logId := tccommon.GetLogId(tccommon.ContextNil) - - ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) - var ( zoneId string ) - var ( - request = teov20220901.NewCreateL7AccRulesRequest() - response = teov20220901.NewCreateL7AccRulesResponse() - ) if v, ok := d.GetOk("zone_id"); ok { zoneId = v.(string) - request.ZoneId = helper.String(v.(string)) - } - - if v, ok := d.GetOk("rules"); ok { - for _, item := range v.([]interface{}) { - rulesMap := item.(map[string]interface{}) - ruleEngineItem := teov20220901.RuleEngineItem{} - if v, ok := rulesMap["status"].(string); ok && v != "" { - ruleEngineItem.Status = helper.String(v) - } - if v, ok := rulesMap["rule_name"].(string); ok && v != "" { - ruleEngineItem.RuleName = helper.String(v) - } - if v, ok := rulesMap["description"]; ok { - descriptionSet := v.([]interface{}) - for i := range descriptionSet { - description := descriptionSet[i].(string) - ruleEngineItem.Description = append(ruleEngineItem.Description, helper.String(description)) - } - } - // if v, ok := rulesMap["rule_priority"].(int); ok { - // ruleEngineItem.RulePriority = helper.IntInt64(v) - // } - if _, ok := rulesMap["branches"]; ok { - ruleEngineItem.Branches = resourceTencentCloudTeoL7AccRuleGetBranchs(rulesMap) - } - - request.Rules = append(request.Rules, &ruleEngineItem) - } - } - - reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { - result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().CreateL7AccRulesWithContext(ctx, request) - if e != nil { - return tccommon.RetryError(e) - } else { - log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) - } - response = result - return nil - }) - if reqErr != nil { - log.Printf("[CRITAL]%s create teo l7 acc rule failed, reason:%+v", logId, reqErr) - return reqErr } d.SetId(zoneId) - _ = response - return resourceTencentCloudTeoL7AccRuleRead(d, meta) + return resourceTencentCloudTeoL7AccRuleUpdate(d, meta) } func resourceTencentCloudTeoL7AccRuleRead(d *schema.ResourceData, meta interface{}) error { @@ -223,64 +172,56 @@ func resourceTencentCloudTeoL7AccRuleUpdate(d *schema.ResourceData, meta interfa zoneId := d.Id() - needChange := false - mutableArgs := []string{"rules"} - for _, v := range mutableArgs { - if d.HasChange(v) { - needChange = true - break - } - } - - if needChange { - if v, ok := d.GetOk("rules"); ok { - for _, item := range v.([]interface{}) { - request := teov20220901.NewModifyL7AccRuleRequest() + if v, ok := d.GetOk("rules"); ok { + request := teov20220901.NewImportZoneConfigRequest() + request.ZoneId = helper.String(zoneId) - if v, ok := d.GetOk("zone_id"); ok { - request.ZoneId = helper.String(v.(string)) - } - rulesMap := item.(map[string]interface{}) - ruleEngineItem := teov20220901.RuleEngineItem{} - if v, ok := rulesMap["status"].(string); ok && v != "" { - ruleEngineItem.Status = helper.String(v) - } - if v, ok := rulesMap["rule_id"].(string); ok && v != "" { - ruleEngineItem.RuleId = helper.String(v) - } - if v, ok := rulesMap["rule_name"].(string); ok && v != "" { - ruleEngineItem.RuleName = helper.String(v) - } - if v, ok := rulesMap["description"]; ok { - descriptionSet := v.([]interface{}) - for i := range descriptionSet { - description := descriptionSet[i].(string) - ruleEngineItem.Description = append(ruleEngineItem.Description, helper.String(description)) - } + rules := []*teov20220901.RuleEngineItem{} + for _, item := range v.([]interface{}) { + rulesMap := item.(map[string]interface{}) + ruleEngineItem := teov20220901.RuleEngineItem{} + if v, ok := rulesMap["rule_name"].(string); ok && v != "" { + ruleEngineItem.RuleName = helper.String(v) + } + if v, ok := rulesMap["description"]; ok { + descriptionSet := v.([]interface{}) + for i := range descriptionSet { + description := descriptionSet[i].(string) + ruleEngineItem.Description = append(ruleEngineItem.Description, helper.String(description)) } - // if v, ok := rulesMap["rule_priority"].(int); ok { - // ruleEngineItem.RulePriority = helper.IntInt64(v) - // } + } + if _, ok := rulesMap["branches"]; ok { + ruleEngineItem.Branches = resourceTencentCloudTeoL7AccRuleGetBranchs(rulesMap) + } + rules = append(rules, &ruleEngineItem) + } - if _, ok := rulesMap["branches"]; ok { - ruleEngineItem.Branches = resourceTencentCloudTeoL7AccRuleGetBranchs(rulesMap) - } - request.Rule = &ruleEngineItem - - reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { - result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().ModifyL7AccRuleWithContext(ctx, request) - if e != nil { - return tccommon.RetryError(e) - } else { - log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) - } - return nil - }) - if reqErr != nil { - log.Printf("[CRITAL]%s update teo l7 acc rule failed, reason:%+v", logId, reqErr) - return reqErr - } + context, err := resourceTencentCloudTeoL7AccRuleContent(rules) + if err != nil { + return err + } + request.Content = helper.String(context) + + response := teov20220901.NewImportZoneConfigResponse() + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().ImportZoneConfigWithContext(ctx, request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) } + response = result + return nil + }) + if reqErr != nil { + log.Printf("[CRITAL]%s update teo l7 acc rule failed, reason:%+v", logId, reqErr) + return reqErr + } + + service := TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + conf := tccommon.BuildStateChangeConf([]string{"doing"}, []string{"success"}, 10*tccommon.ReadRetryTimeout, time.Second, service.TeoL7AccRuleStateRefreshFunc(zoneId, *response.Response.TaskId, []string{"failure"})) + if _, e := conf.WaitForState(); e != nil { + return e } } @@ -292,54 +233,47 @@ func resourceTencentCloudTeoL7AccRuleDelete(d *schema.ResourceData, meta interfa defer tccommon.LogElapsed("resource.tencentcloud_teo_l7_acc_rule.delete")() defer tccommon.InconsistentCheck(d, meta)() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) - - zoneId := d.Id() - - var ( - request = teov20220901.NewDeleteL7AccRulesRequest() - response = teov20220901.NewDeleteL7AccRulesResponse() - ) - request.ZoneId = helper.String(zoneId) - - if v, ok := d.GetOk("rule_ids"); ok { - ruleIdsSet := v.([]interface{}) - for i := range ruleIdsSet { - ruleIds := ruleIdsSet[i].(string) - request.RuleIds = append(request.RuleIds, helper.String(ruleIds)) - } - } - - if v, ok := d.GetOk("rules"); ok { - for _, item := range v.([]interface{}) { - rulesMap := item.(map[string]interface{}) - if v, ok := rulesMap["rule_id"].(string); ok && v != "" { - request.RuleIds = append(request.RuleIds, helper.String(v)) - } - } - } - - if len(request.RuleIds) == 0 { - return errors.New("[CRITAL]%s delete teo l7 acc rule failed, rule_ids is empty") - } - - reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { - result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().DeleteL7AccRulesWithContext(ctx, request) - if e != nil { - return tccommon.RetryError(e) - } else { - log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) - } - response = result - return nil - }) - if reqErr != nil { - log.Printf("[CRITAL]%s delete teo l7 acc rule failed, reason:%+v", logId, reqErr) - return reqErr - } - - _ = response - _ = zoneId - return nil + // logId := tccommon.GetLogId(tccommon.ContextNil) + // ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) + + // zoneId := d.Id() + + // var ( + // request = teov20220901.NewDeleteL7AccRulesRequest() + // response = teov20220901.NewDeleteL7AccRulesResponse() + // ) + // request.ZoneId = helper.String(zoneId) + + // if v, ok := d.GetOk("rules"); ok { + // for _, item := range v.([]interface{}) { + // rulesMap := item.(map[string]interface{}) + // if v, ok := rulesMap["rule_id"].(string); ok && v != "" { + // request.RuleIds = append(request.RuleIds, helper.String(v)) + // } + // } + // } + + // if len(request.RuleIds) == 0 { + // return errors.New("[CRITAL]%s delete teo l7 acc rule failed, rule_ids is empty") + // } + + // reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + // result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().DeleteL7AccRulesWithContext(ctx, request) + // if e != nil { + // return tccommon.RetryError(e) + // } else { + // log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + // } + // response = result + // return nil + // }) + // if reqErr != nil { + // log.Printf("[CRITAL]%s delete teo l7 acc rule failed, reason:%+v", logId, reqErr) + // return reqErr + // } + + // _ = response + // _ = zoneId + d.Set("rules", []interface{}{}) + return resourceTencentCloudTeoL7AccRuleUpdate(d, meta) } diff --git a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.md b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.md index 27781c6277..b7f266c24c 100644 --- a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.md +++ b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.md @@ -1,5 +1,7 @@ Provides a resource to create a teo l7_acc_rule +~> **NOTE:** This feature only supports the sites in the plans of the Standard Edition and the Enterprise Edition. + Example Usage ```hcl @@ -8,7 +10,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["1"] rule_name = "网站加速" - status = "disable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -66,7 +67,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["2"] rule_name = "音视频直播" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" sub_rules { @@ -117,7 +117,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["3"] rule_name = "大文件下载" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -168,7 +167,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["4"] rule_name = "音视频点播" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -219,7 +217,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["5"] rule_name = "API 加速" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -241,7 +238,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["6"] rule_name = "WordPress 建站" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" sub_rules { diff --git a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_extension.go b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_extension.go index ab2e3d7d5b..1af1015c24 100644 --- a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_extension.go +++ b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_extension.go @@ -1,6 +1,8 @@ package teo import ( + "encoding/json" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" teo "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" teov20220901 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo/v20220901" @@ -2464,3 +2466,19 @@ func resourceTencentCloudTeoL7AccRuleSetBranchs(ruleBranches []*teo.RuleBranch) } return branchesList } + +func resourceTencentCloudTeoL7AccRuleContent(rules []*teo.RuleEngineItem) (string, error) { + type Content struct { + FormatVersion string `json:"FormatVersion,omitnil,omitempty"` + Rules []*teo.RuleEngineItem `json:"Rules,omitnil,omitempty"` + } + content := Content{ + FormatVersion: "1.0", + Rules: rules, + } + contentBytes, err := json.Marshal(content) + if err != nil { + return "", err + } + return string(contentBytes), nil +} diff --git a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_test.go b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_test.go index cbe6f50edd..93b95cd8e5 100644 --- a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_test.go +++ b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_test.go @@ -22,7 +22,6 @@ func TestAccTencentCloudTeoL7AccRuleResource_basic(t *testing.T) { resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.#", "6"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.description.#", "1"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.rule_name", "网站加速"), - resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.status", "enable"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.branches.#", "1"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.branches.0.condition", "${http.request.host} in ['aaa.makn.cn']"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.branches.0.actions.#", "2"), @@ -70,10 +69,9 @@ func TestAccTencentCloudTeoL7AccRuleResource_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "id"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "zone_id", "zone-36bjhygh1bxe"), - resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.#", "6"), + resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.#", "5"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.description.#", "1"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.rule_name", "网站加速"), - resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.status", "disable"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.branches.#", "1"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.branches.0.condition", "${http.request.host} in ['aaa.makn.cn']"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.branches.0.actions.#", "2"), @@ -122,7 +120,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["1"] rule_name = "网站加速" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -180,7 +177,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["2"] rule_name = "音视频直播" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" sub_rules { @@ -231,7 +227,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["3"] rule_name = "大文件下载" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -282,7 +277,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["4"] rule_name = "音视频点播" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -333,7 +327,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["5"] rule_name = "API 加速" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -355,7 +348,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["6"] rule_name = "WordPress 建站" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" sub_rules { @@ -433,7 +425,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["1"] rule_name = "网站加速" - status = "disable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -491,7 +482,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["2"] rule_name = "音视频直播" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" sub_rules { @@ -542,7 +532,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["3"] rule_name = "大文件下载" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -590,83 +579,9 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { } } } - rules { - description = ["4"] - rule_name = "音视频点播" - status = "enable" - branches { - condition = "$${http.request.host} in ['aaa.makn.cn']" - actions { - name = "Cache" - cache_parameters { - custom_time { - cache_time = 2592000 - ignore_cache_control = "off" - switch = "on" - } - } - } - actions { - name = "CacheKey" - cache_key_parameters { - full_url_cache = "off" - ignore_case = "off" - scheme = null - query_string { - action = null - switch = "off" - values = [] - } - } - } - actions { - name = "RangeOriginPull" - range_origin_pull_parameters { - switch = "on" - } - } - sub_rules { - description = ["4-1"] - branches { - condition = "$${http.request.file_extension} in ['php', 'jsp', 'asp', 'aspx']" - actions { - name = "Cache" - cache_parameters { - no_cache { - switch = "on" - } - } - } - } - } - } - } - rules { - description = ["5"] - rule_name = "API 加速" - status = "enable" - branches { - condition = "$${http.request.host} in ['aaa.makn.cn']" - actions { - name = "Cache" - cache_parameters { - no_cache { - switch = "on" - } - } - } - actions { - name = "SmartRouting" - smart_routing_parameters { - switch = "off" - } - } - } - } rules { description = ["6"] rule_name = "WordPress 建站" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" sub_rules { @@ -734,5 +649,55 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { } } } + rules { + description = ["4"] + rule_name = "音视频点播" + branches { + condition = "$${http.request.host} in ['aaa.makn.cn']" + actions { + name = "Cache" + cache_parameters { + custom_time { + cache_time = 2592000 + ignore_cache_control = "off" + switch = "on" + } + } + } + actions { + name = "CacheKey" + cache_key_parameters { + full_url_cache = "off" + ignore_case = "off" + scheme = null + query_string { + action = null + switch = "off" + values = [] + } + } + } + actions { + name = "RangeOriginPull" + range_origin_pull_parameters { + switch = "on" + } + } + sub_rules { + description = ["4-1"] + branches { + condition = "$${http.request.file_extension} in ['php', 'jsp', 'asp', 'aspx']" + actions { + name = "Cache" + cache_parameters { + no_cache { + switch = "on" + } + } + } + } + } + } + } } ` diff --git a/tencentcloud/services/teo/service_tencentcloud_teo.go b/tencentcloud/services/teo/service_tencentcloud_teo.go index e315cb04ec..30f67c1b58 100644 --- a/tencentcloud/services/teo/service_tencentcloud_teo.go +++ b/tencentcloud/services/teo/service_tencentcloud_teo.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "strings" tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" @@ -1644,3 +1645,30 @@ func (me *TeoService) DescribeTeoL7AccRuleById(ctx context.Context, zoneId strin ret = response.Response return } + +func (me *TeoService) TeoL7AccRuleStateRefreshFunc(zoneId, taskId string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + request := teov20220901.NewDescribeZoneConfigImportResultRequest() + request.ZoneId = helper.String(zoneId) + request.TaskId = helper.String(taskId) + ratelimit.Check(request.GetAction()) + object, err := me.client.UseTeoV20220901Client().DescribeZoneConfigImportResult(request) + + if err != nil { + return nil, "", err + } + if object == nil || object.Response == nil || object.Response.Status == nil { + return nil, "", nil + } + status := helper.PString(object.Response.Status) + if len(failStates) > 0 { + for _, state := range failStates { + if strings.Contains(status, state) { + return object, status, fmt.Errorf("teo[%s] sync check task[%s] failed, status is on [%s], return...", zoneId, taskId, status) + } + } + } + + return object, status, nil + } +} diff --git a/website/docs/r/teo_l7_acc_rule.html.markdown b/website/docs/r/teo_l7_acc_rule.html.markdown index bdc311d9a6..09b6d2a9e9 100644 --- a/website/docs/r/teo_l7_acc_rule.html.markdown +++ b/website/docs/r/teo_l7_acc_rule.html.markdown @@ -11,6 +11,8 @@ description: |- Provides a resource to create a teo l7_acc_rule +~> **NOTE:** This feature only supports the sites in the plans of the Standard Edition and the Enterprise Edition. + ## Example Usage ```hcl @@ -19,7 +21,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["1"] rule_name = "网站加速" - status = "disable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -77,7 +78,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["2"] rule_name = "音视频直播" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" sub_rules { @@ -128,7 +128,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["3"] rule_name = "大文件下载" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -179,7 +178,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["4"] rule_name = "音视频点播" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -230,7 +228,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["5"] rule_name = "API 加速" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" actions { @@ -252,7 +249,6 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { rules { description = ["6"] rule_name = "WordPress 建站" - status = "enable" branches { condition = "$${http.request.host} in ['aaa.makn.cn']" sub_rules { @@ -646,7 +642,7 @@ The `rules` object supports the following: * `branches` - (Optional, List) Sub-Rule branch. this list currently supports filling in only one rule; multiple entries are invalid. * `description` - (Optional, List) Rule annotation. multiple annotations can be added. * `rule_name` - (Optional, String) Rule name. The name length limit is 255 characters. -* `status` - (Optional, String) Rule status. The possible values are: `enable`: enabled; `disable`: disabled. +* `status` - (Optional, String, **Deprecated**) This field is deprecated and will be removed in the future. No longer valid. If the rule is empty, delete the rule. Rule status. The possible values are: `enable`: enabled; `disable`: disabled. The `set_content_identifier_parameters` object of `actions` supports the following: From 912a6246e434c8bc6f1bb57684ce8f1deac9dd91 Mon Sep 17 00:00:00 2001 From: arunma Date: Thu, 27 Mar 2025 20:16:05 +0800 Subject: [PATCH 2/5] feat: add changelog --- .changelog/3247.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/3247.txt diff --git a/.changelog/3247.txt b/.changelog/3247.txt new file mode 100644 index 0000000000..278505a86a --- /dev/null +++ b/.changelog/3247.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_teo_l7_acc_rule: Fix the problem of modifying rules and supporting the order of rules. +``` \ No newline at end of file From 36f56f96965bec3697c2f035223a9d082494a4fc Mon Sep 17 00:00:00 2001 From: arunma Date: Thu, 27 Mar 2025 20:57:34 +0800 Subject: [PATCH 3/5] fix: modify test --- .../teo/resource_tc_teo_l7_acc_rule.go | 46 ------------------- .../teo/resource_tc_teo_l7_acc_rule_test.go | 8 ++-- 2 files changed, 4 insertions(+), 50 deletions(-) diff --git a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go index 5aa09e4e0f..05b5ece8c0 100644 --- a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go +++ b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go @@ -45,7 +45,6 @@ func ResourceTencentCloudTeoL7AccRule() *schema.Resource { "status": { Type: schema.TypeString, Optional: true, - Computed: true, Description: "Rule status. The possible values are: `enable`: enabled; `disable`: disabled.", Deprecated: "This field is deprecated and will be removed in the future. No longer valid. If the rule is empty, delete the rule.", }, @@ -133,10 +132,6 @@ func resourceTencentCloudTeoL7AccRuleRead(d *schema.ResourceData, meta interface rulesMap["rule_id"] = rules.RuleId } - if rules.Status != nil { - rulesMap["status"] = rules.Status - } - if rules.RuleName != nil { rulesMap["rule_name"] = rules.RuleName } @@ -233,47 +228,6 @@ func resourceTencentCloudTeoL7AccRuleDelete(d *schema.ResourceData, meta interfa defer tccommon.LogElapsed("resource.tencentcloud_teo_l7_acc_rule.delete")() defer tccommon.InconsistentCheck(d, meta)() - // logId := tccommon.GetLogId(tccommon.ContextNil) - // ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) - - // zoneId := d.Id() - - // var ( - // request = teov20220901.NewDeleteL7AccRulesRequest() - // response = teov20220901.NewDeleteL7AccRulesResponse() - // ) - // request.ZoneId = helper.String(zoneId) - - // if v, ok := d.GetOk("rules"); ok { - // for _, item := range v.([]interface{}) { - // rulesMap := item.(map[string]interface{}) - // if v, ok := rulesMap["rule_id"].(string); ok && v != "" { - // request.RuleIds = append(request.RuleIds, helper.String(v)) - // } - // } - // } - - // if len(request.RuleIds) == 0 { - // return errors.New("[CRITAL]%s delete teo l7 acc rule failed, rule_ids is empty") - // } - - // reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { - // result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTeoV20220901Client().DeleteL7AccRulesWithContext(ctx, request) - // if e != nil { - // return tccommon.RetryError(e) - // } else { - // log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) - // } - // response = result - // return nil - // }) - // if reqErr != nil { - // log.Printf("[CRITAL]%s delete teo l7 acc rule failed, reason:%+v", logId, reqErr) - // return reqErr - // } - - // _ = response - // _ = zoneId d.Set("rules", []interface{}{}) return resourceTencentCloudTeoL7AccRuleUpdate(d, meta) } diff --git a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_test.go b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_test.go index 93b95cd8e5..c66eeaf7b6 100644 --- a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_test.go +++ b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_test.go @@ -18,7 +18,7 @@ func TestAccTencentCloudTeoL7AccRuleResource_basic(t *testing.T) { Config: testAccTeoL7AccRule, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "id"), - resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "zone_id", "zone-36bjhygh1bxe"), + resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "zone_id", "zone-39quuimqg8r6"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.#", "6"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.description.#", "1"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.rule_name", "网站加速"), @@ -68,7 +68,7 @@ func TestAccTencentCloudTeoL7AccRuleResource_basic(t *testing.T) { Config: testAccTeoL7AccRuleUp, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "id"), - resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "zone_id", "zone-36bjhygh1bxe"), + resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "zone_id", "zone-39quuimqg8r6"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.#", "5"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.description.#", "1"), resource.TestCheckResourceAttr("tencentcloud_teo_l7_acc_rule.teo_l7_acc_rule", "rules.0.rule_name", "网站加速"), @@ -116,7 +116,7 @@ func TestAccTencentCloudTeoL7AccRuleResource_basic(t *testing.T) { const testAccTeoL7AccRule = ` resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { - zone_id = "zone-36bjhygh1bxe" + zone_id = "zone-39quuimqg8r6" rules { description = ["1"] rule_name = "网站加速" @@ -421,7 +421,7 @@ resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { const testAccTeoL7AccRuleUp = ` resource "tencentcloud_teo_l7_acc_rule" "teo_l7_acc_rule" { - zone_id = "zone-36bjhygh1bxe" + zone_id = "zone-39quuimqg8r6" rules { description = ["1"] rule_name = "网站加速" From a28cf4e020c8dd80954751f10263adc1b53c5097 Mon Sep 17 00:00:00 2001 From: arunma Date: Thu, 27 Mar 2025 21:42:36 +0800 Subject: [PATCH 4/5] fix: modify rule --- .../services/teo/resource_tc_teo_l7_acc_rule.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go index 05b5ece8c0..a0ebd9b99a 100644 --- a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go +++ b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule.go @@ -3,6 +3,7 @@ package teo import ( "context" + "fmt" "log" "time" @@ -213,10 +214,14 @@ func resourceTencentCloudTeoL7AccRuleUpdate(d *schema.ResourceData, meta interfa return reqErr } - service := TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} - conf := tccommon.BuildStateChangeConf([]string{"doing"}, []string{"success"}, 10*tccommon.ReadRetryTimeout, time.Second, service.TeoL7AccRuleStateRefreshFunc(zoneId, *response.Response.TaskId, []string{"failure"})) - if _, e := conf.WaitForState(); e != nil { - return e + if response != nil && response.Response != nil && response.Response.TaskId != nil { + service := TeoService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + conf := tccommon.BuildStateChangeConf([]string{"doing"}, []string{"success"}, 10*tccommon.ReadRetryTimeout, time.Second, service.TeoL7AccRuleStateRefreshFunc(zoneId, *response.Response.TaskId, []string{"failure"})) + if _, e := conf.WaitForState(); e != nil { + return e + } + } else { + return fmt.Errorf("[CRITAL]%s update teo l7 acc rule failed, response body is nil", logId) } } From 7edd14c27a08cef868a4a1ea3024e1ab605c0bc5 Mon Sep 17 00:00:00 2001 From: arunma Date: Fri, 28 Mar 2025 14:44:54 +0800 Subject: [PATCH 5/5] fix: modify rule content --- .../services/teo/resource_tc_teo_l7_acc_rule_extension.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_extension.go b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_extension.go index 1af1015c24..ff3a5d12a7 100644 --- a/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_extension.go +++ b/tencentcloud/services/teo/resource_tc_teo_l7_acc_rule_extension.go @@ -2469,8 +2469,8 @@ func resourceTencentCloudTeoL7AccRuleSetBranchs(ruleBranches []*teo.RuleBranch) func resourceTencentCloudTeoL7AccRuleContent(rules []*teo.RuleEngineItem) (string, error) { type Content struct { - FormatVersion string `json:"FormatVersion,omitnil,omitempty"` - Rules []*teo.RuleEngineItem `json:"Rules,omitnil,omitempty"` + FormatVersion string `json:"FormatVersion,omitempty"` + Rules []*teo.RuleEngineItem `json:"Rules,omitempty"` } content := Content{ FormatVersion: "1.0",