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

Fix settings.data_cache_config permadiff when set to false #20656

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
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/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/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
Loading