/
data_source_tc_user_info.go
127 lines (104 loc) · 3.35 KB
/
data_source_tc_user_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package cam
import (
"context"
"fmt"
"log"
"math/rand"
"strconv"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
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/v2/helper/schema"
)
func DataSourceTencentCloudUserInfo() *schema.Resource {
return &schema.Resource{
Read: datasourceTencentCloudUserInfoRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"app_id": {
Type: schema.TypeString,
Computed: true,
Description: "Current account App ID.",
},
"uin": {
Type: schema.TypeString,
Computed: true,
Description: "Current account UIN.",
},
"owner_uin": {
Type: schema.TypeString,
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,
Description: "Used for save results.",
},
},
}
}
func datasourceTencentCloudUserInfoRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("datasource.tencentcloud_user_info.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
client := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
logId = tccommon.GetLogId(ctx)
request := cam.NewGetUserAppIdRequest()
ratelimit.Check(request.GetAction())
response, err := client.UseCamClient().GetUserAppId(request)
if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
logId, request.GetAction(), request.ToJsonString(), 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 err != nil {
return err
}
result := response.Response
if result == nil {
return fmt.Errorf("get user appid error: empty response")
}
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 := tccommon.WriteToFile(output.(string), map[string]interface{}{
"app_id": appId,
"uin": uin,
"ownerUin": ownerUin,
"name": name,
}); e != nil {
return e
}
}
return nil
}