Skip to content

Commit

Permalink
feat(firestore): add backups to firestore
Browse files Browse the repository at this point in the history
Chosing not to allow the backup variable to have a default to force
module users to at least think about backups when implementing.
  • Loading branch information
tweakster committed Sep 25, 2024
1 parent 838eb79 commit b0fe463
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
30 changes: 29 additions & 1 deletion modules/gcp_firestore/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,35 @@ resource "google_firestore_database" "this" {
concurrency_mode = var.use_optimistic_concurrency ? "OPTIMISTIC" : "PESSIMISTIC"
app_engine_integration_mode = "DISABLED"

point_in_time_recovery_enablement = var.enable_point_in_time_recovery ? "POINT_IN_TIME_RECOVERY_ENABLED" : "POINT_IN_TIME_RECOVERY_DISABLED"
point_in_time_recovery_enablement = lookup(var.backup, "point_in_time", false) ? "POINT_IN_TIME_RECOVERY_ENABLED" : "POINT_IN_TIME_RECOVERY_DISABLED"
}

locals {
weekly_backup_day_lookup = [
"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"
]
}

resource "google_firestore_backup_schedule" "this" {
count = contains(["d", "w"], var.backup.frequency) ? 1 : 0

database = google_firestore_database.this.name

retention = "${(60 * 60 * 24) * var.backup.retention}s"

dynamic "daily_recurrence" {
for_each = var.backup.frequency == "d" ? { day = true } : {}

content {}
}

dynamic "weekly_recurrence" {
for_each = var.backup.frequency == "w" ? { week = true } : {}

content {
day = local.weekly_backup_day_lookup[var.backup.day]
}
}
}

locals {
Expand Down
63 changes: 59 additions & 4 deletions modules/gcp_firestore/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,65 @@ variable "indexes" {
}
}

variable "enable_point_in_time_recovery" {
description = "Whether or not to enable point in time recovery on the Firestore datastore"
type = bool
default = false
variable "backup" {
description = <<EOD
Configure the backup/data recovery options:
frequency = [d]aily [w]eekly or [n]ever
retention = number of days to retain the backup data for
point_in_time = enable point in time recovery
day = 0-6 the day of the week to do backups (0 = Sunday, 6 = Saturday)
To skip all backups set `backup = { frequency = "n" }`
EOD

type = object({
frequency = string
retention = optional(number, 0)
point_in_time = optional(bool, false)
day = optional(number)
})

validation {
condition = contains(["d", "w", "n"], var.backup.frequency)
error_message = "Frequency should be either [d]aily, [w]eekly or [n]ever"
}

validation {
condition = (
var.backup.frequency == "n"
||
(contains(["d", "w"], coalesce(var.backup.frequency, "null")) && var.backup.retention > 0)
)
error_message = "If enabling backups specify retention > 0"
}

validation {
condition = (
var.backup.frequency != "n"
||
(var.backup.frequency == "n" && var.backup.retention == 0)
)
error_message = "Retention not valid if frequency is never"
}

validation {
condition = (
(var.backup.frequency != "w" && coalesce(var.backup.day, 99) == 99)
||
(var.backup.frequency == "w" && coalesce(var.backup.day, 99) != 99)
)
error_message = "Backup day only valid for weekly backups"
}

validation {
condition = (
var.backup.frequency != "w"
||
(var.backup.frequency == "w" && (coalesce(var.backup.day, 99) >= 0 && coalesce(var.backup.day, 99) <= 6))
)
error_message = "Weekly backup day should be between 0 and 6 for weekly backups"
}
}

variable "use_optimistic_concurrency" {
Expand Down

0 comments on commit b0fe463

Please sign in to comment.