|
| 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 | +} |
0 commit comments