Skip to content

Commit

Permalink
feat: Support no read-replication for Memorystore
Browse files Browse the repository at this point in the history
Refs: #DEVOP-4801

Signed-off-by: Christian Witts <christian@honestbank.com>
  • Loading branch information
ChristianWitts committed Jul 19, 2024
1 parent d569afc commit f96bb6a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions modules/memstore_redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

| Name | Version |
|------|---------|
| <a name="provider_google"></a> [google](#provider\_google) | 4.29.0 |
| <a name="provider_google"></a> [google](#provider\_google) | 5.38.0 |

## Modules

Expand All @@ -33,7 +33,7 @@ No modules.
| <a name="input_read_replicas_enabled"></a> [read\_replicas\_enabled](#input\_read\_replicas\_enabled) | Whether to enable read replicas | `bool` | `false` | no |
| <a name="input_redis_version"></a> [redis\_version](#input\_redis\_version) | The version of Redis to use | `string` | n/a | yes |
| <a name="input_region"></a> [region](#input\_region) | The region to create the instance in | `string` | `"asia-southeast2"` | no |
| <a name="input_replicas"></a> [replicas](#input\_replicas) | The number of instances to create | `number` | `1` | no |
| <a name="input_replicas"></a> [replicas](#input\_replicas) | The number of read replicas to create | `number` | `0` | no |
| <a name="input_reserved_ip_range"></a> [reserved\_ip\_range](#input\_reserved\_ip\_range) | The reserved IP range to use for the instance | `string` | `null` | no |
| <a name="input_tier"></a> [tier](#input\_tier) | The tier of the instance | `string` | n/a | yes |
| <a name="input_zone"></a> [zone](#input\_zone) | The location to create the instance in | `string` | n/a | yes |
Expand Down
4 changes: 2 additions & 2 deletions modules/memstore_redis/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions modules/memstore_redis/validations.tf
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 3 additions & 4 deletions modules/memstore_redis/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -49,10 +48,10 @@ 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
condition = var.replicas >= 0 && var.replicas <= 5
error_message = "The valid range for the Standard Tier with read replicas enabled is [1-5] and defaults to 1."
}
}
Expand Down

0 comments on commit f96bb6a

Please sign in to comment.