From d6c6ae943e9a196dd42ab30f543a5d96527e59cd Mon Sep 17 00:00:00 2001 From: Natalia Strelkova Date: Wed, 21 Sep 2022 08:14:24 +0000 Subject: [PATCH] regional MySQL DBs - automatic backup conf --- modules/cloudsql-instance/main.tf | 9 +++++---- tests/modules/cloudsql_instance/test_plan.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/modules/cloudsql-instance/main.tf b/modules/cloudsql-instance/main.tf index de4b5560a1..6cd280be1c 100644 --- a/modules/cloudsql-instance/main.tf +++ b/modules/cloudsql-instance/main.tf @@ -18,10 +18,11 @@ locals { prefix = var.prefix == null ? "" : "${var.prefix}-" is_mysql = can(regex("^MYSQL", var.database_version)) has_replicas = try(length(var.replicas) > 0, false) + is_regional = var.availability_type == "REGIONAL" ? true : false // Enable backup if the user asks for it or if the user is deploying - // MySQL with replicas - enable_backup = var.backup_configuration.enabled || (local.is_mysql && local.has_replicas) + // MySQL in HA configuration (regional or with specified replicas) + enable_backup = var.backup_configuration.enabled || (local.is_mysql && local.has_replicas) || (local.is_mysql && local.is_regional) users = { for user, password in coalesce(var.users, {}) : @@ -76,11 +77,11 @@ resource "google_sql_database_instance" "primary" { content { enabled = true - // enable binary log if the user asks for it or we have replicas, + // enable binary log if the user asks for it or we have replicas (default in regional), // but only for MySQL binary_log_enabled = ( local.is_mysql - ? var.backup_configuration.binary_log_enabled || local.has_replicas + ? var.backup_configuration.binary_log_enabled || local.has_replicas || local.is_regional : null ) start_time = var.backup_configuration.start_time diff --git a/tests/modules/cloudsql_instance/test_plan.py b/tests/modules/cloudsql_instance/test_plan.py index f23c69c7af..72d31daddd 100644 --- a/tests/modules/cloudsql_instance/test_plan.py +++ b/tests/modules/cloudsql_instance/test_plan.py @@ -90,6 +90,17 @@ def test_mysql_replicas_enables_backup(plan_runner): assert backup_config['binary_log_enabled'] +def test_mysql_binary_log_for_regional(plan_runner): + "Test that the binary log will be enabled for regional MySQL DBs." + + _, resources = plan_runner(database_version="MYSQL_8_0", availability_type="REGIONAL") + assert len(resources) == 1 + primary = [r for r in resources if r['name'] == 'primary'][0] + backup_config = primary['values']['settings'][0]['backup_configuration'][0] + assert backup_config['enabled'] + assert backup_config['binary_log_enabled'] + + def test_users(plan_runner): "Test user creation."