diff --git a/tencentcloud/common.go b/tencentcloud/common.go index db21aea52c..9c5179c9cd 100644 --- a/tencentcloud/common.go +++ b/tencentcloud/common.go @@ -191,6 +191,9 @@ func CheckNil(object interface{}, fields map[string]string) (nilFields []string) return } +// BuildTagResourceName builds the Tencent Clould specific name of a resource description. +// The format is `qcs:project_id:service_type:region:account:resource`. +// For more information, go to https://cloud.tencent.com/document/product/598/10606. func BuildTagResourceName(serviceType, resourceType, region, id string) string { switch serviceType { case "cos": diff --git a/tencentcloud/resource_tc_instance.go b/tencentcloud/resource_tc_instance.go index 9bc768ab5d..d70c0156b3 100644 --- a/tencentcloud/resource_tc_instance.go +++ b/tencentcloud/resource_tc_instance.go @@ -541,23 +541,6 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{} request.UserData = &userData } - // tags - if v, ok := d.GetOk("tags"); ok { - tags := make([]*cvm.Tag, 0) - for key, value := range v.(map[string]interface{}) { - tag := &cvm.Tag{ - Key: helper.String(key), - Value: helper.String(value.(string)), - } - tags = append(tags, tag) - } - tagSpecification := &cvm.TagSpecification{ - ResourceType: helper.String("instance"), - Tags: tags, - } - request.TagSpecification = []*cvm.TagSpecification{tagSpecification} - } - instanceId := "" err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { ratelimit.Check("create") @@ -598,6 +581,17 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{} return err } + // Wait for the tags attached to the vm since tags attachment it's async while vm creation. + if tags := helper.GetTags(d, "tags"); len(tags) > 0 { + tcClient := meta.(*TencentCloudClient).apiV3Conn + tagService := &TagService{client: tcClient} + resourceName := BuildTagResourceName("cvm", "instance", tcClient.Region, instanceId) + if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil { + // If tags attachment failed, the user will be notified, then plan/apply/update with terraform. + return err + } + } + if !(d.Get("running_flag").(bool)) { err = cvmService.StopInstance(ctx, instanceId) if err != nil { @@ -960,7 +954,7 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{} client: meta.(*TencentCloudClient).apiV3Conn, } region := meta.(*TencentCloudClient).apiV3Conn.Region - resourceName := fmt.Sprintf("qcs::cvm:%s:uin/:instance/%s", region, instanceId) + resourceName := BuildTagResourceName("cvm", "instance", region, instanceId) err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags) if err != nil { return err