Skip to content

Commit 0b56fcb

Browse files
tongyimingmikatong
and
mikatong
authored
update tags (#3299)
Co-authored-by: mikatong <[email protected]>
1 parent 0e586c3 commit 0b56fcb

File tree

2 files changed

+76
-65
lines changed

2 files changed

+76
-65
lines changed

tencentcloud/services/cls/resource_tc_cls_topic.go

Lines changed: 75 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77

88
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
9+
svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"
910

1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -374,6 +375,7 @@ func resourceTencentCloudClsTopicUpdate(d *schema.ResourceData, meta interface{}
374375
request = cls.NewModifyTopicRequest()
375376
id = d.Id()
376377
isWebTracking bool
378+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
377379
)
378380

379381
immutableArgs := []string{"partition_count", "storage_type"}
@@ -384,110 +386,122 @@ func resourceTencentCloudClsTopicUpdate(d *schema.ResourceData, meta interface{}
384386
}
385387
}
386388

389+
if d.HasChange("tags") {
390+
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
391+
tagService := svctag.NewTagService(tcClient)
392+
oldTags, newTags := d.GetChange("tags")
393+
replaceTags, deleteTags := svctag.DiffTags(oldTags.(map[string]interface{}), newTags.(map[string]interface{}))
394+
resourceName := tccommon.BuildTagResourceName("cls", "topic", tcClient.Region, id)
395+
if err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags); err != nil {
396+
return err
397+
}
398+
}
399+
400+
var hasChange bool
387401
request.TopicId = helper.String(id)
388402

389403
if d.HasChange("topic_name") {
390404
request.TopicName = helper.String(d.Get("topic_name").(string))
391-
}
392-
393-
if d.HasChange("tags") {
394-
tags := d.Get("tags").(map[string]interface{})
395-
request.Tags = make([]*cls.Tag, 0, len(tags))
396-
for k, v := range tags {
397-
key := k
398-
value := v
399-
request.Tags = append(request.Tags, &cls.Tag{
400-
Key: &key,
401-
Value: helper.String(value.(string)),
402-
})
403-
}
405+
hasChange = true
404406
}
405407

406408
if d.HasChange("auto_split") {
407409
request.AutoSplit = helper.Bool(d.Get("auto_split").(bool))
410+
hasChange = true
408411
}
409412

410413
if d.HasChange("max_split_partitions") {
411414
request.MaxSplitPartitions = helper.IntInt64(d.Get("max_split_partitions").(int))
415+
hasChange = true
412416
}
413417

414418
if d.HasChange("period") {
415419
request.Period = helper.IntInt64(d.Get("period").(int))
420+
hasChange = true
416421
}
417422

418423
if d.HasChange("hot_period") {
419424
request.HotPeriod = helper.IntUint64(d.Get("hot_period").(int))
425+
hasChange = true
420426
}
421427

422428
if d.HasChange("describes") {
423429
request.Describes = helper.String(d.Get("describes").(string))
430+
hasChange = true
424431
}
425432

