Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/google: Fix master_instance_name to prevent slave rebuilds #11477

Merged
merged 1 commit into from
Jan 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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