From 175b7a1e4837e82b364fbfdd8b3e112b9908a318 Mon Sep 17 00:00:00 2001 From: Christian Witts Date: Fri, 19 Jul 2024 13:52:19 +0700 Subject: [PATCH] feat: Support no read-replication for Memorystore Refs: #DEVOP-4801 Signed-off-by: Christian Witts --- modules/memstore_redis/README.md | 4 ++-- modules/memstore_redis/main.tf | 4 ++-- modules/memstore_redis/validations.tf | 8 ++++++++ modules/memstore_redis/variables.tf | 9 ++++----- 4 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 modules/memstore_redis/validations.tf diff --git a/modules/memstore_redis/README.md b/modules/memstore_redis/README.md index 1ae7507..fa7fee6 100644 --- a/modules/memstore_redis/README.md +++ b/modules/memstore_redis/README.md @@ -9,7 +9,7 @@ | Name | Version | |------|---------| -| [google](#provider\_google) | 4.29.0 | +| [google](#provider\_google) | 5.38.0 | ## Modules @@ -33,7 +33,7 @@ No modules. | [read\_replicas\_enabled](#input\_read\_replicas\_enabled) | Whether to enable read replicas | `bool` | `false` | no | | [redis\_version](#input\_redis\_version) | The version of Redis to use | `string` | n/a | yes | | [region](#input\_region) | The region to create the instance in | `string` | `"asia-southeast2"` | no | -| [replicas](#input\_replicas) | The number of instances to create | `number` | `1` | no | +| [replicas](#input\_replicas) | The number of read replicas to create | `number` | `0` | no | | [reserved\_ip\_range](#input\_reserved\_ip\_range) | The reserved IP range to use for the instance | `string` | `null` | no | | [tier](#input\_tier) | The tier of the instance | `string` | n/a | yes | | [zone](#input\_zone) | The location to create the instance in | `string` | n/a | yes | diff --git a/modules/memstore_redis/main.tf b/modules/memstore_redis/main.tf index d4e15ca..4846341 100644 --- a/modules/memstore_redis/main.tf +++ b/modules/memstore_redis/main.tf @@ -11,13 +11,13 @@ terraform { resource "google_redis_instance" "cache" { name = replace(var.name, " ", "-") tier = var.tier - memory_size_gb = var.memory_size + memory_size_gb = local.memory_size auth_enabled = true location_id = "${var.region}-${var.zone}" alternative_location_id = var.tier == "STANDARD_HA" ? "${var.region}-${var.alternative_zone}" : "" - replica_count = var.tier == "STANDARD_HA" ? var.replicas : 0 + replica_count = local.replica_count read_replicas_mode = var.read_replicas_enabled ? "READ_REPLICAS_ENABLED" : "READ_REPLICAS_DISABLED" authorized_network = var.network_id diff --git a/modules/memstore_redis/validations.tf b/modules/memstore_redis/validations.tf new file mode 100644 index 0000000..89afb0d --- /dev/null +++ b/modules/memstore_redis/validations.tf @@ -0,0 +1,8 @@ +locals { + # Generate invalid values to be used so that the module fails based on composite + # scenarios as validation conditions only apply to the variable scoped for the + # validation condition. + memory_size = var.read_replicas_enabled && var.memory_size >= 5 ? var.memory_size : 0 + replicas = var.tier == "STANDARD_HA" ? var.replicas : 0 + replica_count = var.read_replicas_enabled && local.replicas > 0 ? var.replicas : 0 +} diff --git a/modules/memstore_redis/variables.tf b/modules/memstore_redis/variables.tf index b8d4624..ce63779 100644 --- a/modules/memstore_redis/variables.tf +++ b/modules/memstore_redis/variables.tf @@ -40,7 +40,6 @@ variable "reserved_ip_range" { description = "The reserved IP range to use for the instance" } - variable "memory_size" { type = string default = "2" @@ -49,11 +48,11 @@ variable "memory_size" { variable "replicas" { type = number - default = 1 - description = "The number of instances to create" + default = 0 + description = "The number of read replicas to create" validation { - condition = var.replicas >= 1 && var.replicas <= 5 - error_message = "The valid range for the Standard Tier with read replicas enabled is [1-5] and defaults to 1." + condition = var.replicas <= 5 + error_message = "The valid range for the Standard Tier with read replicas enabled is [1-5] and defaults to 0 as the default is zero read-replicas." } }