Skip to content

Commit b9d7dd7

Browse files
Fix settings.data_cache_config permadiff when set to false (#12496) (#20656)
[upstream:2936c3ba867221c9bfcdeb2a82b5542b0da91897] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 6b67946 commit b9d7dd7

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

.changelog/12496.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
sql: fixed permadiff when 'settings.data_cache_config' is set to false for 'google_sql_database_instance' resource
3+
```

google/services/sql/resource_sql_database_instance.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,12 @@ func flattenSettings(settings *sqladmin.Settings, d *schema.ResourceData) []map[
22492249

22502250
if settings.DataCacheConfig != nil {
22512251
data["data_cache_config"] = flattenDataCacheConfig(settings.DataCacheConfig)
2252+
} else {
2253+
data["data_cache_config"] = []map[string]interface{}{
2254+
{
2255+
"data_cache_enabled": false,
2256+
},
2257+
}
22522258
}
22532259

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

22612267
func flattenDataCacheConfig(d *sqladmin.DataCacheConfig) []map[string]interface{} {
22622268
if d == nil {
2263-
return nil
2269+
return []map[string]interface{}{
2270+
{
2271+
"data_cache_enabled": false,
2272+
},
2273+
}
22642274
}
22652275
return []map[string]interface{}{
22662276
{

google/services/sql/resource_sql_database_instance_test.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,9 +1757,9 @@ func TestAccSQLDatabaseInstance_sqlMysqlDataCacheConfig(t *testing.T) {
17571757
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
17581758
Steps: []resource.TestStep{
17591759
{
1760-
Config: testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(instanceName),
1760+
Config: testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(instanceName, false),
17611761
Check: resource.ComposeAggregateTestCheckFunc(
1762-
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "true"),
1762+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "false"),
17631763
),
17641764
},
17651765
{
@@ -1768,6 +1768,12 @@ func TestAccSQLDatabaseInstance_sqlMysqlDataCacheConfig(t *testing.T) {
17681768
ImportStateVerify: true,
17691769
ImportStateVerifyIgnore: []string{"deletion_protection"},
17701770
},
1771+
{
1772+
Config: testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(instanceName, true),
1773+
Check: resource.ComposeAggregateTestCheckFunc(
1774+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "true"),
1775+
),
1776+
},
17711777
},
17721778
})
17731779
}
@@ -1784,7 +1790,7 @@ func TestAccSQLDatabaseInstance_sqlPostgresDataCacheConfig(t *testing.T) {
17841790
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
17851791
Steps: []resource.TestStep{
17861792
{
1787-
Config: testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(enterprisePlusInstanceName, enterprisePlusTier, "ENTERPRISE_PLUS"),
1793+
Config: testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(enterprisePlusInstanceName, enterprisePlusTier, "ENTERPRISE_PLUS", true),
17881794
Check: resource.ComposeAggregateTestCheckFunc(
17891795
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "true"),
17901796
),
@@ -1796,7 +1802,19 @@ func TestAccSQLDatabaseInstance_sqlPostgresDataCacheConfig(t *testing.T) {
17961802
ImportStateVerifyIgnore: []string{"deletion_protection"},
17971803
},
17981804
{
1799-
Config: testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(enterpriseInstanceName, enterpriseTier, "ENTERPRISE"),
1805+
Config: testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(enterprisePlusInstanceName, enterprisePlusTier, "ENTERPRISE_PLUS", false),
1806+
Check: resource.ComposeAggregateTestCheckFunc(
1807+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.data_cache_config.0.data_cache_enabled", "false"),
1808+
),
1809+
},
1810+
{
1811+
ResourceName: "google_sql_database_instance.instance",
1812+
ImportState: true,
1813+
ImportStateVerify: true,
1814+
ImportStateVerifyIgnore: []string{"deletion_protection"},
1815+
},
1816+
{
1817+
Config: testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(enterpriseInstanceName, enterpriseTier, "ENTERPRISE", true),
18001818
ExpectError: regexp.MustCompile(
18011819
fmt.Sprintf("Error, failed to create instance %s: googleapi: Error 400: Invalid request: Only ENTERPRISE PLUS edition supports data cache", enterpriseInstanceName)),
18021820
},
@@ -1826,7 +1844,7 @@ func TestAccSqlDatabaseInstance_Mysql_Edition_Upgrade(t *testing.T) {
18261844
ImportStateVerifyIgnore: []string{"deletion_protection"},
18271845
},
18281846
{
1829-
Config: testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(editionUpgrade),
1847+
Config: testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(editionUpgrade, true),
18301848
Check: resource.ComposeAggregateTestCheckFunc(
18311849
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE_PLUS"),
18321850
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" {
29012919
}`, databaseName, tier)
29022920
}
29032921

2904-
func testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(instanceName string) string {
2922+
func testGoogleSqlDatabaseInstance_sqlMysqlDataCacheConfig(instanceName string, datacache bool) string {
29052923
return fmt.Sprintf(`
29062924
29072925
resource "google_sql_database_instance" "instance" {
@@ -2913,13 +2931,13 @@ resource "google_sql_database_instance" "instance" {
29132931
tier = "db-perf-optimized-N-2"
29142932
edition = "ENTERPRISE_PLUS"
29152933
data_cache_config {
2916-
data_cache_enabled = true
2934+
data_cache_enabled = "%t"
29172935
}
29182936
}
2919-
}`, instanceName)
2937+
}`, instanceName, datacache)
29202938
}
29212939

2922-
func testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(instanceName, tier, edition string) string {
2940+
func testGoogleSqlDatabaseInstance_sqlPostgresDataCacheConfig(instanceName, tier, edition string, datacache bool) string {
29232941
return fmt.Sprintf(`
29242942
29252943
resource "google_sql_database_instance" "instance" {
@@ -2931,10 +2949,10 @@ resource "google_sql_database_instance" "instance" {
29312949
tier = "%s"
29322950
edition = "%s"
29332951
data_cache_config {
2934-
data_cache_enabled = true
2952+
data_cache_enabled = "%t"
29352953
}
29362954
}
2937-
}`, instanceName, tier, edition)
2955+
}`, instanceName, tier, edition, datacache)
29382956
}
29392957

29402958
func testGoogleSqlDatabaseInstance_SqlServerTimezone(instance, rootPassword, timezone string) string {

0 commit comments

Comments
 (0)