Skip to content

Commit 360a8bf

Browse files
committed
add field key_state and set is_enabled, is_archived based on key_state
1 parent dc604e0 commit 360a8bf

File tree

6 files changed

+77
-54
lines changed

6 files changed

+77
-54
lines changed

tencentcloud/resource_tc_kms_external_key.go

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ resource "tencentcloud_kms_external_key" "foo" {
1010
wrapping_algorithm = "RSAES_PKCS1_V1_5"
1111
key_material_base64 = "MTIzMTIzMTIzMTIzMTIzQQ=="
1212
valid_to = 2147443200
13+
is_enabled = true
1314
}
1415
```
1516
@@ -124,33 +125,33 @@ func resourceTencentCloudKmsExternalKeyCreate(d *schema.ResourceData, meta inter
124125
log.Printf("[CRITAL]%s Create KMS external key failed, reason:%+v", logId, err)
125126
return err
126127
}
127-
}
128128

129-
if isEnabled := d.Get("is_enabled").(bool); !isEnabled {
130-
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
131-
e := kmsService.DisableKey(ctx, d.Id())
132-
if e != nil {
133-
return retryError(e)
129+
if isEnabled := d.Get("is_enabled").(bool); !isEnabled {
130+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
131+
e := kmsService.DisableKey(ctx, d.Id())
132+
if e != nil {
133+
return retryError(e)
134+
}
135+
return nil
136+
})
137+
if err != nil {
138+
log.Printf("[CRITAL]%s modify key state failed, reason:%+v", logId, err)
139+
return err
134140
}
135-
return nil
136-
})
137-
if err != nil {
138-
log.Printf("[CRITAL]%s modify key state failed, reason:%+v", logId, err)
139-
return err
140141
}
141-
}
142142

143-
if isArchived := d.Get("is_archived").(bool); isArchived {
144-
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
145-
e := kmsService.ArchiveKey(ctx, d.Id())
146-
if e != nil {
147-
return retryError(e)
143+
if isArchived := d.Get("is_archived").(bool); isArchived {
144+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
145+
e := kmsService.ArchiveKey(ctx, d.Id())
146+
if e != nil {
147+
return retryError(e)
148+
}
149+
return nil
150+
})
151+
if err != nil {
152+
log.Printf("[CRITAL]%s modify key state failed, reason:%+v", logId, err)
153+
return err
148154
}
149-
return nil
150-
})
151-
if err != nil {
152-
log.Printf("[CRITAL]%s modify key state failed, reason:%+v", logId, err)
153-
return err
154155
}
155156
}
156157

@@ -202,6 +203,10 @@ func resourceTencentCloudKmsExternalKeyRead(d *schema.ResourceData, meta interfa
202203

203204
_ = d.Set("alias", key.Alias)
204205
_ = d.Set("description", key.Description)
206+
_ = d.Set("valid_to", key.ValidTo)
207+
_ = d.Set("key_state", key.KeyState)
208+
transformKeyState(d)
209+
205210
tcClient := meta.(*TencentCloudClient).apiV3Conn
206211
tagService := &TagService{client: tcClient}
207212
tags, err := tagService.DescribeResourceTags(ctx, "kms", "key", tcClient.Region, *key.ResourceId)
@@ -269,24 +274,21 @@ func resourceTencentCloudKmsExternalKeyUpdate(d *schema.ResourceData, meta inter
269274
}
270275
}
271276

272-
if d.HasChange("is_enabled") {
273-
isEnabled := d.Get("is_enabled").(bool)
274-
err := updateIsEnabled(ctx, kmsService, keyId, isEnabled)
277+
if isArchived, ok := d.GetOk("is_archived"); ok {
278+
err := updateIsArchived(ctx, kmsService, keyId, isArchived.(bool))
275279
if err != nil {
276280
log.Printf("[CRITAL]%s modify key state failed, reason:%+v", logId, err)
277281
return err
278282
}
279-
d.SetPartial("is_enabled")
280-
}
281-
282-
if d.HasChange("is_archived") {
283-
isArchived := d.Get("is_archived").(bool)
284-
err := updateIsArchived(ctx, kmsService, keyId, isArchived)
283+
d.SetPartial("is_archived")
284+
} else {
285+
isEnabled := d.Get("is_enabled").(bool)
286+
err := updateIsEnabled(ctx, kmsService, keyId, isEnabled)
285287
if err != nil {
286288
log.Printf("[CRITAL]%s modify key state failed, reason:%+v", logId, err)
287289
return err
288290
}
289-
d.SetPartial("is_archived")
291+
d.SetPartial("is_enabled")
290292
}
291293

292294
if d.HasChange("tags") {

tencentcloud/resource_tc_kms_external_key_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ resource "tencentcloud_kms_external_key" "test" {
4949
description = %[1]q
5050
wrapping_algorithm = "RSAES_PKCS1_V1_5"
5151
key_material_base64 = "MTIzMTIzMTIzMTIzMTIzQQ=="
52+
is_enabled = true
5253
5354
tags = {
5455
"test-tag" = "unit-test"

tencentcloud/resource_tc_kms_key.go

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ resource "tencentcloud_kms_key" "foo" {
88
alias = "test"
99
description = "describe key test message."
1010
key_rotation_enabled = true
11+
is_enabled = true
1112
1213
tags = {
1314
"test-tag":"key-test"
@@ -49,16 +50,16 @@ func TencentKmsBasicInfo() map[string]*schema.Schema {
4950
Description: "Description of CMK. The maximum is 1024 bytes.",
5051
},
5152
"is_enabled": {
52-
Type: schema.TypeBool,
53-
Optional: true,
54-
Default: true,
55-
Description: "Specify whether to enable key. Default value is `true`.",
53+
Type: schema.TypeBool,
54+
Optional: true,
55+
ConflictsWith: []string{"is_archived"},
56+
Description: "Specify whether to enable key. Default value is `false`.",
5657
},
5758
"is_archived": {
58-
Type: schema.TypeBool,
59-
Optional: true,
60-
Default: false,
61-
Description: "Specify whether to archive key. Default value is `false`.",
59+
Type: schema.TypeBool,
60+
Optional: true,
61+
ConflictsWith: []string{"is_enabled"},
62+
Description: "Specify whether to archive key. Default value is `false`.",
6263
},
6364
"pending_delete_window_in_days": {
6465
Type: schema.TypeInt,
@@ -71,6 +72,11 @@ func TencentKmsBasicInfo() map[string]*schema.Schema {
7172
Optional: true,
7273
Description: "Tags of CMK.",
7374
},
75+
"key_state": {
76+
Type: schema.TypeString,
77+
Computed: true,
78+
Description: "State of CMK.",
79+
},
7480
}
7581
}
7682

@@ -235,8 +241,10 @@ func resourceTencentCloudKmsKeyRead(d *schema.ResourceData, meta interface{}) er
235241

236242
_ = d.Set("alias", key.Alias)
237243
_ = d.Set("description", key.Description)
244+
_ = d.Set("key_state", key.KeyState)
238245
_ = d.Set("key_usage", key.KeyUsage)
239246
_ = d.Set("key_rotation_enabled", key.KeyRotationEnabled)
247+
transformKeyState(d)
240248

241249
tcClient := meta.(*TencentCloudClient).apiV3Conn
242250
tagService := &TagService{client: tcClient}
@@ -291,24 +299,21 @@ func resourceTencentCloudKmsKeyUpdate(d *schema.ResourceData, meta interface{})
291299
d.SetPartial("alias")
292300
}
293301

294-
if d.HasChange("is_enabled") {
295-
isEnabled := d.Get("is_enabled").(bool)
296-
err := updateIsEnabled(ctx, kmsService, keyId, isEnabled)
302+
if isArchived, ok := d.GetOk("is_archived"); ok {
303+
err := updateIsArchived(ctx, kmsService, keyId, isArchived.(bool))
297304
if err != nil {
298305
log.Printf("[CRITAL]%s modify key state failed, reason:%+v", logId, err)
299306
return err
300307
}
301-
d.SetPartial("is_enabled")
302-
}
303-
304-
if d.HasChange("is_archived") {
305-
isArchived := d.Get("is_archived").(bool)
306-
err := updateIsArchived(ctx, kmsService, keyId, isArchived)
308+
d.SetPartial("is_archived")
309+
} else {
310+
isEnabled := d.Get("is_enabled").(bool)
311+
err := updateIsEnabled(ctx, kmsService, keyId, isEnabled)
307312
if err != nil {
308313
log.Printf("[CRITAL]%s modify key state failed, reason:%+v", logId, err)
309314
return err
310315
}
311-
d.SetPartial("is_archived")
316+
d.SetPartial("is_enabled")
312317
}
313318

314319
if d.HasChange("key_rotation_enabled") {
@@ -458,3 +463,15 @@ func updateIsArchived(ctx context.Context, kmsService KmsService, keyId string,
458463
}
459464
return err
460465
}
466+
467+
func transformKeyState(d *schema.ResourceData) {
468+
keyState := d.Get("key_state").(string)
469+
switch keyState {
470+
case KMS_KEY_STATE_ENABLED:
471+
_ = d.Set("is_enabled", true)
472+
case KMS_KEY_STATE_DISABLED:
473+
_ = d.Set("is_enabled", false)
474+
case KMS_KEY_STATE_ARCHIVED:
475+
_ = d.Set("is_archived", true)
476+
}
477+
}

tencentcloud/resource_tc_kms_key_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ resource "tencentcloud_kms_key" "test" {
172172
alias = %[1]q
173173
description = %[1]q
174174
key_rotation_enabled = true
175+
is_enabled = true
175176
176177
tags = {
177178
"test-tag" = "unit-test"

website/docs/r/kms_external_key.html.markdown

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ resource "tencentcloud_kms_external_key" "foo" {
2020
wrapping_algorithm = "RSAES_PKCS1_V1_5"
2121
key_material_base64 = "MTIzMTIzMTIzMTIzMTIzQQ=="
2222
valid_to = 2147443200
23+
is_enabled = true
2324
}
2425
```
2526

