Skip to content

Commit c455915

Browse files
authored
Feat/add cam user tag (#1106)
* add tag * add cam user tag
1 parent 2aa0867 commit c455915

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

tencentcloud/resource_tc_cam_role.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ resource "tencentcloud_cam_role" "foo" {
2424
EOF
2525
description = "test"
2626
console_login = true
27+
tags = {
28+
test = "tf-cam-role",
29+
}
2730
}
2831
```
2932
@@ -134,6 +137,11 @@ func resourceTencentCloudCamRole() *schema.Resource {
134137
Computed: true,
135138
Description: "The last update time of the CAM role.",
136139
},
140+
"tags": {
141+
Type: schema.TypeMap,
142+
Optional: true,
143+
Description: "A list of tags used to associate different resources.",
144+
},
137145
},
138146
}
139147
}
@@ -216,6 +224,15 @@ func resourceTencentCloudCamRoleCreate(d *schema.ResourceData, meta interface{})
216224
log.Printf("[CRITAL]%s read CAM role failed, reason:%s\n", logId, err.Error())
217225
return err
218226
}
227+
228+
//modify tags
229+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
230+
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
231+
resourceName := BuildTagResourceName("cam", "role", "", roleId)
232+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
233+
return err
234+
}
235+
}
219236
time.Sleep(10 * time.Second)
220237
return resourceTencentCloudCamRoleRead(d, meta)
221238
}
@@ -267,13 +284,23 @@ func resourceTencentCloudCamRoleRead(d *schema.ResourceData, meta interface{}) e
267284
} else {
268285
_ = d.Set("console_login", false)
269286
}
287+
288+
//tags
289+
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
290+
tags, err := tagService.DescribeResourceTags(ctx, "cam", "role", "", roleId)
291+
if err != nil {
292+
return err
293+
}
294+
_ = d.Set("tags", tags)
295+
270296
return nil
271297
}
272298

273299
func resourceTencentCloudCamRoleUpdate(d *schema.ResourceData, meta interface{}) error {
274300
defer logElapsed("resource.tencentcloud_cam_role.update")()
275301

276302
logId := getLogId(contextNil)
303+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
277304

278305
d.Partial(true)
279306

@@ -343,6 +370,21 @@ func resourceTencentCloudCamRoleUpdate(d *schema.ResourceData, meta interface{})
343370

344371
d.Partial(false)
345372

373+
//tag
374+
if d.HasChange("tags") {
375+
oldInterface, newInterface := d.GetChange("tags")
376+
replaceTags, deleteTags := diffTags(oldInterface.(map[string]interface{}), newInterface.(map[string]interface{}))
377+
tagService := TagService{
378+
client: meta.(*TencentCloudClient).apiV3Conn,
379+
}
380+
resourceName := BuildTagResourceName("cam", "role", "", roleId)
381+
err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags)
382+
if err != nil {
383+
return err
384+
}
385+
d.SetPartial("tags")
386+
}
387+
346388
return resourceTencentCloudCamRoleRead(d, meta)
347389
}
348390

tencentcloud/resource_tc_cam_user.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ resource "tencentcloud_cam_user" "foo" {
1515
email = "hello@test.com"
1616
country_code = "86"
1717
force_delete = true
18+
tags = {
19+
test = "tf-cam-user",
20+
}
1821
}
1922
```
2023
@@ -135,6 +138,11 @@ func resourceTencentCloudCamUser() *schema.Resource {
135138
Computed: true,
136139
Description: "ID of the CAM user.",
137140
},
141+
"tags": {
142+
Type: schema.TypeMap,
143+
Optional: true,
144+
Description: "A list of tags used to associate different resources.",
145+
},
138146
},
139147
}
140148
}
@@ -239,6 +247,16 @@ func resourceTencentCloudCamUserCreate(d *schema.ResourceData, meta interface{})
239247
log.Printf("[CRITAL]%s wait for CAM user ready failed, reason:%s\n", logId, err.Error())
240248
return err
241249
}
250+
251+
//modify tags
252+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
253+
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
254+
region := meta.(*TencentCloudClient).apiV3Conn.Region
255+
resourceName := BuildTagResourceName("cam", "uin", region, helper.UInt64ToStr(*response.Response.Uin))
256+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
257+
return err
258+
}
259+
}
242260
time.Sleep(10 * time.Second)
243261
return resourceTencentCloudCamUserRead(d, meta)
244262
}
@@ -292,13 +310,23 @@ func resourceTencentCloudCamUserRead(d *schema.ResourceData, meta interface{}) e
292310
_ = d.Set("console_login", true)
293311
}
294312

