Skip to content

Commit 4a38e72

Browse files
make database_encryption updateable (#3728) (#2259)
* make datbase_encryption updateable * add update test Signed-off-by: Modular Magician <[email protected]>
1 parent ea82976 commit 4a38e72

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

.changelog/3728.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
container: added the ability to update `database_encryption` without recreating the cluster.
3+
```

google-beta/resource_container_cluster.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,21 +1016,18 @@ func resourceContainerCluster() *schema.Resource {
10161016
Type: schema.TypeList,
10171017
MaxItems: 1,
10181018
Optional: true,
1019-
ForceNew: true,
10201019
Computed: true,
10211020
Description: `Application-layer Secrets Encryption settings. The object format is {state = string, key_name = string}. Valid values of state are: "ENCRYPTED"; "DECRYPTED". key_name is the name of a CloudKMS key.`,
10221021
Elem: &schema.Resource{
10231022
Schema: map[string]*schema.Schema{
10241023
"state": {
10251024
Type: schema.TypeString,
1026-
ForceNew: true,
10271025
Required: true,
10281026
ValidateFunc: validation.StringInSlice([]string{"ENCRYPTED", "DECRYPTED"}, false),
10291027
Description: `ENCRYPTED or DECRYPTED.`,
10301028
},
10311029
"key_name": {
10321030
Type: schema.TypeString,
1033-
ForceNew: true,
10341031
Optional: true,
10351032
Description: `The key to use to encrypt/decrypt secrets.`,
10361033
},
@@ -2098,6 +2095,31 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
20982095
}
20992096
}
21002097

2098+
if d.HasChange("database_encryption") {
2099+
c := d.Get("database_encryption")
2100+
req := &containerBeta.UpdateClusterRequest{
2101+
Update: &containerBeta.ClusterUpdate{
2102+
DesiredDatabaseEncryption: expandDatabaseEncryption(c),
2103+
},
2104+
}
2105+
2106+
updateF := func() error {
2107+
name := containerClusterFullName(project, location, clusterName)
2108+
op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do()
2109+
if err != nil {
2110+
return err
2111+
}
2112+
// Wait until it's updated
2113+
return containerOperationWait(config, op, project, location, "updating GKE cluster database encryption config", d.Timeout(schema.TimeoutUpdate))
2114+
}
2115+
if err := lockedCall(lockKey, updateF); err != nil {
2116+
return err
2117+
}
2118+
log.Printf("[INFO] GKE cluster %s database encryption config has been updated", d.Id())
2119+
2120+
d.SetPartial("database_encryption")
2121+
}
2122+
21012123
if d.HasChange("pod_security_policy_config") {
21022124
c := d.Get("pod_security_policy_config")
21032125
req := &containerBeta.UpdateClusterRequest{

google-beta/resource_container_cluster_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,15 @@ func TestAccContainerCluster_withDatabaseEncryption(t *testing.T) {
17261726
Config: testAccContainerCluster_withDatabaseEncryption(clusterName, kmsData),
17271727
},
17281728
{
1729-
ResourceName: "google_container_cluster.with_database_encryption",
1729+
ResourceName: "google_container_cluster.primary",
1730+
ImportState: true,
1731+
ImportStateVerify: true,
1732+
},
1733+
{
1734+
Config: testAccContainerCluster_basic(clusterName),
1735+
},
1736+
{
1737+
ResourceName: "google_container_cluster.primary",
17301738
ImportState: true,
17311739
ImportStateVerify: true,
17321740
},
@@ -3848,7 +3856,7 @@ resource "google_kms_key_ring_iam_policy" "test_key_ring_iam_policy" {
38483856
policy_data = data.google_iam_policy.test_kms_binding.policy_data
38493857
}
38503858
3851-
resource "google_container_cluster" "with_database_encryption" {
3859+
resource "google_container_cluster" "primary" {
38523860
name = "%[3]s"
38533861
location = "us-central1-a"
38543862
initial_node_count = 1

0 commit comments

Comments
 (0)