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
42 changes: 42 additions & 0 deletions tencentcloud/resource_tc_cam_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ resource "tencentcloud_cam_role" "foo" {
EOF
description = "test"
console_login = true
tags = {
test = "tf-cam-role",
}
}
```

Expand Down Expand Up @@ -134,6 +137,11 @@ func resourceTencentCloudCamRole() *schema.Resource {
Computed: true,
Description: "The last update time of the CAM role.",
},
"tags": {
Type: schema.TypeMap,
Optional: true,
Description: "A list of tags used to associate different resources.",
},
},
}
}
Expand Down Expand Up @@ -216,6 +224,15 @@ func resourceTencentCloudCamRoleCreate(d *schema.ResourceData, meta interface{})
log.Printf("[CRITAL]%s read CAM role failed, reason:%s\n", logId, err.Error())
return err
}

//modify tags
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
resourceName := BuildTagResourceName("cam", "role", "", roleId)
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
return err
}
}
time.Sleep(10 * time.Second)
return resourceTencentCloudCamRoleRead(d, meta)
}
Expand Down Expand Up @@ -267,13 +284,23 @@ func resourceTencentCloudCamRoleRead(d *schema.ResourceData, meta interface{}) e
} else {
_ = d.Set("console_login", false)
}

//tags
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
tags, err := tagService.DescribeResourceTags(ctx, "cam", "role", "", roleId)
if err != nil {
return err
}
_ = d.Set("tags", tags)

return nil
}

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

logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)

d.Partial(true)

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

d.Partial(false)

//tag
if d.HasChange("tags") {
oldInterface, newInterface := d.GetChange("tags")
replaceTags, deleteTags := diffTags(oldInterface.(map[string]interface{}), newInterface.(map[string]interface{}))
tagService := TagService{
client: meta.(*TencentCloudClient).apiV3Conn,
}
resourceName := BuildTagResourceName("cam", "role", "", roleId)
err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags)
if err != nil {
return err
}
d.SetPartial("tags")
}

return resourceTencentCloudCamRoleRead(d, meta)
}

Expand Down
67 changes: 67 additions & 0 deletions tencentcloud/resource_tc_cam_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ resource "tencentcloud_cam_user" "foo" {
email = "hello@test.com"
country_code = "86"
force_delete = true
tags = {
test = "tf-cam-user",
}
}
```

Expand Down Expand Up @@ -135,6 +138,11 @@ func resourceTencentCloudCamUser() *schema.Resource {
Computed: true,
Description: "ID of the CAM user.",
},
"tags": {
Type: schema.TypeMap,
Optional: true,
Description: "A list of tags used to associate different resources.",
},
},
}
}
Expand Down Expand Up @@ -239,6 +247,16 @@ func resourceTencentCloudCamUserCreate(d *schema.ResourceData, meta interface{})
log.Printf("[CRITAL]%s wait for CAM user ready failed, reason:%s\n", logId, err.Error())
return err
}

//modify tags
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
region := meta.(*TencentCloudClient).apiV3Conn.Region
resourceName := BuildTagResourceName("cam", "uin", region, helper.UInt64ToStr(*response.Response.Uin))
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
return err
}
}
time.Sleep(10 * time.Second)
return resourceTencentCloudCamUserRead(d, meta)
}
Expand Down Expand Up @@ -292,13 +310,23 @@ func resourceTencentCloudCamUserRead(d *schema.ResourceData, meta interface{}) e
_ = d.Set("console_login", true)
}

//tags
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
region := meta.(*TencentCloudClient).apiV3Conn.Region
tags, err := tagService.DescribeResourceTags(ctx, "cam", "uin", region, helper.UInt64ToStr(*instance.Response.Uin))
if err != nil {
return err
}
_ = d.Set("tags", tags)

return nil
}

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

logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)

userId := d.Id()

Expand Down Expand Up @@ -362,6 +390,45 @@ func resourceTencentCloudCamUserUpdate(d *schema.ResourceData, meta interface{})
}
}

//tag
if d.HasChange("tags") {
camService := CamService{
client: meta.(*TencentCloudClient).apiV3Conn,
}

var instance *cam.GetUserResponse
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
result, e := camService.DescribeUserById(ctx, userId)
if e != nil {
return retryError(e)
}
instance = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read CAM user failed, reason:%s\n", logId, err.Error())
return err
}

if instance == nil || instance.Response == nil || instance.Response.Uid == nil {
d.SetId("")
return nil
}

oldInterface, newInterface := d.GetChange("tags")
replaceTags, deleteTags := diffTags(oldInterface.(map[string]interface{}), newInterface.(map[string]interface{}))
tagService := TagService{
client: meta.(*TencentCloudClient).apiV3Conn,
}
region := meta.(*TencentCloudClient).apiV3Conn.Region
resourceName := BuildTagResourceName("cam", "uin", region, helper.UInt64ToStr(*instance.Response.Uin))
err = tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags)
if err != nil {
return err
}
d.SetPartial("tags")
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/cam_role.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ resource "tencentcloud_cam_role" "foo" {
EOF
description = "test"
console_login = true
tags = {
test = "tf-cam-role",
}
}
```

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

## Attributes Reference

Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/cam_user.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ resource "tencentcloud_cam_user" "foo" {
email = "hello@test.com"
country_code = "86"
force_delete = true
tags = {
test = "tf-cam-user",
}
}
```

Expand All @@ -41,6 +44,7 @@ The following arguments are supported:
* `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.
* `phone_num` - (Optional) Phone number of the CAM user.
* `remark` - (Optional) Remark of the CAM user.
* `tags` - (Optional) A list of tags used to associate different resources.
* `use_api` - (Optional) Indicate whether to generate the API secret key or not.

## Attributes Reference
Expand Down