From d0ebd64b981cde39f3e9bf5d79d6c592f838b67f Mon Sep 17 00:00:00 2001 From: The Magician Date: Thu, 24 Jun 2021 10:45:18 -0700 Subject: [PATCH] support binary_logging for replica instances (#4907) (#3379) Signed-off-by: Modular Magician --- .changelog/4907.txt | 3 +++ google-beta/resource_sql_database_instance.go | 26 +++++++++++++++++++ .../resource_sql_database_instance_test.go | 9 ++++--- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .changelog/4907.txt diff --git a/.changelog/4907.txt b/.changelog/4907.txt new file mode 100644 index 0000000000..5b27a69d79 --- /dev/null +++ b/.changelog/4907.txt @@ -0,0 +1,3 @@ +```release-note:bug +sql: add support for `binary_logging` on replica instances for `googe_sql_database_instance` +``` diff --git a/google-beta/resource_sql_database_instance.go b/google-beta/resource_sql_database_instance.go index 3e3106ea07..ca0bb6ae82 100644 --- a/google-beta/resource_sql_database_instance.go +++ b/google-beta/resource_sql_database_instance.go @@ -852,6 +852,17 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) } } + var patchData *sqladmin.DatabaseInstance + + // BinaryLogging can be enabled on replica instances but only after creation. + if instance.MasterInstanceName != "" && instance.Settings != nil && instance.Settings.BackupConfiguration != nil { + bc := instance.Settings.BackupConfiguration + instance.Settings.BackupConfiguration = nil + if bc.BinaryLogEnabled { + patchData = &sqladmin.DatabaseInstance{Settings: &sqladmin.Settings{BackupConfiguration: bc}} + } + } + var op *sqladmin.Operation err = retryTimeDuration(func() (operr error) { if cloneContext != nil { @@ -879,6 +890,21 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) return err } + // patch any fields that need to be sent postcreation + if patchData != nil { + err = retryTimeDuration(func() (rerr error) { + op, rerr = config.NewSqlAdminClient(userAgent).Instances.Patch(project, instance.Name, patchData).Do() + return rerr + }, d.Timeout(schema.TimeoutUpdate), isSqlOperationInProgressError) + if err != nil { + return fmt.Errorf("Error, failed to update instance settings for %s: %s", instance.Name, err) + } + err = sqlAdminOperationWaitTime(config, op, project, "Patch Instance", userAgent, d.Timeout(schema.TimeoutUpdate)) + if err != nil { + return err + } + } + err = resourceSqlDatabaseInstanceRead(d, meta) if err != nil { return err diff --git a/google-beta/resource_sql_database_instance_test.go b/google-beta/resource_sql_database_instance_test.go index 31410e83c9..58c0868854 100644 --- a/google-beta/resource_sql_database_instance_test.go +++ b/google-beta/resource_sql_database_instance_test.go @@ -1137,7 +1137,7 @@ resource "google_sql_database_instance" "instance" { var testGoogleSqlDatabaseInstance_replica = ` resource "google_sql_database_instance" "instance_master" { name = "tf-lw-%d" - database_version = "MYSQL_5_6" + database_version = "MYSQL_5_7" region = "us-central1" deletion_protection = false @@ -1154,12 +1154,15 @@ resource "google_sql_database_instance" "instance_master" { resource "google_sql_database_instance" "replica1" { name = "tf-lw-%d-1" - database_version = "MYSQL_5_6" + database_version = "MYSQL_5_7" region = "us-central1" deletion_protection = false settings { tier = "db-n1-standard-1" + backup_configuration { + binary_log_enabled = true + } } master_instance_name = google_sql_database_instance.instance_master.name @@ -1176,7 +1179,7 @@ resource "google_sql_database_instance" "replica1" { resource "google_sql_database_instance" "replica2" { name = "tf-lw-%d-2" - database_version = "MYSQL_5_6" + database_version = "MYSQL_5_7" region = "us-central1" deletion_protection = false