Skip to content

Commit

Permalink
support binary_logging for replica instances (#4907) (#9428)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jun 24, 2021
1 parent eb82ee9 commit 6d23a8d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/4907.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
sql: add support for `binary_logging` on replica instances for `googe_sql_database_instance`
```
26 changes: 26 additions & 0 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,17 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
defer mutexKV.Unlock(instanceMutexKey(project, instance.MasterInstanceName))
}

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 {
Expand Down Expand Up @@ -866,6 +877,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
Expand Down
9 changes: 6 additions & 3 deletions google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,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
Expand All @@ -1093,12 +1093,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
Expand All @@ -1115,7 +1118,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
Expand Down

0 comments on commit 6d23a8d

Please sign in to comment.