Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tencentcloud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
30 changes: 12 additions & 18 deletions tencentcloud/resource_tc_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down