Skip to content

Commit

Permalink
Fix updates to activation policy, add a test (#7200) (#13736)
Browse files Browse the repository at this point in the history
* Fix updates to activation policy, add a test

* Add import steps and deletion policy to test

* Removed mysql major version upgrade test

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Feb 14, 2023
1 parent de40849 commit 8bb3fcb
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .changelog/7200.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
sql: fixed an issue with updating the `google_sql_database_instance.settings.activation_policy` field
```
26 changes: 24 additions & 2 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1525,10 +1525,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
Expand Down
108 changes: 74 additions & 34 deletions google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1395,40 +1395,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()

Expand Down Expand Up @@ -1526,6 +1492,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()

Expand Down Expand Up @@ -3428,3 +3453,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)
}

0 comments on commit 8bb3fcb

Please sign in to comment.