From 39293f88afb646feac41518aad8ce959c10cdc07 Mon Sep 17 00:00:00 2001 From: mikatong Date: Mon, 13 Jun 2022 17:34:52 +0800 Subject: [PATCH 1/3] fix: tencentcloud_user_info result add user name --- tencentcloud/data_source_tc_user_info.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tencentcloud/data_source_tc_user_info.go b/tencentcloud/data_source_tc_user_info.go index e4b9ee31ff..180f738c7a 100644 --- a/tencentcloud/data_source_tc_user_info.go +++ b/tencentcloud/data_source_tc_user_info.go @@ -18,6 +18,7 @@ import ( "strconv" cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -48,6 +49,11 @@ func datasourceTencentCloudUserInfo() *schema.Resource { Computed: true, Description: "Current account OwnerUIN.", }, + "name": { + Type: schema.TypeString, + Computed: true, + Description: "Current account Name. NOTE: only support subaccount.", + }, "result_output_file": { Type: schema.TypeString, Optional: true, @@ -100,7 +106,17 @@ func datasourceTencentCloudUserInfoRead(d *schema.ResourceData, meta interface{} _ = d.Set("app_id", appId) _ = d.Set("uin", uin) _ = d.Set("owner_uin", ownerUin) - + accountInfoRequest := cam.NewDescribeSubAccountsRequest() + accountInfoRequest.FilterSubAccountUin = []*uint64{helper.Uint64(helper.StrToUInt64(uin))} + accountInfoResponse, err := client.UseCamClient().DescribeSubAccounts(accountInfoRequest) + if err != nil { + return err + } + subAccounts := accountInfoResponse.Response.SubAccounts + if len(subAccounts) > 0 { + name := *subAccounts[0].Name + _ = d.Set("name", name) + } output, ok := d.GetOk("result_output_file") if ok && output.(string) != "" { if e := writeToFile(output.(string), map[string]interface{}{ From e4683c96b2c37f791b9e641e00902d1274803911 Mon Sep 17 00:00:00 2001 From: mikatong Date: Mon, 13 Jun 2022 17:38:28 +0800 Subject: [PATCH 2/3] update doc --- website/docs/d/user_info.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/d/user_info.html.markdown b/website/docs/d/user_info.html.markdown index 41ea0f1b9d..8ed63b818e 100644 --- a/website/docs/d/user_info.html.markdown +++ b/website/docs/d/user_info.html.markdown @@ -28,6 +28,7 @@ The following arguments are supported: In addition to all arguments above, the following attributes are exported: * `app_id` - Current account App ID. +* `name` - Current account Name. NOTE: only support subaccount. * `owner_uin` - Current account OwnerUIN. * `uin` - Current account UIN. From f829d23af064417e6a293fd0d1c2a351bac6cde6 Mon Sep 17 00:00:00 2001 From: mikatong Date: Mon, 13 Jun 2022 20:37:33 +0800 Subject: [PATCH 3/3] add testcase --- tencentcloud/data_source_tc_user_info.go | 18 +++++++----- tencentcloud/data_source_tc_user_info_test.go | 29 +++++++++++++++++-- tencentcloud/provider_test.go | 11 +++++++ 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/tencentcloud/data_source_tc_user_info.go b/tencentcloud/data_source_tc_user_info.go index 180f738c7a..21dd796e48 100644 --- a/tencentcloud/data_source_tc_user_info.go +++ b/tencentcloud/data_source_tc_user_info.go @@ -100,12 +100,6 @@ func datasourceTencentCloudUserInfoRead(d *schema.ResourceData, meta interface{} appId := strconv.FormatUint(*result.AppId, 10) uin := *result.Uin ownerUin := *result.OwnerUin - - d.SetId(fmt.Sprintf("user-%s-%s-%d", uin, appId, rand.Intn(10000))) - - _ = d.Set("app_id", appId) - _ = d.Set("uin", uin) - _ = d.Set("owner_uin", ownerUin) accountInfoRequest := cam.NewDescribeSubAccountsRequest() accountInfoRequest.FilterSubAccountUin = []*uint64{helper.Uint64(helper.StrToUInt64(uin))} accountInfoResponse, err := client.UseCamClient().DescribeSubAccounts(accountInfoRequest) @@ -113,16 +107,24 @@ func datasourceTencentCloudUserInfoRead(d *schema.ResourceData, meta interface{} return err } subAccounts := accountInfoResponse.Response.SubAccounts + var name string if len(subAccounts) > 0 { - name := *subAccounts[0].Name - _ = d.Set("name", name) + name = *subAccounts[0].Name } + d.SetId(fmt.Sprintf("user-%s-%s-%d", uin, appId, rand.Intn(10000))) + + _ = d.Set("app_id", appId) + _ = d.Set("uin", uin) + _ = d.Set("owner_uin", ownerUin) + _ = d.Set("name", name) + output, ok := d.GetOk("result_output_file") if ok && output.(string) != "" { if e := writeToFile(output.(string), map[string]interface{}{ "app_id": appId, "uin": uin, "ownerUin": ownerUin, + "name": name, }); e != nil { return e } diff --git a/tencentcloud/data_source_tc_user_info_test.go b/tencentcloud/data_source_tc_user_info_test.go index 4b2513abb8..33e6a74d7f 100644 --- a/tencentcloud/data_source_tc_user_info_test.go +++ b/tencentcloud/data_source_tc_user_info_test.go @@ -6,14 +6,14 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) -func TestAccTencentCloudDataSourceUserInfo(t *testing.T) { +func TestAccTencentCloudDataSourceUserInfoBasic(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataUserInfoBasic, + PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_COMMON) }, + Config: testAccDataUserInfoBasic, Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttrSet("data.tencentcloud_user_info.info", "app_id"), resource.TestCheckResourceAttrSet("data.tencentcloud_user_info.info", "uin"), @@ -24,6 +24,29 @@ func TestAccTencentCloudDataSourceUserInfo(t *testing.T) { }) } +func TestAccTencentCloudDataSourceUserInfoSubAccount(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + // Need use subaccount aksk + PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_SUB_ACCOUNT) }, + 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"), + resource.TestCheckResourceAttrSet("data.tencentcloud_user_info.info_sub_account", "owner_uin"), + resource.TestCheckResourceAttrSet("data.tencentcloud_user_info.info_sub_account", "name"), + ), + }, + }, + }) +} + const testAccDataUserInfoBasic = ` data "tencentcloud_user_info" "info" {} ` +const testAccDataUserInfoSubAccount = ` +data "tencentcloud_user_info" "info_sub_account" {} +` diff --git a/tencentcloud/provider_test.go b/tencentcloud/provider_test.go index 183b62894f..34c011e346 100644 --- a/tencentcloud/provider_test.go +++ b/tencentcloud/provider_test.go @@ -19,6 +19,7 @@ const ( ACCOUNT_TYPE_PREPAY = "PREPAY" ACCOUNT_TYPE_COMMON = "COMMON" ACCOUNT_TYPE_PRIVATE = "PRIVATE" + ACCOUNT_TYPE_SUB_ACCOUNT = "SUB_ACCOUNT" INTERNATION_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_INTERNATION" INTERNATION_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_INTERNATION" PREPAY_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_PREPAY" @@ -27,6 +28,8 @@ const ( PRIVATE_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_PRIVATE" COMMON_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_COMMON" COMMON_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_COMMON" + SUB_ACCOUNT_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_SUB_ACCOUNT" + SUB_ACCOUNT_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_SUB_ACCOUNT" ) func init() { @@ -109,6 +112,14 @@ func testAccPreCheckCommon(t *testing.T, accountType string) { } os.Setenv(PROVIDER_SECRET_ID, secretId) os.Setenv(PROVIDER_SECRET_KEY, secretKey) + case accountType == ACCOUNT_TYPE_SUB_ACCOUNT: + secretId := os.Getenv(SUB_ACCOUNT_PROVIDER_SECRET_ID) + secretKey := os.Getenv(SUB_ACCOUNT_PROVIDER_SECRET_KEY) + if secretId == "" || secretKey == "" { + t.Fatalf("%v and %v must be set for acceptance tests\n", SUB_ACCOUNT_PROVIDER_SECRET_ID, SUB_ACCOUNT_PROVIDER_SECRET_KEY) + } + os.Setenv(PROVIDER_SECRET_ID, secretId) + os.Setenv(PROVIDER_SECRET_KEY, secretKey) default: if v := os.Getenv(PROVIDER_SECRET_ID); v == "" { t.Fatalf("%v must be set for acceptance tests\n", PROVIDER_SECRET_ID)