-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathdata_source_tc_cam_users.go
213 lines (200 loc) · 5.73 KB
/
data_source_tc_cam_users.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cam
import (
"context"
"log"
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"
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudCamUsers() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCamUsersRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of CAM user to be queried.",
},
"remark": {
Type: schema.TypeString,
Optional: true,
Description: "Remark of the CAM user to be queried.",
},
"phone_num": {
Type: schema.TypeString,
Optional: true,
Description: "Phone num of the CAM user to be queried.",
},
"country_code": {
Type: schema.TypeString,
Optional: true,
Description: "Country code of the CAM user to be queried.",
},
"email": {
Type: schema.TypeString,
Optional: true,
Description: "Email of the CAM user to be queried.",
},
"uin": {
Type: schema.TypeInt,
Optional: true,
Description: "Uin of the CAM user to be queried.",
},
"uid": {
Type: schema.TypeInt,
Optional: true,
Description: "Uid of the CAM user to be queried.",
},
"console_login": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicate whether the user can login in.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
"user_list": {
Type: schema.TypeList,
Computed: true,
Description: "A list of CAM users. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of CAM user. Its value equals to `name` argument.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of CAM user.",
},
"remark": {
Type: schema.TypeString,
Computed: true,
Description: "Remark of the CAM user.",
},
"phone_num": {
Type: schema.TypeString,
Computed: true,
Description: "Phone num of the CAM user.",
},
"country_code": {
Type: schema.TypeString,
Computed: true,
Description: "Country code of the CAM user.",
},
"email": {
Type: schema.TypeString,
Computed: true,
Description: "Email of the CAM user.",
},
"uin": {
Type: schema.TypeInt,
Computed: true,
Description: "Uin of the CAM user.",
},
"uid": {
Type: schema.TypeInt,
Computed: true,
Description: "Uid of the CAM user.",
},
"console_login": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicate whether the user can login in.",
},
},
},
},
},
}
}
func dataSourceTencentCloudCamUsersRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_cam_users.read")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
params := make(map[string]interface{})
if v, ok := d.GetOk("name"); ok {
params["name"] = v.(string)
}
if v, ok := d.GetOk("uin"); ok {
params["uin"] = v.(int)
}
if v, ok := d.GetOk("remark"); ok {
params["remark"] = v.(string)
}
if v, ok := d.GetOk("uid"); ok {
params["uid"] = v.(int)
}
if v, ok := d.GetOk("phone_num"); ok {
params["phone_num"] = v.(string)
}
if v, ok := d.GetOk("country_code"); ok {
params["country_code"] = v.(string)
}
if v, ok := d.GetOk("email"); ok {
params["email"] = v.(string)
}
if v, ok := d.GetOkExists("console_login"); ok {
consoleLogin := v.(bool)
if consoleLogin {
params["console_login"] = 1
} else {
params["console_login"] = 0
}
}
camService := CamService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
var users []*cam.SubAccountInfo
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
results, e := camService.DescribeUsersByFilter(ctx, params)
if e != nil {
return tccommon.RetryError(e)
}
users = results
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read CAM users failed, reason:%s\n", logId, err.Error())
return err
}
userList := make([]map[string]interface{}, 0, len(users))
ids := make([]string, 0, len(users))
for _, user := range users {
mapping := map[string]interface{}{
"uin": int(*user.Uin),
"uid": int(*user.Uid),
"name": *user.Name,
"remark": *user.Remark,
"phone_num": *user.PhoneNum,
"country_code": *user.CountryCode,
"email": *user.Email,
"user_id": *user.Name,
}
if int(*user.ConsoleLogin) == 1 {
mapping["console_login"] = true
} else {
mapping["console_login"] = false
}
userList = append(userList, mapping)
ids = append(ids, *user.Name)
}
d.SetId(helper.DataResourceIdsHash(ids))
if e := d.Set("user_list", userList); e != nil {
log.Printf("[CRITAL]%s provider set CAM user list fail, reason:%s\n", logId, e.Error())
return e
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), userList); e != nil {
return e
}
}
return nil
}