Skip to content

Commit

Permalink
support binary_logging for replica instances (#4907) (#3379)
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 61e9b53 commit d0ebd64
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-beta/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions google-beta/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d0ebd64

Please sign in to comment.