Merge pull request #11477 from gh-mlfowler/google-sql2-slave

provider/google: Fix master_instance_name to prevent slave rebuilds
This commit is contained in:
Dana Hoffman 2017-01-30 14:17:56 -08:00 committed by GitHub
commit f7e2147655
2 changed files with 61 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package google
import ( import (
"fmt" "fmt"
"log" "log"
"strings"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
@ -70,6 +71,7 @@ func resourceSqlDatabaseInstance() *schema.Resource {
"crash_safe_replication": &schema.Schema{ "crash_safe_replication": &schema.Schema{
Type: schema.TypeBool, Type: schema.TypeBool,
Optional: true, Optional: true,
Computed: true,
}, },
"database_flags": &schema.Schema{ "database_flags": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
@ -564,7 +566,7 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
_backupConfiguration["enabled"] = settings.BackupConfiguration.Enabled _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 _backupConfiguration["start_time"] = settings.BackupConfiguration.StartTime
} }
@ -758,7 +760,7 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
d.Set("ip_address", _ipAddresses) d.Set("ip_address", _ipAddresses)
if v, ok := d.GetOk("master_instance_name"); ok && v != nil { 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) d.Set("self_link", instance.SelfLink)

View File

@ -10,6 +10,7 @@ package google
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"testing" "testing"
"github.com/hashicorp/terraform/helper/acctest" "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) { func TestAccGoogleSqlDatabaseInstance_settings_upgrade(t *testing.T) {
var instance sqladmin.DatabaseInstance var instance sqladmin.DatabaseInstance
databaseID := acctest.RandInt() databaseID := acctest.RandInt()
@ -199,7 +228,7 @@ func testAccCheckGoogleSqlDatabaseInstanceEquals(n string,
return fmt.Errorf("Error settings.tier mismatch, (%s, %s)", server, local) 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"] local = attributes["master_instance_name"]
if server != local && len(server) > 0 && len(local) > 0 { if server != local && len(server) > 0 && len(local) > 0 {
return fmt.Errorf("Error master_instance_name mismatch, (%s, %s)", server, local) 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 = ` var testGoogleSqlDatabaseInstance_authNets_step1 = `
resource "google_sql_database_instance" "instance" { resource "google_sql_database_instance" "instance" {
name = "tf-lw-%d" name = "tf-lw-%d"