|
| 1 | +/* |
| 2 | +Provides a resource to create a tag attachment |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +
|
| 8 | +resource "tencentcloud_tag_attachment" "attachment" { |
| 9 | + tag_key = "test3" |
| 10 | + tag_value = "Terraform3" |
| 11 | + resource = "qcs::cvm:ap-guangzhou:uin/100020512675:instance/ins-kfrlvcp4" |
| 12 | +} |
| 13 | +
|
| 14 | +``` |
| 15 | +
|
| 16 | +Import |
| 17 | +
|
| 18 | +tag attachment can be imported using the id, e.g. |
| 19 | +
|
| 20 | +``` |
| 21 | +terraform import tencentcloud_tag_attachment.attachment attachment_id |
| 22 | +``` |
| 23 | +*/ |
| 24 | +package tencentcloud |
| 25 | + |
| 26 | +import ( |
| 27 | + "context" |
| 28 | + "fmt" |
| 29 | + "log" |
| 30 | + "strings" |
| 31 | + |
| 32 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 33 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 34 | + tag "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag/v20180813" |
| 35 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 36 | +) |
| 37 | + |
| 38 | +func resourceTencentCloudTagAttachment() *schema.Resource { |
| 39 | + return &schema.Resource{ |
| 40 | + Create: resourceTencentCloudTagAttachmentCreate, |
| 41 | + Read: resourceTencentCloudTagAttachmentRead, |
| 42 | + Delete: resourceTencentCloudTagAttachmentDelete, |
| 43 | + Importer: &schema.ResourceImporter{ |
| 44 | + State: schema.ImportStatePassthrough, |
| 45 | + }, |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + "tag_key": { |
| 48 | + Required: true, |
| 49 | + ForceNew: true, |
| 50 | + Type: schema.TypeString, |
| 51 | + Description: "tag key.", |
| 52 | + }, |
| 53 | + |
| 54 | + "tag_value": { |
| 55 | + Required: true, |
| 56 | + ForceNew: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "tag value.", |
| 59 | + }, |
| 60 | + |
| 61 | + "resource": { |
| 62 | + Required: true, |
| 63 | + ForceNew: true, |
| 64 | + Type: schema.TypeString, |
| 65 | + Description: "[Six-segment description of resources](https://cloud.tencent.com/document/product/598/10606).", |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func resourceTencentCloudTagAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 72 | + defer logElapsed("resource.tencentcloud_tag_attachment.create")() |
| 73 | + defer inconsistentCheck(d, meta)() |
| 74 | + |
| 75 | + logId := getLogId(contextNil) |
| 76 | + |
| 77 | + var ( |
| 78 | + request = tag.NewAddResourceTagRequest() |
| 79 | + tagKey string |
| 80 | + tagValue string |
| 81 | + resourceId string |
| 82 | + ) |
| 83 | + if v, ok := d.GetOk("tag_key"); ok { |
| 84 | + tagKey = v.(string) |
| 85 | + request.TagKey = helper.String(v.(string)) |
| 86 | + } |
| 87 | + |
| 88 | + if v, ok := d.GetOk("tag_value"); ok { |
| 89 | + tagValue = v.(string) |
| 90 | + request.TagValue = helper.String(v.(string)) |
| 91 | + } |
| 92 | + |
| 93 | + if v, ok := d.GetOk("resource"); ok { |
| 94 | + resourceId = v.(string) |
| 95 | + request.Resource = helper.String(v.(string)) |
| 96 | + } |
| 97 | + |
| 98 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 99 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseTagClient().AddResourceTag(request) |
| 100 | + if e != nil { |
| 101 | + return retryError(e) |
| 102 | + } else { |
| 103 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 104 | + } |
| 105 | + return nil |
| 106 | + }) |
| 107 | + if err != nil { |
| 108 | + log.Printf("[CRITAL]%s create tag tagAttachment failed, reason:%+v", logId, err) |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + d.SetId(tagKey + FILED_SP + tagValue + FILED_SP + resourceId) |
| 113 | + |
| 114 | + return resourceTencentCloudTagAttachmentRead(d, meta) |
| 115 | +} |
| 116 | + |
| 117 | +func resourceTencentCloudTagAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 118 | + defer logElapsed("resource.tencentcloud_tag_attachment.read")() |
| 119 | + defer inconsistentCheck(d, meta)() |
| 120 | + |
| 121 | + logId := getLogId(contextNil) |
| 122 | + |
| 123 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 124 | + |
| 125 | + service := TagService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 126 | + |
| 127 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 128 | + if len(idSplit) != 3 { |
| 129 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 130 | + } |
| 131 | + tagKey := idSplit[0] |
| 132 | + tagValue := idSplit[1] |
| 133 | + resource := idSplit[2] |
| 134 | + |
| 135 | + tagAttachment, err := service.DescribeTagTagAttachmentById(ctx, tagKey, tagValue, resource) |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + if tagAttachment == nil { |
| 141 | + d.SetId("") |
| 142 | + log.Printf("[WARN]%s resource `TagResourceTag` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 143 | + return nil |
| 144 | + } |
| 145 | + if len(tagAttachment.Tags) < 1 { |
| 146 | + log.Printf("[WARN]%s resource `TagResourceTag` [%s] Tags is null, please check if it has been deleted.\n", logId, d.Id()) |
| 147 | + return nil |
| 148 | + } |
| 149 | + if tagAttachment.Tags[0].TagKey != nil { |
| 150 | + _ = d.Set("tag_key", tagAttachment.Tags[0].TagKey) |
| 151 | + } |
| 152 | + |
| 153 | + if tagAttachment.Tags[0].TagValue != nil { |
| 154 | + _ = d.Set("tag_value", tagAttachment.Tags[0].TagValue) |
| 155 | + } |
| 156 | + |
| 157 | + if tagAttachment.Resource != nil { |
| 158 | + _ = d.Set("resource", tagAttachment.Resource) |
| 159 | + } |
| 160 | + |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func resourceTencentCloudTagAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 165 | + defer logElapsed("resource.tencentcloud_tag_attachment.delete")() |
| 166 | + defer inconsistentCheck(d, meta)() |
| 167 | + |
| 168 | + logId := getLogId(contextNil) |
| 169 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 170 | + |
| 171 | + service := TagService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 172 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 173 | + if len(idSplit) != 3 { |
| 174 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 175 | + } |
| 176 | + tagKey := idSplit[0] |
| 177 | + resource := idSplit[2] |
| 178 | + |
| 179 | + if err := service.DeleteTagTagAttachmentById(ctx, tagKey, resource); err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + |
| 183 | + return nil |
| 184 | +} |
0 commit comments