|
| 1 | +package tco |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + organizationv20210331 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331" |
| 12 | + |
| 13 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 14 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceTencentCloudOrganizationMemberAuthPolicyAttachment() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudOrganizationMemberAuthPolicyAttachmentCreate, |
| 20 | + Read: resourceTencentCloudOrganizationMemberAuthPolicyAttachmentRead, |
| 21 | + Delete: resourceTencentCloudOrganizationMemberAuthPolicyAttachmentDelete, |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + State: schema.ImportStatePassthrough, |
| 24 | + }, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "policy_id": { |
| 27 | + Type: schema.TypeInt, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + Description: "Pilicy ID.", |
| 31 | + }, |
| 32 | + |
| 33 | + "org_sub_account_uin": { |
| 34 | + Type: schema.TypeInt, |
| 35 | + Required: true, |
| 36 | + ForceNew: true, |
| 37 | + Description: "Organization administrator sub-account Uin.", |
| 38 | + }, |
| 39 | + |
| 40 | + // computed |
| 41 | + "identity_id": { |
| 42 | + Type: schema.TypeInt, |
| 43 | + Computed: true, |
| 44 | + Description: "Identity ID.", |
| 45 | + }, |
| 46 | + |
| 47 | + "identity_role_name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Description: "Identity role name.", |
| 51 | + }, |
| 52 | + |
| 53 | + "identity_role_alias_name": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + Description: "Identity role alias name.", |
| 57 | + }, |
| 58 | + |
| 59 | + "create_time": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "Create time.", |
| 63 | + }, |
| 64 | + |
| 65 | + "policy_name": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "Policy name.", |
| 69 | + }, |
| 70 | + |
| 71 | + "member_uin": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Computed: true, |
| 74 | + Description: "Member UIN.", |
| 75 | + }, |
| 76 | + |
| 77 | + "member_name": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "Member name.", |
| 81 | + }, |
| 82 | + |
| 83 | + "org_sub_account_name": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + Description: "Org sub account name.", |
| 87 | + }, |
| 88 | + |
| 89 | + "bind_type": { |
| 90 | + Type: schema.TypeInt, |
| 91 | + Computed: true, |
| 92 | + Description: "Bind type. 1-Subaccount, 2-User Group.", |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func resourceTencentCloudOrganizationMemberAuthPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 99 | + defer tccommon.LogElapsed("resource.tencentcloud_organization_member_auth_policy_attachment.create")() |
| 100 | + defer tccommon.InconsistentCheck(d, meta)() |
| 101 | + |
| 102 | + var ( |
| 103 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 104 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 105 | + request = organizationv20210331.NewBindOrganizationPolicySubAccountRequest() |
| 106 | + policyId string |
| 107 | + orgSubAccountUin string |
| 108 | + ) |
| 109 | + |
| 110 | + if v, ok := d.GetOkExists("policy_id"); ok { |
| 111 | + request.PolicyId = helper.IntInt64(v.(int)) |
| 112 | + policyId = helper.IntToStr(v.(int)) |
| 113 | + } |
| 114 | + |
| 115 | + if v, ok := d.GetOkExists("org_sub_account_uin"); ok { |
| 116 | + request.OrgSubAccountUins = append(request.OrgSubAccountUins, helper.IntInt64(v.(int))) |
| 117 | + orgSubAccountUin = helper.IntToStr(v.(int)) |
| 118 | + } |
| 119 | + |
| 120 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 121 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().BindOrganizationPolicySubAccountWithContext(ctx, request) |
| 122 | + if e != nil { |
| 123 | + return tccommon.RetryError(e) |
| 124 | + } else { |
| 125 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | + }) |
| 130 | + |
| 131 | + if reqErr != nil { |
| 132 | + log.Printf("[CRITAL]%s create organization members auth policy attachment failed, reason:%+v", logId, reqErr) |
| 133 | + return reqErr |
| 134 | + } |
| 135 | + |
| 136 | + d.SetId(strings.Join([]string{policyId, orgSubAccountUin}, tccommon.FILED_SP)) |
| 137 | + return resourceTencentCloudOrganizationMemberAuthPolicyAttachmentRead(d, meta) |
| 138 | +} |
| 139 | + |
| 140 | +func resourceTencentCloudOrganizationMemberAuthPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 141 | + defer tccommon.LogElapsed("resource.tencentcloud_organization_member_auth_policy_attachment.read")() |
| 142 | + defer tccommon.InconsistentCheck(d, meta)() |
| 143 | + |
| 144 | + var ( |
| 145 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 146 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 147 | + service = OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 148 | + ) |
| 149 | + |
| 150 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 151 | + if len(idSplit) != 2 { |
| 152 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 153 | + } |
| 154 | + |
| 155 | + policyId := idSplit[0] |
| 156 | + orgSubAccountUin := idSplit[1] |
| 157 | + |
| 158 | + respData, err := service.DescribeOrganizationMembersAuthPolicyAttachmentById(ctx, policyId, orgSubAccountUin) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + if respData == nil { |
| 164 | + log.Printf("[WARN]%s resource `tencentcloud_organization_member_auth_policy_attachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 165 | + d.SetId("") |
| 166 | + return nil |
| 167 | + } |
| 168 | + |
| 169 | + if len(respData) != 1 { |
| 170 | + return fmt.Errorf("Query organization members auth policy attachment by id return more than one.") |
| 171 | + } |
| 172 | + |
| 173 | + for _, item := range respData { |
| 174 | + if item.PolicyId != nil { |
| 175 | + _ = d.Set("policy_id", item.PolicyId) |
| 176 | + } |
| 177 | + |
| 178 | + if item.OrgSubAccountUin != nil { |
| 179 | + _ = d.Set("org_sub_account_uin", item.OrgSubAccountUin) |
| 180 | + } |
| 181 | + |
| 182 | + if item.IdentityId != nil { |
| 183 | + _ = d.Set("identity_id", item.IdentityId) |
| 184 | + } |
| 185 | + |
| 186 | + if item.IdentityRoleName != nil { |
| 187 | + _ = d.Set("identity_role_name", item.IdentityRoleName) |
| 188 | + } |
| 189 | + |
| 190 | + if item.IdentityRoleAliasName != nil { |
| 191 | + _ = d.Set("identity_role_alias_name", item.IdentityRoleAliasName) |
| 192 | + } |
| 193 | + |
| 194 | + if item.CreateTime != nil { |
| 195 | + _ = d.Set("create_time", item.CreateTime) |
| 196 | + } |
| 197 | + |
| 198 | + if item.PolicyName != nil { |
| 199 | + _ = d.Set("policy_name", item.PolicyName) |
| 200 | + } |
| 201 | + |
| 202 | + if item.MemberUin != nil { |
| 203 | + _ = d.Set("member_uin", item.MemberUin) |
| 204 | + } |
| 205 | + |
| 206 | + if item.MemberName != nil { |
| 207 | + _ = d.Set("member_name", item.MemberName) |
| 208 | + } |
| 209 | + |
| 210 | + if item.OrgSubAccountName != nil { |
| 211 | + _ = d.Set("org_sub_account_name", item.OrgSubAccountName) |
| 212 | + } |
| 213 | + |
| 214 | + if item.BindType != nil { |
| 215 | + _ = d.Set("bind_type", item.BindType) |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return nil |
| 220 | +} |
| 221 | + |
| 222 | +func resourceTencentCloudOrganizationMemberAuthPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 223 | + defer tccommon.LogElapsed("resource.tencentcloud_organization_member_auth_policy_attachment.delete")() |
| 224 | + defer tccommon.InconsistentCheck(d, meta)() |
| 225 | + |
| 226 | + var ( |
| 227 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 228 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 229 | + request = organizationv20210331.NewCancelOrganizationPolicySubAccountRequest() |
| 230 | + ) |
| 231 | + |
| 232 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 233 | + if len(idSplit) != 2 { |
| 234 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 235 | + } |
| 236 | + |
| 237 | + policyId := idSplit[0] |
| 238 | + orgSubAccountUin := idSplit[1] |
| 239 | + |
| 240 | + request.PolicyId = helper.StrToInt64Point(policyId) |
| 241 | + request.OrgSubAccountUins = append(request.OrgSubAccountUins, helper.StrToInt64Point(orgSubAccountUin)) |
| 242 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 243 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().CancelOrganizationPolicySubAccountWithContext(ctx, request) |
| 244 | + if e != nil { |
| 245 | + return tccommon.RetryError(e) |
| 246 | + } else { |
| 247 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 248 | + } |
| 249 | + |
| 250 | + return nil |
| 251 | + }) |
| 252 | + |
| 253 | + if reqErr != nil { |
| 254 | + log.Printf("[CRITAL]%s delete organization members auth policy attachment failed, reason:%+v", logId, reqErr) |
| 255 | + return reqErr |
| 256 | + } |
| 257 | + |
| 258 | + return nil |
| 259 | +} |
0 commit comments