Skip to content

Commit f4471f6

Browse files
authored
add session duration (#2320)
* add session duration * add session duration
1 parent e05851d commit f4471f6

File tree

3 files changed

+69
-46
lines changed

3 files changed

+69
-46
lines changed

.changelog/2320.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_cam_role: Support set `session_duration`.
3+
```

tencentcloud/resource_tc_cam_role.go

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,39 @@ Example Usage
66
Create normally
77
88
```hcl
9+
data "tencentcloud_user_info" "info" {}
10+
911
locals {
10-
uin = data.tencentcloud_user_info.info.uin
12+
uin = data.tencentcloud_user_info.info.owner_uin
1113
}
1214
13-
data "tencentcloud_user_info" "info" {}
15+
output "uin" {
16+
value = local.uin
17+
}
1418
1519
resource "tencentcloud_cam_role" "foo" {
16-
name = "cam-role-test"
17-
document = <<EOF
18-
{
19-
"version": "2.0",
20-
"statement": [
20+
name = "cam-role-test"
21+
document = jsonencode(
2122
{
22-
"action": [
23-
"name/sts:AssumeRole"
24-
],
25-
"effect": "allow",
26-
"principal": {
27-
"qcs": [
28-
"qcs::cam::uin/${local.uin}:uin/${local.uin}"
29-
]
30-
}
23+
statement = [
24+
{
25+
action = "name/sts:AssumeRole"
26+
effect = "allow"
27+
principal = {
28+
qcs = [
29+
"qcs::cam::uin/${local.uin}:root",
30+
]
31+
}
32+
},
33+
]
34+
version = "2.0"
3135
}
32-
]
33-
}
34-
EOF
35-
description = "test"
36-
console_login = true
37-
tags = {
38-
test = "tf-cam-role",
36+
)
37+
console_login = true
38+
description = "test"
39+
session_duration = 7200
40+
tags = {
41+
test = "tf-cam-role"
3942
}
4043
}
4144
```
@@ -151,6 +154,11 @@ func resourceTencentCloudCamRole() *schema.Resource {
151154
Optional: true,
152155
Description: "Indicates whether the CAM role can login or not.",
153156
},
157+
"session_duration": {
158+
Type: schema.TypeInt,
159+
Optional: true,
160+
Description: "The maximum validity period of the temporary key for creating a role.",
161+
},
154162
"create_time": {
155163
Type: schema.TypeString,
156164
Computed: true,
@@ -199,6 +207,9 @@ func resourceTencentCloudCamRoleCreate(d *schema.ResourceData, meta interface{})
199207
}
200208
request.ConsoleLogin = &loginInt
201209
}
210+
if v, ok := d.GetOkExists("session_duration"); ok {
211+
request.SessionDuration = helper.IntUint64(v.(int))
212+
}
202213

203214
var response *cam.CreateRoleResponse
204215
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
@@ -257,7 +268,7 @@ func resourceTencentCloudCamRoleCreate(d *schema.ResourceData, meta interface{})
257268
return err
258269
}
259270
}
260-
time.Sleep(10 * time.Second)
271+
time.Sleep(5 * time.Second)
261272
return resourceTencentCloudCamRoleRead(d, meta)
262273
}
263274

@@ -293,6 +304,7 @@ func resourceTencentCloudCamRoleRead(d *schema.ResourceData, meta interface{}) e
293304

294305
_ = d.Set("name", instance.RoleName)
295306
_ = d.Set("document", instance.PolicyDocument)
307+
_ = d.Set("session_duration", instance.SessionDuration)
296308
_ = d.Set("create_time", instance.AddTime)
297309
_ = d.Set("update_time", instance.UpdateTime)
298310
if instance.Description != nil {
@@ -441,6 +453,10 @@ func resourceTencentCloudCamRoleUpdate(d *schema.ResourceData, meta interface{})
441453
return err
442454
}
443455
}
456+
457+
if d.HasChange("session_duration") {
458+
return fmt.Errorf("`session_duration` do not support change now.")
459+
}
444460
return resourceTencentCloudCamRoleRead(d, meta)
445461
}
446462

website/docs/r/cam_role.html.markdown

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,39 @@ Provides a resource to create a CAM role.
1616
### Create normally
1717

1818
```hcl
19+
data "tencentcloud_user_info" "info" {}
20+
1921
locals {
20-
uin = data.tencentcloud_user_info.info.uin
22+
uin = data.tencentcloud_user_info.info.owner_uin
2123
}
2224
23-
data "tencentcloud_user_info" "info" {}
25+
output "uin" {
26+
value = local.uin
27+
}
2428
2529
resource "tencentcloud_cam_role" "foo" {
26-
name = "cam-role-test"
27-
document = <<EOF
28-
{
29-
"version": "2.0",
30-
"statement": [
30+
name = "cam-role-test"
31+
document = jsonencode(
3132
{
32-
"action": [
33-
"name/sts:AssumeRole"
34-
],
35-
"effect": "allow",
36-
"principal": {
37-
"qcs": [
38-
"qcs::cam::uin/${local.uin}:uin/${local.uin}"
39-
]
40-
}
33+
statement = [
34+
{
35+
action = "name/sts:AssumeRole"
36+
effect = "allow"
37+
principal = {
38+
qcs = [
39+
"qcs::cam::uin/${local.uin}:root",
40+
]
41+
}
42+
},
43+
]
44+
version = "2.0"
4145
}
42-
]
43-
}
44-
EOF
45-
description = "test"
46-
console_login = true
46+
)
47+
console_login = true
48+
description = "test"
49+
session_duration = 7200
4750
tags = {
48-
test = "tf-cam-role",
51+
test = "tf-cam-role"
4952
}
5053
}
5154
```
@@ -97,6 +100,7 @@ The following arguments are supported:
97100
* `name` - (Required, String, ForceNew) Name of CAM role.
98101
* `console_login` - (Optional, Bool) Indicates whether the CAM role can login or not.
99102
* `description` - (Optional, String) Description of the CAM role.
103+
* `session_duration` - (Optional, Int) The maximum validity period of the temporary key for creating a role.
100104
* `tags` - (Optional, Map) A list of tags used to associate different resources.
101105

102106
## Attributes Reference

0 commit comments

Comments
 (0)