Skip to content

Commit 25bf02d

Browse files
authored
support cam policy with restrict tag (#1275)
1 parent 7269703 commit 25bf02d

23 files changed

+3689
-554
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.199
2727
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.0.466
2828
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.409
29-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.199
29+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.493
3030
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb v1.0.409
3131
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn v1.0.466
3232
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs v1.0.199

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.409 h1:ToZpNh7
447447
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.409/go.mod h1:U24yUxCDruJLayOsP/onO2E/7+9ljeNsNO+phu+PeiM=
448448
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.199 h1:MkIdFgEGF+baYAU9Z/PUmudfuamCGtLsedQpopwyHNU=
449449
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.199/go.mod h1:PTp058qpOV//RukBVdYQT962rZg71lIt6eHLK1zdvEc=
450+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.493 h1:CDH65MOmHZnvHlcPAcpo2lceQ8AT1kU6ONlabGJjm/Y=
451+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.493/go.mod h1:Dzo3Z7Ln3bJ9jsgTIH1GslwuW+D6hbEJ1f1Ju4x/dcU=
450452
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb v1.0.409 h1:fKFSxvMzS8T+z2z2qm67dgTClnkryeVTykclVkHh3qE=
451453
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb v1.0.409/go.mod h1:q89YBv3T1bzENpcovtwnjxfVn9vx9pCYAssp0HPuivU=
452454
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn v1.0.466 h1:64xA7zyG0jrtBE63cqjALXiRRzAQIE719kZUsp79018=
@@ -482,6 +484,7 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.486/go.mod
482484
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.488/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
483485
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.489/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
484486
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.490/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
487+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.493/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
485488
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.494 h1:IC3vvhWYomlthgXvBAQbP/M/YW+OpcodyYz4gU3JCiQ=
486489
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.494/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
487490
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445 h1:Bh7XD0ypNMHYyBOM8hhKsSu+y0VVKUnJVS+YKKhfpGg=

tencentcloud/resource_tc_cbs_snapshot.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"fmt"
2626
"log"
2727

28+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
29+
2830
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
2931
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
3032
cbs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
@@ -53,6 +55,12 @@ func resourceTencentCloudCbsSnapshot() *schema.Resource {
5355
ForceNew: true,
5456
Description: "ID of the the CBS which this snapshot created from.",
5557
},
58+
"tags": {
59+
Type: schema.TypeMap,
60+
Optional: true,
61+
Deprecated: "cbs snapshot do not support tag now.",
62+
Description: "The available tags within this CBS Snapshot.",
63+
},
5664
"storage_size": {
5765
Type: schema.TypeInt,
5866
Computed: true,
@@ -91,14 +99,19 @@ func resourceTencentCloudCbsSnapshotCreate(d *schema.ResourceData, meta interfac
9199
storageId := d.Get("storage_id").(string)
92100
snapshotName := d.Get("snapshot_name").(string)
93101

102+
var tags map[string]string
103+
104+
if temp := helper.GetTags(d, "tags"); len(temp) > 0 {
105+
tags = temp
106+
}
94107
cbsService := CbsService{
95108
client: meta.(*TencentCloudClient).apiV3Conn,
96109
}
97110

98111
snapshotId := ""
99112
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
100113
var e error
101-
snapshotId, e = cbsService.CreateSnapshot(ctx, storageId, snapshotName)
114+
snapshotId, e = cbsService.CreateSnapshot(ctx, storageId, snapshotName, tags)
102115
if e != nil {
103116
return retryError(e)
104117
}
@@ -110,6 +123,15 @@ func resourceTencentCloudCbsSnapshotCreate(d *schema.ResourceData, meta interfac
110123
return err
111124
}
112125

126+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
127+
tcClient := meta.(*TencentCloudClient).apiV3Conn
128+
tagService := &TagService{client: tcClient}
129+
resourceName := BuildTagResourceName("cvm", "volume", tcClient.Region, d.Id())
130+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
131+
return err
132+
}
133+
}
134+
113135
err = resource.Retry(20*readRetryTimeout, func() *resource.RetryError {
114136
snapshot, e := cbsService.DescribeSnapshotById(ctx, snapshotId)
115137
if e != nil {
@@ -171,6 +193,15 @@ func resourceTencentCloudCbsSnapshotRead(d *schema.ResourceData, meta interface{
171193
_ = d.Set("snapshot_name", snapshot.SnapshotName)
172194
_ = d.Set("snapshot_status", snapshot.SnapshotState)
173195

196+
tcClient := meta.(*TencentCloudClient).apiV3Conn
197+
tagService := &TagService{client: tcClient}
198+
tags, err := tagService.DescribeResourceTags(ctx, "cvm", "volume", tcClient.Region, d.Id())
199+
if err != nil {
200+
return err
201+
}
202+
203+
_ = d.Set("tags", tags)
204+
174205
return nil
175206
}
176207

@@ -200,6 +231,20 @@ func resourceTencentCloudCbsSnapshotUpdate(d *schema.ResourceData, meta interfac
200231
}
201232
}
202233

234+
if d.HasChange("tags") {
235+
236+
oldValue, newValue := d.GetChange("tags")
237+
replaceTags, deleteTags := diffTags(oldValue.(map[string]interface{}), newValue.(map[string]interface{}))
238+
239+
tcClient := meta.(*TencentCloudClient).apiV3Conn
240+
tagService := &TagService{client: tcClient}
241+
resourceName := BuildTagResourceName("cvm", "volume", tcClient.Region, d.Id())
242+
err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags)
243+
if err != nil {
244+
return err
245+
}
246+
}
247+
203248
return nil
204249
}
205250

tencentcloud/resource_tc_cbs_storage.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ func resourceTencentCloudCbsStorageCreate(d *schema.ResourceData, meta interface
201201
}
202202
}
203203

204+
if v := helper.GetTags(d, "tags"); len(v) > 0 {
205+
for tagKey, tagValue := range v {
206+
tag := cbs.Tag{
207+
Key: helper.String(tagKey),
208+
Value: helper.String(tagValue),
209+
}
210+
request.Tags = append(request.Tags, &tag)
211+
}
212+
}
213+
204214
storageId := ""
205215
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
206216
response, e := meta.(*TencentCloudClient).apiV3Conn.UseCbsClient().CreateDisks(request)
@@ -223,6 +233,15 @@ func resourceTencentCloudCbsStorageCreate(d *schema.ResourceData, meta interface
223233
}
224234
d.SetId(storageId)
225235

236+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
237+
tcClient := meta.(*TencentCloudClient).apiV3Conn
238+
tagService := &TagService{client: tcClient}
239+
resourceName := BuildTagResourceName("cvm", "volume", tcClient.Region, d.Id())
240+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
241+
return err
242+
}
243+
}
244+
226245
// must wait for finishing creating disk
227246
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
228247
storage, e := cbsService.DescribeDiskById(ctx, storageId)
@@ -237,14 +256,6 @@ func resourceTencentCloudCbsStorageCreate(d *schema.ResourceData, meta interface
237256
if err != nil {
238257
return err
239258
}
240-
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
241-
tcClient := meta.(*TencentCloudClient).apiV3Conn
242-
tagService := &TagService{client: tcClient}
243-
resourceName := BuildTagResourceName("cvm", "volume", tcClient.Region, d.Id())
244-
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
245-
return err
246-
}
247-
}
248259

