diff --git a/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb b/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb index 39df21521ae3..6966214fb5a8 100644 --- a/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb +++ b/mmv1/third_party/terraform/resources/resource_sql_database_instance.go.erb @@ -1527,10 +1527,32 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{}) var op *sqladmin.Operation var instance *sqladmin.DatabaseInstance + desiredDatabaseVersion := d.Get("database_version") + + // Check if the activation policy is being updated. If it is being changed to ALWAYS this should be done first. + if d.HasChange("settings.0.activation_policy") && d.Get("settings.0.activation_policy").(string) == "ALWAYS" { + instance = &sqladmin.DatabaseInstance{Settings: &sqladmin.Settings{ActivationPolicy: "ALWAYS"}} + err = retryTimeDuration(func() (rerr error) { + op, rerr = config.NewSqlAdminClient(userAgent).Instances.Patch(project, d.Get("name").(string), instance).Do() + return rerr + }, d.Timeout(schema.TimeoutUpdate), isSqlOperationInProgressError) + if err != nil { + return fmt.Errorf("Error, failed to patch instance settings for %s: %s", instance.Name, err) + } + err = sqlAdminOperationWaitTime(config, op, project, "Patch Instance", userAgent, d.Timeout(schema.TimeoutUpdate)) + if err != nil { + return err + } + err = resourceSqlDatabaseInstanceRead(d, meta) + if err != nil { + return err + } + } + // Check if the database version is being updated, because patching database version is an atomic operation and can not be // performed with other fields, we first patch database version before updating the rest of the fields. - if v, ok := d.GetOk("database_version"); ok { - instance = &sqladmin.DatabaseInstance{DatabaseVersion: v.(string)} + if d.HasChange("database_version") { + instance = &sqladmin.DatabaseInstance{DatabaseVersion: desiredDatabaseVersion.(string)} err = retryTimeDuration(func() (rerr error) { op, rerr = config.NewSqlAdminClient(userAgent).Instances.Patch(project, d.Get("name").(string), instance).Do() return rerr @@ -1548,7 +1570,7 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{}) } } - // Check if the root_password is being updated, because updating root_password is an atomic operation and can not be + // Check if the root_password is being updated, because updating root_password is an atomic operation and can not be // performed with other fields, we first update root password before updating the rest of the fields. if d.HasChange("root_password") { oldPwd, newPwd := d.GetChange("root_password") diff --git a/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb b/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb index f705bc118249..a7f4d1d86b77 100644 --- a/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb @@ -1399,40 +1399,6 @@ func TestAccSqlDatabaseInstance_Timezone(t *testing.T) { } -func TestAccSqlDatabaseInstance_mysqlMajorVersionUpgrade(t *testing.T) { - t.Parallel() - - databaseName := "tf-test-" + randString(t, 10) - - vcrTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: fmt.Sprintf( - testGoogleSqlDatabaseInstance_basic3, databaseName), - }, - { - ResourceName: "google_sql_database_instance.instance", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"}, - }, - { - Config: fmt.Sprintf( - testGoogleSqlDatabaseInstance_basic3_update, databaseName), - }, - { - ResourceName: "google_sql_database_instance.instance", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"}, - }, - }, - }) -} - func TestAccSqlDatabaseInstance_sqlMysqlInstancePvpExample(t *testing.T) { t.Parallel() @@ -1530,6 +1496,65 @@ func TestAccSqlDatabaseInstance_rootPasswordShouldBeUpdatable(t *testing.T) { }) } +func TestAccSqlDatabaseInstance_activationPolicy(t *testing.T) { + t.Parallel() + + instanceName := "tf-test-" + randString(t, 10) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_5_7", "ALWAYS", true), + }, + { + ResourceName: "google_sql_database_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"}, + }, + { + Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_5_7", "NEVER", true), + }, + { + ResourceName: "google_sql_database_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"}, + }, + { + Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_8_0_18", "ALWAYS", true), + }, + { + ResourceName: "google_sql_database_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"}, + }, + { + Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_8_0_26", "NEVER", true), + }, + { + ResourceName: "google_sql_database_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"}, + }, + { + Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_8_0_26", "ALWAYS", false), + }, + { + ResourceName: "google_sql_database_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"}, + }, + }, + }) +} + func TestAccSqlDatabaseInstance_ReplicaPromoteSuccessful(t *testing.T) { t.Parallel() @@ -3433,3 +3458,18 @@ resource "google_sql_database_instance" "main" { } }`, instance, databaseVersion, rootPassword) } + +func testGoogleSqlDatabaseInstance_activationPolicy(instance, databaseVersion, activationPolicy string, deletionProtection bool) string { + return fmt.Sprintf(` +resource "google_sql_database_instance" "instance" { + name = "%s" + region = "us-central1" + database_version = "%s" + deletion_protection = %t + settings { + tier = "db-f1-micro" + activation_policy = "%s" + } +} +`, instance, databaseVersion, deletionProtection, activationPolicy) +}