@@ -2,6 +2,7 @@ package tencentcloud
22
33import (
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