Skip to content

Commit

Permalink
feat: cam permission boundary (#2177)
Browse files Browse the repository at this point in the history
* feat: cam permission boundary

* feat: changelog

---------

Co-authored-by: WeiMengXS <[email protected]>
  • Loading branch information
WeiMengXS and WeiMengXS authored Oct 9, 2023
1 parent f99dc74 commit 9e37bf3
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/2177.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
tencentcloud_cam_user_permission_boundary_attachment
```
2 changes: 2 additions & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ Cloud Access Management(CAM)
tencentcloud_cam_service_linked_role
tencentcloud_cam_mfa_flag
tencentcloud_cam_user_saml_config
tencentcloud_cam_user_permission_boundary_attachment
Customer Identity and Access Management(CIAM)
Resource
Expand Down Expand Up @@ -2654,6 +2655,7 @@ func Provider() *schema.Provider {
"tencentcloud_cam_service_linked_role": resourceTencentCloudCamServiceLinkedRole(),
"tencentcloud_cam_mfa_flag": resourceTencentCloudCamMfaFlag(),
"tencentcloud_cam_user_saml_config": resourceTencentCloudCamUserSamlConfig(),
"tencentcloud_cam_user_permission_boundary_attachment": resourceTencentCloudCamUserPermissionBoundaryAttachment(),
"tencentcloud_ciam_user_group": resourceTencentCloudCiamUserGroup(),
"tencentcloud_ciam_user_store": resourceTencentCloudCiamUserStore(),
"tencentcloud_scf_function": resourceTencentCloudScfFunction(),
Expand Down
153 changes: 153 additions & 0 deletions tencentcloud/resource_tc_cam_user_permission_boundary_attachment.go
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
}
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
}
`
53 changes: 53 additions & 0 deletions tencentcloud/service_tencentcloud_cam.go
Original file line number Diff line number Diff line change
Expand Up @@ -1384,3 +1384,56 @@ func (me *CamService) DescribeCamMfaFlagById(ctx context.Context, id uint64) (lo
actionFlag = response.Response.ActionFlag
return
}

func (me *CamService) DescribeCamUserPermissionBoundaryById(ctx context.Context, targetUin string) (UserPermissionBoundary *cam.GetUserPermissionBoundaryResponse, errRet error) {
logId := getLogId(ctx)

request := cam.NewGetUserPermissionBoundaryRequest()
request.TargetUin = helper.StrToInt64Point(targetUin)

defer func() {
if errRet != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
}
}()

ratelimit.Check(request.GetAction())

response, err := me.client.UseCamClient().GetUserPermissionBoundary(request)
if err != nil {
errRet = err
return
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())

if response == nil {
return
}

UserPermissionBoundary = response
return
}

func (me *CamService) DeleteCamUserPermissionBoundaryById(ctx context.Context, targetUin string) (errRet error) {
logId := getLogId(ctx)

request := cam.NewDeleteUserPermissionsBoundaryRequest()
request.TargetUin = helper.StrToInt64Point(targetUin)

defer func() {
if errRet != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
}
}()

ratelimit.Check(request.GetAction())

response, err := me.client.UseCamClient().DeleteUserPermissionsBoundary(request)
if err != nil {
errRet = err
return
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())

return
}
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
```

3 changes: 3 additions & 0 deletions website/tencentcloud.erb
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@
<li>
<a href="/docs/providers/tencentcloud/r/cam_user.html">tencentcloud_cam_user</a>
</li>
<li>
<a href="/docs/providers/tencentcloud/r/cam_user_permission_boundary_attachment.html">tencentcloud_cam_user_permission_boundary_attachment</a>
</li>
<li>
<a href="/docs/providers/tencentcloud/r/cam_user_policy_attachment.html">tencentcloud_cam_user_policy_attachment</a>
</li>
Expand Down

0 comments on commit 9e37bf3

Please sign in to comment.