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 24, 2024
1 parent d569afc commit 38c6fe9
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/checkov.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: "Checkov GitHub Action"
permissions: read-all
on:
pull_request:
branches: [test, dev, qa, prod, main]
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/semantic-pr.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: "Semantic Pull Request"

permissions:
contents: write
pull-requests: write
on:
pull_request:
types:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: "terraform"
permissions: read-all
on:
pull_request:
branches:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/terratest.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
name: terratest
permissions:
contents: write
pull-requests: write
on:
pull_request:
branches:
Expand Down
2 changes: 1 addition & 1 deletion examples/create_redis_public_ip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

| Name | Version |
|------|---------|
| <a name="provider_random"></a> [random](#provider\_random) | 3.3.2 |
| <a name="provider_random"></a> [random](#provider\_random) | n/a |

## Modules

Expand Down
3 changes: 3 additions & 0 deletions examples/create_redis_public_ip/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ resource "random_id" "instance_suffix" {
}

module "private_network" {
#checkov:skip=CKV_TF_1:We use the version tag instead of the commit hash
source = "git::https://github.com/honestbank/terraform-gcp-sql.git//modules/google_compute_network?ref=v1.1.1"
name = "test-redis-terraform-${random_id.instance_suffix.hex}"
}

module "google_compute_global_address_private_ip_address" {
#checkov:skip=CKV_TF_1:We use the version tag instead of the commit hash
source = "git::https://github.com/honestbank/terraform-gcp-sql.git//modules/google_compute_global_address?ref=v1.1.1"

name = "redis-pip-${random_id.instance_suffix.hex}"
Expand All @@ -28,6 +30,7 @@ module "google_compute_global_address_private_ip_address" {
}

module "google_service_networking_connection_private_vpc_connection" {
#checkov:skip=CKV_TF_1:We use the version tag instead of the commit hash
source = "git::https://github.com/honestbank/terraform-gcp-sql.git//modules/google_service_networking_connection?ref=v1.1.1"

network = module.private_network.id
Expand Down
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
}
9 changes: 4 additions & 5 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,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."
}
}

Expand Down

0 comments on commit 38c6fe9

Please sign in to comment.