249260
return resourceTencentCloudCbsStorageRead(d, meta)
250261
}

tencentcloud/resource_tc_eip.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ func resourceTencentCloudEipCreate(d *schema.ResourceData, meta interface{}) err
138138
if v, ok := d.GetOk("internet_max_bandwidth_out"); ok {
139139
request.InternetMaxBandwidthOut = helper.IntInt64(v.(int))
140140
}
141+
if v := helper.GetTags(d, "tags"); len(v) > 0 {
142+
for tagKey, tagValue := range v {
143+
tag := vpc.Tag{
144+
Key: helper.String(tagKey),
145+
Value: helper.String(tagValue),
146+
}
147+
request.Tags = append(request.Tags, &tag)
148+
}
149+
}
141150

142151
eipId := ""
143152
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
@@ -162,6 +171,14 @@ func resourceTencentCloudEipCreate(d *schema.ResourceData, meta interface{}) err
162171
}
163172
d.SetId(eipId)
164173

174+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
175+
resourceName := BuildTagResourceName(VPC_SERVICE_TYPE, EIP_RESOURCE_TYPE, region, eipId)
176+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
177+
log.Printf("[CRITAL]%s set eip tags failed: %+v", logId, err)
178+
return err
179+
}
180+
}
181+
165182
// wait for status
166183
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
167184
eip, errRet := vpcService.DescribeEipById(ctx, eipId)
@@ -191,14 +208,6 @@ func resourceTencentCloudEipCreate(d *schema.ResourceData, meta interface{}) err
191208
}
192209
}
193210

