Skip to content

Commit dba09c1

Browse files
Fix permadiff when edition field is added to configuration of a google_sql_database_instance resource (#9198) (#16215)
* Suppress diff on `google_sql_database_instance`'s `edition` field when state contains `""` and config contains `"ENTERPRISE"` * Remove `DiffSuppressFunc`, add flattener and expander to handle conversion of `""` to `"ENTERPRISE"` * Remove duplicate import * Set default value of `edition` to `"ENTERPRISE"` * Remove expander, add state upgrader * Add acceptance test step that provisions an instance without `edition` set * Update tests to assert the value in state matches the edition used, including when edition is not set in config * Remove state upgrader [upstream:241a15cb0ed6ae99c3290825051f168deda648fd] Signed-off-by: Modular Magician <[email protected]>
1 parent 704023e commit dba09c1

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

.changelog/9198.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
sql: Fixed a bug where adding the `edition` field to a `google_sql_database_instance` resource that already existed and used ENTERPRISE edition resulted in a permant diff in plans
3+
```

google/services/sql/resource_sql_database_instance.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"log"
10+
"reflect"
1011
"strings"
1112
"time"
1213

@@ -177,6 +178,7 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
177178
"edition": {
178179
Type: schema.TypeString,
179180
Optional: true,
181+
Default: "ENTERPRISE",
180182
ValidateFunc: validation.StringInSlice([]string{"ENTERPRISE", "ENTERPRISE_PLUS"}, false),
181183
Description: `The edition of the instance, can be ENTERPRISE or ENTERPRISE_PLUS.`,
182184
},
@@ -2003,7 +2005,7 @@ func flattenSettings(settings *sqladmin.Settings) []map[string]interface{} {
20032005
data := map[string]interface{}{
20042006
"version": settings.SettingsVersion,
20052007
"tier": settings.Tier,
2006-
"edition": settings.Edition,
2008+
"edition": flattenEdition(settings.Edition),
20072009
"activation_policy": settings.ActivationPolicy,
20082010
"availability_type": settings.AvailabilityType,
20092011
"collation": settings.Collation,
@@ -2331,6 +2333,14 @@ func flattenPasswordValidationPolicy(passwordValidationPolicy *sqladmin.Password
23312333
return []map[string]interface{}{data}
23322334
}
23332335

2336+
func flattenEdition(v interface{}) string {
2337+
if v == nil || tpgresource.IsEmptyValue(reflect.ValueOf(v)) {
2338+
return "ENTERPRISE"
2339+
}
2340+
2341+
return v.(string)
2342+
}
2343+
23342344
func instanceMutexKey(project, instance_name string) string {
23352345
return fmt.Sprintf("google-sql-database-instance-%s-%s", project, instance_name)
23362346
}

google/services/sql/resource_sql_database_instance_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -1520,22 +1520,43 @@ func TestAccSqlDatabaseInstance_Edition(t *testing.T) {
15201520
enterprisePlusTier := "db-perf-optimized-N-2"
15211521
enterpriseName := "tf-test-enterprise-" + acctest.RandString(t, 10)
15221522
enterpriseTier := "db-custom-2-13312"
1523+
noEditionName := "tf-test-enterprise-noedition-" + acctest.RandString(t, 10)
15231524
acctest.VcrTest(t, resource.TestCase{
15241525
PreCheck: func() { acctest.AccTestPreCheck(t) },
15251526
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
15261527
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
15271528
Steps: []resource.TestStep{
1529+
{
1530+
Config: testGoogleSqlDatabaseInstance_EditionConfig_noEdition(noEditionName, enterpriseTier),
1531+
Check: resource.ComposeAggregateTestCheckFunc(
1532+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"),
1533+
),
1534+
},
1535+
{
1536+
ResourceName: "google_sql_database_instance.instance",
1537+
ImportState: true,
1538+
ImportStateVerify: true,
1539+
ImportStateVerifyIgnore: []string{"deletion_protection"},
1540+
},
1541+
// Delete and recreate (ForceNew) triggered by passing in a new `name` value
15281542
{
15291543
Config: testGoogleSqlDatabaseInstance_EditionConfig(enterprisePlusName, enterprisePlusTier, "ENTERPRISE_PLUS"),
1544+
Check: resource.ComposeAggregateTestCheckFunc(
1545+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE_PLUS"),
1546+
),
15301547
},
15311548
{
15321549
ResourceName: "google_sql_database_instance.instance",
15331550
ImportState: true,
15341551
ImportStateVerify: true,
15351552
ImportStateVerifyIgnore: []string{"deletion_protection"},
15361553
},
1554+
// Delete and recreate (ForceNew) triggered by passing in a new `name` value
15371555
{
15381556
Config: testGoogleSqlDatabaseInstance_EditionConfig(enterpriseName, enterpriseTier, "ENTERPRISE"),
1557+
Check: resource.ComposeAggregateTestCheckFunc(
1558+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"),
1559+
),
15391560
},
15401561
{
15411562
ResourceName: "google_sql_database_instance.instance",
@@ -2241,6 +2262,20 @@ resource "google_sql_database_instance" "instance" {
22412262
}`, databaseName, endDate, startDate, time)
22422263
}
22432264

2265+
func testGoogleSqlDatabaseInstance_EditionConfig_noEdition(databaseName, tier string) string {
2266+
return fmt.Sprintf(`
2267+
2268+
resource "google_sql_database_instance" "instance" {
2269+
name = "%s"
2270+
region = "us-east1"
2271+
database_version = "POSTGRES_14"
2272+
deletion_protection = false
2273+
settings {
2274+
tier = "%s"
2275+
}
2276+
}`, databaseName, tier)
2277+
}
2278+
22442279
func testGoogleSqlDatabaseInstance_EditionConfig(databaseName, tier, edition string) string {
22452280
return fmt.Sprintf(`
22462281

0 commit comments

Comments
 (0)