Skip to content

Commit 3a5a453

Browse files
tongyimingmikatong
andauthored
fix cvm tag (#2464)
* fix cvm tag * add changelog * fix cvm basic test * update * update cvm test --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 777da25 commit 3a5a453

File tree

4 files changed

+50
-99
lines changed

4 files changed

+50
-99
lines changed

.changelog/2464.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_instance: fix tag conflict
3+
```

tencentcloud/acctest/basic.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ const (
6464

6565
DefaultCvmAZone = "ap-guangzhou-7"
6666
DefaultCvmInternationalZone = "ap-guangzhou-3"
67-
DefaultCvmVpcId = "vpc-l0dw94uh"
68-
DefaultCvmSubnetId = "subnet-ccj2qg0m"
67+
DefaultCvmVpcId = "vpc-gmq0mxoj"
68+
DefaultCvmSubnetId = "subnet-cpknsqgo"
6969
DefaultCvmTestingAZone = "ap-guangzhou-2"
7070
DefaultCvmTestingVpcId = "vpc-701bm52d"
7171
DefaultCvmTestingSubnetId = "subnet-1q62lj3m"
@@ -358,9 +358,10 @@ data "tencentcloud_instance_types" "default" {
358358
}
359359
filter {
360360
name = "instance-family"
361-
values = ["S1", "S2", "S3", "S4", "S5", "SR1", "SA1", "SA2"]
361+
values = ["S1", "S2", "S3", "S4", "S5"]
362362
}
363363
cpu_core_count = 2
364+
memory_size = 2
364365
exclude_sold_out = true
365366
}
366367
`

tencentcloud/services/cvm/resource_tc_instance.go

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,6 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{}
669669
}
670670
d.SetId(instanceId)
671671

672-
//get system disk ID and data disk ID
673-
var systemDiskId string
674-
var dataDiskIds []string
675672
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
676673
instance, errRet := cvmService.DescribeInstanceById(ctx, instanceId)
677674
if errRet != nil {
@@ -682,17 +679,6 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{}
682679
return resource.NonRetryableError(fmt.Errorf("cvm instance %s launch failed, this resource will not be stored to tfstate and will auto removed\n.", *instance.InstanceId))
683680
}
684681
if instance != nil && *instance.InstanceState == CVM_STATUS_RUNNING {
685-
//get system disk ID
686-
if instance.SystemDisk != nil && instance.SystemDisk.DiskId != nil {
687-
systemDiskId = *instance.SystemDisk.DiskId
688-
}
689-
if instance.DataDisks != nil {
690-
for _, dataDisk := range instance.DataDisks {
691-
if dataDisk != nil && dataDisk.DiskId != nil {
692-
dataDiskIds = append(dataDiskIds, *dataDisk.DiskId)
693-
}
694-
}
695-
}
696682
return nil
697683
}
698684
return resource.RetryableError(fmt.Errorf("cvm instance status is %s, retry...", *instance.InstanceState))
@@ -706,31 +692,20 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{}
706692
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
707693
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
708694
tagService := svctag.NewTagService(tcClient)
709-
resourceName := tccommon.BuildTagResourceName("cvm", "instance", tcClient.Region, instanceId)
710-
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
711-
// If tags attachment failed, the user will be notified, then plan/apply/update with terraform.
712-
return err
713-
}
714-
715-
//except instance ,system disk and data disk will be tagged
716-
//keep logical consistence with the console
717-
//tag system disk
718-
if systemDiskId != "" {
719-
resourceName = tccommon.BuildTagResourceName("cvm", "volume", tcClient.Region, systemDiskId)
720-
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
721-
// If tags attachment failed, the user will be notified, then plan/apply/update with terraform.
722-
return err
695+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
696+
actualTags, e := tagService.DescribeResourceTags(ctx, "cvm", "instance", tcClient.Region, instanceId)
697+
if e != nil {
698+
return resource.RetryableError(e)
723699
}
724-
}
725-
//tag disk ids
726-
for _, diskId := range dataDiskIds {
727-
if diskId != "" {
728-
resourceName = tccommon.BuildTagResourceName("cvm", "volume", tcClient.Region, diskId)
729-
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
730-
// If tags attachment failed, the user will be notified, then plan/apply/update with terraform.
731-
return err
700+
for tagKey, tagValue := range tags {
701+
if v, ok := actualTags[tagKey]; !ok || v != tagValue {
702+
return resource.RetryableError(fmt.Errorf("tag(%s, %s) modification is not completed", tagKey, tagValue))
732703
}
733704
}
705+
return nil
706+
})
707+
if err != nil {
708+
return err
734709
}
735710
}
736711

0 commit comments

Comments
 (0)