|
| 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 | + organization "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 ResourceTencentCloudOrganizationOrgManagePolicy() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudOrganizationOrgManagePolicyCreate, |
| 20 | + Read: resourceTencentCloudOrganizationOrgManagePolicyRead, |
| 21 | + Update: resourceTencentCloudOrganizationOrgManagePolicyUpdate, |
| 22 | + Delete: resourceTencentCloudOrganizationOrgManagePolicyDelete, |
| 23 | + Importer: &schema.ResourceImporter{ |
| 24 | + State: schema.ImportStatePassthrough, |
| 25 | + }, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "name": { |
| 28 | + Required: true, |
| 29 | + Type: schema.TypeString, |
| 30 | + Description: "Policy name.\nThe length is 1~128 characters, which can include Chinese characters, English letters, numbers, and underscores.", |
| 31 | + }, |
| 32 | + |
| 33 | + "content": { |
| 34 | + Required: true, |
| 35 | + Type: schema.TypeString, |
| 36 | + Description: "Policy content. Refer to the CAM policy syntax.", |
| 37 | + }, |
| 38 | + |
| 39 | + "type": { |
| 40 | + Optional: true, |
| 41 | + Default: ServiceControlPolicyType, |
| 42 | + Type: schema.TypeString, |
| 43 | + Description: "Policy type. Default value is SERVICE_CONTROL_POLICY.\nValid values:\n - `SERVICE_CONTROL_POLICY`: Service control policy.\n - `TAG_POLICY`: Tag policy.", |
| 44 | + }, |
| 45 | + |
| 46 | + "description": { |
| 47 | + Optional: true, |
| 48 | + Type: schema.TypeString, |
| 49 | + Description: "Policy description.", |
| 50 | + }, |
| 51 | + |
| 52 | + "policy_id": { |
| 53 | + Computed: true, |
| 54 | + Type: schema.TypeString, |
| 55 | + Description: "Policy Id.", |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func resourceTencentCloudOrganizationOrgManagePolicyCreate(d *schema.ResourceData, meta interface{}) error { |
| 62 | + defer tccommon.LogElapsed("resource.tencentcloud_organization_org_manage_policy.create")() |
| 63 | + defer tccommon.InconsistentCheck(d, meta)() |
| 64 | + |
| 65 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 66 | + |
| 67 | + var ( |
| 68 | + policyType string |
| 69 | + request = organization.NewCreatePolicyRequest() |
| 70 | + response = organization.NewCreatePolicyResponse() |
| 71 | + ) |
| 72 | + if v, ok := d.GetOk("name"); ok { |
| 73 | + request.Name = helper.String(v.(string)) |
| 74 | + } |
| 75 | + |
| 76 | + if v, ok := d.GetOk("content"); ok { |
| 77 | + request.Content = helper.String(v.(string)) |
| 78 | + } |
| 79 | + |
| 80 | + if v, ok := d.GetOk("type"); ok { |
| 81 | + policyType = v.(string) |
| 82 | + request.Type = helper.String(v.(string)) |
| 83 | + } |
| 84 | + |
| 85 | + if v, ok := d.GetOk("description"); ok { |
| 86 | + request.Description = helper.String(v.(string)) |
| 87 | + } |
| 88 | + |
| 89 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 90 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().CreatePolicy(request) |
| 91 | + if e != nil { |
| 92 | + return tccommon.RetryError(e) |
| 93 | + } else { |
| 94 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 95 | + } |
| 96 | + response = result |
| 97 | + return nil |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + log.Printf("[CRITAL]%s create organization OrgManagePolicy failed, reason:%+v", logId, err) |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + d.SetId(strings.Join([]string{helper.UInt64ToStr(*response.Response.PolicyId), policyType}, tccommon.FILED_SP)) |
| 105 | + return resourceTencentCloudOrganizationOrgManagePolicyRead(d, meta) |
| 106 | +} |
| 107 | + |
| 108 | +func resourceTencentCloudOrganizationOrgManagePolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 109 | + defer tccommon.LogElapsed("resource.tencentcloud_organization_org_manage_policy.read")() |
| 110 | + defer tccommon.InconsistentCheck(d, meta)() |
| 111 | + |
| 112 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 113 | + |
| 114 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 115 | + |
| 116 | + service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 117 | + |
| 118 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 119 | + if len(idSplit) != 2 { |
| 120 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 121 | + } |
| 122 | + policyId := idSplit[0] |
| 123 | + policyType := idSplit[1] |
| 124 | + |
| 125 | + OrgManagePolicy, err := service.DescribeOrganizationOrgManagePolicyById(ctx, policyId, policyType) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + if OrgManagePolicy == nil { |
| 131 | + d.SetId("") |
| 132 | + log.Printf("[WARN]%s resource `OrganizationOrgManagePolicy` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 133 | + return nil |
| 134 | + } |
| 135 | + |
| 136 | + if OrgManagePolicy.PolicyName != nil { |
| 137 | + _ = d.Set("name", OrgManagePolicy.PolicyName) |
| 138 | + } |
| 139 | + |
| 140 | + if OrgManagePolicy.PolicyDocument != nil { |
| 141 | + _ = d.Set("content", OrgManagePolicy.PolicyDocument) |
| 142 | + } |
| 143 | + |
| 144 | + if OrgManagePolicy.Type != nil { |
| 145 | + _ = d.Set("type", policyType) |
| 146 | + } |
| 147 | + |
| 148 | + if OrgManagePolicy.Description != nil { |
| 149 | + _ = d.Set("description", OrgManagePolicy.Description) |
| 150 | + } |
| 151 | + _ = d.Set("policy_id", policyId) |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func resourceTencentCloudOrganizationOrgManagePolicyUpdate(d *schema.ResourceData, meta interface{}) error { |
| 157 | + defer tccommon.LogElapsed("resource.tencentcloud_organization_org_manage_policy.update")() |
| 158 | + defer tccommon.InconsistentCheck(d, meta)() |
| 159 | + |
| 160 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 161 | + |
| 162 | + request := organization.NewUpdatePolicyRequest() |
| 163 | + |
| 164 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 165 | + if len(idSplit) != 2 { |
| 166 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 167 | + } |
| 168 | + policyId := idSplit[0] |
| 169 | + |
| 170 | + request.PolicyId = helper.StrToInt64Point(policyId) |
| 171 | + |
| 172 | + needChange := false |
| 173 | + mutableArgs := []string{"name", "content", "type", "description"} |
| 174 | + for _, v := range mutableArgs { |
| 175 | + if d.HasChange(v) { |
| 176 | + needChange = true |
| 177 | + break |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + if needChange { |
| 182 | + if v, ok := d.GetOk("name"); ok { |
| 183 | + request.Name = helper.String(v.(string)) |
| 184 | + } |
| 185 | + if v, ok := d.GetOk("content"); ok { |
| 186 | + request.Content = helper.String(v.(string)) |
| 187 | + } |
| 188 | + if v, ok := d.GetOk("type"); ok { |
| 189 | + request.Type = helper.String(v.(string)) |
| 190 | + } |
| 191 | + if v, ok := d.GetOk("description"); ok { |
| 192 | + request.Description = helper.String(v.(string)) |
| 193 | + } |
| 194 | + |
| 195 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 196 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseOrganizationClient().UpdatePolicy(request) |
| 197 | + if e != nil { |
| 198 | + return tccommon.RetryError(e) |
| 199 | + } else { |
| 200 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 201 | + } |
| 202 | + return nil |
| 203 | + }) |
| 204 | + if err != nil { |
| 205 | + log.Printf("[CRITAL]%s update organization OrgManagePolicy failed, reason:%+v", logId, err) |
| 206 | + return err |
| 207 | + } |
| 208 | + |
| 209 | + } |
| 210 | + return resourceTencentCloudOrganizationOrgManagePolicyRead(d, meta) |
| 211 | +} |
| 212 | + |
| 213 | +func resourceTencentCloudOrganizationOrgManagePolicyDelete(d *schema.ResourceData, meta interface{}) error { |
| 214 | + defer tccommon.LogElapsed("resource.tencentcloud_organization_org_manage_policy.delete")() |
| 215 | + defer tccommon.InconsistentCheck(d, meta)() |
| 216 | + |
| 217 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 218 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 219 | + |
| 220 | + service := OrganizationService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 221 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 222 | + if len(idSplit) != 2 { |
| 223 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 224 | + } |
| 225 | + policyId := idSplit[0] |
| 226 | + policyType := idSplit[1] |
| 227 | + |
| 228 | + if err := service.DeleteOrganizationOrgManagePolicyById(ctx, policyId, policyType); err != nil { |
| 229 | + return err |
| 230 | + } |
| 231 | + |
| 232 | + return nil |
| 233 | +} |
0 commit comments