@@ -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