@@ -30,7 +31,7 @@ The following arguments are supported:
3031
* `alias` - (Required) Name of CMK. The name can only contain English letters, numbers, underscore and hyphen '-'. The first character must be a letter or number.
3132
* `description` - (Optional) Description of CMK. The maximum is 1024 bytes.
3233
* `is_archived` - (Optional) Specify whether to archive key. Default value is `false`.
33-
* `is_enabled` - (Optional) Specify whether to enable key. Default value is `true`.
34+
* `is_enabled` - (Optional) Specify whether to enable key. Default value is `false`.
3435
* `key_material_base64` - (Optional) The base64-encoded key material encrypted with the public_key. For regions using the national secret version, the length of the imported key material is required to be 128 bits, and for regions using the FIPS version, the length of the imported key material is required to be 256 bits.
3536
* `pending_delete_window_in_days` - (Optional) Duration in days after which the key is deleted after destruction of the resource, must be between 7 and 30 days. Defaults to 7 days.
3637
* `tags` - (Optional) Tags of CMK.
@@ -42,7 +43,7 @@ The following arguments are supported:
4243
In addition to all arguments above, the following attributes are exported:
4344

4445
* `id` - ID of the resource.
45-
46+
* `key_state` - State of CMK.
4647

4748

4849
## Import

