From b9d7dd78bb4372ac0283598a9aeff4a8cdd3e3b0 Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 10 Dec 2024 13:12:55 -0800 Subject: [PATCH] Fix settings.data_cache_config permadiff when set to false (#12496) (#20656) [upstream:2936c3ba867221c9bfcdeb2a82b5542b0da91897] Signed-off-by: Modular Magician --- .changelog/12496.txt | 3 ++ .../sql/resource_sql_database_instance.go | 12 +++++- .../resource_sql_database_instance_test.go | 40 ++++++++++++++----- 3 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 .changelog/12496.txt diff --git a/.changelog/12496.txt b/.changelog/12496.txt new file mode 100644 index 00000000000..bc40a53e0bb --- /dev/null +++ b/.changelog/12496.txt @@ -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 +``` \ No newline at end of file diff --git a/google/services/sql/resource_sql_database_instance.go b/google/services/sql/resource_sql_database_instance.go index 08af179842d..b24ffd97816 100644 --- a/google/services/sql/resource_sql_database_instance.go +++ b/google/services/sql/resource_sql_database_instance.go @@ -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 { @@ -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{}{ { diff --git a/google/services/sql/resource_sql_database_instance_test.go b/google/services/sql/resource_sql_database_instance_test.go index f48257700b7..ec50b7cad2f 100644 --- a/google/services/sql/resource_sql_database_instance_test.go +++ b/google/services/sql/resource_sql_database_instance_test.go @@ -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"), ), }, { @@ -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"), + ), + }, }, }) } @@ -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"), ), @@ -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)), }, @@ -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"), @@ -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" { @@ -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" { @@ -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 {