313+
//tags
314+
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
315+
region := meta.(*TencentCloudClient).apiV3Conn.Region
316+
tags, err := tagService.DescribeResourceTags(ctx, "cam", "uin", region, helper.UInt64ToStr(*instance.Response.Uin))
317+
if err != nil {
318+
return err
319+
}
320+
_ = d.Set("tags", tags)
321+
295322
return nil
296323
}
297324

298325
func resourceTencentCloudCamUserUpdate(d *schema.ResourceData, meta interface{}) error {
299326
defer logElapsed("resource.tencentcloud_cam_user.update")()
300327

301328
logId := getLogId(contextNil)
329+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
302330

303331
userId := d.Id()
304332

@@ -362,6 +390,45 @@ func resourceTencentCloudCamUserUpdate(d *schema.ResourceData, meta interface{})
362390
}
363391
}
364392

393+
//tag
394+
if d.HasChange("tags") {
395+
camService := CamService{
396+
client: meta.(*TencentCloudClient).apiV3Conn,
397+
}
398+
399+
var instance *cam.GetUserResponse
400+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
401+
result, e := camService.DescribeUserById(ctx, userId)
402+
if e != nil {
403+
return retryError(e)
404+
}
405+
instance = result
406+
return nil
407+
})
408+
if err != nil {
409+
log.Printf("[CRITAL]%s read CAM user failed, reason:%s\n", logId, err.Error())
410+
return err
411+
}
412+
413+
if instance == nil || instance.Response == nil || instance.Response.Uid == nil {
414+
d.SetId("")
415+
return nil
416+
}
417+
418+
oldInterface, newInterface := d.GetChange("tags")
419+
replaceTags, deleteTags := diffTags(oldInterface.(map[string]interface{}), newInterface.(map[string]interface{}))
420+
tagService := TagService{
421+
client: meta.(*TencentCloudClient).apiV3Conn,
422+
}
423+
region := meta.(*TencentCloudClient).apiV3Conn.Region
424+
resourceName := BuildTagResourceName("cam", "uin", region, helper.UInt64ToStr(*instance.Response.Uin))
425+
err = tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags)
426+
if err != nil {
427+
return err
428+
}
429+
d.SetPartial("tags")
430+
}
431+
365432
return nil
366433
}
367434

website/docs/r/cam_role.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ resource "tencentcloud_cam_role" "foo" {
3434
EOF
3535
description = "test"
3636
console_login = true
37+
tags = {
38+
test = "tf-cam-role",
39+
}
3740
}
3841
```
3942

@@ -69,6 +72,7 @@ The following arguments are supported:
6972
* `name` - (Required, ForceNew) Name of CAM role.
7073
* `console_login` - (Optional, ForceNew) Indicates whether the CAM role can login or not.
7174
* `description` - (Optional) Description of the CAM role.
75+
* `tags` - (Optional) A list of tags used to associate different resources.
7276

7377
## Attributes Reference
7478

website/docs/r/cam_user.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ resource "tencentcloud_cam_user" "foo" {
2525
email = "hello@test.com"
2626
country_code = "86"
2727
force_delete = true
28+
tags = {
29+
test = "tf-cam-user",
30+
}
2831
}
2932
```
3033

@@ -41,6 +44,7 @@ The following arguments are supported:
4144
* `password` - (Optional) The password of the CAM user. Password should be at least 8 characters and no more than 32 characters, includes uppercase letters, lowercase letters, numbers and special characters. Only required when `console_login` is true. If not set, a random password will be automatically generated.
4245
* `phone_num` - (Optional) Phone number of the CAM user.
4346
* `remark` - (Optional) Remark of the CAM user.
47+
* `tags` - (Optional) A list of tags used to associate different resources.
4448
* `use_api` - (Optional) Indicate whether to generate the API secret key or not.
4549

4650
## Attributes Reference

0 commit comments

Comments
 (0)