|
| 1 | +/* |
| 2 | +Provides a resource to create a organization org_member_policy_attachment |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_organization_org_member_policy_attachment" "org_member_policy_attachment" { |
| 8 | + member_uins = [100033905366,100033905356] |
| 9 | + policy_name = "example-iac" |
| 10 | + identity_id = 1 |
| 11 | +} |
| 12 | +``` |
| 13 | +
|
| 14 | +Import |
| 15 | +
|
| 16 | +organization org_member_policy_attachment can be imported using the id, e.g. |
| 17 | +
|
| 18 | +``` |
| 19 | +terraform import tencentcloud_organization_org_member_policy_attachment.org_member_policy_attachment org_member_policy_attachment_id |
| 20 | +``` |
| 21 | +*/ |
| 22 | +package tencentcloud |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "fmt" |
| 27 | + "log" |
| 28 | + |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 31 | + organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331" |
| 32 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 33 | +) |
| 34 | + |
| 35 | +func resourceTencentCloudOrganizationOrgMemberPolicyAttachment() *schema.Resource { |
| 36 | + return &schema.Resource{ |
| 37 | + Create: resourceTencentCloudOrganizationOrgMemberPolicyAttachmentCreate, |
| 38 | + Read: resourceTencentCloudOrganizationOrgMemberPolicyAttachmentRead, |
| 39 | + Delete: resourceTencentCloudOrganizationOrgMemberPolicyAttachmentDelete, |
| 40 | + Importer: &schema.ResourceImporter{ |
| 41 | + State: schema.ImportStatePassthrough, |
| 42 | + }, |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "member_uins": { |
| 45 | + Required: true, |
| 46 | + ForceNew: true, |
| 47 | + Type: schema.TypeSet, |
| 48 | + Elem: &schema.Schema{ |
| 49 | + Type: schema.TypeInt, |
| 50 | + }, |
| 51 | + Description: "Member Uin list. Up to 10.", |
| 52 | + }, |
| 53 | + |
| 54 | + "policy_name": { |
| 55 | + Required: true, |
| 56 | + ForceNew: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "Policy name.The maximum length is 128 characters, supporting English letters, numbers, and symbols +=,.@_-.", |
| 59 | + }, |
| 60 | + |
| 61 | + "identity_id": { |
| 62 | + Required: true, |
| 63 | + ForceNew: true, |
| 64 | + Type: schema.TypeInt, |
| 65 | + Description: "Organization identity ID.", |
| 66 | + }, |
| 67 | + |
| 68 | + "description": { |
| 69 | + Optional: true, |
| 70 | + ForceNew: true, |
| 71 | + Type: schema.TypeString, |
| 72 | + Description: "Notes.The maximum length is 128 characters.", |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func resourceTencentCloudOrganizationOrgMemberPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 79 | + defer logElapsed("resource.tencentcloud_organization_org_member_policy_attachment.create")() |
| 80 | + defer inconsistentCheck(d, meta)() |
| 81 | + |
| 82 | + logId := getLogId(contextNil) |
| 83 | + |
| 84 | + var ( |
| 85 | + request = organization.NewCreateOrganizationMembersPolicyRequest() |
| 86 | + response = organization.NewCreateOrganizationMembersPolicyResponse() |
| 87 | + ) |
| 88 | + if v, ok := d.GetOk("member_uins"); ok { |
| 89 | + memberUinsSet := v.(*schema.Set).List() |
| 90 | + for i := range memberUinsSet { |
| 91 | + memberUins := memberUinsSet[i].(int) |
| 92 | + request.MemberUins = append(request.MemberUins, helper.IntInt64(memberUins)) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if v, ok := d.GetOk("policy_name"); ok { |
| 97 | + request.PolicyName = helper.String(v.(string)) |
| 98 | + } |
| 99 | + |
| 100 | + if v, ok := d.GetOkExists("identity_id"); ok { |
| 101 | + request.IdentityId = helper.IntInt64(v.(int)) |
| 102 | + } |
| 103 | + |
| 104 | + if v, ok := d.GetOk("description"); ok { |
| 105 | + request.Description = helper.String(v.(string)) |
| 106 | + } |
| 107 | + |
| 108 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 109 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseOrganizationClient().CreateOrganizationMembersPolicy(request) |
| 110 | + if e != nil { |
| 111 | + return retryError(e) |
| 112 | + } else { |
| 113 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 114 | + } |
| 115 | + response = result |
| 116 | + return nil |
| 117 | + }) |
| 118 | + if err != nil { |
| 119 | + log.Printf("[CRITAL]%s create organization orgMemberPolicyAttachment failed, reason:%+v", logId, err) |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + if response == nil || response.Response == nil || response.Response.PolicyId == nil { |
| 124 | + return fmt.Errorf("policy id is null") |
| 125 | + } |
| 126 | + policyId := *response.Response.PolicyId |
| 127 | + d.SetId(helper.Int64ToStr(policyId)) |
| 128 | + |
| 129 | + return resourceTencentCloudOrganizationOrgMemberPolicyAttachmentRead(d, meta) |
| 130 | +} |
| 131 | + |
| 132 | +func resourceTencentCloudOrganizationOrgMemberPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 133 | + defer logElapsed("resource.tencentcloud_organization_org_member_policy_attachment.read")() |
| 134 | + defer inconsistentCheck(d, meta)() |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func resourceTencentCloudOrganizationOrgMemberPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 140 | + defer logElapsed("resource.tencentcloud_organization_org_member_policy_attachment.delete")() |
| 141 | + defer inconsistentCheck(d, meta)() |
| 142 | + |
| 143 | + logId := getLogId(contextNil) |
| 144 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 145 | + |
| 146 | + service := OrganizationService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 147 | + orgMemberPolicyAttachmentId := d.Id() |
| 148 | + |
| 149 | + if err := service.DeleteOrganizationOrgMemberPolicyAttachmentById(ctx, orgMemberPolicyAttachmentId); err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
0 commit comments