@@ -4,7 +4,7 @@ Use this data source to query detailed information of KMS key
44Example 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}
0 commit comments