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
20 changes: 19 additions & 1 deletion tencentcloud/data_source_tc_user_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -94,19 +100,31 @@ func datasourceTencentCloudUserInfoRead(d *schema.ResourceData, meta interface{}
appId := strconv.FormatUint(*result.AppId, 10)
uin := *result.Uin
ownerUin := *result.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
var name string
if len(subAccounts) > 0 {
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
}
Expand Down
29 changes: 26 additions & 3 deletions tencentcloud/data_source_tc_user_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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" {}
`
11 changes: 11 additions & 0 deletions tencentcloud/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/user_info.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down