Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DeletionProtectionEnabled to Redis Cluster #10367

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions mmv1/products/redis/Cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ examples:
policy_name: "mypolicy"
subnet_name: "mysubnet"
network_name: "mynetwork"
prevent_destroy: 'true'
deletion_protection_enabled: 'true'
test_vars_overrides:
prevent_destroy: 'false'
deletion_protection_enabled: 'false'
oics_vars_overrides:
prevent_destroy: 'false'
deletion_protection_enabled: 'false'
- !ruby/object:Provider::Terraform::Examples
name: "redis_cluster_ha_single_zone"
primary_resource_id: "cluster-ha-single-zone"
Expand All @@ -70,11 +70,11 @@ examples:
policy_name: "mypolicy"
subnet_name: "mysubnet"
network_name: "mynetwork"
prevent_destroy: 'true'
deletion_protection_enabled: 'true'
test_vars_overrides:
prevent_destroy: 'false'
deletion_protection_enabled: 'false'
oics_vars_overrides:
prevent_destroy: 'false'
deletion_protection_enabled: 'false'
properties:
- !ruby/object:Api::Type::Time
name: createTime
Expand Down Expand Up @@ -262,6 +262,14 @@ properties:
description: |
Required. Number of shards for the Redis cluster.
required: true
- !ruby/object:Api::Type::Boolean
name: deletionProtectionEnabled
description: |
Optional. Indicates if the cluster is deletion protected or not.
If the value if set to true, any delete cluster operation will fail.
Default value is true.
required: false
default_value: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be a better fit for default_from_api if this is also a server-side default -- if this config was manually overridden for a pre-existing resource by a user via cloud console or pantheon, adding a field with a default value could potentially introduce a breaking change for users' configs (i.e. it potentially planning to do false -> true for users who don't manually add deletion_protection_enabled = false to their config)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @NickElliot , it is not the default value. The server-side default is false. It will also not be backward compatible for existing terraform plans. Is my understanding correct?

- !ruby/object:Api::Type::KeyValuePairs
name: 'redisConfigs'
description: |
Expand Down
6 changes: 2 additions & 4 deletions mmv1/templates/terraform/examples/redis_cluster_ha.tf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ resource "google_redis_cluster" "<%= ctx[:primary_resource_id] %>" {
redis_configs = {
maxmemory-policy = "volatile-ttl"
}
deletion_protection_enabled = <%= ctx[:vars]['deletion_protection_enabled'] == 'true' %>

zone_distribution_config {
mode = "MULTI_ZONE"
}
depends_on = [
google_network_connectivity_service_connection_policy.default
]

lifecycle {
prevent_destroy = <%= ctx[:vars]['prevent_destroy'] %>
}
}

resource "google_network_connectivity_service_connection_policy" "default" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ resource "google_redis_cluster" "<%= ctx[:primary_resource_id] %>" {
mode = "SINGLE_ZONE"
zone = "us-central1-f"
}
deletion_protection_enabled = <%= ctx[:vars]['deletion_protection_enabled'] == 'true' %>
depends_on = [
google_network_connectivity_service_connection_policy.default
]

lifecycle {
prevent_destroy = <%= ctx[:vars]['prevent_destroy'] %>
}
}

