diff --git a/.changelog/3621.txt b/.changelog/3621.txt new file mode 100644 index 0000000000..a22b244b8d --- /dev/null +++ b/.changelog/3621.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_cfw_nat_policy: optimiz update function code logic +``` \ No newline at end of file diff --git a/tencentcloud/services/cfw/resource_tc_cfw_nat_policy.go b/tencentcloud/services/cfw/resource_tc_cfw_nat_policy.go index e6cdbc262e..458fbf94cc 100644 --- a/tencentcloud/services/cfw/resource_tc_cfw_nat_policy.go +++ b/tencentcloud/services/cfw/resource_tc_cfw_nat_policy.go @@ -165,7 +165,6 @@ func resourceTencentCloudCfwNatPolicyCreate(d *schema.ResourceData, meta interfa } request.Rules = append(request.Rules, &createNatRuleItem) - err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCfwClient().AddNatAcRule(request) if e != nil { @@ -174,6 +173,10 @@ func resourceTencentCloudCfwNatPolicyCreate(d *schema.ResourceData, meta interfa log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) } + if result == nil || result.Response == nil || response.Response.RuleUuid == nil || len(result.Response.RuleUuid) == 0 { + return resource.NonRetryableError(fmt.Errorf("Create cfw natPolicy failed, Response is nil.")) + } + response = result return nil }) @@ -207,8 +210,8 @@ func resourceTencentCloudCfwNatPolicyRead(d *schema.ResourceData, meta interface } if natPolicy == nil { + log.Printf("[WARN]%s resource `tencentcloud_cfw_nat_policy` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) d.SetId("") - log.Printf("[WARN]%s resource `CfwNatPolicy` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) return nil } @@ -282,8 +285,7 @@ func resourceTencentCloudCfwNatPolicyUpdate(d *schema.ResourceData, meta interfa uuid = d.Id() ) - immutableArgs := []string{"uuid", "direction"} - + immutableArgs := []string{"direction"} for _, v := range immutableArgs { if d.HasChange(v) { return fmt.Errorf("argument `%s` cannot be changed", v) @@ -321,7 +323,7 @@ func resourceTencentCloudCfwNatPolicyUpdate(d *schema.ResourceData, meta interfa modifyRuleItem.Port = helper.String(v.(string)) } - if v, ok := d.GetOk("direction"); ok { + if v, ok := d.GetOkExists("direction"); ok { modifyRuleItem.Direction = helper.IntUint64(v.(int)) } @@ -342,7 +344,6 @@ func resourceTencentCloudCfwNatPolicyUpdate(d *schema.ResourceData, meta interfa } request.Rules = append(request.Rules, &modifyRuleItem) - err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCfwClient().ModifyNatAcRule(request) if e != nil { diff --git a/tencentcloud/services/cfw/service_tencentcloud_cfw.go b/tencentcloud/services/cfw/service_tencentcloud_cfw.go index 7695fab418..97ff6caac6 100644 --- a/tencentcloud/services/cfw/service_tencentcloud_cfw.go +++ b/tencentcloud/services/cfw/service_tencentcloud_cfw.go @@ -418,6 +418,7 @@ func (me *CfwService) DescribeCfwNatPolicyById(ctx context.Context, uuid string) logId := tccommon.GetLogId(ctx) request := cfw.NewDescribeNatAcRuleRequest() + response := cfw.NewDescribeNatAcRuleResponse() request.Limit = common.Uint64Ptr(20) request.Offset = common.Uint64Ptr(0) request.Filters = []*cfw.CommonFilter{ @@ -434,16 +435,28 @@ func (me *CfwService) DescribeCfwNatPolicyById(ctx context.Context, uuid string) } }() - ratelimit.Check(request.GetAction()) + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { + ratelimit.Check(request.GetAction()) + result, e := me.client.UseCfwClient().DescribeNatAcRule(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(), response.ToJsonString()) + } + + if result == nil || result.Response == nil || result.Response.Data == nil { + return resource.NonRetryableError(fmt.Errorf("Describe nat ac rule failed, Response is nil.")) + } + + response = result + return nil + }) - response, err := me.client.UseCfwClient().DescribeNatAcRule(request) if err != nil { errRet = err return } - log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) - if len(response.Response.Data) < 1 { return } @@ -465,16 +478,23 @@ func (me *CfwService) DeleteCfwNatPolicyById(ctx context.Context, uuid string) ( } }() - ratelimit.Check(request.GetAction()) + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + ratelimit.Check(request.GetAction()) + result, e := me.client.UseCfwClient().RemoveNatAcRule(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 + }) - response, err := me.client.UseCfwClient().RemoveNatAcRule(request) if err != nil { errRet = err return } - log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) - return }