@@ -93,6 +93,11 @@ func resourceTencentCloudImage() *schema.Resource {
9393 },
9494 Description : "Cloud disk ID list, When creating a whole machine image based on an instance, specify the data disk ID contained in the image." ,
9595 },
96+ "tags" : {
97+ Type : schema .TypeMap ,
98+ Optional : true ,
99+ Description : "Tags of the image." ,
100+ },
96101 },
97102 }
98103}
@@ -150,6 +155,22 @@ func resourceTencentCloudImageCreate(d *schema.ResourceData, meta interface{}) e
150155 "snapshot_ids" , "data_disk_ids" , "data_disk_ids" , "instance_id" )
151156 }
152157
158+ if v := helper .GetTags (d , "tags" ); len (v ) > 0 {
159+ tags := make ([]* cvm.Tag , 0 )
160+ for tagKey , tagValue := range v {
161+ tag := cvm.Tag {
162+ Key : helper .String (tagKey ),
163+ Value : helper .String (tagValue ),
164+ }
165+ tags = append (tags , & tag )
166+ }
167+ tagSpecification := cvm.TagSpecification {
168+ ResourceType : helper .String ("image" ),
169+ Tags : tags ,
170+ }
171+ request .TagSpecification = append (request .TagSpecification , & tagSpecification )
172+ }
173+
153174 imageId := ""
154175 err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
155176 ratelimit .Check (request .GetAction ())
@@ -167,6 +188,17 @@ func resourceTencentCloudImageCreate(d *schema.ResourceData, meta interface{}) e
167188 }
168189 d .SetId (imageId )
169190
191+ // Wait for the tags attached to the vm since tags attachment it's async while vm creation.
192+ if tags := helper .GetTags (d , "tags" ); len (tags ) > 0 {
193+ tcClient := meta .(* TencentCloudClient ).apiV3Conn
194+ tagService := & TagService {client : tcClient }
195+ resourceName := BuildTagResourceName ("cvm" , "image" , tcClient .Region , imageId )
196+ if err := tagService .ModifyTags (ctx , resourceName , tags , nil ); err != nil {
197+ // If tags attachment failed, the user will be notified, then plan/apply/update with terraform.
198+ return err
199+ }
200+ }
201+
170202 // wait for status
171203 _ , has , errRet := cvmService .DescribeImageById (ctx , imageId , false )
172204 if errRet != nil {
@@ -223,6 +255,14 @@ func resourceTencentCloudImageRead(d *schema.ResourceData, meta interface{}) err
223255 _ = d .Set ("snapshot_ids" , snapShotSysDisk )
224256 }
225257
258+ client := meta .(* TencentCloudClient ).apiV3Conn
259+ tagService := TagService {client }
260+
261+ tags , err := tagService .DescribeResourceTags (ctx , "cvm" , "image" , client .Region , d .Id ())
262+ if err != nil {
263+ return err
264+ }
265+ _ = d .Set ("tags" , tags )
226266 return nil
227267}
228268
@@ -246,6 +286,20 @@ func resourceTencentCloudImageUpdate(d *schema.ResourceData, meta interface{}) e
246286 }
247287 }
248288
289+ if d .HasChange ("tags" ) {
290+ oldInterface , newInterface := d .GetChange ("tags" )
291+ replaceTags , deleteTags := diffTags (oldInterface .(map [string ]interface {}), newInterface .(map [string ]interface {}))
292+ tagService := TagService {
293+ client : meta .(* TencentCloudClient ).apiV3Conn ,
294+ }
295+ region := meta .(* TencentCloudClient ).apiV3Conn .Region
296+ resourceName := BuildTagResourceName ("cvm" , "image" , region , instanceId )
297+ err := tagService .ModifyTags (ctx , resourceName , replaceTags , deleteTags )
298+ if err != nil {
299+ return err
300+ }
301+ }
302+
249303 return resourceTencentCloudImageRead (d , meta )
250304}
251305
0 commit comments