Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#1803 from jingyih/patch_sqlins…
Browse files Browse the repository at this point in the history
…tance_edition

terraform: fix perm diff on edition field in SQL instance
  • Loading branch information
google-oss-prow[bot] authored May 15, 2024
2 parents c181cae + 2cdd726 commit a65d79b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"log"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -161,6 +162,7 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
"edition": {
Type: schema.TypeString,
Optional: true,
Default: "ENTERPRISE",
ValidateFunc: validation.StringInSlice([]string{"ENTERPRISE", "ENTERPRISE_PLUS"}, false),
Description: `The edition of the instance, can be ENTERPRISE or ENTERPRISE_PLUS.`,
},
Expand Down Expand Up @@ -1994,7 +1996,7 @@ func flattenSettings(settings *sqladmin.Settings) []map[string]interface{} {
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,
Expand Down Expand Up @@ -2322,6 +2324,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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1500,22 +1500,43 @@ func TestAccSqlDatabaseInstance_Edition(t *testing.T) {
enterprisePlusTier := "db-perf-optimized-N-2"
enterpriseName := "tf-test-enterprise-" + acctest.RandString(t, 10)
enterpriseTier := "db-custom-2-13312"
noEditionName := "tf-test-enterprise-noedition-" + acctest.RandString(t, 10)
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlDatabaseInstance_EditionConfig_noEdition(noEditionName, enterpriseTier),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"),
),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
// Delete and recreate (ForceNew) triggered by passing in a new `name` value
{
Config: testGoogleSqlDatabaseInstance_EditionConfig(enterprisePlusName, enterprisePlusTier, "ENTERPRISE_PLUS"),
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"},
},
// Delete and recreate (ForceNew) triggered by passing in a new `name` value
{
Config: testGoogleSqlDatabaseInstance_EditionConfig(enterpriseName, enterpriseTier, "ENTERPRISE"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"),
),
},
{
ResourceName: "google_sql_database_instance.instance",
Expand Down Expand Up @@ -2208,6 +2229,19 @@ resource "google_sql_database_instance" "instance" {
}`, databaseName, endDate, startDate, time)
}

func testGoogleSqlDatabaseInstance_EditionConfig_noEdition(databaseName, tier string) string {
return fmt.Sprintf(`
resource "google_sql_database_instance" "instance" {
name = "%s"
region = "us-east1"
database_version = "POSTGRES_14"
deletion_protection = false
settings {
tier = "%s"
}
}`, databaseName, tier)
}

func testGoogleSqlDatabaseInstance_EditionConfig(databaseName, tier, edition string) string {
return fmt.Sprintf(`
Expand Down

0 comments on commit a65d79b

Please sign in to comment.