194-
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
195-
resourceName := BuildTagResourceName(VPC_SERVICE_TYPE, EIP_RESOURCE_TYPE, region, eipId)
196-
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
197-
log.Printf("[CRITAL]%s set eip tags failed: %+v", logId, err)
198-
return err
199-
}
200-
}
201-
202211
return resourceTencentCloudEipRead(d, meta)
203212
}
204213

tencentcloud/resource_tc_eni.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ func resourceTencentCloudEniCreate(d *schema.ResourceData, m interface{}) error
198198
securityGroups []string
199199
ipv4s []VpcEniIP
200200
ipv4Count *int
201+
tags map[string]string
201202
)
202203

203204
if raw, ok := d.GetOk("security_groups"); ok {
@@ -251,7 +252,9 @@ func resourceTencentCloudEniCreate(d *schema.ResourceData, m interface{}) error
251252
return errors.New("ipv4s or ipv4_count must be set")
252253
}
253254

254-
tags := helper.GetTags(d, "tags")
255+
if raw := helper.GetTags(d, "tags"); len(raw) > 0 {
256+
tags = raw
257+
}
255258

256259
client := m.(*TencentCloudClient).apiV3Conn
257260
vpcService := VpcService{client: client}
@@ -265,7 +268,7 @@ func resourceTencentCloudEniCreate(d *schema.ResourceData, m interface{}) error
265268

266269
switch {
267270
case len(ipv4s) > 0 && len(ipv4s) <= 10:
268-
id, err = vpcService.CreateEni(ctx, name, vpcId, subnetId, desc, securityGroups, nil, ipv4s)
271+
id, err = vpcService.CreateEni(ctx, name, vpcId, subnetId, desc, securityGroups, nil, ipv4s, tags)
269272
if err != nil {
270273
return err
271274
}
@@ -289,7 +292,7 @@ func resourceTencentCloudEniCreate(d *schema.ResourceData, m interface{}) error
289292
ipv4ss := chunkEniIP(ipv4s)
290293
withPrimaryIpv4s := ipv4ss[0]
291294

292-
id, err = vpcService.CreateEni(ctx, name, vpcId, subnetId, desc, securityGroups, nil, withPrimaryIpv4s)
295+
id, err = vpcService.CreateEni(ctx, name, vpcId, subnetId, desc, securityGroups, nil, withPrimaryIpv4s, tags)
293296
if err != nil {
294297
return err
295298
}
@@ -303,7 +306,7 @@ func resourceTencentCloudEniCreate(d *schema.ResourceData, m interface{}) error
303306
}
304307

305308
case ipv4Count != nil && *ipv4Count <= 10:
306-
id, err = vpcService.CreateEni(ctx, name, vpcId, subnetId, desc, securityGroups, ipv4Count, nil)
309+
id, err = vpcService.CreateEni(ctx, name, vpcId, subnetId, desc, securityGroups, ipv4Count, nil, tags)
307310
if err != nil {
308311
return err
309312
}
@@ -313,7 +316,7 @@ func resourceTencentCloudEniCreate(d *schema.ResourceData, m interface{}) error
313316
case ipv4Count != nil:
314317
count := *ipv4Count
315318

316-
id, err = vpcService.CreateEni(ctx, name, vpcId, subnetId, desc, securityGroups, common.IntPtr(10), nil)
319+
id, err = vpcService.CreateEni(ctx, name, vpcId, subnetId, desc, securityGroups, common.IntPtr(10), nil, tags)
317320
if err != nil {
318321
return err
319322
}

