Skip to content

Commit 1c7166d

Browse files
WeiMengXSWeiMengXS
andauthored
feat/cam-role-permission-boundary (#2226)
* feat: changelog * feat: changelog --------- Co-authored-by: WeiMengXS <nickcchen@tencent.com>
1 parent 07ad91f commit 1c7166d

7 files changed

+335
-0
lines changed

.changelog/2226.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_cam_role_permission_boundary_attachment
3+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ Cloud Access Management(CAM)
247247
tencentcloud_cam_tag_role_attachment
248248
tencentcloud_cam_policy_version
249249
tencentcloud_cam_user_permission_boundary_attachment
250+
tencentcloud_cam_role_permission_boundary_attachment
250251
251252
Customer Identity and Access Management(CIAM)
252253
Resource
@@ -2856,6 +2857,7 @@ func Provider() *schema.Provider {
28562857
"tencentcloud_cam_tag_role_attachment": resourceTencentCloudCamTagRoleAttachment(),
28572858
"tencentcloud_cam_policy_version": resourceTencentCloudCamPolicyVersion(),
28582859
"tencentcloud_cam_user_permission_boundary_attachment": resourceTencentCloudCamUserPermissionBoundaryAttachment(),
2860+
"tencentcloud_cam_role_permission_boundary_attachment": resourceTencentCloudCamRolePermissionBoundaryAttachment(),
28592861
"tencentcloud_ciam_user_group": resourceTencentCloudCiamUserGroup(),
28602862
"tencentcloud_ciam_user_store": resourceTencentCloudCiamUserStore(),
28612863
"tencentcloud_scf_function": resourceTencentCloudScfFunction(),
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
Provides a resource to create a cam role_permission_boundary_attachment
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_cam_role_permission_boundary_attachment" "role_permission_boundary_attachment" {
8+
policy_id = 1
9+
role_name = "test-cam-tag"
10+
}
11+
```
12+
13+
Import
14+
15+
cam role_permission_boundary_attachment can be imported using the id, e.g.
16+
17+
```
18+
terraform import tencentcloud_cam_role_permission_boundary_attachment.role_permission_boundary_attachment role_permission_boundary_attachment_id
19+
```
20+
*/
21+
package tencentcloud
22+
23+
import (
24+
"context"
25+
"fmt"
26+
"log"
27+
"strings"
28+
29+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
31+
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
32+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
33+
)
34+
35+
func resourceTencentCloudCamRolePermissionBoundaryAttachment() *schema.Resource {
36+
return &schema.Resource{
37+
Create: resourceTencentCloudCamRolePermissionBoundaryAttachmentCreate,
38+
Read: resourceTencentCloudCamRolePermissionBoundaryAttachmentRead,
39+
Delete: resourceTencentCloudCamRolePermissionBoundaryAttachmentDelete,
40+
Importer: &schema.ResourceImporter{
41+
State: schema.ImportStatePassthrough,
42+
},
43+
Schema: map[string]*schema.Schema{
44+
"policy_id": {
45+
Required: true,
46+
ForceNew: true,
47+
Type: schema.TypeInt,
48+
Description: "Role ID.",
49+
},
50+
51+
"role_id": {
52+
Optional: true,
53+
Computed: true,
54+
ForceNew: true,
55+
Type: schema.TypeString,
56+
Description: "Role ID (at least one should be filled in with the role name).",
57+
},
58+
59+
"role_name": {
60+
Optional: true,
61+
ForceNew: true,
62+
Type: schema.TypeString,
63+
Description: "Role name (at least one should be filled in with the role ID).",
64+
},
65+
},
66+
}
67+
}
68+
69+
func resourceTencentCloudCamRolePermissionBoundaryAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
70+
defer logElapsed("resource.tencentcloud_cam_role_permission_boundary_attachment.create")()
71+
defer inconsistentCheck(d, meta)()
72+
73+
logId := getLogId(contextNil)
74+
75+
var (
76+
request = cam.NewPutRolePermissionsBoundaryRequest()
77+
policyId string
78+
roleId string
79+
roleName string
80+
)
81+
if v, ok := d.GetOkExists("policy_id"); ok {
82+
policyId = helper.IntToStr(v.(int))
83+
request.PolicyId = helper.IntInt64(v.(int))
84+
}
85+
86+
if v, ok := d.GetOk("role_id"); ok {
87+
roleId = v.(string)
88+
request.RoleId = helper.String(v.(string))
89+
}
90+
91+
if v, ok := d.GetOk("role_name"); ok {
92+
roleName = v.(string)
93+
request.RoleName = helper.String(v.(string))
94+
}
95+
96+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
97+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCamClient().PutRolePermissionsBoundary(request)
98+
if e != nil {
99+
return retryError(e)
100+
} else {
101+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
102+
}
103+
return nil
104+
})
105+
if err != nil {
106+
log.Printf("[CRITAL]%s create cam RolePermissionBoundaryAttachment failed, reason:%+v", logId, err)
107+
return err
108+
}
109+
110+
d.SetId(policyId + FILED_SP + roleId + FILED_SP + roleName)
111+
112+
return resourceTencentCloudCamRolePermissionBoundaryAttachmentRead(d, meta)
113+
}
114+
115+
func resourceTencentCloudCamRolePermissionBoundaryAttachmentRead(d *schema.ResourceData, meta interface{}) error {
116+
defer logElapsed("resource.tencentcloud_cam_role_permission_boundary_attachment.read")()
117+
defer inconsistentCheck(d, meta)()
118+
119+
logId := getLogId(contextNil)
120+
121+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
122+
123+
service := CamService{client: meta.(*TencentCloudClient).apiV3Conn}
124+
125+
idSplit := strings.Split(d.Id(), FILED_SP)
126+
if len(idSplit) != 3 {
127+
return fmt.Errorf("id is broken,%s", d.Id())
128+
}
129+
policyId := idSplit[0]
130+
roleId := idSplit[1]
131+
roleName := idSplit[2]
132+
133+
if roleId == "" {
134+
roleInfo, err := service.DescribeCamTagRoleById(ctx, roleName, roleId)
135+
if err != nil {
136+
return err
137+
}
138+
if roleInfo == nil {
139+
return fmt.Errorf("role info is null")
140+
}
141+
roleId = *roleInfo.RoleId
142+
}
143+
144+
RolePermissionBoundaryAttachment, err := service.DescribeCamRolePermissionBoundaryAttachmentById(ctx, roleId, policyId)
145+
if err != nil {
146+
return err
147+
}
148+
149+
if RolePermissionBoundaryAttachment == nil {
150+
d.SetId("")
151+
log.Printf("[WARN]%s resource `CamRolePermissionBoundaryAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
152+
return nil
153+
}
154+
155+
if RolePermissionBoundaryAttachment.PolicyId != nil {
156+
_ = d.Set("policy_id", RolePermissionBoundaryAttachment.PolicyId)
157+
}
158+
159+
_ = d.Set("role_id", roleId)
160+
_ = d.Set("role_name", roleName)
161+
162+
return nil
163+
}
164+
165+
func resourceTencentCloudCamRolePermissionBoundaryAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
166+
defer logElapsed("resource.tencentcloud_cam_role_permission_boundary_attachment.delete")()
167+
defer inconsistentCheck(d, meta)()
168+
169+
logId := getLogId(contextNil)
170+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
171+
172+
service := CamService{client: meta.(*TencentCloudClient).apiV3Conn}
173+
idSplit := strings.Split(d.Id(), FILED_SP)
174+
if len(idSplit) != 3 {
175+
return fmt.Errorf("id is broken,%s", d.Id())
176+
}
177+
roleId := idSplit[1]
178+
roleName := idSplit[2]
179+
180+
if err := service.DeleteCamRolePermissionBoundaryAttachmentById(ctx, roleId, roleName); err != nil {
181+
return err
182+
}
183+
184+
return nil
185+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCamRolePermissionBoundaryAttachmentResource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccCamRolePermissionBoundaryAttachment,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_cam_role_permission_boundary_attachment.role_permission_boundary_attachment", "id"),
20+
resource.TestCheckResourceAttr("tencentcloud_cam_role_permission_boundary_attachment.role_permission_boundary_attachment", "policy_id", "1"),
21+
resource.TestCheckResourceAttr("tencentcloud_cam_role_permission_boundary_attachment.role_permission_boundary_attachment", "role_name", "test-cam-tag")),
22+
},
23+
{
24+
ResourceName: "tencentcloud_cam_role_permission_boundary_attachment.role_permission_boundary_attachment",
25+
ImportState: true,
26+
ImportStateVerify: true,
27+
},
28+
},
29+
})
30+
}
31+
32+
const testAccCamRolePermissionBoundaryAttachment = `
33+
34+
resource "tencentcloud_cam_role_permission_boundary_attachment" "role_permission_boundary_attachment" {
35+
policy_id = 1
36+
role_name = "test-cam-tag"
37+
}
38+
39+
`

