-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cam permission boundary (#2177)
* feat: cam permission boundary * feat: changelog --------- Co-authored-by: WeiMengXS <[email protected]>
- Loading branch information
Showing
7 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
tencentcloud_cam_user_permission_boundary_attachment | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
tencentcloud/resource_tc_cam_user_permission_boundary_attachment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
Provides a resource to create a cam user_permission_boundary | ||
Example Usage | ||
```hcl | ||
resource "tencentcloud_cam_user_permission_boundary_attachment" "user_permission_boundary" { | ||
target_uin = 100032767426 | ||
policy_id = 151113272 | ||
} | ||
``` | ||
Import | ||
cam user_permission_boundary can be imported using the id, e.g. | ||
``` | ||
terraform import tencentcloud_cam_user_permission_boundary_attachment.user_permission_boundary user_permission_boundary_id | ||
``` | ||
*/ | ||
package tencentcloud | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"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 resourceTencentCloudCamUserPermissionBoundaryAttachment() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceTencentCloudCamUserPermissionBoundaryAttachmentCreate, | ||
Read: resourceTencentCloudCamUserPermissionBoundaryAttachmentRead, | ||
Delete: resourceTencentCloudCamUserPermissionBoundaryAttachmentDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"target_uin": { | ||
Required: true, | ||
ForceNew: true, | ||
Type: schema.TypeInt, | ||
Description: "Sub account Uin.", | ||
}, | ||
|
||
"policy_id": { | ||
Required: true, | ||
ForceNew: true, | ||
Type: schema.TypeInt, | ||
Description: "Policy ID.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceTencentCloudCamUserPermissionBoundaryAttachmentCreate(d *schema.ResourceData, meta interface{}) error { | ||
defer logElapsed("resource.tencentcloud_cam_user_permission_boundary_attachment.create")() | ||
defer inconsistentCheck(d, meta)() | ||
|
||
logId := getLogId(contextNil) | ||
|
||
var ( | ||
request = cam.NewPutUserPermissionsBoundaryRequest() | ||
targetUin string | ||
policyId string | ||
) | ||
if v, ok := d.GetOkExists("target_uin"); ok { | ||
targetUin = helper.IntToStr(v.(int)) | ||
request.TargetUin = helper.IntInt64(v.(int)) | ||
} | ||
|
||
if v, ok := d.GetOkExists("policy_id"); ok { | ||
policyId = helper.IntToStr(v.(int)) | ||
request.PolicyId = helper.IntInt64(v.(int)) | ||
} | ||
|
||
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { | ||
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCamClient().PutUserPermissionsBoundary(request) | ||
if e != nil { | ||
return retryError(e) | ||
} else { | ||
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
log.Printf("[CRITAL]%s create cam UserPermissionBoundary failed, reason:%+v", logId, err) | ||
return err | ||
} | ||
|
||
d.SetId(targetUin + FILED_SP + policyId) | ||
|
||
return resourceTencentCloudCamUserPermissionBoundaryAttachmentRead(d, meta) | ||
} | ||
|
||
func resourceTencentCloudCamUserPermissionBoundaryAttachmentRead(d *schema.ResourceData, meta interface{}) error { | ||
defer logElapsed("resource.tencentcloud_cam_user_permission_boundary_attachment.read")() | ||
defer inconsistentCheck(d, meta)() | ||
|
||
logId := getLogId(contextNil) | ||
|
||
ctx := context.WithValue(context.TODO(), logIdKey, logId) | ||
|
||
service := CamService{client: meta.(*TencentCloudClient).apiV3Conn} | ||
|
||
idSplit := strings.Split(d.Id(), FILED_SP) | ||
if len(idSplit) != 2 { | ||
return fmt.Errorf("id is broken,%s", d.Id()) | ||
} | ||
targetUin := idSplit[0] | ||
|
||
UserPermissionBoundary, err := service.DescribeCamUserPermissionBoundaryById(ctx, targetUin) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if UserPermissionBoundary == nil || UserPermissionBoundary.Response == nil { | ||
d.SetId("") | ||
log.Printf("[WARN]%s resource `CamUserPermissionBoundary` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) | ||
return nil | ||
} | ||
|
||
if UserPermissionBoundary.Response.PolicyId != nil { | ||
_ = d.Set("policy_id", UserPermissionBoundary.Response.PolicyId) | ||
} | ||
return nil | ||
} | ||
|
||
func resourceTencentCloudCamUserPermissionBoundaryAttachmentDelete(d *schema.ResourceData, meta interface{}) error { | ||
defer logElapsed("resource.tencentcloud_cam_user_permission_boundary_attachment.delete")() | ||
defer inconsistentCheck(d, meta)() | ||
|
||
logId := getLogId(contextNil) | ||
ctx := context.WithValue(context.TODO(), logIdKey, logId) | ||
|
||
service := CamService{client: meta.(*TencentCloudClient).apiV3Conn} | ||
idSplit := strings.Split(d.Id(), FILED_SP) | ||
if len(idSplit) != 2 { | ||
return fmt.Errorf("id is broken,%s", d.Id()) | ||
} | ||
targetUin := idSplit[0] | ||
|
||
if err := service.DeleteCamUserPermissionBoundaryById(ctx, targetUin); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
41 changes: 41 additions & 0 deletions
41
tencentcloud/resource_tc_cam_user_permission_boundary_attachment_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package tencentcloud | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccTencentCloudCamUserPermissionBoundaryAttachmentResource_basic(t *testing.T) { | ||
t.Parallel() | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCamUserPermissionBoundary, | ||
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_cam_user_permission_boundary_attachment.user_permission_boundary", "id"), | ||
resource.TestCheckResourceAttr("tencentcloud_cam_user_permission_boundary_attachment.user_permission_boundary", "target_uin", "100032767426"), | ||
resource.TestCheckResourceAttr("tencentcloud_cam_user_permission_boundary_attachment.user_permission_boundary", "policy_id", "151113272"), | ||
), | ||
}, | ||
{ | ||
ResourceName: "tencentcloud_cam_user_permission_boundary_attachment.user_permission_boundary", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"target_uin"}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccCamUserPermissionBoundary = ` | ||
resource "tencentcloud_cam_user_permission_boundary_attachment" "user_permission_boundary" { | ||
target_uin = 100032767426 | ||
policy_id = 151113272 | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
website/docs/r/cam_user_permission_boundary_attachment.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
subcategory: "Cloud Access Management(CAM)" | ||
layout: "tencentcloud" | ||
page_title: "TencentCloud: tencentcloud_cam_user_permission_boundary_attachment" | ||
sidebar_current: "docs-tencentcloud-resource-cam_user_permission_boundary_attachment" | ||
description: |- | ||
Provides a resource to create a cam user_permission_boundary | ||
--- | ||
|
||
# tencentcloud_cam_user_permission_boundary_attachment | ||
|
||
Provides a resource to create a cam user_permission_boundary | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "tencentcloud_cam_user_permission_boundary_attachment" "user_permission_boundary" { | ||
target_uin = 100032767426 | ||
policy_id = 151113272 | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `policy_id` - (Required, Int, ForceNew) Policy ID. | ||
* `target_uin` - (Required, Int, ForceNew) Sub account Uin. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - ID of the resource. | ||
|
||
|
||
|
||
## Import | ||
|
||
cam user_permission_boundary can be imported using the id, e.g. | ||
|
||
``` | ||
terraform import tencentcloud_cam_user_permission_boundary_attachment.user_permission_boundary user_permission_boundary_id | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters