Skip to content

feat(cam): [117618077]support retry #2671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 11, 2024
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/2671.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_user_info: Support retry
```
48 changes: 35 additions & 13 deletions tencentcloud/services/cam/data_source_tc_user_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cam
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"log"
"math/rand"
"strconv"
Expand Down Expand Up @@ -67,9 +68,18 @@ func datasourceTencentCloudUserInfoRead(d *schema.ResourceData, meta interface{}

logId = tccommon.GetLogId(ctx)
request := cam.NewGetUserAppIdRequest()
response := cam.NewGetUserAppIdResponse()

ratelimit.Check(request.GetAction())
response, err := client.UseCamClient().GetUserAppId(request)

err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := client.UseCamClient().GetUserAppId(request)
if e != nil {
return tccommon.RetryError(e)
}
response = result
return nil
})

if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
Expand All @@ -80,25 +90,37 @@ func datasourceTencentCloudUserInfoRead(d *schema.ResourceData, meta interface{}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())

if err != nil {
return err
}

result := response.Response

if result == nil {
if response == nil || response.Response == nil {
return fmt.Errorf("get user appid error: empty response")
}

appId := strconv.FormatUint(*result.AppId, 10)
uin := *result.Uin
ownerUin := *result.OwnerUin
var appId, uin, ownerUin string
accountInfoRequest := cam.NewDescribeSubAccountsRequest()
accountInfoResponse := cam.NewDescribeSubAccountsResponse()

if response.Response.AppId != nil {
appId = strconv.FormatUint(*response.Response.AppId, 10)
}
if response.Response.Uin != nil {
uin = *response.Response.Uin
}
if response.Response.OwnerUin != nil {
ownerUin = *response.Response.Uin
}
accountInfoRequest.FilterSubAccountUin = []*uint64{helper.Uint64(helper.StrToUInt64(uin))}
accountInfoResponse, err := client.UseCamClient().DescribeSubAccounts(accountInfoRequest)

err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
accountInfoResult, e := client.UseCamClient().DescribeSubAccounts(accountInfoRequest)
if e != nil {
return tccommon.RetryError(e)
}
accountInfoResponse = accountInfoResult
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read CAM users failed, reason:%s\n", logId, err.Error())
return err
}

subAccounts := accountInfoResponse.Response.SubAccounts
var name string
if len(subAccounts) > 0 {
Expand Down
6 changes: 2 additions & 4 deletions tencentcloud/services/cam/data_source_tc_user_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ func TestAccTencentCloudDataSourceUserInfoBasic(t *testing.T) {
Providers: tcacctest.AccProviders,
Steps: []resource.TestStep{
{
PreConfig: func() { tcacctest.AccStepPreConfigSetTempAKSK(t, tcacctest.ACCOUNT_TYPE_COMMON) },
Config: testAccDataUserInfoBasic,
Config: testAccDataUserInfoBasic,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.tencentcloud_user_info.info", "app_id"),
resource.TestCheckResourceAttrSet("data.tencentcloud_user_info.info", "uin"),
Expand All @@ -33,8 +32,7 @@ func TestAccTencentCloudDataSourceUserInfoSubAccount(t *testing.T) {
Steps: []resource.TestStep{
{
// Need use subaccount aksk
PreConfig: func() { tcacctest.AccStepPreConfigSetTempAKSK(t, tcacctest.ACCOUNT_TYPE_SUB_ACCOUNT) },
Config: testAccDataUserInfoSubAccount,
Config: testAccDataUserInfoSubAccount,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.tencentcloud_user_info.info_sub_account", "app_id"),
resource.TestCheckResourceAttrSet("data.tencentcloud_user_info.info_sub_account", "uin"),
Expand Down
Loading