Skip to content

Commit 796c7ea

Browse files
resource_sql_database_instance.go normalizes failover_dr_replica_name name for diff (#13579) (#22319)
[upstream:2321dedaa0d38225ba429fa252c673a614c2c036] Signed-off-by: Modular Magician <[email protected]>
1 parent e5566db commit 796c7ea

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

.changelog/13579.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
sql: fixed the issue of shortened version of failover_dr_replica_name causes unnecessary diff
3+
```

google/services/sql/resource_sql_database_instance.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,9 +935,10 @@ is set to true. Defaults to ZONAL.`,
935935
Elem: &schema.Resource{
936936
Schema: map[string]*schema.Schema{
937937
"failover_dr_replica_name": {
938-
Type: schema.TypeString,
939-
Optional: true,
940-
Description: fmt.Sprintf(`If the instance is a primary instance, then this field identifies the disaster recovery (DR) replica. The standard format of this field is "your-project:your-instance". You can also set this field to "your-instance", but cloud SQL backend will convert it to the aforementioned standard format.`),
938+
Type: schema.TypeString,
939+
Optional: true,
940+
Description: fmt.Sprintf(`If the instance is a primary instance, then this field identifies the disaster recovery (DR) replica. The standard format of this field is "your-project:your-instance". You can also set this field to "your-instance", but cloud SQL backend will convert it to the aforementioned standard format.`),
941+
DiffSuppressFunc: areDRReplicaNamesEqual,
941942
},
942943
"dr_replica": {
943944
Type: schema.TypeBool,
@@ -2787,3 +2788,21 @@ func validatePromoteConfigurations(masterInstanceName cty.Value, replicaConfigur
27872788
}
27882789
return nil
27892790
}
2791+
2792+
// areDRReplicaNamesEqual compares 2 DR replica names.
2793+
func areDRReplicaNamesEqual(_, oldValue, newValue string, d *schema.ResourceData) bool {
2794+
project := d.Get("project").(string)
2795+
// When we can't find project, just fallback to naive comparison.
2796+
if project == "" {
2797+
return oldValue == newValue
2798+
}
2799+
return normalizeDRReplicaName(oldValue, project) == normalizeDRReplicaName(newValue, project)
2800+
}
2801+
2802+
// normalizeDRReplicaName normalizes DR replica name by prefixing project name when it's missing.
2803+
func normalizeDRReplicaName(drReplicaName, project string) string {
2804+
if strings.Contains(drReplicaName, ":") {
2805+
return drReplicaName
2806+
}
2807+
return fmt.Sprintf("%s:%s", project, drReplicaName)
2808+
}

google/services/sql/resource_sql_database_instance_test.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,14 +2618,16 @@ func TestAccSqlDatabaseInstance_MysqlSwitchoverSuccess(t *testing.T) {
26182618
ImportStateVerifyIgnore: ignoredReplicaConfigurationFields,
26192619
},
26202620
// Let's make sure that setting and unsetting failover replica works.
2621+
// Make sure we test both normalized and shortened version of DR replica
2622+
// names.
26212623
{
2622-
Config: googleSqlDatabaseInstance_mysqlSetFailoverReplica(project, primaryName, replicaName),
2624+
Config: googleSqlDatabaseInstance_mysqlSetFailoverReplica(project, primaryName, replicaName, false /* =useNormalizedDrReplicaName */),
26232625
},
26242626
{
26252627
Config: googleSqlDatabaseInstance_mysqlUnsetFailoverReplica(project, primaryName, replicaName),
26262628
},
26272629
{
2628-
Config: googleSqlDatabaseInstance_mysqlSetFailoverReplica(project, primaryName, replicaName),
2630+
Config: googleSqlDatabaseInstance_mysqlSetFailoverReplica(project, primaryName, replicaName, true /* =useNormalizedDrReplicaName */),
26292631
},
26302632
{
26312633
// Split into two configs because current TestStep implementation checks diff before refreshing.
@@ -2694,14 +2696,16 @@ func TestAccSqlDatabaseInstance_PostgresSwitchoverSuccess(t *testing.T) {
26942696
ImportStateVerifyIgnore: ignoredReplicaConfigurationFields,
26952697
},
26962698
// Let's make sure that setting and unsetting failover replica works.
2699+
// Make sure we test both normalized and shortened version of DR replica
2700+
// names.
26972701
{
2698-
Config: googleSqlDatabaseInstance_postgresSetFailoverReplica(project, primaryName, replicaName),
2702+
Config: googleSqlDatabaseInstance_postgresSetFailoverReplica(project, primaryName, replicaName, false /* =useNormalizedDrReplicaName */),
26992703
},
27002704
{
27012705
Config: googleSqlDatabaseInstance_postgresUnsetFailoverReplica(project, primaryName, replicaName),
27022706
},
27032707
{
2704-
Config: googleSqlDatabaseInstance_postgresSetFailoverReplica(project, primaryName, replicaName),
2708+
Config: googleSqlDatabaseInstance_postgresSetFailoverReplica(project, primaryName, replicaName, true /* =useNormalizedDrReplicaName */),
27052709
},
27062710
{
27072711
// Split into two configs because current TestStep implementation checks diff before refreshing.
@@ -3781,7 +3785,12 @@ resource "google_sql_database_instance" "original-replica" {
37813785
`, project, primaryName, project, replicaName)
37823786
}
37833787

3784-
func googleSqlDatabaseInstance_mysqlSetFailoverReplica(project, primaryName, replicaName string) string {
3788+
func googleSqlDatabaseInstance_mysqlSetFailoverReplica(project, primaryName, replicaName string, useNormalizedDrReplicaName bool) string {
3789+
drReplicaName := fmt.Sprintf("%s:%s", project, replicaName)
3790+
if !useNormalizedDrReplicaName {
3791+
drReplicaName = replicaName
3792+
}
3793+
37853794
return fmt.Sprintf(`
37863795
resource "google_sql_database_instance" "original-primary" {
37873796
project = "%s"
@@ -3792,7 +3801,7 @@ resource "google_sql_database_instance" "original-primary" {
37923801
deletion_protection = false
37933802
37943803
replication_cluster {
3795-
failover_dr_replica_name = "%s:%s"
3804+
failover_dr_replica_name = "%s"
37963805
}
37973806
37983807
settings {
@@ -3819,7 +3828,7 @@ resource "google_sql_database_instance" "original-replica" {
38193828
edition = "ENTERPRISE_PLUS"
38203829
}
38213830
}
3822-
`, project, primaryName, project, replicaName, project, replicaName, primaryName)
3831+
`, project, primaryName, drReplicaName, project, replicaName, primaryName)
38233832
}
38243833

38253834
func googleSqlDatabaseInstance_mysqlUnsetFailoverReplica(project, primaryName, replicaName string) string {
@@ -4055,7 +4064,11 @@ resource "google_sql_database_instance" "original-replica" {
40554064
`, project, primaryName, project, replicaName)
40564065
}
40574066

4058-
func googleSqlDatabaseInstance_postgresSetFailoverReplica(project, primaryName, replicaName string) string {
4067+
func googleSqlDatabaseInstance_postgresSetFailoverReplica(project, primaryName, replicaName string, useNormalizedDrReplicaName bool) string {
4068+
drReplicaName := fmt.Sprintf("%s:%s", project, replicaName)
4069+
if !useNormalizedDrReplicaName {
4070+
drReplicaName = replicaName
4071+
}
40594072
return fmt.Sprintf(`
40604073
resource "google_sql_database_instance" "original-primary" {
40614074
project = "%s"
@@ -4066,7 +4079,7 @@ resource "google_sql_database_instance" "original-primary" {
40664079
deletion_protection = false
40674080
40684081
replication_cluster {
4069-
failover_dr_replica_name = "%s:%s"
4082+
failover_dr_replica_name = "%s"
40704083
}
40714084
40724085
settings {
@@ -4093,7 +4106,7 @@ resource "google_sql_database_instance" "original-replica" {
40934106
edition = "ENTERPRISE_PLUS"
40944107
}
40954108
}
4096-
`, project, primaryName, project, replicaName, project, replicaName, primaryName)
4109+
`, project, primaryName, drReplicaName, project, replicaName, primaryName)
40974110
}
40984111

40994112
func googleSqlDatabaseInstance_postgresUnsetFailoverReplica(project, primaryName, replicaName string) string {

0 commit comments

Comments
 (0)