Skip to content

Commit 34136b0

Browse files
Bigtable: Check ForceNew for kms_key_name field in the diff function (#6765) (#13018)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent d5a9111 commit 34136b0

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

.changelog/6765.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
bigtable: update checking ForceNew logic for `kms_key_name`
3+
```

google/resource_bigtable_instance.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ func resourceBigtableInstance() *schema.Resource {
8181
"kms_key_name": {
8282
Type: schema.TypeString,
8383
Optional: true,
84-
ForceNew: true,
8584
Computed: true,
8685
Description: `Describes the Cloud KMS encryption key that will be used to protect the destination Bigtable cluster. The requirements for this key are: 1) The Cloud Bigtable service account associated with the project that contains this cluster must be granted the cloudkms.cryptoKeyEncrypterDecrypter role on the CMEK key. 2) Only regional keys can be used and the region of the CMEK key must match the region of the cluster. 3) All clusters within an instance must use the same CMEK key. Values are of the form projects/{project}/locations/{location}/keyRings/{keyring}/cryptoKeys/{key}`,
8786
},
@@ -561,6 +560,14 @@ func resourceBigtableInstanceClusterReorderTypeList(_ context.Context, diff *sch
561560
return fmt.Errorf("Error setting cluster diff: %s", err)
562561
}
563562
}
563+
564+
oKey, nKey := diff.GetChange(fmt.Sprintf("cluster.%d.kms_key_name", i))
565+
if oKey != nKey {
566+
err := diff.ForceNew(fmt.Sprintf("cluster.%d.kms_key_name", i))
567+
if err != nil {
568+
return fmt.Errorf("Error setting cluster diff: %s", err)
569+
}
570+
}
564571
}
565572

566573
return nil

google/resource_bigtable_instance_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ func TestAccBigtableInstance_kms(t *testing.T) {
172172
skipIfVcr(t)
173173
t.Parallel()
174174

175-
kms := BootstrapKMSKeyInLocation(t, "us-central1")
175+
kms1 := BootstrapKMSKeyInLocation(t, "us-central1")
176+
kms2 := BootstrapKMSKeyInLocation(t, "us-east1")
176177
pid := getTestProjectFromEnv()
177178
instanceName := fmt.Sprintf("tf-test-%s", randString(t, 10))
178179

@@ -182,14 +183,20 @@ func TestAccBigtableInstance_kms(t *testing.T) {
182183
CheckDestroy: testAccCheckBigtableInstanceDestroyProducer(t),
183184
Steps: []resource.TestStep{
184185
{
185-
Config: testAccBigtableInstance_kms(pid, instanceName, kms.CryptoKey.Name, 3),
186+
Config: testAccBigtableInstance_kms(pid, instanceName, kms1.CryptoKey.Name, 3),
186187
},
187188
{
188189
ResourceName: "google_bigtable_instance.instance",
189190
ImportState: true,
190191
ImportStateVerify: true,
191192
ImportStateVerifyIgnore: []string{"deletion_protection", "instance_type"}, // we don't read instance type back
192193
},
194+
// TODO(kevinsi4508): Verify that the instance can be recreated due to `kms_key_name` change.
195+
{
196+
Config: testAccBigtableInstance_kms(pid, instanceName, kms2.CryptoKey.Name, 3),
197+
PlanOnly: true,
198+
ExpectNonEmptyPlan: true,
199+
},
193200
},
194201
})
195202
}

website/docs/r/bigtable_instance.html.markdown

+2-4
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,9 @@ for a `DEVELOPMENT` instance.
131131

132132
* `kms_key_name` - (Optional) Describes the Cloud KMS encryption key that will be used to protect the destination Bigtable cluster. The requirements for this key are: 1) The Cloud Bigtable service account associated with the project that contains this cluster must be granted the `cloudkms.cryptoKeyEncrypterDecrypter` role on the CMEK key. 2) Only regional keys can be used and the region of the CMEK key must match the region of the cluster.
133133

134-
!> **Warning**: Modifying this field will cause Terraform to delete/recreate the entire resource.
134+
-> **Note**: Removing the field entirely from the config will cause the provider to default to the backend value.
135135

136-
-> **Note**: To remove this field once it is set, set the value to an empty string. Removing the field entirely from the config will cause the provider to default to the backend value.
137-
138-
!> **Warning:** Modifying the `storage_type` or `zone` of an existing cluster (by
136+
!> **Warning:** Modifying the `storage_type`, `zone` or `kms_key_name` of an existing cluster (by
139137
`cluster_id`) will cause Terraform to delete/recreate the entire
140138
`google_bigtable_instance` resource. If these values are changing, use a new
141139
`cluster_id`.

0 commit comments

Comments
 (0)