Skip to content

Commit fa79f95

Browse files
authored
fix(cvm): [123456789] add computed private_key (#3353)
* add private key * add changelog
1 parent 2093ad6 commit fa79f95

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

.changelog/3353.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_key_pair: add computed attr `private_key` and `create_time`
3+
```

tencentcloud/services/cvm/resource_tc_key_pair.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,21 @@ func ResourceTencentCloudKeyPair() *schema.Resource {
6666
Optional: true,
6767
Description: "Tags of the key pair.",
6868
},
69+
"private_key": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
Description: "Content of private key in a key pair. Tencent Cloud do not keep private keys. Please keep it properly.",
73+
},
74+
"created_time": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
Description: "Creation time, which follows the `ISO8601` standard and uses `UTC` time in the format of `YYYY-MM-DDThh:mm:ssZ`.",
78+
},
6979
},
7080
}
7181
}
7282

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

100110
keyId = *response.Response.KeyPair.KeyId
111+
if response.Response.KeyPair.PrivateKey != nil {
112+
privateKey = *response.Response.KeyPair.PrivateKey
113+
}
101114
return
102115
}
103116

@@ -139,20 +152,24 @@ func resourceTencentCloudKeyPairCreate(d *schema.ResourceData, meta interface{})
139152
logId := tccommon.GetLogId(tccommon.ContextNil)
140153
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
141154
var (
142-
keyId string
143-
err error
155+
keyId, privateKey string
156+
err error
144157
)
145158

146159
if _, ok := d.GetOk("public_key"); ok {
147160
keyId, err = cvmCreateKeyPairByImportPublicKey(ctx, d, meta)
148161
} else {
149-
keyId, err = cvmCreateKeyPair(ctx, d, meta)
162+
keyId, privateKey, err = cvmCreateKeyPair(ctx, d, meta)
150163
}
151164
if err != nil {
152165
return err
153166
}
154167
d.SetId(keyId)
155168

169+
if privateKey != "" {
170+
_ = d.Set("private_key", privateKey)
171+
}
172+
156173
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
157174
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
158175
tagService := svctag.NewTagService(tcClient)
@@ -204,6 +221,14 @@ func resourceTencentCloudKeyPairRead(d *schema.ResourceData, meta interface{}) e
204221
_ = d.Set("public_key", publicKey)
205222
}
206223

224+
if keyPair.PrivateKey != nil {
225+
_ = d.Set("private_key", keyPair.PrivateKey)
226+
}
227+
228+
if keyPair.CreatedTime != nil {
229+
_ = d.Set("created_time", keyPair.CreatedTime)
230+
}
231+
207232
client := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
208233
tagService := svctag.NewTagService(client)
209234

tencentcloud/services/cvm/resource_tc_key_pair.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ Example Usage
44

55
```hcl
66
resource "tencentcloud_key_pair" "foo" {
7-
key_name = "terraform_test"
7+
key_name = "terraform_test"
8+
}
9+
10+
output "private_key" {
11+
value = tencentcloud_key_pair.foo.private_key
12+
}
13+
14+
output "create_time" {
15+
value = tencentcloud_key_pair.foo.created_time
816
}
917
1018
resource "tencentcloud_key_pair" "foo1" {

website/docs/r/key_pair.html.markdown

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ resource "tencentcloud_key_pair" "foo" {
1818
key_name = "terraform_test"
1919
}
2020
21+
output "private_key" {
22+
value = tencentcloud_key_pair.foo.private_key
23+
}
24+
25+
output "create_time" {
26+
value = tencentcloud_key_pair.foo.created_time
27+
}
28+
2129
resource "tencentcloud_key_pair" "foo1" {
2230
key_name = "terraform_test"
2331
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDjd8fTnp7Dcuj4mLaQxf9Zs/ORgUL9fQxRCNKkPgP1paTy1I513maMX126i36Lxxl3+FUB52oVbo/FgwlIfX8hyCnv8MCxqnuSDozf1CD0/wRYHcTWAtgHQHBPCC2nJtod6cVC3kB18KeV4U7zsxmwFeBIxojMOOmcOBuh7+trRw=="
@@ -38,7 +46,8 @@ The following arguments are supported:
3846
In addition to all arguments above, the following attributes are exported:
3947

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

4352

4453
## Import

0 commit comments

Comments
 (0)