From 2e9dd6921fc3296ff723e9c3d283c12c91359d2b Mon Sep 17 00:00:00 2001 From: Sarthak Tandon Date: Tue, 22 Oct 2024 00:24:45 +0000 Subject: [PATCH 1/2] Allow ENTERPRISE_PLUS as default Edition for POSTGRES_16 --- .../resource_sql_database_instance.go.tmpl | 13 +---- .../resource_sql_database_instance_test.go | 53 ++++++++++++++++--- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl b/mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl index 160f138a7519..17cd9baaffb6 100644 --- a/mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl +++ b/mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl @@ -5,7 +5,6 @@ import ( "errors" "fmt" "log" - "reflect" "strings" "time" @@ -177,7 +176,7 @@ func ResourceSqlDatabaseInstance() *schema.Resource { "edition": { Type: schema.TypeString, Optional: true, - Default: "ENTERPRISE", + Computed: true, ValidateFunc: validation.StringInSlice([]string{"ENTERPRISE", "ENTERPRISE_PLUS"}, false), Description: `The edition of the instance, can be ENTERPRISE or ENTERPRISE_PLUS.`, }, @@ -2090,7 +2089,7 @@ func flattenSettings(settings *sqladmin.Settings, d *schema.ResourceData) []map[ data := map[string]interface{}{ "version": settings.SettingsVersion, "tier": settings.Tier, - "edition": flattenEdition(settings.Edition), + "edition": settings.Edition, "activation_policy": settings.ActivationPolicy, "availability_type": settings.AvailabilityType, "collation": settings.Collation, @@ -2422,14 +2421,6 @@ func flattenPasswordValidationPolicy(passwordValidationPolicy *sqladmin.Password return []map[string]interface{}{data} } -func flattenEdition(v interface{}) string { - if v == nil || tpgresource.IsEmptyValue(reflect.ValueOf(v)) { - return "ENTERPRISE" - } - - return v.(string) -} - func instanceMutexKey(project, instance_name string) string { return fmt.Sprintf("google-sql-database-instance-%s-%s", project, instance_name) } diff --git a/mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go b/mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go index 40cd23f6eec8..8365f0252b22 100644 --- a/mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go +++ b/mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go @@ -1596,6 +1596,32 @@ func TestAccSQLDatabaseInstance_DenyMaintenancePeriod(t *testing.T) { }) } +func TestAccSQLDatabaseInstance_DefaultEdition(t *testing.T) { + t.Parallel() + databaseName := "tf-test-" + acctest.RandString(t, 10) + databaseVersion := "POSTGRES_16" + enterprisePlusTier := "db-perf-optimized-N-2" + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testGoogleSqlDatabaseInstance_DefaultEdition(databaseName, databaseVersion, enterprisePlusTier), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE_PLUS"), + ), + }, + { + ResourceName: "google_sql_database_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection"}, + }, + }, + }) +} + func TestAccSqlDatabaseInstance_Edition(t *testing.T) { t.Parallel() enterprisePlusName := "tf-test-enterprise-plus" + acctest.RandString(t, 10) @@ -1611,7 +1637,7 @@ func TestAccSqlDatabaseInstance_Edition(t *testing.T) { { Config: testGoogleSqlDatabaseInstance_EditionConfig_noEdition(noEditionName, enterpriseTier), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"), + resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", ""), ), }, { @@ -1718,7 +1744,7 @@ func TestAccSqlDatabaseInstance_Mysql_Edition_Upgrade(t *testing.T) { { Config: testGoogleSqlDatabaseInstance_sqlMysql(editionUpgrade, enterpriseTier), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"), + resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", ""), ), }, { @@ -1755,7 +1781,7 @@ func TestAccSqlDatabaseInstance_Postgres_Edition_Upgrade(t *testing.T) { CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t), Steps: []resource.TestStep{ { - Config: testGoogleSqlDatabaseInstance_EditionConfig_noEdition(editionUpgrade, enterpriseTier), + Config: testGoogleSqlDatabaseInstance_EditionConfig(editionUpgrade, enterpriseTier, "ENTERPRISE"), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"), ), @@ -1805,7 +1831,7 @@ func TestAccSqlDatabaseInstance_Edition_Downgrade(t *testing.T) { ImportStateVerifyIgnore: []string{"deletion_protection"}, }, { - Config: testGoogleSqlDatabaseInstance_EditionConfig_noEdition(editionDowngrade, enterpriseTier), + Config: testGoogleSqlDatabaseInstance_EditionConfig(editionDowngrade, enterpriseTier, "ENTERPRISE"), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"), ), @@ -2681,6 +2707,19 @@ resource "google_sql_database_instance" "instance" { }`, databaseName, endDate, startDate, time) } +func testGoogleSqlDatabaseInstance_DefaultEdition(databaseName, databaseVersion, tier string) string { + return fmt.Sprintf(` +resource "google_sql_database_instance" "instance" { + name = "%s" + region = "us-east1" + database_version = "%s" + deletion_protection = false + settings { + tier = "%s" + } +}`, databaseName, databaseVersion, tier) +} + func testGoogleSqlDatabaseInstance_EditionConfig_noEdition(databaseName, tier string) string { return fmt.Sprintf(` @@ -2691,9 +2730,6 @@ resource "google_sql_database_instance" "instance" { deletion_protection = false settings { tier = "%s" - backup_configuration { - transaction_log_retention_days = 7 - } } }`, databaseName, tier) } @@ -2709,6 +2745,9 @@ resource "google_sql_database_instance" "instance" { settings { tier = "%s" edition = "%s" + backup_configuration { + transaction_log_retention_days = 7 + } } }`, databaseName, tier, edition) } From 8ccfbb0542a833831e194ebf31cd67d854882e72 Mon Sep 17 00:00:00 2001 From: Sarthak Tandon Date: Wed, 23 Oct 2024 00:10:29 +0000 Subject: [PATCH 2/2] Preserve flattening logic for settings.Edition --- .../sql/resource_sql_database_instance.go.tmpl | 11 ++++++++++- .../sql/resource_sql_database_instance_test.go | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl b/mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl index dc054a799f3a..08da2c84defc 100644 --- a/mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl +++ b/mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log" + "reflect" "strings" "time" @@ -2090,7 +2091,7 @@ func flattenSettings(settings *sqladmin.Settings, d *schema.ResourceData) []map[ data := map[string]interface{}{ "version": settings.SettingsVersion, "tier": settings.Tier, - "edition": settings.Edition, + "edition": flattenEdition(settings.Edition), "activation_policy": settings.ActivationPolicy, "availability_type": settings.AvailabilityType, "collation": settings.Collation, @@ -2422,6 +2423,14 @@ func flattenPasswordValidationPolicy(passwordValidationPolicy *sqladmin.Password return []map[string]interface{}{data} } +func flattenEdition(v interface{}) string { + if v == nil || tpgresource.IsEmptyValue(reflect.ValueOf(v)) { + return "ENTERPRISE" + } + + return v.(string) +} + func instanceMutexKey(project, instance_name string) string { return fmt.Sprintf("google-sql-database-instance-%s-%s", project, instance_name) } diff --git a/mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go b/mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go index 8365f0252b22..d2a75cd1767f 100644 --- a/mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go +++ b/mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go @@ -1637,7 +1637,7 @@ func TestAccSqlDatabaseInstance_Edition(t *testing.T) { { Config: testGoogleSqlDatabaseInstance_EditionConfig_noEdition(noEditionName, enterpriseTier), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", ""), + resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"), ), }, { @@ -1744,7 +1744,7 @@ func TestAccSqlDatabaseInstance_Mysql_Edition_Upgrade(t *testing.T) { { Config: testGoogleSqlDatabaseInstance_sqlMysql(editionUpgrade, enterpriseTier), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", ""), + resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"), ), }, {