426-
if v, ok := d.GetOkExists("is_web_tracking"); ok {
427-
request.IsWebTracking = helper.Bool(v.(bool))
428-
isWebTracking = v.(bool)
433+
if d.HasChange("is_web_tracking") {
434+
if v, ok := d.GetOkExists("is_web_tracking"); ok {
435+
request.IsWebTracking = helper.Bool(v.(bool))
436+
isWebTracking = v.(bool)
437+
hasChange = true
438+
}
429439
}
440+
if d.HasChange("extends") {
441+
if isWebTracking {
442+
if dMap, ok := helper.InterfacesHeadMap(d, "extends"); ok {
443+
if anonymousAccessMap, ok := helper.InterfaceToMap(dMap, "anonymous_access"); ok {
444+
topicExtendInfo := cls.TopicExtendInfo{}
445+
anonymousInfo := cls.AnonymousInfo{}
446+
if v, ok := anonymousAccessMap["operations"]; ok {
447+
tmpList := make([]*string, 0)
448+
for _, operation := range v.([]interface{}) {
449+
tmpList = append(tmpList, helper.String(operation.(string)))
450+
}
430451

431-
if isWebTracking {
432-
if dMap, ok := helper.InterfacesHeadMap(d, "extends"); ok {
433-
if anonymousAccessMap, ok := helper.InterfaceToMap(dMap, "anonymous_access"); ok {
434-
topicExtendInfo := cls.TopicExtendInfo{}
435-
anonymousInfo := cls.AnonymousInfo{}
436-
if v, ok := anonymousAccessMap["operations"]; ok {
437-
tmpList := make([]*string, 0)
438-
for _, operation := range v.([]interface{}) {
439-
tmpList = append(tmpList, helper.String(operation.(string)))
452+
anonymousInfo.Operations = tmpList
440453
}
441454

442-
anonymousInfo.Operations = tmpList
443-
}
455+
if v, ok := anonymousAccessMap["conditions"]; ok {
456+
for _, condition := range v.([]interface{}) {
457+
conditionMap := condition.(map[string]interface{})
458+
conditionInfo := cls.ConditionInfo{}
459+
if v, ok := conditionMap["attributes"]; ok {
460+
conditionInfo.Attributes = helper.String(v.(string))
461+
}
444462

445-
if v, ok := anonymousAccessMap["conditions"]; ok {
446-
for _, condition := range v.([]interface{}) {
447-
conditionMap := condition.(map[string]interface{})
448-
conditionInfo := cls.ConditionInfo{}
449-
if v, ok := conditionMap["attributes"]; ok {
450-
conditionInfo.Attributes = helper.String(v.(string))
451-
}
463+
if v, ok := conditionMap["rule"]; ok {
464+
conditionInfo.Rule = helper.IntUint64(v.(int))
465+
}
452466

453-
if v, ok := conditionMap["rule"]; ok {
454-
conditionInfo.Rule = helper.IntUint64(v.(int))
455-
}
467+
if v, ok := conditionMap["condition_value"]; ok {
468+
conditionInfo.ConditionValue = helper.String(v.(string))
469+
}
456470

457-
if v, ok := conditionMap["condition_value"]; ok {
458-
conditionInfo.ConditionValue = helper.String(v.(string))
471+
anonymousInfo.Conditions = append(anonymousInfo.Conditions, &conditionInfo)
459472
}
460-
461-
anonymousInfo.Conditions = append(anonymousInfo.Conditions, &conditionInfo)
462473
}
463-
}
464474

465-
topicExtendInfo.AnonymousAccess = &anonymousInfo
466-
request.Extends = &topicExtendInfo
475+
topicExtendInfo.AnonymousAccess = &anonymousInfo
476+
request.Extends = &topicExtendInfo
477+
}
478+
} else {
479+
return fmt.Errorf("If `is_web_tracking` is true, Must set `extends` params.\n.")
467480
}
468481
} else {
469-
return fmt.Errorf("If `is_web_tracking` is true, Must set `extends` params.\n.")
470-
}
471-
} else {
472-
if _, ok := helper.InterfacesHeadMap(d, "extends"); ok {
473-
return fmt.Errorf("If `is_web_tracking` is false, Not support set `extends` params.\n.")
482+
if _, ok := helper.InterfacesHeadMap(d, "extends"); ok {
483+
return fmt.Errorf("If `is_web_tracking` is false, Not support set `extends` params.\n.")
484+
}
474485
}
486+
hasChange = true
475487
}
476488

477-
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
478-
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClsClient().ModifyTopic(request)
479-
if e != nil {
480-
return tccommon.RetryError(e)
481-
} else {
482-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
483-
logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
484-
}
489+
if hasChange {
490+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
491+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClsClient().ModifyTopic(request)
492+
if e != nil {
493+
return tccommon.RetryError(e)
494+
} else {
495+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
496+
logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
497+
}
485498

486-
return nil
487-
})
499+
return nil
500+
})
488501

489-
if err != nil {
490-
return err
502+
if err != nil {
503+
return err
504+
}
491505
}
492506

493507
return resourceTencentCloudClsTopicRead(d, meta)

tencentcloud/services/cls/resource_tc_cls_topic_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@ resource "tencentcloud_cls_topic" "example" {
181181
period = 30
182182
storage_type = "hot"
183183
describes = "Test Demo Update."
184-
hot_period = 15
185-
tags = {
186-
"test" = "test",
187-
}
184+
hot_period = 10
188185
}
189186
`

0 commit comments

Comments
 (0)