Skip to content

Commit 78845f7

Browse files
committed
modify code base on conversation
1 parent bcedbb6 commit 78845f7

15 files changed

+368
-399
lines changed

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
## 1.54.1 (Unreleased)
2-
## 1.54.0 (March 10, 2021)
1+
## 1.54.0 (Unreleased)
32

43
FEATURES:
54

65
* **New Resource**: `tencentcloud_kms_key`
76
* **New Resource**: `tencentcloud_kms_external_key`
8-
* **New Data Source**: `tencentcloud_kms_key`
7+
* **New Data Source**: `tencentcloud_kms_keys`
98

109
## 1.53.7 (March 09, 2021)
1110

tencentcloud/connectivity/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package connectivity
33
import (
44
"fmt"
55

6-
kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118"
7-
86
"github.com/aws/aws-sdk-go/aws"
97
"github.com/aws/aws-sdk-go/aws/credentials"
108
"github.com/aws/aws-sdk-go/aws/endpoints"
@@ -28,6 +26,7 @@ import (
2826
dc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc/v20180410"
2927
es "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es/v20180416"
3028
gaap "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap/v20180529"
29+
kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118"
3130
mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725"
3231
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
3332
postgre "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Use this data source to query detailed information of KMS key
44
Example Usage
55
66
```hcl
7-
data "tencentcloud_kms_key" "foo" {
7+
data "tencentcloud_kms_keys" "foo" {
88
search_key_alias = "test"
99
key_state = "All"
1010
origin = "TENCENT_KMS"
@@ -18,56 +18,50 @@ import (
1818
"context"
1919
"log"
2020

21-
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22-
2321
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
2422
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
2523
kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118"
24+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
2625
)
2726

28-
func dataSourceTencentCloudKmsKey() *schema.Resource {
27+
func dataSourceTencentCloudKmsKeys() *schema.Resource {
2928
return &schema.Resource{
30-
Read: dataSourceTencentCloudKmsKeyRead,
29+
Read: dataSourceTencentCloudKmsKeysRead,
3130
Schema: map[string]*schema.Schema{
3231
"role": {
33-
Type: schema.TypeInt,
34-
Optional: true,
35-
ValidateFunc: validateAllowedIntValue([]int{0, 1}),
36-
Default: 0,
37-
Description: "Role of the CMK creator.`0` - created by user, `1` - created by cloud product.Default value is `0`.",
32+
Type: schema.TypeInt,
33+
Optional: true,
34+
Default: 0,
35+
Description: "Filter by role of the CMK creator. `0` - created by user, `1` - created by cloud product. Default value is `0`.",
3836
},
3937
"order_type": {
40-
Type: schema.TypeInt,
41-
Optional: true,
42-
ValidateFunc: validateAllowedIntValue([]int{0, 1}),
43-
Default: 0,
44-
Description: "Order to sort the CMK create time.`0` - desc, `1` - asc.Default value is `0`.",
38+
Type: schema.TypeInt,
39+
Optional: true,
40+
Default: 0,
41+
Description: "Order to sort the CMK create time. `0` - desc, `1` - asc. Default value is `0`.",
4542
},
4643
"key_state": {
47-
Type: schema.TypeString,
48-
Optional: true,
49-
ValidateFunc: validateAllowedStringValue(KMS_KEY_STATE_FILTER),
50-
Default: KMS_KEY_STATE_ALL,
51-
Description: "State of CMK.Available values include `All`, `Enabled`, `Disabled`, `PendingDelete`, `PendingImport`, `Archived`.",
44+
Type: schema.TypeInt,
45+
Optional: true,
46+
Default: 0,
47+
Description: "Filter by state of CMK. `0` - all CMKs are queried, `1` - only Enabled CMKs are queried, `2` - only Disabled CMKs are queried, `3` - only PendingDelete CMKs are queried, `4` - only PendingImport CMKs are queried, `5` - only Archived CMKs are queried.",
5248
},
5349
"search_key_alias": {
5450
Type: schema.TypeString,
5551
Optional: true,
56-
Description: "Words used to match the results,and the words can be: key_id and alias.",
52+
Description: "Words used to match the results, and the words can be: key_id and alias.",
5753
},
5854
"origin": {
59-
Type: schema.TypeString,
60-
Optional: true,
61-
ValidateFunc: validateAllowedStringValue(KMS_ORIGIN_FILTER),
62-
Default: KMS_ORIGIN_ALL,
63-
Description: "Origin of CMK.`TENCENT_KMS` - CMK created by KMS, `EXTERNAL` - CMK imported by user, `ALL` - All CMK.Default value is `ALL`.",
55+
Type: schema.TypeString,
56+
Optional: true,
57+
Default: KMS_ORIGIN_ALL,
58+
Description: "Filter by origin of CMK. `TENCENT_KMS` - CMK created by KMS, `EXTERNAL` - CMK imported by user, `ALL` - all CMKs. Default value is `ALL`.",
6459
},
6560
"key_usage": {
66-
Type: schema.TypeString,
67-
Optional: true,
68-
ValidateFunc: validateAllowedStringValue(KMS_KEY_USAGE_FILTER),
69-
Default: KMS_KEY_USAGE_ENCRYPT_DECRYPT,
70-
Description: "Usage of CMK.Available values include `ALL`, `ENCRYPT_DECRYPT`, `ASYMMETRIC_DECRYPT_RSA_2048`, `ASYMMETRIC_DECRYPT_SM2`, `ASYMMETRIC_SIGN_VERIFY_SM2`, `ASYMMETRIC_SIGN_VERIFY_RSA_2048`, `ASYMMETRIC_SIGN_VERIFY_ECC`.Default value is `ENCRYPT_DECRYPT`.",
61+
Type: schema.TypeString,
62+
Optional: true,
63+
Default: KMS_KEY_USAGE_ENCRYPT_DECRYPT,
64+
Description: "Filter by usage of CMK. Available values include `ALL`, `ENCRYPT_DECRYPT`, `ASYMMETRIC_DECRYPT_RSA_2048`, `ASYMMETRIC_DECRYPT_SM2`, `ASYMMETRIC_SIGN_VERIFY_SM2`, `ASYMMETRIC_SIGN_VERIFY_RSA_2048`, `ASYMMETRIC_SIGN_VERIFY_ECC`. Default value is `ENCRYPT_DECRYPT`.",
7165
},
7266
"tags": {
7367
Type: schema.TypeMap,
@@ -96,7 +90,7 @@ func dataSourceTencentCloudKmsKey() *schema.Resource {
9690
Description: "Name of CMK.",
9791
},
9892
"create_time": {
99-
Type: schema.TypeString,
93+
Type: schema.TypeInt,
10094
Computed: true,
10195
Description: "Create time of CMK.",
10296
},
@@ -108,12 +102,12 @@ func dataSourceTencentCloudKmsKey() *schema.Resource {
108102
"key_state": {
109103
Type: schema.TypeString,
110104
Computed: true,
111-
Description: "State of CMK.Available values include `Enabled`, `Disabled`, `PendingDelete`, `PendingImport`, `Archived`.",
105+
Description: "State of CMK.",
112106
},
113107
"key_usage": {
114108
Type: schema.TypeString,
115109
Computed: true,
116-
Description: "Usage of CMK.Available values include `ENCRYPT_DECRYPT`, `ASYMMETRIC_DECRYPT_RSA_2048`, `ASYMMETRIC_DECRYPT_SM2`, `ASYMMETRIC_SIGN_VERIFY_SM2`, `ASYMMETRIC_SIGN_VERIFY_RSA_2048`, `ASYMMETRIC_SIGN_VERIFY_ECC`.",
110+
Description: "Usage of CMK.",
117111
},
118112
"creator_uin": {
119113
Type: schema.TypeInt,
@@ -131,24 +125,24 @@ func dataSourceTencentCloudKmsKey() *schema.Resource {
131125
Description: "Creator of CMK.",
132126
},
133127
"next_rotate_time": {
134-
Type: schema.TypeString,
128+
Type: schema.TypeInt,
135129
Computed: true,
136130
Description: "Next rotate time of CMK when key_rotation_enabled is true.",
137131
},
138132
"deletion_date": {
139-
Type: schema.TypeString,
133+
Type: schema.TypeInt,
140134
Computed: true,
141135
Description: "Delete time of CMK.",
142136
},
143137
"origin": {
144138
Type: schema.TypeString,
145139
Computed: true,
146-
Description: "Origin of CMK.`TENCENT_KMS` - CMK created by KMS, `EXTERNAL` - CMK imported by user.",
140+
Description: "Origin of CMK. `TENCENT_KMS` - CMK created by KMS, `EXTERNAL` - CMK imported by user.",
147141
},
148142
"valid_to": {
149-
Type: schema.TypeString,
143+
Type: schema.TypeInt,
150144
Computed: true,
151-
Description: "Valid when Origin is EXTERNAL, it means the effective date of the key material.",
145+
Description: "Valid when origin is `EXTERNAL`, it means the effective date of the key material.",
152146
},
153147
},
154148
},
@@ -157,8 +151,8 @@ func dataSourceTencentCloudKmsKey() *schema.Resource {
157151
}
158152
}
159153

160-
func dataSourceTencentCloudKmsKeyRead(d *schema.ResourceData, meta interface{}) error {
161-
defer logElapsed("data_source.tencentcloud_kms_key.read")()
154+
func dataSourceTencentCloudKmsKeysRead(d *schema.ResourceData, meta interface{}) error {
155+
defer logElapsed("data_source.tencentcloud_kms_keys.read")()
162156

163157
logId := getLogId(contextNil)
164158
ctx := context.WithValue(context.TODO(), logIdKey, logId)
@@ -171,8 +165,8 @@ func dataSourceTencentCloudKmsKeyRead(d *schema.ResourceData, meta interface{})
171165
param["order_type"] = v.(int)
172166
}
173167
if v, ok := d.GetOk("key_state"); ok {
174-
keyState := v.(string)
175-
param["key_state"] = KMS_KEY_STATE_MAP[keyState]
168+
keyState := v.(int)
169+
param["key_state"] = uint64(keyState)
176170
}
177171
if v, ok := d.GetOk("search_key_alias"); ok {
178172
param["search_key_alias"] = v.(string)
@@ -209,28 +203,19 @@ func dataSourceTencentCloudKmsKeyRead(d *schema.ResourceData, meta interface{})
209203
mapping := map[string]interface{}{
210204
"key_id": key.KeyId,
211205
"alias": key.Alias,
212-
"create_time": helper.FormatUnixTime(*key.CreateTime),
206+
"create_time": key.CreateTime,
213207
"description": key.Description,
214208
"key_state": key.KeyState,
215209
"key_usage": key.KeyUsage,
216210
"creator_uin": key.CreatorUin,
217211
"key_rotation_enabled": key.KeyRotationEnabled,
218212
"owner": key.Owner,
213+
"next_rotate_time": key.NextRotateTime,
214+
"deletion_date": key.DeletionDate,
219215
"origin": key.Origin,
216+
"valid_to": key.ValidTo,
220217
}
221-
if *key.KeyRotationEnabled {
222-
mapping["next_rotate_time"] = helper.FormatUnixTime(*key.NextRotateTime)
223-
}
224-
if *key.KeyState == KMS_KEY_STATE_PENDINGDELETE {
225-
mapping["deletion_date"] = helper.FormatUnixTime(*key.DeletionDate)
226-
}
227-
if *key.Origin == KMS_ORIGIN_EXTERNAL {
228-
if *key.ValidTo != 0 {
229-
mapping["valid_to"] = helper.FormatUnixTime(*key.ValidTo)
230-
} else {
231-
mapping["valid_to"] = "never expire"
232-
}
233-
}
218+
234219
keyList = append(keyList, mapping)
235220
ids = append(ids, *key.KeyId)
236221
}
@@ -240,5 +225,8 @@ func dataSourceTencentCloudKmsKeyRead(d *schema.ResourceData, meta interface{})
240225
log.Printf("[CRITAL]%s provider set KMS key list fail, reason:%+v", logId, e)
241226
return e
242227
}
228+
if output, ok := d.GetOk("result_output_file"); ok && output.(string) != "" {
229+
return writeToFile(output.(string), keyList)
230+
}
243231
return nil
244232
}

tencentcloud/data_source_tc_kms_key_test.go renamed to tencentcloud/data_source_tc_kms_keys_test.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"testing"
66

77
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8-
"github.com/hashicorp/terraform-plugin-sdk/terraform"
9-
108
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
119
)
1210

1311
func TestAccTencentCloudKmsKeyDataSource(t *testing.T) {
14-
dataSourceName := "data.tencentcloud_kms_key.test"
12+
dataSourceName := "data.tencentcloud_kms_keys.test"
1513
rName := fmt.Sprintf("tf-testacc-kms-key-%s", acctest.RandString(13))
1614

1715
resource.Test(t, resource.TestCase{
@@ -21,7 +19,7 @@ func TestAccTencentCloudKmsKeyDataSource(t *testing.T) {
2119
{
2220
Config: testAccDataSourceKmsKeyConfig(rName),
2321
Check: resource.ComposeTestCheckFunc(
24-
testAccDataSourceKmsKeyCheck(dataSourceName),
22+
testAccCheckTencentCloudDataSourceID(dataSourceName),
2523
resource.TestCheckResourceAttrSet(dataSourceName, "key_list.0.key_id"),
2624
resource.TestCheckResourceAttrSet(dataSourceName, "key_list.0.create_time"),
2725
resource.TestCheckResourceAttrSet(dataSourceName, "key_list.0.description"),
@@ -39,26 +37,15 @@ func TestAccTencentCloudKmsKeyDataSource(t *testing.T) {
3937
})
4038
}
4139

42-
func testAccDataSourceKmsKeyCheck(name string) resource.TestCheckFunc {
43-
return func(s *terraform.State) error {
44-
_, ok := s.RootModule().Resources[name]
45-
if !ok {
46-
return fmt.Errorf("root module has no resource called %s", name)
47-
}
48-
49-
return nil
50-
}
51-
}
52-
5340
func testAccDataSourceKmsKeyConfig(rName string) string {
5441
return fmt.Sprintf(`
5542
resource "tencentcloud_kms_key" "test" {
5643
alias = %[1]q
5744
description = %[1]q
58-
key_state = "Disabled"
45+
is_enabled = false
5946
key_rotation_enabled = true
6047
}
61-
data "tencentcloud_kms_key" "test" {
48+
data "tencentcloud_kms_keys" "test" {
6249
search_key_alias = tencentcloud_kms_key.test.alias
6350
}
6451
`, rName)

tencentcloud/extension_kms.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ const (
66
KMS_ORIGIN_ALL = "ALL"
77
)
88

9-
var KMS_ORIGIN_FILTER = []string{
10-
KMS_ORIGIN_TENCENT_KMS,
11-
KMS_ORIGIN_EXTERNAL,
12-
KMS_ORIGIN_ALL,
13-
}
14-
15-
var KMS_ORIGIN = []string{
16-
KMS_ORIGIN_TENCENT_KMS,
17-
KMS_ORIGIN_EXTERNAL,
18-
}
19-
209
var KMS_ORIGIN_TYPE = map[string]uint64{
2110
KMS_ORIGIN_TENCENT_KMS: 1,
2211
KMS_ORIGIN_EXTERNAL: 2,
@@ -32,25 +21,6 @@ const (
3221
KMS_KEY_USAGE_ASYMMETRIC_SIGN_VERIFY_ECC = "ASYMMETRIC_SIGN_VERIFY_ECC"
3322
)
3423

35-
var KMS_KEY_USAGE_FILTER = []string{
36-
KMS_KEY_USAGE_ALL,
37-
KMS_KEY_USAGE_ENCRYPT_DECRYPT,
38-
KMS_KEY_USAGE_ASYMMETRIC_DECRYPT_RSA_2048,
39-
KMS_KEY_USAGE_ASYMMETRIC_DECRYPT_SM2,
40-
KMS_KEY_USAGE_ASYMMETRIC_SIGN_VERIFY_SM2,
41-
KMS_KEY_USAGE_ASYMMETRIC_SIGN_VERIFY_RSA_2048,
42-
KMS_KEY_USAGE_ASYMMETRIC_SIGN_VERIFY_ECC,
43-
}
44-
45-
var KMS_KEY_USAGE = []string{
46-
KMS_KEY_USAGE_ENCRYPT_DECRYPT,
47-
KMS_KEY_USAGE_ASYMMETRIC_DECRYPT_RSA_2048,
48-
KMS_KEY_USAGE_ASYMMETRIC_DECRYPT_SM2,
49-
KMS_KEY_USAGE_ASYMMETRIC_SIGN_VERIFY_SM2,
50-
KMS_KEY_USAGE_ASYMMETRIC_SIGN_VERIFY_RSA_2048,
51-
KMS_KEY_USAGE_ASYMMETRIC_SIGN_VERIFY_ECC,
52-
}
53-
5424
const (
5525
KMS_PAGE_LIMIT = 200
5626
)
@@ -61,20 +31,10 @@ const (
6131
KMS_WRAPPING_ALGORITHM_RSAES_OAEP_SHA_256 = "RSAES_OAEP_SHA_256"
6232
)
6333

64-
var KMS_WRAPPING_ALGORITHM = []string{
65-
KMS_WRAPPING_ALGORITHM_RSAES_PKCS1_V1_5,
66-
KMS_WRAPPING_ALGORITHM_RSAES_OAEP_SHA_1,
67-
KMS_WRAPPING_ALGORITHM_RSAES_OAEP_SHA_256,
68-
}
69-
7034
const (
7135
KMS_WRAPPING_KEY_SPEC_RSA_2048 = "RSA_2048"
7236
)
7337

74-
var KMS_WRAPPING_KEY_SPEC = []string{
75-
KMS_WRAPPING_KEY_SPEC_RSA_2048,
76-
}
77-
7838
const (
7939
KMS_KEY_STATE_ALL = "All"
8040
KMS_KEY_STATE_ENABLED = "Enabled"
@@ -83,29 +43,3 @@ const (
8343
KMS_KEY_STATE_PENDINGIMPORT = "PendingImport"
8444
KMS_KEY_STATE_ARCHIVED = "Archived"
8545
)
86-
87-
var KMS_KEY_STATE_FILTER = []string{
88-
KMS_KEY_STATE_ALL,
89-
KMS_KEY_STATE_ENABLED,
90-
KMS_KEY_STATE_DISABLED,
91-
KMS_KEY_STATE_PENDINGDELETE,
92-
KMS_KEY_STATE_PENDINGIMPORT,
93-
KMS_KEY_STATE_ARCHIVED,
94-
}
95-
96-
var KMS_KEY_STATE = []string{
97-
KMS_KEY_STATE_ENABLED,
98-
KMS_KEY_STATE_DISABLED,
99-
KMS_KEY_STATE_PENDINGDELETE,
100-
KMS_KEY_STATE_PENDINGIMPORT,
101-
KMS_KEY_STATE_ARCHIVED,
102-
}
103-
104-
var KMS_KEY_STATE_MAP = map[string]uint64{
105-
KMS_KEY_STATE_ALL: 0,
106-
KMS_KEY_STATE_ENABLED: 1,
107-
KMS_KEY_STATE_DISABLED: 2,
108-
KMS_KEY_STATE_PENDINGDELETE: 3,
109-
KMS_KEY_STATE_PENDINGIMPORT: 4,
110-
KMS_KEY_STATE_ARCHIVED: 5,
111-
}

0 commit comments

Comments
 (0)