Skip to content

Commit 8bb3fcb

Browse files
Fix updates to activation policy, add a test (#7200) (#13736)
* 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 <[email protected]>
1 parent de40849 commit 8bb3fcb

File tree

3 files changed

+101
-36
lines changed

3 files changed

+101
-36
lines changed

.changelog/7200.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
sql: fixed an issue with updating the `google_sql_database_instance.settings.activation_policy` field
3+
```

google/resource_sql_database_instance.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -1525,10 +1525,32 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
15251525
var op *sqladmin.Operation
15261526
var instance *sqladmin.DatabaseInstance
15271527

1528+
desiredDatabaseVersion := d.Get("database_version")
1529+
1530+
// Check if the activation policy is being updated. If it is being changed to ALWAYS this should be done first.
1531+
if d.HasChange("settings.0.activation_policy") && d.Get("settings.0.activation_policy").(string) == "ALWAYS" {
1532+
instance = &sqladmin.DatabaseInstance{Settings: &sqladmin.Settings{ActivationPolicy: "ALWAYS"}}
1533+
err = retryTimeDuration(func() (rerr error) {
1534+
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Patch(project, d.Get("name").(string), instance).Do()
1535+
return rerr
1536+
}, d.Timeout(schema.TimeoutUpdate), isSqlOperationInProgressError)
1537+
if err != nil {
1538+
return fmt.Errorf("Error, failed to patch instance settings for %s: %s", instance.Name, err)
1539+
}
1540+
err = sqlAdminOperationWaitTime(config, op, project, "Patch Instance", userAgent, d.Timeout(schema.TimeoutUpdate))
1541+
if err != nil {
1542+
return err
1543+
}
1544+
err = resourceSqlDatabaseInstanceRead(d, meta)
1545+
if err != nil {
1546+
return err
1547+
}
1548+
}
1549+
15281550
// Check if the database version is being updated, because patching database version is an atomic operation and can not be
15291551
// performed with other fields, we first patch database version before updating the rest of the fields.
1530-
if v, ok := d.GetOk("database_version"); ok {
1531-
instance = &sqladmin.DatabaseInstance{DatabaseVersion: v.(string)}
1552+
if d.HasChange("database_version") {
1553+
instance = &sqladmin.DatabaseInstance{DatabaseVersion: desiredDatabaseVersion.(string)}
15321554
err = retryTimeDuration(func() (rerr error) {
15331555
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Patch(project, d.Get("name").(string), instance).Do()
15341556
return rerr

google/resource_sql_database_instance_test.go

+74-34
Original file line numberDiff line numberDiff line change
@@ -1395,40 +1395,6 @@ func TestAccSqlDatabaseInstance_Timezone(t *testing.T) {
13951395
})
13961396
}
13971397

1398-
func TestAccSqlDatabaseInstance_mysqlMajorVersionUpgrade(t *testing.T) {
1399-
t.Parallel()
1400-
1401-
databaseName := "tf-test-" + randString(t, 10)
1402-
1403-
vcrTest(t, resource.TestCase{
1404-
PreCheck: func() { testAccPreCheck(t) },
1405-
Providers: testAccProviders,
1406-
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
1407-
Steps: []resource.TestStep{
1408-
{
1409-
Config: fmt.Sprintf(
1410-
testGoogleSqlDatabaseInstance_basic3, databaseName),
1411-
},
1412-
{
1413-
ResourceName: "google_sql_database_instance.instance",
1414-
ImportState: true,
1415-
ImportStateVerify: true,
1416-
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
1417-
},
1418-
{
1419-
Config: fmt.Sprintf(
1420-
testGoogleSqlDatabaseInstance_basic3_update, databaseName),
1421-
},
1422-
{
1423-
ResourceName: "google_sql_database_instance.instance",
1424-
ImportState: true,
1425-
ImportStateVerify: true,
1426-
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
1427-
},
1428-
},
1429-
})
1430-
}
1431-
14321398
func TestAccSqlDatabaseInstance_sqlMysqlInstancePvpExample(t *testing.T) {
14331399
t.Parallel()
14341400

@@ -1526,6 +1492,65 @@ func TestAccSqlDatabaseInstance_rootPasswordShouldBeUpdatable(t *testing.T) {
15261492
})
15271493
}
15281494

1495+
func TestAccSqlDatabaseInstance_activationPolicy(t *testing.T) {
1496+
t.Parallel()
1497+
1498+
instanceName := "tf-test-" + randString(t, 10)
1499+
1500+
vcrTest(t, resource.TestCase{
1501+
PreCheck: func() { testAccPreCheck(t) },
1502+
Providers: testAccProviders,
1503+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
1504+
Steps: []resource.TestStep{
1505+
{
1506+
Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_5_7", "ALWAYS", true),
1507+
},
1508+
{
1509+
ResourceName: "google_sql_database_instance.instance",
1510+
ImportState: true,
1511+
ImportStateVerify: true,
1512+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
1513+
},
1514+
{
1515+
Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_5_7", "NEVER", true),
1516+
},
1517+
{
1518+
ResourceName: "google_sql_database_instance.instance",
1519+
ImportState: true,
1520+
ImportStateVerify: true,
1521+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
1522+
},
1523+
{
1524+
Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_8_0_18", "ALWAYS", true),
1525+
},
1526+
{
1527+
ResourceName: "google_sql_database_instance.instance",
1528+
ImportState: true,
1529+
ImportStateVerify: true,
1530+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
1531+
},
1532+
{
1533+
Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_8_0_26", "NEVER", true),
1534+
},
1535+
{
1536+
ResourceName: "google_sql_database_instance.instance",
1537+
ImportState: true,
1538+
ImportStateVerify: true,
1539+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
1540+
},
1541+
{
1542+
Config: testGoogleSqlDatabaseInstance_activationPolicy(instanceName, "MYSQL_8_0_26", "ALWAYS", false),
1543+
},
1544+
{
1545+
ResourceName: "google_sql_database_instance.instance",
1546+
ImportState: true,
1547+
ImportStateVerify: true,
1548+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
1549+
},
1550+
},
1551+
})
1552+
}
1553+
15291554
func TestAccSqlDatabaseInstance_ReplicaPromoteSuccessful(t *testing.T) {
15301555
t.Parallel()
15311556

@@ -3428,3 +3453,18 @@ resource "google_sql_database_instance" "main" {
34283453
}
34293454
}`, instance, databaseVersion, rootPassword)
34303455
}
3456+
3457+
func testGoogleSqlDatabaseInstance_activationPolicy(instance, databaseVersion, activationPolicy string, deletionProtection bool) string {
3458+
return fmt.Sprintf(`
3459+
resource "google_sql_database_instance" "instance" {
3460+
name = "%s"
3461+
region = "us-central1"
3462+
database_version = "%s"
3463+
deletion_protection = %t
3464+
settings {
3465+
tier = "db-f1-micro"
3466+
activation_policy = "%s"
3467+
}
3468+
}
3469+
`, instance, databaseVersion, deletionProtection, activationPolicy)
3470+
}

0 commit comments

Comments
 (0)