Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/15038.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
sql: added `auto_upgrade_enabled` field to `google_sql_database_instance` resource.
```
45 changes: 43 additions & 2 deletions google-beta/services/sql/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
privateNetworkCustomizeDiff,
pitrSupportDbCustomizeDiff,
nodeCountCustomDiff,
autoUpgradeEnabledCustomizeDiff,
),

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -530,6 +531,11 @@ API (for read pools, effective_availability_type may differ from availability_ty
Optional: true,
Description: `Enables Vertex AI Integration.`,
},
"auto_upgrade_enabled": {
Type: schema.TypeBool,
Optional: true,
Description: `Enables Automatic Version Upgrade feature. Can be used with MySQL only.`,
},
"enable_dataplex_integration": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -1462,6 +1468,24 @@ func privateNetworkCustomizeDiff(_ context.Context, d *schema.ResourceDiff, meta
return nil
}

// Ensures auto_upgrade_enabled is never unset (in-place) if already set to true.
func autoUpgradeEnabledCustomizeDiff(_ context.Context, d *schema.ResourceDiff, _ interface{}) error {
if !d.HasChange("settings.0.auto_upgrade_enabled") {
return nil
}
oldValueI, newValueI := d.GetChange("settings.0.auto_upgrade_enabled")
if oldValueI == nil && !(newValueI.(bool)) {
if err := d.SetNew("settings.0.auto_upgrade_enabled", nil); err != nil {
return err
}
} else if oldValueI != nil && oldValueI.(bool) && (newValueI == nil || !(newValueI.(bool))) {
if err := d.ForceNew("settings.0.auto_upgrade_enabled"); err != nil {
return fmt.Errorf("The setting auto_upgrade_enabled cannot be set to false after being set true: %s", err)
}
}
return nil
}

// helper function to see if string within list contains a particular substring
func stringContainsSlice(arr []string, str string) bool {
for _, i := range arr {
Expand Down Expand Up @@ -1537,6 +1561,12 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
cloneContext, cloneSource := expandCloneContext(d.Get("clone").([]interface{}))
pointInTimeRestoreContext := expandPointInTimeRestoreContext(d.Get("point_in_time_restore_context").([]interface{}))

if valueI, ok := d.GetOk("settings.0.auto_upgrade_enabled"); ok && !(valueI.(bool)) {
if err := d.Set("settings.0.auto_upgrade_enabled", nil); err != nil {
return err
}
}

s, ok := d.GetOk("settings")
desiredSettings := expandSqlDatabaseInstanceSettings(s.([]interface{}), databaseVersion)
if ok {
Expand Down Expand Up @@ -1780,6 +1810,10 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}, databaseVersion
settings.StorageAutoResize = &resize
settings.StorageAutoResizeLimit = int64(_settings["disk_autoresize_limit"].(int))

if _settings["auto_upgrade_enabled"] != nil {
settings.AutoUpgradeEnabled = _settings["auto_upgrade_enabled"].(bool)
}

return settings
}

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

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

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

if settings.AutoUpgradeEnabled {
data["auto_upgrade_enabled"] = settings.AutoUpgradeEnabled
}

if settings.UserLabels != nil {
data["user_labels"] = settings.UserLabels
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ fields:
- api_field: 'settings.activationPolicy'
- api_field: 'settings.activeDirectoryConfig.domain'
- api_field: 'settings.advancedMachineFeatures.threadsPerCore'
- api_field: 'settings.autoUpgradeEnabled'
- api_field: 'settings.availabilityType'
- api_field: 'settings.availabilityType'
field: 'settings.effective_availability_type'
Expand Down
66 changes: 66 additions & 0 deletions google-beta/services/sql/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,56 @@ func TestAccSqlDatabaseInstance_EnableGoogleMlIntegration(t *testing.T) {
})
}

func TestAccSqlDatabaseInstance_AutoUpgradeEnabled(t *testing.T) {
t.Parallel()

masterID := acctest.RandInt(t)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlDatabaseInstance_AutoUpgradeEnabled(masterID, false, "MYSQL_8_0_31", "db-custom-2-13312"),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
},
{
Config: testGoogleSqlDatabaseInstance_AutoUpgradeEnabled(masterID, false, "MYSQL_8_0_36", "db-custom-2-13312"),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
},
{
Config: testGoogleSqlDatabaseInstance_AutoUpgradeEnabled(masterID, true, "MYSQL_8_0", "db-custom-2-13312"),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
},
{
Config: testGoogleSqlDatabaseInstance_AutoUpgradeEnabled(masterID, true, "MYSQL_8_0", "db-custom-2-10240"),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
},
},
})
}

func TestAccSqlDatabaseInstance_EnableGoogleDataplexIntegration(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -7757,6 +7807,22 @@ resource "google_sql_database_instance" "instance" {
`, masterID, dbVersion, masterID, tier, enableGoogleMlIntegration)
}

func testGoogleSqlDatabaseInstance_AutoUpgradeEnabled(masterID int, autoUpgradeEnabled bool, dbVersion string, tier string) string {
return fmt.Sprintf(`
resource "google_sql_database_instance" "instance" {
name = "tf-test-%d"
region = "us-central1"
database_version = "%s"
deletion_protection = false
root_password = "rand-pwd-%d"
settings {
tier = "%s"
auto_upgrade_enabled = %t
}
}
`, masterID, dbVersion, masterID, tier, autoUpgradeEnabled)
}

func testGoogleSqlDatabaseInstance_EnableDataplexIntegration(masterID int, enableDataplexIntegration bool) string {
return fmt.Sprintf(`
resource "google_sql_database_instance" "instance" {
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/sql_database_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,13 @@ The `settings` block supports:

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

* `auto_upgrade_enabled` - (Optional) Enables
[Automatic Version Upgrade](https://cloud.google.com/sql/docs/mysql/upgrade-minor-db-version#auto-upgrade)
feature. When this field is set to `true`, Automatic Upgrade is enabled for
`MYSQL_8_0` based minor versions. The `database_version` must be
`MYSQL_8_0_35` or higher. Can be used with MySQL only. Can't be unset or
changed if set to `true`.

* `activation_policy` - (Optional) This specifies when the instance should be
active. Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`.

Expand Down
Loading