Skip to content

Commit 4944853

Browse files
authored
Merge pull request #2093 from tencentcloudstack/feat/kms
Feat/kms
2 parents 0c8d58e + 0d0b1ea commit 4944853

File tree

7 files changed

+242
-0
lines changed

7 files changed

+242
-0
lines changed

.changelog/2093.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
tencentcloud_kms_public_key
3+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Use this data source to query detailed information of kms public_key
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_kms_public_key" "example" {
8+
key_id = tencentcloud_kms_key.example.id
9+
}
10+
11+
resource "tencentcloud_kms_key" "example" {
12+
alias = "tf-example-kms-key"
13+
description = "example of kms key"
14+
key_usage = "ASYMMETRIC_DECRYPT_RSA_2048"
15+
is_enabled = true
16+
pending_delete_window_in_days = 7
17+
}
18+
```
19+
*/
20+
package tencentcloud
21+
22+
import (
23+
"context"
24+
25+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
26+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
27+
kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118"
28+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
29+
)
30+
31+
func dataSourceTencentCloudKmsPublicKey() *schema.Resource {
32+
return &schema.Resource{
33+
Read: dataSourceTencentCloudKmsPublicKeyRead,
34+
Schema: map[string]*schema.Schema{
35+
"key_id": {
36+
Required: true,
37+
Type: schema.TypeString,
38+
Description: "CMK unique identifier.",
39+
},
40+
"public_key": {
41+
Computed: true,
42+
Type: schema.TypeString,
43+
Description: "Base64-encoded public key content.",
44+
},
45+
"public_key_pem": {
46+
Computed: true,
47+
Type: schema.TypeString,
48+
Description: "Public key content in PEM format.",
49+
},
50+
"result_output_file": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Description: "Used to save results.",
54+
},
55+
},
56+
}
57+
}
58+
59+
func dataSourceTencentCloudKmsPublicKeyRead(d *schema.ResourceData, meta interface{}) error {
60+
defer logElapsed("data_source.tencentcloud_kms_public_key.read")()
61+
defer inconsistentCheck(d, meta)()
62+
63+
var (
64+
logId = getLogId(contextNil)
65+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
66+
service = KmsService{client: meta.(*TencentCloudClient).apiV3Conn}
67+
publicKey *kms.GetPublicKeyResponseParams
68+
keyId string
69+
)
70+
71+
paramMap := make(map[string]interface{})
72+
if v, ok := d.GetOk("key_id"); ok {
73+
paramMap["KeyId"] = helper.String(v.(string))
74+
keyId = v.(string)
75+
}
76+
77+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
78+
result, e := service.DescribeKmsPublicKeyByFilter(ctx, paramMap)
79+
if e != nil {
80+
return retryError(e)
81+
}
82+
83+
publicKey = result
84+
return nil
85+
})
86+
87+
if err != nil {
88+
return err
89+
}
90+
91+
if publicKey.KeyId != nil {
92+
_ = d.Set("key_id", publicKey.KeyId)
93+
}
94+
95+
if publicKey.PublicKey != nil {
96+
_ = d.Set("public_key", publicKey.PublicKey)
97+
}
98+
99+
if publicKey.PublicKeyPem != nil {
100+
_ = d.Set("public_key_pem", publicKey.PublicKeyPem)
101+
}
102+
103+
d.SetId(keyId)
104+
output, ok := d.GetOk("result_output_file")
105+
if ok && output.(string) != "" {
106+
if e := writeToFile(output.(string), d); e != nil {
107+
return e
108+
}
109+
}
110+
111+
return nil
112+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package tencentcloud
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
// go test -i; go test -test.run TestAccTencentCloudKmsPublicKeyDataSource_basic -v
12+
func TestAccTencentCloudKmsPublicKeyDataSource_basic(t *testing.T) {
13+
t.Parallel()
14+
rName := fmt.Sprintf("tf-testacc-kms-key-%s", acctest.RandString(13))
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() {
17+
testAccPreCheck(t)
18+
},
19+
Providers: testAccProviders,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: fmt.Sprintf(testAccKmsPublicKeyDataSource, rName),
23+
Check: resource.ComposeTestCheckFunc(
24+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_kms_public_key.example"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
const testAccKmsPublicKeyDataSource = `
32+
data "tencentcloud_kms_public_key" "example" {
33+
key_id = tencentcloud_kms_key.example.id
34+
}
35+
36+
resource "tencentcloud_kms_key" "example" {
37+
alias = "%s"
38+
description = "example of kms key"
39+
key_usage = "ASYMMETRIC_DECRYPT_RSA_2048"
40+
is_enabled = true
41+
}
42+
`

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ Global Application Acceleration(GAAP)
536536
Key Management Service(KMS)
537537
Data Source
538538
tencentcloud_kms_keys
539+
tencentcloud_kms_public_key
539540
540541
Resource
541542
tencentcloud_kms_key
@@ -2043,6 +2044,7 @@ func Provider() *schema.Provider {
20432044
"tencentcloud_protocol_templates": dataSourceTencentCloudProtocolTemplates(),
20442045
"tencentcloud_protocol_template_groups": dataSourceTencentCloudProtocolTemplateGroups(),
20452046
"tencentcloud_kms_keys": dataSourceTencentCloudKmsKeys(),
2047+
"tencentcloud_kms_public_key": dataSourceTencentCloudKmsPublicKey(),
20462048
"tencentcloud_ssm_products": dataSourceTencentCloudSsmProducts(),
20472049
"tencentcloud_ssm_secrets": dataSourceTencentCloudSsmSecrets(),
20482050
"tencentcloud_ssm_secret_versions": dataSourceTencentCloudSsmSecretVersions(),

tencentcloud/service_tencentcloud_kms.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,39 @@ func (me *KmsService) DeleteImportKeyMaterial(ctx context.Context, keyId string)
404404

405405
return nil
406406
}
407+
408+
func (me *KmsService) DescribeKmsPublicKeyByFilter(ctx context.Context, param map[string]interface{}) (publicKey *kms.GetPublicKeyResponseParams, errRet error) {
409+
var (
410+
logId = getLogId(ctx)
411+
request = kms.NewGetPublicKeyRequest()
412+
)
413+
414+
defer func() {
415+
if errRet != nil {
416+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
417+
}
418+
}()
419+
420+
for k, v := range param {
421+
if k == "KeyId" {
422+
request.KeyId = v.(*string)
423+
}
424+
}
425+
426+
ratelimit.Check(request.GetAction())
427+
428+
response, err := me.client.UseKmsClient().GetPublicKey(request)
429+
if err != nil {
430+
errRet = err
431+
return
432+
}
433+
434+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
435+
436+
if response == nil {
437+
return
438+
}
439+
440+
publicKey = response.Response
441+
return
442+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
subcategory: "Key Management Service(KMS)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_kms_public_key"
5+
sidebar_current: "docs-tencentcloud-datasource-kms_public_key"
6+
description: |-
7+
Use this data source to query detailed information of kms public_key
8+
---
9+
10+
# tencentcloud_kms_public_key
11+
12+
Use this data source to query detailed information of kms public_key
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "tencentcloud_kms_public_key" "example" {
18+
key_id = tencentcloud_kms_key.example.id
19+
}
20+
21+
resource "tencentcloud_kms_key" "example" {
22+
alias = "tf-example-kms-key"
23+
description = "example of kms key"
24+
key_usage = "ASYMMETRIC_DECRYPT_RSA_2048"
25+
is_enabled = true
26+
pending_delete_window_in_days = 7
27+
}
28+
```
29+
30+
## Argument Reference
31+
32+
The following arguments are supported:
33+
34+
* `key_id` - (Required, String) CMK unique identifier.
35+
* `result_output_file` - (Optional, String) Used to save results.
36+
37+
## Attributes Reference
38+
39+
In addition to all arguments above, the following attributes are exported:
40+
41+
* `public_key_pem` - Public key content in PEM format.
42+
* `public_key` - Base64-encoded public key content.
43+
44+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,9 @@
18631863
<li>
18641864
<a href="/docs/providers/tencentcloud/d/kms_keys.html">tencentcloud_kms_keys</a>
18651865
</li>
1866+
<li>
1867+
<a href="/docs/providers/tencentcloud/d/kms_public_key.html">tencentcloud_kms_public_key</a>
1868+
</li>
18661869
</ul>
18671870
</li>
18681871
<li>

0 commit comments

Comments
 (0)