Skip to content

Commit f2e1fb2

Browse files
authored
feat/kms (#2171)
* feat/kms * feat/kms
1 parent d8c42e1 commit f2e1fb2

17 files changed

+1454
-0
lines changed

.changelog/2171.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```release-note:new-resource
2+
tencentcloud_kms_white_box_key
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_kms_describe_keys
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_kms_white_box_key_details
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_kms_list_keys
15+
```
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
Use this data source to query detailed information of kms key_lists
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_kms_describe_keys" "example" {
8+
key_ids = [
9+
"9ffacc8b-6461-11ee-a54e-525400dd8a7d",
10+
"bffae4ed-6465-11ee-90b2-5254000ef00e"
11+
]
12+
}
13+
```
14+
*/
15+
package tencentcloud
16+
17+
import (
18+
"context"
19+
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
22+
kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118"
23+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
24+
)
25+
26+
func dataSourceTencentCloudKmsDescribeKeys() *schema.Resource {
27+
return &schema.Resource{
28+
Read: dataSourceTencentCloudKmsDescribeKeysRead,
29+
Schema: map[string]*schema.Schema{
30+
"key_ids": {
31+
Required: true,
32+
Type: schema.TypeSet,
33+
Elem: &schema.Schema{Type: schema.TypeString},
34+
Description: "Query the ID list of CMK, batch query supports up to 100 KeyIds at a time.",
35+
},
36+
"key_list": {
37+
Type: schema.TypeList,
38+
Computed: true,
39+
Description: "A list of KMS keys.",
40+
Elem: &schema.Resource{
41+
Schema: map[string]*schema.Schema{
42+
"key_id": {
43+
Type: schema.TypeString,
44+
Computed: true,
45+
Description: "ID of CMK.",
46+
},
47+
"alias": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
Description: "Name of CMK.",
51+
},
52+
"create_time": {
53+
Type: schema.TypeInt,
54+
Computed: true,
55+
Description: "Create time of CMK.",
56+
},
57+
"description": {
58+
Type: schema.TypeString,
59+
Computed: true,
60+
Description: "Description of CMK.",
61+
},
62+
"key_state": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
Description: "State of CMK.",
66+
},
67+
"key_usage": {
68+
Type: schema.TypeString,
69+
Computed: true,
70+
Description: "Usage of CMK.",
71+
},
72+
"creator_uin": {
73+
Type: schema.TypeInt,
74+
Computed: true,
75+
Description: "Uin of CMK Creator.",
76+
},
77+
"key_rotation_enabled": {
78+
Type: schema.TypeBool,
79+
Computed: true,
80+
Description: "Specify whether to enable key rotation.",
81+
},
82+
"owner": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
Description: "Creator of CMK.",
86+
},
87+
"next_rotate_time": {
88+
Type: schema.TypeInt,
89+
Computed: true,
90+
Description: "Next rotate time of CMK when key_rotation_enabled is true.",
91+
},
92+
"deletion_date": {
93+
Type: schema.TypeInt,
94+
Computed: true,
95+
Description: "Delete time of CMK.",
96+
},
97+
"origin": {
98+
Type: schema.TypeString,
99+
Computed: true,
100+
Description: "Origin of CMK. `TENCENT_KMS` - CMK created by KMS, `EXTERNAL` - CMK imported by user.",
101+
},
102+
"valid_to": {
103+
Type: schema.TypeInt,
104+
Computed: true,
105+
Description: "Valid when origin is `EXTERNAL`, it means the effective date of the key material.",
106+
},
107+
},
108+
},
109+
},
110+
"result_output_file": {
111+
Type: schema.TypeString,
112+
Optional: true,
113+
Description: "Used to save results.",
114+
},
115+
},
116+
}
117+
}
118+
119+
func dataSourceTencentCloudKmsDescribeKeysRead(d *schema.ResourceData, meta interface{}) error {
120+
defer logElapsed("data_source.tencentcloud_kms_describe_keys.read")()
121+
defer inconsistentCheck(d, meta)()
122+
123+
var (
124+
logId = getLogId(contextNil)
125+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
126+
service = KmsService{client: meta.(*TencentCloudClient).apiV3Conn}
127+
keyMetadata []*kms.KeyMetadata
128+
)
129+
130+
paramMap := make(map[string]interface{})
131+
if v, ok := d.GetOk("key_ids"); ok {
132+
keyIdsSet := v.(*schema.Set).List()
133+
paramMap["KeyIds"] = helper.InterfacesStringsPoint(keyIdsSet)
134+
}
135+
136+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
137+
result, e := service.DescribeKmsKeyListsByFilter(ctx, paramMap)
138+
if e != nil {
139+
return retryError(e)
140+
}
141+
142+
keyMetadata = result
143+
return nil
144+
})
145+
146+
if err != nil {
147+
return err
148+
}
149+
150+
ids := make([]string, 0, len(keyMetadata))
151+
tmpList := make([]map[string]interface{}, 0, len(keyMetadata))
152+
153+
if keyMetadata != nil {
154+
for _, key := range keyMetadata {
155+
mapping := map[string]interface{}{
156+
"key_id": key.KeyId,
157+
"alias": key.Alias,
158+
"create_time": key.CreateTime,
159+
"description": key.Description,
160+
"key_state": key.KeyState,
161+
"key_usage": key.KeyUsage,
162+
"creator_uin": key.CreatorUin,
163+
"key_rotation_enabled": key.KeyRotationEnabled,
164+
"owner": key.Owner,
165+
"next_rotate_time": key.NextRotateTime,
166+
"deletion_date": key.DeletionDate,
167+
"origin": key.Origin,
168+
"valid_to": key.ValidTo,
169+
}
170+
171+
tmpList = append(tmpList, mapping)
172+
ids = append(ids, *key.KeyId)
173+
}
174+
175+
_ = d.Set("key_list", tmpList)
176+
}
177+
178+
d.SetId(helper.DataResourceIdsHash(ids))
179+
output, ok := d.GetOk("result_output_file")
180+
if ok && output.(string) != "" {
181+
if e := writeToFile(output.(string), tmpList); e != nil {
182+
return e
183+
}
184+
}
185+
186+
return nil
187+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudKmsDescribeKeysDataSource_basic -v
10+
func TestAccTencentCloudKmsDescribeKeysDataSource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
testAccPreCheck(t)
15+
},
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccKmsKeyListsDataSource,
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_kms_describe_keys.example"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.key_id"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.create_time"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.description"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.key_state"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.key_usage"),
27+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.creator_uin"),
28+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.key_rotation_enabled"),
29+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.owner"),
30+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.next_rotate_time"),
31+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.origin"),
32+
resource.TestCheckResourceAttrSet("data.tencentcloud_kms_describe_keys.example", "key_list.0.valid_to"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
const testAccKmsKeyListsDataSource = `
40+
data "tencentcloud_kms_describe_keys" "example" {
41+
key_ids = [
42+
"72688f39-1fe8-11ee-9f1a-525400cf25a4"
43+
]
44+
}
45+
`
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Use this data source to query detailed information of kms list_keys
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_kms_list_keys" "example" {
8+
role = 1
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
19+
kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118"
20+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
21+
)
22+
23+
func dataSourceTencentCloudKmsListKeys() *schema.Resource {
24+
return &schema.Resource{
25+
Read: dataSourceTencentCloudKmsListKeysRead,
26+
Schema: map[string]*schema.Schema{
27+
"role": {
28+
Optional: true,
29+
Type: schema.TypeInt,
30+
Description: "Filter based on the creator role. The default value is 0, which indicates the cmk created by the user himself, and 1, which indicates the cmk automatically created by authorizing other cloud products.",
31+
},
32+
"hsm_cluster_id": {
33+
Optional: true,
34+
Type: schema.TypeString,
35+
Description: "HSM cluster ID (only valid for KMS exclusive/managed service instances).",
36+
},
37+
"keys": {
38+
Type: schema.TypeList,
39+
Computed: true,
40+
Description: "A list of KMS keys.",
41+
Elem: &schema.Resource{
42+
Schema: map[string]*schema.Schema{
43+
"key_id": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
Description: "ID of CMK.",
47+
},
48+
},
49+
},
50+
},
51+
"result_output_file": {
52+
Type: schema.TypeString,
53+
Optional: true,
54+
Description: "Used to save results.",
55+
},
56+
},
57+
}
58+
}
59+
60+
func dataSourceTencentCloudKmsListKeysRead(d *schema.ResourceData, meta interface{}) error {
61+
defer logElapsed("data_source.tencentcloud_kms_list_keys.read")()
62+
defer inconsistentCheck(d, meta)()
63+
64+
var (
65+
logId = getLogId(contextNil)
66+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
67+
service = KmsService{client: meta.(*TencentCloudClient).apiV3Conn}
68+
listKeys []*kms.Key
69+
)
70+
71+
paramMap := make(map[string]interface{})
72+
if v, _ := d.GetOk("role"); v != nil {
73+
paramMap["Role"] = helper.IntUint64(v.(int))
74+
}
75+
76+
if v, ok := d.GetOk("hsm_cluster_id"); ok {
77+
paramMap["HsmClusterId"] = helper.String(v.(string))
78+
}
79+
80+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
81+
result, e := service.DescribeKmsListKeysByFilter(ctx, paramMap)
82+
if e != nil {
83+
return retryError(e)
84+
}
85+
86+
listKeys = result
87+
return nil
88+
})
89+
90+
if err != nil {
91+
return err
92+
}
93+
94+
ids := make([]string, 0, len(listKeys))
95+
tmpList := make([]map[string]interface{}, 0, len(listKeys))
96+
97+
if listKeys != nil {
98+
for _, key := range listKeys {
99+
mapping := map[string]interface{}{
100+
"key_id": key.KeyId,
101+
}
102+
103+
tmpList = append(tmpList, mapping)
104+
ids = append(ids, *key.KeyId)
105+
}
106+
107+
_ = d.Set("keys", tmpList)
108+
}
109+
110+
d.SetId(helper.DataResourceIdsHash(ids))
111+
output, ok := d.GetOk("result_output_file")
112+
if ok && output.(string) != "" {
113+
if e := writeToFile(output.(string), tmpList); e != nil {
114+
return e
115+
}
116+
}
117+
118+
return nil
119+
}

0 commit comments

Comments
 (0)