Skip to content

Commit 86b1cd8

Browse files
authored
Merge pull request #458 from longkai/fix-cvm-tag-async
fix cvm creation while its tags not sync issue
2 parents b0bd09c + 1ec3141 commit 86b1cd8

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

tencentcloud/common.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ func CheckNil(object interface{}, fields map[string]string) (nilFields []string)
191191
return
192192
}
193193

194+
// BuildTagResourceName builds the Tencent Clould specific name of a resource description.
195+
// The format is `qcs:project_id:service_type:region:account:resource`.
196+
// For more information, go to https://cloud.tencent.com/document/product/598/10606.
194197
func BuildTagResourceName(serviceType, resourceType, region, id string) string {
195198
switch serviceType {
196199
case "cos":

tencentcloud/resource_tc_instance.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -540,23 +540,6 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{}
540540
request.UserData = &userData
541541
}
542542

543-
// tags
544-
if v, ok := d.GetOk("tags"); ok {
545-
tags := make([]*cvm.Tag, 0)
546-
for key, value := range v.(map[string]interface{}) {
547-
tag := &cvm.Tag{
548-
Key: helper.String(key),
549-
Value: helper.String(value.(string)),
550-
}
551-
tags = append(tags, tag)
552-
}
553-
tagSpecification := &cvm.TagSpecification{
554-
ResourceType: helper.String("instance"),
555-
Tags: tags,
556-
}
557-
request.TagSpecification = []*cvm.TagSpecification{tagSpecification}
558-
}
559-
560543
instanceId := ""
561544
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
562545
ratelimit.Check("create")
@@ -597,6 +580,17 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{}
597580
return err
598581
}
599582

583+
// Wait for the tags attached to the vm since tags attachment it's async while vm creation.
584+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
585+
tcClient := meta.(*TencentCloudClient).apiV3Conn
586+
tagService := &TagService{client: tcClient}
587+
resourceName := BuildTagResourceName("cvm", "instance", tcClient.Region, instanceId)
588+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
589+
// If tags attachment failed, the user will be notified, then plan/apply/update with terraform.
590+
return err
591+
}
592+
}
593+
600594
if !(d.Get("running_flag").(bool)) {
601595
err = cvmService.StopInstance(ctx, instanceId)
602596
if err != nil {
@@ -941,7 +935,7 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
941935
client: meta.(*TencentCloudClient).apiV3Conn,
942936
}
943937
region := meta.(*TencentCloudClient).apiV3Conn.Region
944-
resourceName := fmt.Sprintf("qcs::cvm:%s:uin/:instance/%s", region, instanceId)
938+
resourceName := BuildTagResourceName("cvm", "instance", region, instanceId)
945939
err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags)
946940
if err != nil {
947941
return err

0 commit comments

Comments
 (0)