Skip to content
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/3621.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_cfw_nat_policy: optimiz update function code logic
```
13 changes: 7 additions & 6 deletions tencentcloud/services/cfw/resource_tc_cfw_nat_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
})
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}

Expand All @@ -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 {
Expand Down
36 changes: 28 additions & 8 deletions tencentcloud/services/cfw/service_tencentcloud_cfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
Loading