Skip to content

fix(cvm): [123456789] add computed private_key #3353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3353.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_key_pair: add computed attr `private_key` and `create_time`
```
33 changes: 29 additions & 4 deletions tencentcloud/services/cvm/resource_tc_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,21 @@ func ResourceTencentCloudKeyPair() *schema.Resource {
Optional: true,
Description: "Tags of the key pair.",
},
"private_key": {
Type: schema.TypeString,
Computed: true,
Description: "Content of private key in a key pair. Tencent Cloud do not keep private keys. Please keep it properly.",
},
"created_time": {
Type: schema.TypeString,
Computed: true,
Description: "Creation time, which follows the `ISO8601` standard and uses `UTC` time in the format of `YYYY-MM-DDThh:mm:ssZ`.",
},
},
}
}

func cvmCreateKeyPair(ctx context.Context, d *schema.ResourceData, meta interface{}) (keyId string, err error) {
func cvmCreateKeyPair(ctx context.Context, d *schema.ResourceData, meta interface{}) (keyId, privateKey string, err error) {
logId := tccommon.GetLogId(ctx)
request := cvm.NewCreateKeyPairRequest()
response := cvm.NewCreateKeyPairResponse()
Expand Down Expand Up @@ -98,6 +108,9 @@ func cvmCreateKeyPair(ctx context.Context, d *schema.ResourceData, meta interfac
}

keyId = *response.Response.KeyPair.KeyId
if response.Response.KeyPair.PrivateKey != nil {
privateKey = *response.Response.KeyPair.PrivateKey
}
return
}

Expand Down Expand Up @@ -139,20 +152,24 @@ func resourceTencentCloudKeyPairCreate(d *schema.ResourceData, meta interface{})
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
var (
keyId string
err error
keyId, privateKey string
err error
)

if _, ok := d.GetOk("public_key"); ok {
keyId, err = cvmCreateKeyPairByImportPublicKey(ctx, d, meta)
} else {
keyId, err = cvmCreateKeyPair(ctx, d, meta)
keyId, privateKey, err = cvmCreateKeyPair(ctx, d, meta)
}
if err != nil {
return err
}
d.SetId(keyId)

if privateKey != "" {
_ = d.Set("private_key", privateKey)
}

if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
tagService := svctag.NewTagService(tcClient)
Expand Down Expand Up @@ -204,6 +221,14 @@ func resourceTencentCloudKeyPairRead(d *schema.ResourceData, meta interface{}) e
_ = d.Set("public_key", publicKey)
}

if keyPair.PrivateKey != nil {
_ = d.Set("private_key", keyPair.PrivateKey)
}

if keyPair.CreatedTime != nil {
_ = d.Set("created_time", keyPair.CreatedTime)
}

client := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
tagService := svctag.NewTagService(client)

Expand Down
10 changes: 9 additions & 1 deletion tencentcloud/services/cvm/resource_tc_key_pair.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ Example Usage

```hcl
resource "tencentcloud_key_pair" "foo" {
key_name = "terraform_test"
key_name = "terraform_test"
}

output "private_key" {
value = tencentcloud_key_pair.foo.private_key
}

output "create_time" {
value = tencentcloud_key_pair.foo.created_time
}

resource "tencentcloud_key_pair" "foo1" {
Expand Down
11 changes: 10 additions & 1 deletion website/docs/r/key_pair.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ resource "tencentcloud_key_pair" "foo" {
key_name = "terraform_test"
}

output "private_key" {
value = tencentcloud_key_pair.foo.private_key
}

output "create_time" {
value = tencentcloud_key_pair.foo.created_time
}

resource "tencentcloud_key_pair" "foo1" {
key_name = "terraform_test"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDjd8fTnp7Dcuj4mLaQxf9Zs/ORgUL9fQxRCNKkPgP1paTy1I513maMX126i36Lxxl3+FUB52oVbo/FgwlIfX8hyCnv8MCxqnuSDozf1CD0/wRYHcTWAtgHQHBPCC2nJtod6cVC3kB18KeV4U7zsxmwFeBIxojMOOmcOBuh7+trRw=="
Expand All @@ -38,7 +46,8 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

* `id` - ID of the resource.

* `created_time` - Creation time, which follows the `ISO8601` standard and uses `UTC` time in the format of `YYYY-MM-DDThh:mm:ssZ`.
* `private_key` - Content of private key in a key pair. Tencent Cloud do not keep private keys. Please keep it properly.


## Import
Expand Down
Loading