Skip to content

Commit a024ab7

Browse files
Add auto_upgrade_enabled setting to google_sql_database_instance. (#15038) (#11604)
[upstream:db5a6dda37a5d194b1afd5e2aa607f1f4ec55ec8] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 86102e6 commit a024ab7

File tree

5 files changed

+120
-2
lines changed

5 files changed

+120
-2
lines changed

.changelog/15038.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
sql: added `auto_upgrade_enabled` field to `google_sql_database_instance` resource.
3+
```

google-beta/services/sql/resource_sql_database_instance.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
245245
privateNetworkCustomizeDiff,
246246
pitrSupportDbCustomizeDiff,
247247
nodeCountCustomDiff,
248+
autoUpgradeEnabledCustomizeDiff,
248249
),
249250

250251
Schema: map[string]*schema.Schema{
@@ -530,6 +531,11 @@ API (for read pools, effective_availability_type may differ from availability_ty
530531
Optional: true,
531532
Description: `Enables Vertex AI Integration.`,
532533
},
534+
"auto_upgrade_enabled": {
535+
Type: schema.TypeBool,
536+
Optional: true,
537+
Description: `Enables Automatic Version Upgrade feature. Can be used with MySQL only.`,
538+
},
533539
"enable_dataplex_integration": {
534540
Type: schema.TypeBool,
535541
Optional: true,
@@ -1462,6 +1468,24 @@ func privateNetworkCustomizeDiff(_ context.Context, d *schema.ResourceDiff, meta
14621468
return nil
14631469
}
14641470

1471+
// Ensures auto_upgrade_enabled is never unset (in-place) if already set to true.
1472+
func autoUpgradeEnabledCustomizeDiff(_ context.Context, d *schema.ResourceDiff, _ interface{}) error {
1473+
if !d.HasChange("settings.0.auto_upgrade_enabled") {
1474+
return nil
1475+
}
1476+
oldValueI, newValueI := d.GetChange("settings.0.auto_upgrade_enabled")
1477+
if oldValueI == nil && !(newValueI.(bool)) {
1478+
if err := d.SetNew("settings.0.auto_upgrade_enabled", nil); err != nil {
1479+
return err
1480+
}
1481+
} else if oldValueI != nil && oldValueI.(bool) && (newValueI == nil || !(newValueI.(bool))) {
1482+
if err := d.ForceNew("settings.0.auto_upgrade_enabled"); err != nil {
1483+
return fmt.Errorf("The setting auto_upgrade_enabled cannot be set to false after being set true: %s", err)
1484+
}
1485+
}
1486+
return nil
1487+
}
1488+
14651489
// helper function to see if string within list contains a particular substring
14661490
func stringContainsSlice(arr []string, str string) bool {
14671491
for _, i := range arr {
@@ -1537,6 +1561,12 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
15371561
cloneContext, cloneSource := expandCloneContext(d.Get("clone").([]interface{}))
15381562
pointInTimeRestoreContext := expandPointInTimeRestoreContext(d.Get("point_in_time_restore_context").([]interface{}))
15391563

1564+
if valueI, ok := d.GetOk("settings.0.auto_upgrade_enabled"); ok && !(valueI.(bool)) {
1565+
if err := d.Set("settings.0.auto_upgrade_enabled", nil); err != nil {
1566+
return err
1567+
}
1568+
}
1569+
15401570
s, ok := d.GetOk("settings")
15411571
desiredSettings := expandSqlDatabaseInstanceSettings(s.([]interface{}), databaseVersion)
15421572
if ok {
@@ -1780,6 +1810,10 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}, databaseVersion
17801810
settings.StorageAutoResize = &resize
17811811
settings.StorageAutoResizeLimit = int64(_settings["disk_autoresize_limit"].(int))
17821812

1813+
if _settings["auto_upgrade_enabled"] != nil {
1814+
settings.AutoUpgradeEnabled = _settings["auto_upgrade_enabled"].(bool)
1815+
}
1816+
17831817
return settings
17841818
}
17851819

@@ -2600,8 +2634,11 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
26002634
instance.PscServiceAttachmentLink = d.Get("psc_service_attachment_link").(string)
26012635
}
26022636

2603-
// Database Version is required for all calls with Google ML integration enabled or it will be rejected by the API.
2604-
if d.Get("settings.0.enable_google_ml_integration").(bool) || len(_settings["connection_pool_config"].(*schema.Set).List()) > 0 {
2637+
// Database Version is required for all calls with Google ML Integration, Auto Upgrade,
2638+
// and Connection Pool enabled, or it will be rejected by the API.
2639+
if d.Get("settings.0.enable_google_ml_integration").(bool) ||
2640+
d.Get("settings.0.auto_upgrade_enabled").(bool) ||
2641+
len(_settings["connection_pool_config"].(*schema.Set).List()) > 0 {
26052642
instance.DatabaseVersion = databaseVersion
26062643
}
26072644

@@ -2900,6 +2937,10 @@ func flattenSettings(settings *sqladmin.Settings, iType string, d *schema.Resour
29002937
data["enable_google_ml_integration"] = settings.EnableGoogleMlIntegration
29012938
data["enable_dataplex_integration"] = settings.EnableDataplexIntegration
29022939

2940+
if settings.AutoUpgradeEnabled {
2941+
data["auto_upgrade_enabled"] = settings.AutoUpgradeEnabled
2942+
}
2943+
29032944
if settings.UserLabels != nil {
29042945
data["user_labels"] = settings.UserLabels
29052946
}

google-beta/services/sql/resource_sql_database_instance_meta.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ fields:
9393
- api_field: 'settings.activationPolicy'
9494
- api_field: 'settings.activeDirectoryConfig.domain'
9595
- api_field: 'settings.advancedMachineFeatures.threadsPerCore'
96+
- api_field: 'settings.autoUpgradeEnabled'
9697
- api_field: 'settings.availabilityType'
9798
- api_field: 'settings.availabilityType'
9899
field: 'settings.effective_availability_type'

google-beta/services/sql/resource_sql_database_instance_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,56 @@ func TestAccSqlDatabaseInstance_EnableGoogleMlIntegration(t *testing.T) {
20922092
})
20932093
}
20942094

2095+
func TestAccSqlDatabaseInstance_AutoUpgradeEnabled(t *testing.T) {
2096+
t.Parallel()
2097+
2098+
masterID := acctest.RandInt(t)
2099+
2100+
acctest.VcrTest(t, resource.TestCase{
2101+
PreCheck: func() { acctest.AccTestPreCheck(t) },
2102+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
2103+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
2104+
Steps: []resource.TestStep{
2105+
{
2106+
Config: testGoogleSqlDatabaseInstance_AutoUpgradeEnabled(masterID, false, "MYSQL_8_0_31", "db-custom-2-13312"),
2107+
},
2108+
{
2109+
ResourceName: "google_sql_database_instance.instance",
2110+
ImportState: true,
2111+
ImportStateVerify: true,
2112+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
2113+
},
2114+
{
2115+
Config: testGoogleSqlDatabaseInstance_AutoUpgradeEnabled(masterID, false, "MYSQL_8_0_36", "db-custom-2-13312"),
2116+
},
2117+
{
2118+
ResourceName: "google_sql_database_instance.instance",
2119+
ImportState: true,
2120+
ImportStateVerify: true,
2121+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
2122+
},
2123+
{
2124+
Config: testGoogleSqlDatabaseInstance_AutoUpgradeEnabled(masterID, true, "MYSQL_8_0", "db-custom-2-13312"),
2125+
},
2126+
{
2127+
ResourceName: "google_sql_database_instance.instance",
2128+
ImportState: true,
2129+
ImportStateVerify: true,
2130+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
2131+
},
2132+
{
2133+
Config: testGoogleSqlDatabaseInstance_AutoUpgradeEnabled(masterID, true, "MYSQL_8_0", "db-custom-2-10240"),
2134+
},
2135+
{
2136+
ResourceName: "google_sql_database_instance.instance",
2137+
ImportState: true,
2138+
ImportStateVerify: true,
2139+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
2140+
},
2141+
},
2142+
})
2143+
}
2144+
20952145
func TestAccSqlDatabaseInstance_EnableGoogleDataplexIntegration(t *testing.T) {
20962146
t.Parallel()
20972147

@@ -7757,6 +7807,22 @@ resource "google_sql_database_instance" "instance" {
77577807
`, masterID, dbVersion, masterID, tier, enableGoogleMlIntegration)
77587808
}
77597809