tencentcloud/service_tencentcloud_cam.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,3 +1719,60 @@ func (me *CamService) DeleteCamTagRoleById(ctx context.Context, roleName, roleId
17191719

17201720
return
17211721
}
1722+
1723+
func (me *CamService) DescribeCamRolePermissionBoundaryAttachmentById(ctx context.Context, roleId string, policyId string) (RolePermissionBoundaryAttachment *cam.GetRolePermissionBoundaryResponseParams, errRet error) {
1724+
logId := getLogId(ctx)
1725+
1726+
request := cam.NewGetRolePermissionBoundaryRequest()
1727+
request.RoleId = &roleId
1728+
1729+
defer func() {
1730+
if errRet != nil {
1731+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
1732+
}
1733+
}()
1734+
1735+
ratelimit.Check(request.GetAction())
1736+
1737+
response, err := me.client.UseCamClient().GetRolePermissionBoundary(request)
1738+
if err != nil {
1739+
errRet = err
1740+
return
1741+
}
1742+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
1743+
if response == nil || response.Response == nil {
1744+
return
1745+
}
1746+
if *response.Response.PolicyId != helper.StrToInt64(policyId) {
1747+
return
1748+
}
1749+
RolePermissionBoundaryAttachment = response.Response
1750+
return
1751+
}
1752+
1753+
func (me *CamService) DeleteCamRolePermissionBoundaryAttachmentById(ctx context.Context, roleId string, roleName string) (errRet error) {
1754+
logId := getLogId(ctx)
1755+
1756+
request := cam.NewDeleteRolePermissionsBoundaryRequest()
1757+
if roleId == "" {
1758+
request.RoleName = &roleName
1759+
} else {
1760+
request.RoleId = &roleId
1761+
}
1762+
defer func() {
1763+
if errRet != nil {
1764+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
1765+
}
1766+
}()
1767+
1768+
ratelimit.Check(request.GetAction())
1769+
1770+
response, err := me.client.UseCamClient().DeleteRolePermissionsBoundary(request)
1771+
if err != nil {
1772+
errRet = err
1773+
return
1774+
}
1775+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
1776+
1777+
return
1778+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
subcategory: "Cloud Access Management(CAM)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_cam_role_permission_boundary_attachment"
5+
sidebar_current: "docs-tencentcloud-resource-cam_role_permission_boundary_attachment"
6+
description: |-
7+
Provides a resource to create a cam role_permission_boundary_attachment
8+
---
9+
10+
# tencentcloud_cam_role_permission_boundary_attachment
11+
12+
Provides a resource to create a cam role_permission_boundary_attachment
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_cam_role_permission_boundary_attachment" "role_permission_boundary_attachment" {
18+
policy_id = 1
19+
role_name = "test-cam-tag"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `policy_id` - (Required, Int, ForceNew) Role ID.
28+
* `role_id` - (Optional, String, ForceNew) Role ID (at least one should be filled in with the role name).
29+
* `role_name` - (Optional, String, ForceNew) Role name (at least one should be filled in with the role ID).
30+
31+
## Attributes Reference
32+
33+
In addition to all arguments above, the following attributes are exported:
34+
35+
* `id` - ID of the resource.
36+
37+
38+
39+
## Import
40+
41+
cam role_permission_boundary_attachment can be imported using the id, e.g.
42+
43+
```
44+
terraform import tencentcloud_cam_role_permission_boundary_attachment.role_permission_boundary_attachment role_permission_boundary_attachment_id
45+
```
46+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@
553553
<li>
554554
<a href="/docs/providers/tencentcloud/r/cam_role_by_name.html">tencentcloud_cam_role_by_name</a>
555555
</li>
556+
<li>
557+
<a href="/docs/providers/tencentcloud/r/cam_role_permission_boundary_attachment.html">tencentcloud_cam_role_permission_boundary_attachment</a>
558+
</li>
556559
<li>
557560
<a href="/docs/providers/tencentcloud/r/cam_role_policy_attachment.html">tencentcloud_cam_role_policy_attachment</a>
558561
</li>

0 commit comments

Comments
 (0)