Skip to content

Commit

Permalink
Fix settings.data_cache_config permadiff when set to false (#12496) (#…
Browse files Browse the repository at this point in the history
…8889)

[upstream:2936c3ba867221c9bfcdeb2a82b5542b0da91897]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Dec 10, 2024
1 parent dcc0e42 commit d408dc5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .changelog/12496.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
sql: fixed permadiff when 'settings.data_cache_config' is set to false for 'google_sql_database_instance' resource
```
12 changes: 11 additions & 1 deletion google-beta/services/sql/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,12 @@ func flattenSettings(settings *sqladmin.Settings, d *schema.ResourceData) []map[

if settings.DataCacheConfig != nil {
data["data_cache_config"] = flattenDataCacheConfig(settings.DataCacheConfig)
} else {
data["data_cache_config"] = []map[string]interface{}{
{
"data_cache_enabled": false,
},
}
}

if settings.AdvancedMachineFeatures != nil {
Expand All @@ -2260,7 +2266,11 @@ func flattenSettings(settings *sqladmin.Settings, d *schema.ResourceData) []map[

func flattenDataCacheConfig(d *sqladmin.DataCacheConfig) []map[string]interface{} {
if d == nil {
return nil
return []map[string]interface{}{
{
"data_cache_enabled": false,
},
}
}
return []map[string]interface{}{
{
Expand Down
40 changes: 29 additions & 11 deletions google-beta/services/sql/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1757,9 +1757,9 @@ func TestAccSQLDatabaseInstance_sqlMysqlDataCacheConfig(t *testing.T) {
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(instanceName),
Config: testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(instanceName, false),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "true"),
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "false"),
),
},
{
Expand All @@ -1768,6 +1768,12 @@ func TestAccSQLDatabaseInstance_sqlMysqlDataCacheConfig(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
{
Config: testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(instanceName, true),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "true"),
),
},
},
})
}
Expand All @@ -1784,7 +1790,7 @@ func TestAccSQLDatabaseInstance_sqlPostgresDataCacheConfig(t *testing.T) {
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(enterprisePlusInstanceName, enterprisePlusTier, "ENTERPRISE_PLUS"),
Config: testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(enterprisePlusInstanceName, enterprisePlusTier, "ENTERPRISE_PLUS", true),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "true"),
),
Expand All @@ -1796,7 +1802,19 @@ func TestAccSQLDatabaseInstance_sqlPostgresDataCacheConfig(t *testing.T) {
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
{
Config: testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(enterpriseInstanceName, enterpriseTier, "ENTERPRISE"),
Config: testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(enterprisePlusInstanceName, enterprisePlusTier, "ENTERPRISE_PLUS", false),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "false"),
),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
{
Config: testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(enterpriseInstanceName, enterpriseTier, "ENTERPRISE", true),
ExpectError: regexp.MustCompile(
fmt.Sprintf("Error, failed to create instance %s: googleapi: Error 400: Invalid request: Only ENTERPRISE PLUS edition supports data cache", enterpriseInstanceName)),
},
Expand Down Expand Up @@ -1826,7 +1844,7 @@ func TestAccSqlDatabaseInstance_Mysql_Edition_Upgrade(t *testing.T) {
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
{
Config: testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(editionUpgrade),
Config: testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(editionUpgrade, true),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE_PLUS"),
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "true"),
Expand Down Expand Up @@ -2901,7 +2919,7 @@ resource "google_sql_database_instance" "instance" {
}`, databaseName, tier)
}

func testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(instanceName string) string {
func testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(instanceName string, datacache bool) string {
return fmt.Sprintf(`
resource "google_sql_database_instance" "instance" {
Expand All @@ -2913,13 +2931,13 @@ resource "google_sql_database_instance" "instance" {
tier = "db-perf-optimized-N-2"
edition = "ENTERPRISE_PLUS"
data_cache_config {
data_cache_enabled = true
data_cache_enabled = "%t"
}
}
}`, instanceName)
}`, instanceName, datacache)
}

func testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(instanceName, tier, edition string) string {
func testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(instanceName, tier, edition string, datacache bool) string {
return fmt.Sprintf(`
resource "google_sql_database_instance" "instance" {
Expand All @@ -2931,10 +2949,10 @@ resource "google_sql_database_instance" "instance" {
tier = "%s"
edition = "%s"
data_cache_config {
data_cache_enabled = true
data_cache_enabled = "%t"
}
}
}`, instanceName, tier, edition)
}`, instanceName, tier, edition, datacache)
}

func testGoogleSqlDatabaseInstance_SqlServerTimezone(instance, rootPassword, timezone string) string {
Expand Down

0 comments on commit d408dc5

Please sign in to comment.