Skip to content

Commit

Permalink
Fix master_instance_name to prevent slave rebuilds (hashicorp#11477)
Browse files Browse the repository at this point in the history
  • Loading branch information
gh-mlfowler authored and arcadiatea committed Feb 7, 2017
1 parent ec99314 commit c3a9d83
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
6 changes: 4 additions & 2 deletions builtin/providers/google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package google
import (
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -70,6 +71,7 @@ func resourceSqlDatabaseInstance() *schema.Resource {
"crash_safe_replication": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"database_flags": &schema.Schema{
Type: schema.TypeList,
Expand Down Expand Up @@ -564,7 +566,7 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
_backupConfiguration["enabled"] = settings.BackupConfiguration.Enabled
}

if vp, okp := _backupConfiguration["start_time"]; okp && vp != nil {
if vp, okp := _backupConfiguration["start_time"]; okp && len(vp.(string)) > 0 {
_backupConfiguration["start_time"] = settings.BackupConfiguration.StartTime
}

Expand Down Expand Up @@ -758,7 +760,7 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
d.Set("ip_address", _ipAddresses)

if v, ok := d.GetOk("master_instance_name"); ok && v != nil {
d.Set("master_instance_name", instance.MasterInstanceName)
d.Set("master_instance_name", strings.TrimPrefix(instance.MasterInstanceName, project+":"))
}

d.Set("self_link", instance.SelfLink)
Expand Down
58 changes: 57 additions & 1 deletion builtin/providers/google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package google
import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -86,6 +87,34 @@ func TestAccGoogleSqlDatabaseInstance_settings_basic(t *testing.T) {
})
}

func TestAccGoogleSqlDatabaseInstance_slave(t *testing.T) {
var instance sqladmin.DatabaseInstance
masterID := acctest.RandInt()
slaveID := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: fmt.Sprintf(
testGoogleSqlDatabaseInstance_slave, masterID, slaveID),
Check: resource.ComposeTestCheckFunc(
testAccCheckGoogleSqlDatabaseInstanceExists(
"google_sql_database_instance.instance_master", &instance),
testAccCheckGoogleSqlDatabaseInstanceEquals(
"google_sql_database_instance.instance_master", &instance),
testAccCheckGoogleSqlDatabaseInstanceExists(
"google_sql_database_instance.instance_slave", &instance),
testAccCheckGoogleSqlDatabaseInstanceEquals(
"google_sql_database_instance.instance_slave", &instance),
),
},
},
})
}

func TestAccGoogleSqlDatabaseInstance_settings_upgrade(t *testing.T) {
var instance sqladmin.DatabaseInstance
databaseID := acctest.RandInt()
Expand Down Expand Up @@ -199,7 +228,7 @@ func testAccCheckGoogleSqlDatabaseInstanceEquals(n string,
return fmt.Errorf("Error settings.tier mismatch, (%s, %s)", server, local)
}

server = instance.MasterInstanceName
server = strings.TrimPrefix(instance.MasterInstanceName, instance.Project+":")
local = attributes["master_instance_name"]
if server != local && len(server) > 0 && len(local) > 0 {
return fmt.Errorf("Error master_instance_name mismatch, (%s, %s)", server, local)
Expand Down Expand Up @@ -474,6 +503,33 @@ resource "google_sql_database_instance" "instance" {
}
`

var testGoogleSqlDatabaseInstance_slave = `
resource "google_sql_database_instance" "instance_master" {
name = "tf-lw-%d"
region = "us-central1"
settings {
tier = "db-f1-micro"
backup_configuration {
enabled = true
binary_log_enabled = true
}
}
}
resource "google_sql_database_instance" "instance_slave" {
name = "tf-lw-%d"
region = "us-central1"
master_instance_name = "${google_sql_database_instance.instance_master.name}"
settings {
tier = "db-f1-micro"
}
}
`

var testGoogleSqlDatabaseInstance_authNets_step1 = `
resource "google_sql_database_instance" "instance" {
name = "tf-lw-%d"
Expand Down

0 comments on commit c3a9d83

Please sign in to comment.