|
| 1 | +package billing |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + billingv20180709 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing/v20180709" |
| 11 | + |
| 12 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 13 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 14 | +) |
| 15 | + |
| 16 | +func ResourceTencentCloudBillingAllocationTag() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Create: resourceTencentCloudBillingAllocationTagCreate, |
| 19 | + Read: resourceTencentCloudBillingAllocationTagRead, |
| 20 | + Delete: resourceTencentCloudBillingAllocationTagDelete, |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + State: schema.ImportStatePassthrough, |
| 23 | + }, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "tag_key": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + ForceNew: true, |
| 29 | + Description: "Cost allocation tag key.", |
| 30 | + }, |
| 31 | + |
| 32 | + // computed |
| 33 | + "status": { |
| 34 | + Type: schema.TypeInt, |
| 35 | + Computed: true, |
| 36 | + Description: "Tag type, 0 normal tag, 1 account tag.", |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func resourceTencentCloudBillingAllocationTagCreate(d *schema.ResourceData, meta interface{}) error { |
| 43 | + defer tccommon.LogElapsed("resource.tencentcloud_billing_allocation_tag.create")() |
| 44 | + defer tccommon.InconsistentCheck(d, meta)() |
| 45 | + |
| 46 | + var ( |
| 47 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 48 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 49 | + request = billingv20180709.NewCreateAllocationTagRequest() |
| 50 | + tagKey string |
| 51 | + ) |
| 52 | + |
| 53 | + if v, ok := d.GetOk("tag_key"); ok { |
| 54 | + tagKey = v.(string) |
| 55 | + request.TagKey = append(request.TagKey, helper.String(tagKey)) |
| 56 | + } |
| 57 | + |
| 58 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 59 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseBillingV20180709Client().CreateAllocationTagWithContext(ctx, request) |
| 60 | + if e != nil { |
| 61 | + return tccommon.RetryError(e) |
| 62 | + } else { |
| 63 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 64 | + } |
| 65 | + |
| 66 | + if result == nil || result.Response == nil { |
| 67 | + return resource.NonRetryableError(fmt.Errorf("Create billing allocation tag failed, Response is nil.")) |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | + }) |
| 72 | + |
| 73 | + if reqErr != nil { |
| 74 | + log.Printf("[CRITAL]%s create billing allocation tag failed, reason:%+v", logId, reqErr) |
| 75 | + return reqErr |
| 76 | + } |
| 77 | + |
| 78 | + d.SetId(tagKey) |
| 79 | + return resourceTencentCloudBillingAllocationTagRead(d, meta) |
| 80 | +} |
| 81 | + |
| 82 | +func resourceTencentCloudBillingAllocationTagRead(d *schema.ResourceData, meta interface{}) error { |
| 83 | + defer tccommon.LogElapsed("resource.tencentcloud_billing_allocation_tag.read")() |
| 84 | + defer tccommon.InconsistentCheck(d, meta)() |
| 85 | + |
| 86 | + var ( |
| 87 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 88 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 89 | + service = BillingService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 90 | + tagKey = d.Id() |
| 91 | + ) |
| 92 | + |
| 93 | + respData, err := service.DescribeBillingAllocationTagById(ctx, tagKey) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + if respData == nil { |
| 99 | + log.Printf("[WARN]%s resource `tencentcloud_billing_allocation_tag` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 100 | + d.SetId("") |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + if respData.TagKey != nil { |
| 105 | + _ = d.Set("tag_key", respData.TagKey) |
| 106 | + } |
| 107 | + |
| 108 | + if respData.Status != nil { |
| 109 | + _ = d.Set("status", respData.Status) |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func resourceTencentCloudBillingAllocationTagDelete(d *schema.ResourceData, meta interface{}) error { |
| 116 | + defer tccommon.LogElapsed("resource.tencentcloud_billing_allocation_tag.delete")() |
| 117 | + defer tccommon.InconsistentCheck(d, meta)() |
| 118 | + |
| 119 | + var ( |
| 120 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 121 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 122 | + request = billingv20180709.NewDeleteAllocationTagRequest() |
| 123 | + tagKey = d.Id() |
| 124 | + ) |
| 125 | + |
| 126 | + request.TagKey = append(request.TagKey, helper.String(tagKey)) |
| 127 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 128 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseBillingV20180709Client().DeleteAllocationTagWithContext(ctx, request) |
| 129 | + if e != nil { |
| 130 | + return tccommon.RetryError(e) |
| 131 | + } else { |
| 132 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | + }) |
| 137 | + |
| 138 | + if reqErr != nil { |
| 139 | + log.Printf("[CRITAL]%s delete billing allocation tag failed, reason:%+v", logId, reqErr) |
| 140 | + return reqErr |
| 141 | + } |
| 142 | + |
| 143 | + return nil |
| 144 | +} |
0 commit comments