diff --git a/.changelog/3024.txt b/.changelog/3024.txt new file mode 100644 index 0000000000..ddb7d0c2c4 --- /dev/null +++ b/.changelog/3024.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_as_lifecycle_hook: update create function +``` \ No newline at end of file diff --git a/tencentcloud/services/as/resource_tc_as_lifecycle_hook.go b/tencentcloud/services/as/resource_tc_as_lifecycle_hook.go index 3055980f53..e332186abe 100644 --- a/tencentcloud/services/as/resource_tc_as_lifecycle_hook.go +++ b/tencentcloud/services/as/resource_tc_as_lifecycle_hook.go @@ -4,12 +4,14 @@ import ( "context" "fmt" "log" + "strings" tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419" + sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" ) @@ -160,19 +162,35 @@ func resourceTencentCloudAsLifecycleHookCreate(d *schema.ResourceData, meta inte request.LifecycleCommand = &lifecycleCommand } - response, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAsClient().CreateLifecycleHook(request) + var lifecycleHookId string + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { + response, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAsClient().CreateLifecycleHook(request) + if err != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), err.Error()) + if e, ok := err.(*sdkErrors.TencentCloudSDKError); ok { + if strings.Contains(e.GetCode(), "LimitExceeded.QuotaNotEnough") { + return resource.RetryableError(err) + } + } + + return tccommon.RetryError(err) + } + + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + if response == nil || response.Response == nil || response.Response.LifecycleHookId == nil { + return resource.NonRetryableError(fmt.Errorf("AS LifecycleHook not exists")) + } + + lifecycleHookId = *response.Response.LifecycleHookId + return nil + }) + if err != nil { - log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", - logId, request.GetAction(), request.ToJsonString(), err.Error()) + log.Printf("[CRITAL]%s create AS LifecycleHook failed, reason:%s\n", logId, err.Error()) return err } - log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", - logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) - if response.Response.LifecycleHookId == nil { - return fmt.Errorf("lifecycle hook id is nil") - } - d.SetId(*response.Response.LifecycleHookId) + d.SetId(lifecycleHookId) return resourceTencentCloudAsLifecycleHookRead(d, meta) } diff --git a/tencentcloud/services/as/resource_tc_as_lifecycle_hook.md b/tencentcloud/services/as/resource_tc_as_lifecycle_hook.md index 3b7c7ccf57..c280db60ab 100644 --- a/tencentcloud/services/as/resource_tc_as_lifecycle_hook.md +++ b/tencentcloud/services/as/resource_tc_as_lifecycle_hook.md @@ -56,7 +56,7 @@ resource "tencentcloud_as_lifecycle_hook" "example" { } ``` -If `notification_target_type` is `CMQ_QUEUE` +If notification_target_type is CMQ_QUEUE ```hcl resource "tencentcloud_as_lifecycle_hook" "example" { @@ -71,7 +71,7 @@ resource "tencentcloud_as_lifecycle_hook" "example" { } ``` -Or `notification_target_type` is `CMQ_TOPIC` +Or notification_target_type is CMQ_TOPIC ```hcl resource "tencentcloud_as_lifecycle_hook" "example" { @@ -107,5 +107,5 @@ Import lifecycle hook can be imported using the id, e.g. ``` -terraform import tencentcloud_as_lifecycle_hook.example lifecycle_hook_id +terraform import tencentcloud_as_lifecycle_hook.example ash-ahg67203 ``` \ No newline at end of file diff --git a/website/docs/r/as_lifecycle_hook.html.markdown b/website/docs/r/as_lifecycle_hook.html.markdown index 880cc67d55..cf7d4f1cb2 100644 --- a/website/docs/r/as_lifecycle_hook.html.markdown +++ b/website/docs/r/as_lifecycle_hook.html.markdown @@ -67,7 +67,7 @@ resource "tencentcloud_as_lifecycle_hook" "example" { } ``` - +### If notification_target_type is CMQ_QUEUE ```hcl resource "tencentcloud_as_lifecycle_hook" "example" { @@ -82,7 +82,7 @@ resource "tencentcloud_as_lifecycle_hook" "example" { } ``` - +### Or notification_target_type is CMQ_TOPIC ```hcl resource "tencentcloud_as_lifecycle_hook" "example" { @@ -147,6 +147,6 @@ In addition to all arguments above, the following attributes are exported: lifecycle hook can be imported using the id, e.g. ``` -terraform import tencentcloud_as_lifecycle_hook.example lifecycle_hook_id +terraform import tencentcloud_as_lifecycle_hook.example ash-ahg67203 ```