@@ -87,7 +87,6 @@ import (
8787 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8888 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
8989 cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
90- vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
9190 "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
9291 "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/ratelimit"
9392)
@@ -541,23 +540,6 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{}
541540 request .UserData = & userData
542541 }
543542
544- // tags
545- if v , ok := d .GetOk ("tags" ); ok {
546- tags := make ([]* cvm.Tag , 0 )
547- for key , value := range v .(map [string ]interface {}) {
548- tag := & cvm.Tag {
549- Key : helper .String (key ),
550- Value : helper .String (value .(string )),
551- }
552- tags = append (tags , tag )
553- }
554- tagSpecification := & cvm.TagSpecification {
555- ResourceType : helper .String ("instance" ),
556- Tags : tags ,
557- }
558- request .TagSpecification = []* cvm.TagSpecification {tagSpecification }
559- }
560-
561543 instanceId := ""
562544 err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
563545 ratelimit .Check ("create" )
@@ -598,6 +580,17 @@ func resourceTencentCloudInstanceCreate(d *schema.ResourceData, meta interface{}
598580 return err
599581 }
600582
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+
601594 if ! (d .Get ("running_flag" ).(bool )) {
602595 err = cvmService .StopInstance (ctx , instanceId )
603596 if err != nil {
@@ -695,26 +688,8 @@ func resourceTencentCloudInstanceRead(d *schema.ResourceData, meta interface{})
695688 _ = d .Set ("create_time" , instance .CreatedTime )
696689 _ = d .Set ("expired_time" , instance .ExpiredTime )
697690
698- if len (instance .PublicIpAddresses ) > 0 {
699- vpcService := VpcService {client : client }
700- filter := map [string ][]string {
701- "address-ip" : {* instance .PublicIpAddresses [0 ]},
702- }
703- var eips []* vpc.Address
704- var errRet error
705- err := resource .Retry (readRetryTimeout , func () * resource.RetryError {
706- eips , errRet = vpcService .DescribeEipByFilter (ctx , filter )
707- if errRet != nil {
708- return retryError (errRet , InternalError )
709- }
710- return nil
711- })
712- if err != nil {
713- return err
714- }
715- _ = d .Set ("allocate_public_ip" , len (eips ) < 1 )
716- } else {
717- _ = d .Set ("allocate_public_ip" , false )
691+ if _ , ok := d .GetOkExists ("allocate_public_ip" ); ! ok {
692+ _ = d .Set ("allocate_public_ip" , len (instance .PublicIpAddresses ) > 0 )
718693 }
719694
720695 // as attachment add tencentcloud:autoscaling:auto-scaling-group-id tag automatically
@@ -960,7 +935,7 @@ func resourceTencentCloudInstanceUpdate(d *schema.ResourceData, meta interface{}
960935 client : meta .(* TencentCloudClient ).apiV3Conn ,
961936 }
962937 region := meta .(* TencentCloudClient ).apiV3Conn .Region
963- resourceName := fmt . Sprintf ( "qcs:: cvm:%s:uin/: instance/%s " , region , instanceId )
938+ resourceName := BuildTagResourceName ( " cvm" , " instance" , region , instanceId )
964939 err := tagService .ModifyTags (ctx , resourceName , replaceTags , deleteTags )
965940 if err != nil {
966941 return err
0 commit comments