|
| 1 | +/* |
| 2 | +Provides a resource to create a organization org_identity |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_organization_org_identity" "org_identity" { |
| 8 | + identity_alias_name = "example-iac-test" |
| 9 | + identity_policy { |
| 10 | + policy_id = 1 |
| 11 | + policy_name = "AdministratorAccess" |
| 12 | + policy_type = 2 |
| 13 | + } |
| 14 | + description = "iac-test" |
| 15 | +} |
| 16 | +``` |
| 17 | +
|
| 18 | +Import |
| 19 | +
|
| 20 | +organization org_identity can be imported using the id, e.g. |
| 21 | +
|
| 22 | +``` |
| 23 | +terraform import tencentcloud_organization_org_identity.org_identity org_identity_id |
| 24 | +``` |
| 25 | +*/ |
| 26 | +package tencentcloud |
| 27 | + |
| 28 | +import ( |
| 29 | + "context" |
| 30 | + "fmt" |
| 31 | + "log" |
| 32 | + |
| 33 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 34 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 35 | + organization "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization/v20210331" |
| 36 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 37 | +) |
| 38 | + |
| 39 | +func resourceTencentCloudOrganizationOrgIdentity() *schema.Resource { |
| 40 | + return &schema.Resource{ |
| 41 | + Create: resourceTencentCloudOrganizationOrgIdentityCreate, |
| 42 | + Read: resourceTencentCloudOrganizationOrgIdentityRead, |
| 43 | + Update: resourceTencentCloudOrganizationOrgIdentityUpdate, |
| 44 | + Delete: resourceTencentCloudOrganizationOrgIdentityDelete, |
| 45 | + Importer: &schema.ResourceImporter{ |
| 46 | + State: schema.ImportStatePassthrough, |
| 47 | + }, |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "identity_alias_name": { |
| 50 | + Required: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Identity name.Supports English letters and numbers, the length cannot exceed 40 characters.", |
| 53 | + }, |
| 54 | + |
| 55 | + "identity_policy": { |
| 56 | + Required: true, |
| 57 | + Type: schema.TypeList, |
| 58 | + Description: "Identity policy list.", |
| 59 | + Elem: &schema.Resource{ |
| 60 | + Schema: map[string]*schema.Schema{ |
| 61 | + "policy_id": { |
| 62 | + Type: schema.TypeInt, |
| 63 | + Optional: true, |
| 64 | + Description: "CAM default policy ID. Valid and required when PolicyType is the 2-preset policy.", |
| 65 | + }, |
| 66 | + "policy_name": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Optional: true, |
| 69 | + Description: "CAM default policy name. Valid and required when PolicyType is the 2-preset policy.", |
| 70 | + }, |
| 71 | + "policy_type": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Optional: true, |
| 74 | + Description: "Policy type. Value 1-custom policy 2-preset policy; default value 2.", |
| 75 | + }, |
| 76 | + "policy_document": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Optional: true, |
| 79 | + Description: "Customize policy content and follow CAM policy syntax. Valid and required when PolicyType is the 1-custom policy.", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + |
| 85 | + "description": { |
| 86 | + Optional: true, |
| 87 | + Type: schema.TypeString, |
| 88 | + Description: "Identity description.", |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func resourceTencentCloudOrganizationOrgIdentityCreate(d *schema.ResourceData, meta interface{}) error { |
| 95 | + defer logElapsed("resource.tencentcloud_organization_org_identity.create")() |
| 96 | + defer inconsistentCheck(d, meta)() |
| 97 | + |
| 98 | + logId := getLogId(contextNil) |
| 99 | + |
| 100 | + var ( |
| 101 | + request = organization.NewCreateOrganizationIdentityRequest() |
| 102 | + response = organization.NewCreateOrganizationIdentityResponse() |
| 103 | + identityId string |
| 104 | + ) |
| 105 | + if v, ok := d.GetOk("identity_alias_name"); ok { |
| 106 | + request.IdentityAliasName = helper.String(v.(string)) |
| 107 | + } |
| 108 | + |
| 109 | + if v, ok := d.GetOk("identity_policy"); ok { |
| 110 | + for _, item := range v.([]interface{}) { |
| 111 | + dMap := item.(map[string]interface{}) |
| 112 | + identityPolicy := organization.IdentityPolicy{} |
| 113 | + if v, ok := dMap["policy_id"]; ok { |
| 114 | + identityPolicy.PolicyId = helper.IntUint64(v.(int)) |
| 115 | + } |
| 116 | + if v, ok := dMap["policy_name"]; ok { |
| 117 | + identityPolicy.PolicyName = helper.String(v.(string)) |
| 118 | + } |
| 119 | + if v, ok := dMap["policy_type"]; ok { |
| 120 | + identityPolicy.PolicyType = helper.IntUint64(v.(int)) |
| 121 | + } |
| 122 | + if v, ok := dMap["policy_document"]; ok { |
| 123 | + identityPolicy.PolicyDocument = helper.String(v.(string)) |
| 124 | + } |
| 125 | + request.IdentityPolicy = append(request.IdentityPolicy, &identityPolicy) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if v, ok := d.GetOk("description"); ok { |
| 130 | + request.Description = helper.String(v.(string)) |
| 131 | + } |
| 132 | + |
| 133 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 134 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseOrganizationClient().CreateOrganizationIdentity(request) |
| 135 | + if e != nil { |
| 136 | + return retryError(e) |
| 137 | + } else { |
| 138 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 139 | + } |
| 140 | + response = result |
| 141 | + return nil |
| 142 | + }) |
| 143 | + if err != nil { |
| 144 | + log.Printf("[CRITAL]%s create organization orgIdentity failed, reason:%+v", logId, err) |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + identityId = helper.UInt64ToStr(*response.Response.IdentityId) |
| 149 | + d.SetId(identityId) |
| 150 | + |
| 151 | + return resourceTencentCloudOrganizationOrgIdentityRead(d, meta) |
| 152 | +} |
| 153 | + |
| 154 | +func resourceTencentCloudOrganizationOrgIdentityRead(d *schema.ResourceData, meta interface{}) error { |
| 155 | + defer logElapsed("resource.tencentcloud_organization_org_identity.read")() |
| 156 | + defer inconsistentCheck(d, meta)() |
| 157 | + |
| 158 | + logId := getLogId(contextNil) |
| 159 | + |
| 160 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 161 | + |
| 162 | + service := OrganizationService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 163 | + |
| 164 | + orgIdentityId := d.Id() |
| 165 | + |
| 166 | + orgIdentity, err := service.DescribeOrganizationOrgIdentityById(ctx, orgIdentityId) |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + if orgIdentity == nil { |
| 172 | + d.SetId("") |
| 173 | + log.Printf("[WARN]%s resource `OrganizationOrgIdentity` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 174 | + return nil |
| 175 | + } |
| 176 | + |
| 177 | + if orgIdentity.IdentityAliasName != nil { |
| 178 | + _ = d.Set("identity_alias_name", orgIdentity.IdentityAliasName) |
| 179 | + } |
| 180 | + |
| 181 | + if orgIdentity.IdentityPolicy != nil { |
| 182 | + var identityPolicyList []interface{} |
| 183 | + for _, identityPolicy := range orgIdentity.IdentityPolicy { |
| 184 | + identityPolicyMap := map[string]interface{}{} |
| 185 | + |
| 186 | + if identityPolicy.PolicyId != nil { |
| 187 | + identityPolicyMap["policy_id"] = identityPolicy.PolicyId |
| 188 | + } |
| 189 | + |
| 190 | + if identityPolicy.PolicyName != nil { |
| 191 | + identityPolicyMap["policy_name"] = identityPolicy.PolicyName |
| 192 | + } |
| 193 | + |
| 194 | + if identityPolicy.PolicyType != nil { |
| 195 | + identityPolicyMap["policy_type"] = identityPolicy.PolicyType |
| 196 | + } |
| 197 | + |
| 198 | + if identityPolicy.PolicyDocument != nil { |
| 199 | + identityPolicyMap["policy_document"] = identityPolicy.PolicyDocument |
| 200 | + } |
| 201 | + |
| 202 | + identityPolicyList = append(identityPolicyList, identityPolicyMap) |
| 203 | + } |
| 204 | + |
| 205 | + _ = d.Set("identity_policy", identityPolicyList) |
| 206 | + |
| 207 | + } |
| 208 | + |
| 209 | + if orgIdentity.Description != nil { |
| 210 | + _ = d.Set("description", orgIdentity.Description) |
| 211 | + } |
| 212 | + |
| 213 | + return nil |
| 214 | +} |
| 215 | + |
| 216 | +func resourceTencentCloudOrganizationOrgIdentityUpdate(d *schema.ResourceData, meta interface{}) error { |
| 217 | + defer logElapsed("resource.tencentcloud_organization_org_identity.update")() |
| 218 | + defer inconsistentCheck(d, meta)() |
| 219 | + |
| 220 | + logId := getLogId(contextNil) |
| 221 | + |
| 222 | + request := organization.NewUpdateOrganizationIdentityRequest() |
| 223 | + |
| 224 | + orgIdentityId := d.Id() |
| 225 | + |
| 226 | + request.IdentityId = helper.StrToUint64Point(orgIdentityId) |
| 227 | + |
| 228 | + immutableArgs := []string{"identity_alias_name"} |
| 229 | + |
| 230 | + for _, v := range immutableArgs { |
| 231 | + if d.HasChange(v) { |
| 232 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + if v, ok := d.GetOk("identity_policy"); ok { |
| 237 | + for _, item := range v.([]interface{}) { |
| 238 | + dMap := item.(map[string]interface{}) |
| 239 | + identityPolicy := organization.IdentityPolicy{} |
| 240 | + if v, ok := dMap["policy_id"]; ok { |
| 241 | + identityPolicy.PolicyId = helper.IntUint64(v.(int)) |
| 242 | + } |
| 243 | + if v, ok := dMap["policy_name"]; ok { |
| 244 | + identityPolicy.PolicyName = helper.String(v.(string)) |
| 245 | + } |
| 246 | + if v, ok := dMap["policy_type"]; ok { |
| 247 | + identityPolicy.PolicyType = helper.IntUint64(v.(int)) |
| 248 | + } |
| 249 | + if v, ok := dMap["policy_document"]; ok { |
| 250 | + identityPolicy.PolicyDocument = helper.String(v.(string)) |
| 251 | + } |
| 252 | + request.IdentityPolicy = append(request.IdentityPolicy, &identityPolicy) |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + if v, ok := d.GetOk("description"); ok { |
| 257 | + request.Description = helper.String(v.(string)) |
| 258 | + } |
| 259 | + |
| 260 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 261 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseOrganizationClient().UpdateOrganizationIdentity(request) |
| 262 | + if e != nil { |
| 263 | + return retryError(e) |
| 264 | + } else { |
| 265 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 266 | + } |
| 267 | + return nil |
| 268 | + }) |
| 269 | + if err != nil { |
| 270 | + log.Printf("[CRITAL]%s update organization orgIdentity failed, reason:%+v", logId, err) |
| 271 | + return err |
| 272 | + } |
| 273 | + |
| 274 | + return resourceTencentCloudOrganizationOrgIdentityRead(d, meta) |
| 275 | +} |
| 276 | + |
| 277 | +func resourceTencentCloudOrganizationOrgIdentityDelete(d *schema.ResourceData, meta interface{}) error { |
| 278 | + defer logElapsed("resource.tencentcloud_organization_org_identity.delete")() |
| 279 | + defer inconsistentCheck(d, meta)() |
| 280 | + |
| 281 | + logId := getLogId(contextNil) |
| 282 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 283 | + |
| 284 | + service := OrganizationService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 285 | + orgIdentityId := d.Id() |
| 286 | + |
| 287 | + if err := service.DeleteOrganizationOrgIdentityById(ctx, orgIdentityId); err != nil { |
| 288 | + return err |
| 289 | + } |
| 290 | + |
| 291 | + return nil |
| 292 | +} |
0 commit comments