website/docs/r/kms_key.html.markdown

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ resource "tencentcloud_kms_key" "foo" {
1818
alias = "test"
1919
description = "describe key test message."
2020
key_rotation_enabled = true
21+
is_enabled = true
2122
2223
tags = {
2324
"test-tag" : "key-test"
@@ -32,7 +33,7 @@ The following arguments are supported:
3233
* `alias` - (Required) Name of CMK. The name can only contain English letters, numbers, underscore and hyphen '-'. The first character must be a letter or number.
3334
* `description` - (Optional) Description of CMK. The maximum is 1024 bytes.
3435
* `is_archived` - (Optional) Specify whether to archive key. Default value is `false`.
35-
* `is_enabled` - (Optional) Specify whether to enable key. Default value is `true`.
36+
* `is_enabled` - (Optional) Specify whether to enable key. Default value is `false`.
3637
* `key_rotation_enabled` - (Optional) Specify whether to enable key rotation, valid when key_usage is `ENCRYPT_DECRYPT`. Default value is `false`.
3738
* `key_usage` - (Optional, ForceNew) 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`. Default value is `ENCRYPT_DECRYPT`.
3839
* `pending_delete_window_in_days` - (Optional) Duration in days after which the key is deleted after destruction of the resource, must be between 7 and 30 days. Defaults to 7 days.
@@ -43,7 +44,7 @@ The following arguments are supported:
4344
In addition to all arguments above, the following attributes are exported:
4445

4546
* `id` - ID of the resource.
46-
47+
* `key_state` - State of CMK.
4748

4849

4950
## Import

0 commit comments

Comments
 (0)