resource "google_network_connectivity_service_connection_policy" "default" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {

t.Parallel()

name := fmt.Sprintf("tf-test-%d", acctest.RandInt(t))
Expand All @@ -23,7 +24,7 @@ func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
Steps: []resource.TestStep{
{
// create cluster with replica count 1
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, nodeType: "REDIS_STANDARD_SMALL", zoneDistributionMode: "MULTI_ZONE"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, deletionProtectionEnabled: true, nodeType: "REDIS_STANDARD_SMALL", zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -33,7 +34,7 @@ func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, nodeType: "REDIS_STANDARD_SMALL", zoneDistributionMode: "MULTI_ZONE"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, deletionProtectionEnabled: false, nodeType: "REDIS_STANDARD_SMALL", zoneDistributionMode: "MULTI_ZONE"}),
},
},
})
Expand All @@ -53,7 +54,7 @@ func TestAccRedisCluster_createClusterWithZoneDistribution(t *testing.T) {
Steps: []resource.TestStep{
{
// create cluster with replica count 1
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, zoneDistributionMode: "SINGLE_ZONE", zone: "us-central1-b"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, deletionProtectionEnabled: false, zoneDistributionMode: "SINGLE_ZONE", zone: "us-central1-b"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -63,7 +64,7 @@ func TestAccRedisCluster_createClusterWithZoneDistribution(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, zoneDistributionMode: "SINGLE_ZONE", zone: "us-central1-b"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, deletionProtectionEnabled: false, zoneDistributionMode: "SINGLE_ZONE", zone: "us-central1-b"}),
},
},
})
Expand All @@ -82,7 +83,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
Steps: []resource.TestStep{
{
// create cluster with replica count 1
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, deletionProtectionEnabled: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -92,21 +93,17 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
},
{
// update replica count to 2
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 2, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 2, shardCount: 3, deletionProtectionEnabled: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"psc_configs"},
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: false, zoneDistributionMode: "MULTI_ZONE"}),
},
{
// update replica count to 0
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, deletionProtectionEnabled: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -116,7 +113,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, zoneDistributionMode: "MULTI_ZONE"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, deletionProtectionEnabled: false, zoneDistributionMode: "MULTI_ZONE"}),
},
},
})
Expand All @@ -135,7 +132,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
Steps: []resource.TestStep{
{
// create cluster with shard count 3
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, deletionProtectionEnabled: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -145,7 +142,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
},
{
// update shard count to 5
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, deletionProtectionEnabled: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -155,7 +152,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, preventDestroy: false, zoneDistributionMode: "MULTI_ZONE"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, deletionProtectionEnabled: false, zoneDistributionMode: "MULTI_ZONE"}),
},
},
})
Expand Down Expand Up @@ -214,25 +211,58 @@ func TestAccRedisCluster_updateRedisConfigs(t *testing.T) {
})
}

// Validate that deletion protection enabled/disabled cluster is created updated
func TestAccRedisCluster_createUpdateDeletionProtection(t *testing.T) {
t.Parallel()

name := fmt.Sprintf("tf-test-%d", acctest.RandInt(t))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
CheckDestroy: testAccCheckRedisClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
// create cluster with deletion protection set to false
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, deletionProtectionEnabled: false, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"psc_configs"},
},
{
// update deletion protection to true
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, deletionProtectionEnabled: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"psc_configs"},
},
{
// update deletion protection to false and delete the cluster
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, deletionProtectionEnabled: false, zoneDistributionMode: "MULTI_ZONE"}),
},

},
})
}

type ClusterParams struct {
name string
replicaCount int
shardCount int
preventDestroy bool
deletionProtectionEnabled bool
nodeType string
redisConfigs map[string]string
zoneDistributionMode string
zone string
}

func createOrUpdateRedisCluster(params *ClusterParams) string {
lifecycleBlock := ""
if params.preventDestroy {
lifecycleBlock = `
lifecycle {
prevent_destroy = true
}`
}
var strBuilder strings.Builder
for key, value := range params.redisConfigs {
strBuilder.WriteString(fmt.Sprintf("%s = \"%s\"\n", key, value))
Expand All @@ -255,6 +285,7 @@ resource "google_redis_cluster" "test" {
replica_count = %d
shard_count = %d
node_type = "%s"
deletion_protection_enabled = %v
region = "us-central1"
psc_configs {
network = google_compute_network.producer_net.id
Expand All @@ -264,9 +295,8 @@ resource "google_redis_cluster" "test" {
}
%s
depends_on = [
google_network_connectivity_service_connection_policy.default
]
%s
google_network_connectivity_service_connection_policy.default
]
}

resource "google_network_connectivity_service_connection_policy" "default" {
Expand Down Expand Up @@ -294,7 +324,7 @@ resource "google_compute_network" "producer_net" {
name = "%s"
auto_create_subnetworks = false
}
`, params.name, params.replicaCount, params.shardCount, params.nodeType, strBuilder.String(), zoneDistributionConfigBlock, lifecycleBlock, params.name, params.name, params.name)
`, params.name, params.replicaCount, params.shardCount, params.nodeType, params.deletionProtectionEnabled, strBuilder.String(), zoneDistributionConfigBlock, params.name, params.name, params.name)
}

<% end -%>
Loading