Skip to content

Commit

Permalink
Fix updates to activation policy, add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
trodge committed Feb 1, 2023
1 parent 4498a00 commit 809a4e0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1501,11 +1501,33 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
desiredSetting := d.Get("settings")
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
Expand All @@ -1523,7 +1545,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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,32 @@ 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"),
},
{
Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_5_7", "NEVER"),
},
{
Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_8_0_18", "ALWAYS"),
},
{
Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_8_0_26", "NEVER"),
},
},
})
}



func testAccSqlDatabaseInstance_sqlMysqlInstancePvpExample(context map[string]interface{}) string {
Expand Down Expand Up @@ -2956,4 +2982,19 @@ resource "google_sql_database_instance" "main" {
tier = "db-custom-2-13312"
}
}`, instance, databaseVersion, rootPassword)
}
}

func testGoogleSqlDatabaseInstance_activationPolicy(instance, databaseVersion, activationPolicy string) string {
return fmt.Sprintf(`
resource "google_sql_database_instance" "instance" {
name = "%s"
region = "us-central1"
database_version = "%s"
deletion_protection = false
settings {
tier = "db-f1-micro"
activation_policy = "%s"
}
}
`, instance, databaseVersion, activationPolicy)
}

0 comments on commit 809a4e0

Please sign in to comment.