Skip to content

Commit d8e5216

Browse files
authored
key pair support tag (#1279)
1 parent 74669e2 commit d8e5216

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

tencentcloud/resource_tc_key_pair.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

tencentcloud/service_tencentcloud_cvm.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,13 +615,29 @@ func (me *CvmService) DescribeKeyPairByFilter(ctx context.Context, id, name stri
615615
return
616616
}
617617

618-
func (me *CvmService) CreateKeyPair(ctx context.Context, keyName, publicKey string, projectId int64) (keyId string, errRet error) {
618+
func (me *CvmService) CreateKeyPair(ctx context.Context, keyName, publicKey string, projectId int64, tags map[string]string) (keyId string, errRet error) {
619619
logId := getLogId(ctx)
620620
request := cvm.NewImportKeyPairRequest()
621621
request.KeyName = &keyName
622622
request.ProjectId = &projectId
623623
request.PublicKey = &publicKey
624624

625+
if len(tags) > 0 {
626+
tagsSpec := make([]*cvm.Tag, 0)
627+
for tagKey, tagValue := range tags {
628+
tag := cvm.Tag{
629+
Key: helper.String(tagKey),
630+
Value: helper.String(tagValue),
631+
}
632+
tagsSpec = append(tagsSpec, &tag)
633+
}
634+
tagSpecification := cvm.TagSpecification{
635+
ResourceType: helper.String("keypair"),
636+
Tags: tagsSpec,
637+
}
638+
request.TagSpecification = append(request.TagSpecification, &tagSpecification)
639+
}
640+
625641
ratelimit.Check(request.GetAction())
626642
response, err := me.client.UseCvmClient().ImportKeyPair(request)
627643
if err != nil {

0 commit comments

Comments
 (0)