tencentcloud/resource_tc_image.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ func resourceTencentCloudImage() *schema.Resource {
9393
},
9494
Description: "Cloud disk ID list, When creating a whole machine image based on an instance, specify the data disk ID contained in the image.",
9595
},
96+
"tags": {
97+
Type: schema.TypeMap,
98+
Optional: true,
99+
Description: "Tags of the image.",
100+
},
96101
},
97102
}
98103
}
@@ -150,6 +155,22 @@ func resourceTencentCloudImageCreate(d *schema.ResourceData, meta interface{}) e
150155
"snapshot_ids", "data_disk_ids", "data_disk_ids", "instance_id")
151156
}
152157

158+
if v := helper.GetTags(d, "tags"); len(v) > 0 {
159+
tags := make([]*cvm.Tag, 0)
160+
for tagKey, tagValue := range v {
161+
tag := cvm.Tag{
162+
Key: helper.String(tagKey),
163+
Value: helper.String(tagValue),
164+
}
165+
tags = append(tags, &tag)
166+
}
167+
tagSpecification := cvm.TagSpecification{
168+
ResourceType: helper.String("image"),
169+
Tags: tags,
170+
}
171+
request.TagSpecification = append(request.TagSpecification, &tagSpecification)
172+
}
173+
153174
imageId := ""
154175
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
155176
ratelimit.Check(request.GetAction())
@@ -167,6 +188,17 @@ func resourceTencentCloudImageCreate(d *schema.ResourceData, meta interface{}) e
167188
}
168189
d.SetId(imageId)
169190

191+
// Wait for the tags attached to the vm since tags attachment it's async while vm creation.
192+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
193+
tcClient := meta.(*TencentCloudClient).apiV3Conn
194+
tagService := &TagService{client: tcClient}
195+
resourceName := BuildTagResourceName("cvm", "image", tcClient.Region, imageId)
196+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
197+
// If tags attachment failed, the user will be notified, then plan/apply/update with terraform.
198+
return err
199+
}
200+
}
201+
170202
// wait for status
171203
_, has, errRet := cvmService.DescribeImageById(ctx, imageId, false)
172204
if errRet != nil {
@@ -223,6 +255,14 @@ func resourceTencentCloudImageRead(d *schema.ResourceData, meta interface{}) err
223255
_ = d.Set("snapshot_ids", snapShotSysDisk)
224256
}
225257

258+
client := meta.(*TencentCloudClient).apiV3Conn
259+
tagService := TagService{client}
260+
261+
tags, err := tagService.DescribeResourceTags(ctx, "cvm", "image", client.Region, d.Id())
262+
if err != nil {
263+
return err
264+
}
265+
_ = d.Set("tags", tags)
226266
return nil
227267
}
228268

@@ -246,6 +286,20 @@ func resourceTencentCloudImageUpdate(d *schema.ResourceData, meta interface{}) e
246286
}
247287
}
248288

289+
if d.HasChange("tags") {
290+
oldInterface, newInterface := d.GetChange("tags")
291+
replaceTags, deleteTags := diffTags(oldInterface.(map[string]interface{}), newInterface.(map[string]interface{}))
292+
tagService := TagService{
293+
client: meta.(*TencentCloudClient).apiV3Conn,
294+
}
295+
region := meta.(*TencentCloudClient).apiV3Conn.Region
296+
resourceName := BuildTagResourceName("cvm", "image", region, instanceId)
297+
err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags)
298+
if err != nil {
299+
return err
300+
}
301+
}
302+
249303
return resourceTencentCloudImageRead(d, meta)
250304
}
251305

tencentcloud/resource_tc_key_pair.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func resourceTencentCloudKeyPair() *schema.Resource {
7373
ForceNew: true,
7474
Description: "Specifys to which project the key pair belongs.",
7575
},
76+
"tags": {
77+
Type: schema.TypeMap,
78+
Optional: true,
79+
Description: "Tags of the key pair.",
80+
},
7681
},
7782
}
7883
}

0 commit comments

Comments
 (0)