Skip to content

Commit 981cdc1

Browse files
tongyimingmikatong
andauthored
use emr tag api (#2059)
* use emr tag api * add changelog --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 17d0765 commit 981cdc1

File tree

9 files changed

+6640
-978
lines changed

9 files changed

+6640
-978
lines changed

.changelog/2059.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: Update `tags` logic
3+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ require (
5555
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain v1.0.414
5656
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dts v1.0.628
5757
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/eb v1.0.715
58-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.287
58+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.725
5959
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es v1.0.699
6060
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.514
6161
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/eb v1.0.715 h1:k9wDdxbF
886886
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/eb v1.0.715/go.mod h1:JoFrUxweQaDBzl5jvphkz8uIxvm+pugI25kY8aUdIAo=
887887
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.287 h1:+9COBXAbQmL7aJ39Q/mF50Ykxq4m5kq/y6vbO+u3zgI=
888888
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.287/go.mod h1:xRvd0xsyCoviCiMRfJMh5lODPnLx+bnTfENNx6GHzFA=
889+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.725 h1:r2klzw/HohGsw/piykh3QW5Ft8+7kl0y5KlgNQ8TdAc=
890+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.725/go.mod h1:SW2tOlPfjXsCTKU2fYwG5Ydt8w2ZmrK8p3WBgm7sTTE=
889891
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es v1.0.699 h1:7lFObucmWErYOh+5/AzpgerUAFoO72XqhXrAzFltTIo=
890892
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es v1.0.699/go.mod h1:Iq+JZDHZlYu/dcmpT48SLFESqxL+C9GjRKrQHcw+FjQ=
891893
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.514 h1:MKSaqFCa4PCzzb2gVV+oFpxsiPmnCf6uDJwxf9eOoNU=

tencentcloud/resource_tc_emr_cluster.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ 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"
105104
)
106105

107106
func resourceTencentCloudEmrCluster() *schema.Resource {
@@ -277,12 +276,9 @@ func resourceTencentCloudEmrClusterUpdate(d *schema.ResourceData, meta interface
277276
return innerErr.New("Time_unit, time_span or pay_mode must be set.")
278277
}
279278
if d.HasChange("tags") {
280-
tcClient := meta.(*TencentCloudClient).apiV3Conn
281-
tagService := &TagService{client: tcClient}
282279
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 {
280+
err := emrService.ModifyResourcesTags(ctx, meta.(*TencentCloudClient).apiV3Conn.Region, instanceId, oldTags.(map[string]interface{}), newTags.(map[string]interface{}))
281+
if err != nil {
286282
return err
287283
}
288284
}
@@ -390,14 +386,6 @@ func resourceTencentCloudEmrClusterCreate(d *schema.ResourceData, meta interface
390386
return err
391387
}
392388

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-
}
401389
return nil
402390
}
403391

tencentcloud/service_tencentcloud_emr.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tencentcloud
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"strconv"
78

@@ -163,6 +164,18 @@ func (me *EMRService) CreateInstance(ctx context.Context, d *schema.ResourceData
163164
if v, ok := d.GetOk("extend_fs_field"); ok {
164165
request.ExtendFsField = common.StringPtr(v.(string))
165166
}
167+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
168+
emrTags := make([]*emr.Tag, 0)
169+
for k, v := range tags {
170+
tagKey := k
171+
tagValue := v
172+
emrTags = append(emrTags, &emr.Tag{
173+
TagKey: helper.String(tagKey),
174+
TagValue: helper.String(tagValue),
175+
})
176+
}
177+
request.Tags = emrTags
178+
}
166179

167180
ratelimit.Check(request.GetAction())
168181
//API: https://cloud.tencent.com/document/api/589/34261
@@ -267,3 +280,71 @@ func (me *EMRService) DescribeClusterNodes(ctx context.Context, instanceId, node
267280
nodes = response.Response.NodeList
268281
return
269282
}
283+
284+
func (me *EMRService) ModifyResourcesTags(ctx context.Context, region string, instanceId string, oldTags, newTags map[string]interface{}) error {
285+
resourceName := BuildTagResourceName("emr", "emr-instance", region, instanceId)
286+
rTags, dTags := diffTags(oldTags, newTags)
287+
tagService := &TagService{client: me.client}
288+
if err := tagService.ModifyTags(ctx, resourceName, rTags, dTags); err != nil {
289+
return err
290+
}
291+
292+
addTags := make([]*emr.Tag, 0)
293+
modifyTags := make([]*emr.Tag, 0)
294+
deleteTags := make([]*emr.Tag, 0)
295+
for k, v := range newTags {
296+
tagKey := k
297+
tageValue := v.(string)
298+
_, ok := oldTags[tagKey]
299+
if !ok {
300+
addTags = append(addTags, &emr.Tag{
301+
TagKey: &tagKey,
302+
TagValue: &tageValue,
303+
})
304+
} else if oldTags[tagKey].(string) != tageValue {
305+
modifyTags = append(modifyTags, &emr.Tag{
306+
TagKey: &tagKey,
307+
TagValue: &tageValue,
308+
})
309+
}
310+
}
311+
for k, v := range oldTags {
312+
tagKey := k
313+
tageValue := v.(string)
314+
_, ok := newTags[tagKey]
315+
if !ok {
316+
deleteTags = append(deleteTags, &emr.Tag{
317+
TagKey: &tagKey,
318+
TagValue: &tageValue,
319+
})
320+
}
321+
}
322+
modifyResourceTags := &emr.ModifyResourceTags{
323+
Resource: helper.String(resourceName),
324+
ResourceId: helper.String(instanceId),
325+
ResourceRegion: helper.String(region),
326+
}
327+
if len(addTags) > 0 {
328+
modifyResourceTags.AddTags = addTags
329+
}
330+
if len(modifyTags) > 0 {
331+
modifyResourceTags.ModifyTags = modifyTags
332+
}
333+
if len(deleteTags) > 0 {
334+
modifyResourceTags.DeleteTags = deleteTags
335+
}
336+
337+
request := emr.NewModifyResourcesTagsRequest()
338+
ratelimit.Check(request.GetAction())
339+
request.ModifyType = helper.String("Cluster")
340+
request.ModifyResourceTagsInfoList = []*emr.ModifyResourceTags{modifyResourceTags}
341+
342+
response, err := me.client.UseEmrClient().ModifyResourcesTags(request)
343+
if err != nil {
344+
return err
345+
}
346+
if response != nil && response.Response != nil && len(response.Response.FailList) > 0 {
347+
return fmt.Errorf("file resource list: %v", response.Response.FailList)
348+
}
349+
return nil
350+
}

0 commit comments

Comments
 (0)