Skip to content

Commit 323d58a

Browse files
WeiMengXSnickcchen
andauthored
Dev/add tag (#1991)
* feat: Add tag and resourceTag * feat: Add tag and resourceTag * doc: add change log * fix: go mod * fix: go mod * fix: go vendor * fix: doc * fix: doc * fmt * fmt * fmt * fmt * fmt * doc * doc * fmt * fix: 单测修改 * fix: 单测修改 * fix: 单测修改 * fix: 单测修改 --------- Co-authored-by: nickcchen <nickcchen@tencent.com>
1 parent c39ec93 commit 323d58a

File tree

10 files changed

+771
-0
lines changed

10 files changed

+771
-0
lines changed

.changelog/1991.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-resource
2+
tencentcloud_tag
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_tag_attachment
7+
```

tencentcloud/provider.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,10 @@ Tencent Cloud Service Engine(TSE)
15801580
ClickHouse(CDWCH)
15811581
Resource
15821582
tencentcloud_clickhouse_instance
1583+
Tag
1584+
Resource
1585+
tencentcloud_tag
1586+
tencentcloud_tag_attachment
15831587
15841588
*/
15851589
package tencentcloud
@@ -2263,6 +2267,8 @@ func Provider() *schema.Provider {
22632267
"tencentcloud_nat_gateway": resourceTencentCloudNatGateway(),
22642268
"tencentcloud_nat_gateway_snat": resourceTencentCloudNatGatewaySnat(),
22652269
"tencentcloud_nat_refresh_nat_dc_route": resourceTencentCloudNatRefreshNatDcRoute(),
2270+
"tencentcloud_tag": resourceTencentCloudTag(),
2271+
"tencentcloud_tag_attachment": resourceTencentCloudTagAttachment(),
22662272
"tencentcloud_eip": resourceTencentCloudEip(),
22672273
"tencentcloud_eip_association": resourceTencentCloudEipAssociation(),
22682274
"tencentcloud_eip_address_transform": resourceTencentCloudEipAddressTransform(),

tencentcloud/resource_tc_tag.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
Provides a resource to create a tag
3+
4+
Example Usage
5+
6+
```hcl
7+
8+
resource "tencentcloud_tag" "tag" {
9+
tag_key = "test"
10+
tag_value = "Terraform"
11+
}
12+
13+
```
14+
15+
Import
16+
17+
tag tag can be imported using the id, e.g.
18+
19+
```
20+
terraform import tencentcloud_tag.tag tag_id
21+
```
22+
*/
23+
package tencentcloud
24+
25+
import (
26+
"context"
27+
"fmt"
28+
"log"
29+
"strings"
30+
31+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
32+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
33+
tag "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tag/v20180813"
34+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
35+
)
36+
37+
func resourceTencentCloudTag() *schema.Resource {
38+
return &schema.Resource{
39+
Create: resourceTencentCloudTagResourceCreate,
40+
Read: resourceTencentCloudTagResourceRead,
41+
Delete: resourceTencentCloudTagResourceDelete,
42+
Importer: &schema.ResourceImporter{
43+
State: schema.ImportStatePassthrough,
44+
},
45+
Schema: map[string]*schema.Schema{
46+
"tag_key": {
47+
Required: true,
48+
ForceNew: true,
49+
Type: schema.TypeString,
50+
Description: "tag key.",
51+
},
52+
53+
"tag_value": {
54+
Required: true,
55+
ForceNew: true,
56+
Type: schema.TypeString,
57+
Description: "tag value.",
58+
},
59+
},
60+
}
61+
}
62+
63+
func resourceTencentCloudTagResourceCreate(d *schema.ResourceData, meta interface{}) error {
64+
defer logElapsed("resource.tencentcloud_tag.create")()
65+
defer inconsistentCheck(d, meta)()
66+
67+
logId := getLogId(contextNil)
68+
69+
var (
70+
request = tag.NewCreateTagRequest()
71+
tagKey string
72+
tagValue string
73+
)
74+
if v, ok := d.GetOk("tag_key"); ok {
75+
tagKey = v.(string)
76+
request.TagKey = helper.String(v.(string))
77+
}
78+
79+
if v, ok := d.GetOk("tag_value"); ok {
80+
tagValue = v.(string)
81+
request.TagValue = helper.String(v.(string))
82+
}
83+
84+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
85+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseTagClient().CreateTag(request)
86+
if e != nil {
87+
return retryError(e)
88+
} else {
89+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
90+
}
91+
return nil
92+
})
93+
if err != nil {
94+
log.Printf("[CRITAL]%s create tag tag failed, reason:%+v", logId, err)
95+
return err
96+
}
97+
98+
d.SetId(tagKey + FILED_SP + tagValue)
99+
100+
return resourceTencentCloudTagResourceRead(d, meta)
101+
}
102+
103+
func resourceTencentCloudTagResourceRead(d *schema.ResourceData, meta interface{}) error {
104+
defer logElapsed("resource.tencentcloud_tag.read")()
105+
defer inconsistentCheck(d, meta)()
106+
107+
logId := getLogId(contextNil)
108+
109+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
110+
111+
service := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
112+
113+
idSplit := strings.Split(d.Id(), FILED_SP)
114+
if len(idSplit) != 2 {
115+
return fmt.Errorf("id is broken,%s", d.Id())
116+
}
117+
tagKey := idSplit[0]
118+
tagValue := idSplit[1]
119+
120+
tagRes, err := service.DescribeTagResourceById(ctx, tagKey, tagValue)
121+
if err != nil {
122+
return err
123+
}
124+
125+
if tagRes == nil {
126+
d.SetId("")
127+
log.Printf("[WARN]%s resource `TagTag` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
128+
return nil
129+
}
130+
131+
if tagRes.TagKey != nil {
132+
_ = d.Set("tag_key", tagRes.TagKey)
133+
}
134+
135+
if tagRes.TagValue != nil {
136+
_ = d.Set("tag_value", tagRes.TagValue)
137+
}
138+
139+
return nil
140+
}
141+
142+
func resourceTencentCloudTagResourceDelete(d *schema.ResourceData, meta interface{}) error {
143+
defer logElapsed("resource.tencentcloud_tag.delete")()
144+
defer inconsistentCheck(d, meta)()
145+
146+
logId := getLogId(contextNil)
147+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
148+
149+
service := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
150+
idSplit := strings.Split(d.Id(), FILED_SP)
151+
if len(idSplit) != 2 {
152+
return fmt.Errorf("id is broken,%s", d.Id())
153+
}
154+
tagKey := idSplit[0]
155+
tagValue := idSplit[1]
156+
157+
if err := service.DeleteTagResourceById(ctx, tagKey, tagValue); err != nil {
158+
return err
159+
}
160+
161+
return nil
162+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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

Comments
 (0)