From 103c7941dea68ec7fa181db6bd8e18d37c84d39c Mon Sep 17 00:00:00 2001
From: Dariusch Ochlast <Dariusch@users.noreply.github.com>
Date: Tue, 21 Feb 2023 18:59:13 +0100
Subject: [PATCH] feat: add persistence configuration for Redis (#125)

---
 README.md              |  1 +
 examples/redis/main.tf |  4 ++++
 main.tf                |  8 ++++++++
 variables.tf           | 10 +++++++++-
 versions.tf            |  2 +-
 5 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 48114502..bd5f71b9 100644
--- a/README.md
+++ b/README.md
@@ -40,6 +40,7 @@ module "memorystore" {
 | maintenance\_policy | The maintenance policy for an instance. | <pre>object({<br>    day = string<br>    start_time = object({<br>      hours   = number<br>      minutes = number<br>      seconds = number<br>      nanos   = number<br>    })<br>  })</pre> | `null` | no |
 | memory\_size\_gb | Redis memory size in GiB. Defaulted to 1 GiB | `number` | `1` | no |
 | name | The ID of the instance or a fully qualified identifier for the instance. | `string` | n/a | yes |
+| persistence\_config | The Redis persistence configuration parameters. https://cloud.google.com/memorystore/docs/redis/reference/rest/v1/projects.locations.instances#persistenceconfig | <pre>object({<br>    persistence_mode    = string<br>    rdb_snapshot_period = string<br>  })</pre> | `null` | no |
 | project | The ID of the project in which the resource belongs to. | `string` | n/a | yes |
 | read\_replicas\_mode | Read replicas mode. https://cloud.google.com/memorystore/docs/redis/reference/rest/v1/projects.locations.instances#readreplicasmode | `string` | `"READ_REPLICAS_DISABLED"` | no |
 | redis\_configs | The Redis configuration parameters. See [more details](https://cloud.google.com/memorystore/docs/redis/reference/rest/v1/projects.locations.instances#Instance.FIELDS.redis_configs) | `map(any)` | `{}` | no |
diff --git a/examples/redis/main.tf b/examples/redis/main.tf
index dfba804b..979b0657 100644
--- a/examples/redis/main.tf
+++ b/examples/redis/main.tf
@@ -28,4 +28,8 @@ module "memstore" {
   transit_encryption_mode = "SERVER_AUTHENTICATION"
   authorized_network      = module.test-vpc-module.network_id
   memory_size_gb          = 1
+  persistence_config = {
+    persistence_mode    = "RDB"
+    rdb_snapshot_period = "ONE_HOUR"
+  }
 }
diff --git a/main.tf b/main.tf
index ff9aefb6..7e68a01d 100644
--- a/main.tf
+++ b/main.tf
@@ -57,6 +57,14 @@ resource "google_redis_instance" "default" {
       }
     }
   }
+
+  dynamic "persistence_config" {
+    for_each = var.persistence_config != null ? [var.persistence_config] : []
+    content {
+      persistence_mode    = persistence_config.value["persistence_mode"]
+      rdb_snapshot_period = persistence_config.value["rdb_snapshot_period"]
+    }
+  }
 }
 
 module "enable_apis" {
diff --git a/variables.tf b/variables.tf
index b2723207..fc19ab75 100644
--- a/variables.tf
+++ b/variables.tf
@@ -141,9 +141,17 @@ variable "maintenance_policy" {
   default = null
 }
 
-
 variable "customer_managed_key" {
   description = "Default encryption key to apply to the Redis instance. Defaults to null (Google-managed)."
   type        = string
   default     = null
 }
+
+variable "persistence_config" {
+  description = "The Redis persistence configuration parameters. https://cloud.google.com/memorystore/docs/redis/reference/rest/v1/projects.locations.instances#persistenceconfig"
+  type = object({
+    persistence_mode    = string
+    rdb_snapshot_period = string
+  })
+  default = null
+}
diff --git a/versions.tf b/versions.tf
index cd1a6298..1f1fbf62 100644
--- a/versions.tf
+++ b/versions.tf
@@ -20,7 +20,7 @@ terraform {
 
     google = {
       source  = "hashicorp/google"
-      version = ">= 4.28.0, < 5.0"
+      version = ">= 4.38.0, < 5.0"
     }
   }