Skip to content

Commit 66ddc7c

Browse files
committed
Added state upgrader for CMK resource
1 parent d4a30ea commit 66ddc7c

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
* Mounting clusters are recreated now, even when they are deleted ([#637](https://github.com/databrickslabs/terraform-provider-databricks/issues/637))
99
* Fixed handling of empty blocks for clusters/jobs/instance pools ([22cdf2f](https://github.com/databrickslabs/terraform-provider-databricks/commit/22cdf2fc9d50f67b14b49d11e7fbaacce0f52399))
1010
* Mark instance pool attributes as ForceNew when it's requited ([#629](https://github.com/databrickslabs/terraform-provider-databricks/issues/629))
11+
* Switched to use https://staticcheck.io/ for static code analysis ([#602](https://github.com/databrickslabs/terraform-provider-databricks/issues/602))
1112

1213
**Behavior changes**
1314

14-
* The `customer_managed_key_id` field in `databricks_mws_workspaces` resource is deprecated and should be replaced with `managed_services_customer_managed_key_id` (and optionally `storage_customer_managed_key_id`). `databricks_mws_customer_managed_keys` now requires the parameter `use_cases` ([#642](https://github.com/databrickslabs/terraform-provider-databricks/pull/642))
15+
* The `customer_managed_key_id` field in `databricks_mws_workspaces` resource is deprecated and should be replaced with `managed_services_customer_managed_key_id` (and optionally `storage_customer_managed_key_id`). `databricks_mws_customer_managed_keys` now requires the parameter `use_cases` ([#642](https://github.com/databrickslabs/terraform-provider-databricks/pull/642)). *If you've used the resource before, please add `use_cases = ["MANAGED_SERVICES"]` to keep the behaviour.*
1516

1617
Updated dependency versions:
1718

docs/resources/mws_customer_managed_keys.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Please follow this [complete runnable example](../guides/aws-workspace.md) with
1414

1515
## Example Usage
1616

17+
-> **Note** If you've used the resource before, please add `use_cases = ["MANAGED_SERVICES"]` to keep the previous behaviour.
18+
1719
```hcl
1820
variable "databricks_account_id" {
1921
description = "Account Id that could be found in the bottom left corner of https://accounts.cloud.databricks.com/"
@@ -26,7 +28,9 @@ resource "aws_kms_grant" "databricks-grant" {
2628
name = "databricks-grant"
2729
key_id = aws_kms_key.customer_managed_key.key_id
2830
grantee_principal = "arn:aws:iam::414351767826:root"
29-
operations = ["Encrypt", "Decrypt"]
31+
operations = ["Encrypt", "Decrypt", "DescribeKey",
32+
"GenerateDataKey", "ReEncryptFrom", "ReEncryptTo",
33+
"GenerateDataKeyWithoutPlaintext"]
3034
}
3135
3236
resource "aws_kms_alias" "customer_managed_key_alias" {
@@ -51,7 +55,7 @@ The following arguments are required:
5155

5256
* `aws_key_info` - This field is a block and is documented below.
5357
* `account_id` - Account Id that could be found in the bottom left corner of [Accounts Console](https://accounts.cloud.databricks.com/)
54-
* `use_cases` - list of use cases for which this key will be used. Possible values are:
58+
* `use_cases` - *(since v0.3.4)* List of use cases for which this key will be used. *If you've used the resource before, please add `use_cases = ["MANAGED_SERVICES"]` to keep the previous behaviour.* Possible values are:
5559
* `MANAGED_SERVICES` - for encryption of the workspace objects (notebooks, secrets) that are stored in the control plane
5660
* `STORAGE` - for encryption of the DBFS Storage & Cluster EBS Volumes
5761

mws/resource_customer_managed_key.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/databrickslabs/terraform-provider-databricks/common"
88

9+
"github.com/hashicorp/go-cty/cty"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
)
1112

@@ -104,6 +105,60 @@ func ResourceCustomerManagedKey() *schema.Resource {
104105
}
105106
return NewCustomerManagedKeysAPI(ctx, c).Delete(accountID, cmkID)
106107
},
107-
Schema: s,
108+
Schema: s,
109+
SchemaVersion: 1,
110+
StateUpgraders: []schema.StateUpgrader{
111+
{
112+
Version: 0,
113+
Type: ResourceCustomerManagedKeyV0(),
114+
Upgrade: migrateResourceCustomerManagedKeyV0,
115+
},
116+
},
108117
}.ToResource()
109118
}
119+
120+
func migrateResourceCustomerManagedKeyV0(ctx context.Context,
121+
rawState map[string]interface{},
122+
meta interface{}) (map[string]interface{}, error) {
123+
rawState["use_cases"] = []string{"MANAGED_SERVICES"}
124+
return rawState, nil
125+
}
126+
127+
func ResourceCustomerManagedKeyV0() cty.Type {
128+
return (&schema.Resource{
129+
Schema: map[string]*schema.Schema{
130+
"account_id": {
131+
Type: schema.TypeString,
132+
ForceNew: true,
133+
},
134+
"customer_managed_key_id": {
135+
Type: schema.TypeString,
136+
Optional: true,
137+
Computed: true,
138+
},
139+
"creation_time": {
140+
Type: schema.TypeInt,
141+
Computed: true,
142+
},
143+
"aws_key_info": {
144+
Type: schema.TypeList,
145+
ForceNew: true,
146+
Elem: &schema.Resource{
147+
Schema: map[string]*schema.Schema{
148+
"key_arn": {
149+
Type: schema.TypeString,
150+
},
151+
"key_alias": {
152+
Type: schema.TypeString,
153+
},
154+
"key_region": {
155+
Type: schema.TypeString,
156+
Optional: true,
157+
Computed: true,
158+
},
159+
},
160+
},
161+
},
162+
},
163+
}).CoreConfigSchema().ImpliedType()
164+
}

mws/resource_customer_managed_key_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,11 @@ func TestResourceCustomerManagedKeyDelete(t *testing.T) {
244244
assert.NoError(t, err, err)
245245
assert.Equal(t, "abc/cmkid", d.Id())
246246
}
247+
248+
func TestCmkStateUpgrader(t *testing.T) {
249+
state, err := migrateResourceCustomerManagedKeyV0(context.Background(),
250+
map[string]interface{}{}, nil)
251+
assert.NoError(t, err)
252+
_, ok := state["use_cases"]
253+
assert.True(t, ok)
254+
}

scripts/modules/aws-mws-common/kms.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ resource "aws_kms_grant" "databricks-grant" {
1010
key_id = aws_kms_key.customer_managed_key.key_id
1111
grantee_principal = "arn:aws:iam::${var.databricks_aws_account_id}:root"
1212

13-
operations = ["Encrypt", "Decrypt"]
13+
operations = ["Encrypt", "Decrypt", "DescribeKey",
14+
"GenerateDataKey", "ReEncryptFrom", "ReEncryptTo",
15+
"GenerateDataKeyWithoutPlaintext"]
1416
}
1517

1618

0 commit comments

Comments
 (0)