7810+
func testGoogleSqlDatabaseInstance_AutoUpgradeEnabled(masterID int, autoUpgradeEnabled bool, dbVersion string, tier string) string {
7811+
return fmt.Sprintf(`
7812+
resource "google_sql_database_instance" "instance" {
7813+
name = "tf-test-%d"
7814+
region = "us-central1"
7815+
database_version = "%s"
7816+
deletion_protection = false
7817+
root_password = "rand-pwd-%d"
7818+
settings {
7819+
tier = "%s"
7820+
auto_upgrade_enabled = %t
7821+
}
7822+
}
7823+
`, masterID, dbVersion, masterID, tier, autoUpgradeEnabled)
7824+
}
7825+
77607826
func testGoogleSqlDatabaseInstance_EnableDataplexIntegration(masterID int, enableDataplexIntegration bool) string {
77617827
return fmt.Sprintf(`
77627828
resource "google_sql_database_instance" "instance" {

website/docs/r/sql_database_instance.html.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,13 @@ The `settings` block supports:
421421

422422
* `user_labels` - (Optional) A set of key/value user label pairs to assign to the instance.
423423

424+
* `auto_upgrade_enabled` - (Optional) Enables
425+
[Automatic Version Upgrade](https://cloud.google.com/sql/docs/mysql/upgrade-minor-db-version#auto-upgrade)
426+
feature. When this field is set to `true`, Automatic Upgrade is enabled for
427+
`MYSQL_8_0` based minor versions. The `database_version` must be
428+
`MYSQL_8_0_35` or higher. Can be used with MySQL only. Can't be unset or
429+
changed if set to `true`.
430+
424431
* `activation_policy` - (Optional) This specifies when the instance should be
425432
active. Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`.
426433

0 commit comments

Comments
 (0)