Skip to content

Commit 2c3926c

Browse files
tongyimingmikatong
andauthored
emr support tag (#2026)
* emr support tag * add changelog --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 70c7e80 commit 2c3926c

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

.changelog/2026.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_emr_cluster: support `tags` param
3+
```

tencentcloud/resource_tc_emr_cluster.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import (
101101
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
102102
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
103103
emr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr/v20190103"
104+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
104105
)
105106

106107
func resourceTencentCloudEmrCluster() *schema.Resource {
@@ -251,6 +252,12 @@ func resourceTencentCloudEmrCluster() *schema.Resource {
251252
ForceNew: true,
252253
Description: "The ID of the security group to which the instance belongs, in the form of sg-xxxxxxxx.",
253254
},
255+
"tags": {
256+
Type: schema.TypeMap,
257+
Optional: true,
258+
Computed: true,
259+
Description: "Tag description list.",
260+
},
254261
},
255262
}
256263
}
@@ -269,6 +276,18 @@ func resourceTencentCloudEmrClusterUpdate(d *schema.ResourceData, meta interface
269276
if !hasTimeUnit || !hasTimeSpan || !hasPayMode {
270277
return innerErr.New("Time_unit, time_span or pay_mode must be set.")
271278
}
279+
if d.HasChange("tags") {
280+
tcClient := meta.(*TencentCloudClient).apiV3Conn
281+
tagService := &TagService{client: tcClient}
282+
oldTags, newTags := d.GetChange("tags")
283+
replaceTags, deleteTags := diffTags(oldTags.(map[string]interface{}), newTags.(map[string]interface{}))
284+
resourceName := BuildTagResourceName("emr", "emr-instance", tcClient.Region, instanceId)
285+
if err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags); err != nil {
286+
return err
287+
}
288+
}
289+
290+
hasChange := false
272291
request := emr.NewScaleOutInstanceRequest()
273292
request.TimeUnit = common.StringPtr(timeUnit.(string))
274293
request.TimeSpan = common.Uint64Ptr((uint64)(timeSpan.(int)))
@@ -280,16 +299,22 @@ func resourceTencentCloudEmrClusterUpdate(d *schema.ResourceData, meta interface
280299

281300
if d.HasChange("resource_spec.0.master_count") {
282301
request.MasterCount = common.Uint64Ptr((uint64)(resourceSpec["master_count"].(int)))
302+
hasChange = true
283303
}
284304
if d.HasChange("resource_spec.0.task_count") {
285305
request.TaskCount = common.Uint64Ptr((uint64)(resourceSpec["task_count"].(int)))
306+
hasChange = true
286307
}
287308
if d.HasChange("resource_spec.0.core_count") {
288309
request.CoreCount = common.Uint64Ptr((uint64)(resourceSpec["core_count"].(int)))
310+
hasChange = true
289311
}
290312
if d.HasChange("extend_fs_field") {
291313
return innerErr.New("extend_fs_field not support update.")
292314
}
315+
if !hasChange {
316+
return nil
317+
}
293318
_, err := emrService.UpdateInstance(ctx, request)
294319
if err != nil {
295320
return err
@@ -364,6 +389,15 @@ func resourceTencentCloudEmrClusterCreate(d *schema.ResourceData, meta interface
364389
if err != nil {
365390
return err
366391
}
392+
393+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
394+
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
395+
region := meta.(*TencentCloudClient).apiV3Conn.Region
396+
resourceName := fmt.Sprintf("qcs::emr:%s:uin/:emr-instance/%s", region, d.Id())
397+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
398+
return err
399+
}
400+
}
367401
return nil
368402
}
369403

@@ -458,5 +492,13 @@ func resourceTencentCloudEmrClusterRead(d *schema.ResourceData, meta interface{}
458492
if err != nil {
459493
return err
460494
}
495+
496+
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
497+
region := meta.(*TencentCloudClient).apiV3Conn.Region
498+
tags, err := tagService.DescribeResourceTags(ctx, "emr", "emr-instance", region, d.Id())
499+
if err != nil {
500+
return err
501+
}
502+
_ = d.Set("tags", tags)
461503
return nil
462504
}

tencentcloud/resource_tc_emr_cluster_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func TestAccTencentCloudEmrClusterResource(t *testing.T) {
132132
resource.TestCheckResourceAttr(testEmrClusterResourceKey, "placement.project_id", "0"),
133133
resource.TestCheckResourceAttrSet(testEmrClusterResourceKey, "instance_id"),
134134
resource.TestCheckResourceAttr(testEmrClusterResourceKey, "sg_id", defaultEMRSgId),
135+
resource.TestCheckResourceAttr(testEmrClusterResourceKey, "tags.emr-key", "emr-value"),
135136
),
136137
},
137138
},
@@ -242,5 +243,8 @@ resource "tencentcloud_emr_cluster" "emrrrr" {
242243
project_id=0
243244
}
244245
sg_id=var.sg_id
246+
tags = {
247+
emr-key = "emr-value"
248+
}
245249
}
246250
`

website/docs/r/emr_cluster.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ When TimeUnit is m, the number filled in by this parameter indicates the length
134134
By default, the cluster Master node internet is enabled.
135135
* `resource_spec` - (Optional, List) Resource specification of EMR instance.
136136
* `sg_id` - (Optional, String, ForceNew) The ID of the security group to which the instance belongs, in the form of sg-xxxxxxxx.
137+
* `tags` - (Optional, Map) Tag description list.
137138

138139
The `resource_spec` object supports the following:
139140

0 commit comments

Comments
 (0)