@@ -24,6 +24,8 @@ import (
2424 "context"
2525 "strings"
2626
27+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
28+
2729 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
2830 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
2931 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
@@ -95,9 +97,14 @@ func resourceTencentCloudKeyPairCreate(d *schema.ResourceData, meta interface{})
9597 publicKey := d .Get ("public_key" ).(string )
9698 projectId := d .Get ("project_id" ).(int )
9799
100+ var tags map [string ]string
101+ if temp := helper .GetTags (d , "tags" ); len (temp ) > 0 {
102+ tags = temp
103+ }
104+
98105 keyId := ""
99106 err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
100- id , err := cvmService .CreateKeyPair (ctx , keyName , publicKey , int64 (projectId ))
107+ id , err := cvmService .CreateKeyPair (ctx , keyName , publicKey , int64 (projectId ), tags )
101108 if err != nil {
102109 return retryError (err )
103110 }
@@ -109,6 +116,16 @@ func resourceTencentCloudKeyPairCreate(d *schema.ResourceData, meta interface{})
109116 }
110117 d .SetId (keyId )
111118
119+ if tags := helper .GetTags (d , "tags" ); len (tags ) > 0 {
120+ tcClient := meta .(* TencentCloudClient ).apiV3Conn
121+ tagService := & TagService {client : tcClient }
122+ resourceName := BuildTagResourceName ("cvm" , "keypair" , tcClient .Region , keyId )
123+ if err := tagService .ModifyTags (ctx , resourceName , tags , nil ); err != nil {
124+ // If tags attachment failed, the user will be notified, then plan/apply/update with terraform.
125+ return err
126+ }
127+ }
128+
112129 return resourceTencentCloudKeyPairRead (d , meta )
113130}
114131
@@ -151,6 +168,15 @@ func resourceTencentCloudKeyPairRead(d *schema.ResourceData, meta interface{}) e
151168 _ = d .Set ("public_key" , publicKey )
152169 }
153170
171+ client := meta .(* TencentCloudClient ).apiV3Conn
172+ tagService := TagService {client }
173+
174+ tags , err := tagService .DescribeResourceTags (ctx , "cvm" , "keypair" , client .Region , d .Id ())
175+ if err != nil {
176+ return err
177+ }
178+ _ = d .Set ("tags" , tags )
179+
154180 return nil
155181}
156182
@@ -173,6 +199,20 @@ func resourceTencentCloudKeyPairUpdate(d *schema.ResourceData, meta interface{})
173199 }
174200 }
175201
202+ if d .HasChange ("tags" ) {
203+ oldInterface , newInterface := d .GetChange ("tags" )
204+ replaceTags , deleteTags := diffTags (oldInterface .(map [string ]interface {}), newInterface .(map [string ]interface {}))
205+ tagService := TagService {
206+ client : meta .(* TencentCloudClient ).apiV3Conn ,
207+ }
208+ region := meta .(* TencentCloudClient ).apiV3Conn .Region
209+ resourceName := BuildTagResourceName ("cvm" , "keypair" , region , keyId )
210+ err := tagService .ModifyTags (ctx , resourceName , replaceTags , deleteTags )
211+ if err != nil {
212+ return err
213+ }
214+ }
215+
176216 return resourceTencentCloudKeyPairRead (d , meta )
177217}
178218
0 commit comments