Fix master_instance_name to prevent slave rebuilds

This commit is contained in:
Mike Fowler 2017-01-27 21:06:46 +00:00
parent 017d7ebf4f
commit b2ac5e9b64
2 changed files with 61 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package google
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
@ -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,
@ -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
}
@ -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)

View File

@ -10,6 +10,7 @@ package google
import (
"fmt"
"strconv"
"strings"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
@ -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()
@ -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)
@ -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"