Skip to content

Commit 48173fe

Browse files
add final_backup_config field to google_sql_database_instance (#14891) (#10678)
[upstream:defbc8d07bc1c0e896471cb81036571b1f1c6870] Signed-off-by: Modular Magician <[email protected]>
1 parent b5db8e8 commit 48173fe

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed

.changelog/14891.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: add `final_backup_description` and `final_backup_config` fields to `google_sql_database_instance` resource
3+
```

google-beta/services/sql/resource_sql_database_instance.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
188188
Optional: true,
189189
Description: `Used to block Terraform from deleting a SQL Instance. Defaults to true.`,
190190
},
191+
"final_backup_description": {
192+
Type: schema.TypeString,
193+
Optional: true,
194+
Description: `The description of final backup if instance enable create final backup during instance deletion. `,
195+
},
191196
"settings": {
192197
Type: schema.TypeList,
193198
Optional: true,
@@ -806,6 +811,27 @@ API (for read pools, effective_availability_type may differ from availability_ty
806811
Optional: true,
807812
Description: `When this parameter is set to true, Cloud SQL retains backups of the instance even after the instance is deleted. The ON_DEMAND backup will be retained until customer deletes the backup or the project. The AUTOMATED backup will be retained based on the backups retention setting.`,
808813
},
814+
"final_backup_config": {
815+
Type: schema.TypeList,
816+
Optional: true,
817+
MaxItems: 1,
818+
Elem: &schema.Resource{
819+
Schema: map[string]*schema.Schema{
820+
"enabled": {
821+
Type: schema.TypeBool,
822+
Optional: true,
823+
Description: `When this parameter is set to true, the final backup is enabled for the instance`,
824+
},
825+
"retention_days": {
826+
Type: schema.TypeInt,
827+
Optional: true,
828+
ValidateFunc: validation.IntBetween(1, 36135),
829+
Description: `The number of days to retain the final backup after the instance deletion. The valid range is between 1 and 365. For instances managed by BackupDR, the valid range is between 1 day and 99 years. The final backup will be purged at (time_of_instance_deletion + retention_days).`,
830+
},
831+
},
832+
},
833+
Description: `Config used to determine the final backup settings for the instance`,
834+
},
809835
},
810836
},
811837
Description: `The settings to use for the database. The configuration is detailed below.`,
@@ -1518,6 +1544,7 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}, databaseVersion
15181544
EnableGoogleMlIntegration: _settings["enable_google_ml_integration"].(bool),
15191545
EnableDataplexIntegration: _settings["enable_dataplex_integration"].(bool),
15201546
RetainBackupsOnDelete: _settings["retain_backups_on_delete"].(bool),
1547+
FinalBackupConfig: expandFinalBackupConfig(_settings["final_backup_config"].([]interface{})),
15211548
UserLabels: tpgresource.ConvertStringMap(_settings["user_labels"].(map[string]interface{})),
15221549
BackupConfiguration: expandBackupConfiguration(_settings["backup_configuration"].([]interface{})),
15231550
DatabaseFlags: expandDatabaseFlags(_settings["database_flags"].(*schema.Set).List()),
@@ -1760,6 +1787,19 @@ func expandBackupConfiguration(configured []interface{}) *sqladmin.BackupConfigu
17601787
}
17611788
}
17621789

1790+
func expandFinalBackupConfig(configured []interface{}) *sqladmin.FinalBackupConfig {
1791+
if len(configured) == 0 || configured[0] == nil {
1792+
return nil
1793+
}
1794+
1795+
_finalBackupConfig := configured[0].(map[string]interface{})
1796+
return &sqladmin.FinalBackupConfig{
1797+
Enabled: _finalBackupConfig["enabled"].(bool),
1798+
RetentionDays: int64(_finalBackupConfig["retention_days"].(int)),
1799+
ForceSendFields: []string{"Enabled"},
1800+
}
1801+
}
1802+
17631803
func expandBackupRetentionSettings(configured interface{}) *sqladmin.BackupRetentionSettings {
17641804
l := configured.([]interface{})
17651805
if len(l) == 0 {
@@ -2388,9 +2428,13 @@ func resourceSqlDatabaseInstanceDelete(d *schema.ResourceData, meta interface{})
23882428
}
23892429

23902430
var op *sqladmin.Operation
2431+
finalBackupDescription := ""
2432+
if v, ok := d.GetOk("finalBackupDescription"); ok {
2433+
finalBackupDescription = v.(string)
2434+
}
23912435
err = transport_tpg.Retry(transport_tpg.RetryOptions{
23922436
RetryFunc: func() (rerr error) {
2393-
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Delete(project, d.Get("name").(string)).Do()
2437+
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Delete(project, d.Get("name").(string)).FinalBackupDescription(finalBackupDescription).Do()
23942438
if rerr != nil {
23952439
return rerr
23962440
}
@@ -2452,6 +2496,7 @@ func flattenSettings(settings *sqladmin.Settings, iType string, d *schema.Resour
24522496
"time_zone": settings.TimeZone,
24532497
"deletion_protection_enabled": settings.DeletionProtectionEnabled,
24542498
"retain_backups_on_delete": settings.RetainBackupsOnDelete,
2499+
"final_backup_config": settings.FinalBackupConfig,
24552500
}
24562501

24572502
if data["availability_type"] == "" {
@@ -2482,6 +2527,10 @@ func flattenSettings(settings *sqladmin.Settings, iType string, d *schema.Resour
24822527
data["backup_configuration"] = flattenBackupConfiguration(settings.BackupConfiguration)
24832528
}
24842529

2530+
if settings.FinalBackupConfig != nil {
2531+
data["final_backup_config"] = flattenFinalBackupConfig(settings.FinalBackupConfig)
2532+
}
2533+
24852534
if settings.DatabaseFlags != nil {
24862535
data["database_flags"] = flattenDatabaseFlags(settings.DatabaseFlags)
24872536
}
@@ -2566,6 +2615,15 @@ func flattenBackupConfiguration(backupConfiguration *sqladmin.BackupConfiguratio
25662615
return []map[string]interface{}{data}
25672616
}
25682617

2618+
func flattenFinalBackupConfig(finalBackupConfig *sqladmin.FinalBackupConfig) []map[string]interface{} {
2619+
data := map[string]interface{}{
2620+
"enabled": finalBackupConfig.Enabled,
2621+
"retention_days": finalBackupConfig.RetentionDays,
2622+
}
2623+
2624+
return []map[string]interface{}{data}
2625+
}
2626+
25692627
func flattenBackupRetentionSettings(b *sqladmin.BackupRetentionSettings) []map[string]interface{} {
25702628
if b == nil {
25712629
return nil

google-beta/services/sql/resource_sql_database_instance_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,38 @@ func TestAccSqlDatabaseInstance_RetainBackupOnDelete(t *testing.T) {
17081708
})
17091709
}
17101710

1711+
func TestAccSqlDatabaseInstance_FinalBackupConfig(t *testing.T) {
1712+
t.Parallel()
1713+
1714+
masterID := acctest.RandInt(t)
1715+
1716+
acctest.VcrTest(t, resource.TestCase{
1717+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1718+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1719+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
1720+
Steps: []resource.TestStep{
1721+
{
1722+
Config: testGoogleSqlDatabaseInstance_FinalBackupConfig(masterID, true, 10),
1723+
},
1724+
{
1725+
ResourceName: "google_sql_database_instance.instance",
1726+
ImportState: true,
1727+
ImportStateVerify: true,
1728+
ImportStateVerifyIgnore: []string{"deletion_protection", "final_backup_description"},
1729+
},
1730+
{
1731+
Config: testGoogleSqlDatabaseInstance_FinalBackupConfig(masterID, false, -1),
1732+
},
1733+
{
1734+
ResourceName: "google_sql_database_instance.instance",
1735+
ImportState: true,
1736+
ImportStateVerify: true,
1737+
ImportStateVerifyIgnore: []string{"deletion_protection", "final_backup_description"},
1738+
},
1739+
},
1740+
})
1741+
}
1742+
17111743
func TestAccSqlDatabaseInstance_PointInTimeRecoveryEnabled(t *testing.T) {
17121744
t.Parallel()
17131745

@@ -6437,6 +6469,47 @@ resource "google_sql_database_instance" "instance" {
64376469
`, masterID, retainBackupOnDelete)
64386470
}
64396471

6472+
func testGoogleSqlDatabaseInstance_FinalBackupConfig(masterID int, enabled bool, retention_days int64) string {
6473+
retentionSetting := ""
6474+
if retention_days >= 0 {
6475+
retentionSetting = fmt.Sprintf(`retention_days = %d`, retention_days)
6476+
}
6477+
6478+
finalBackupConfig := fmt.Sprintf(`final_backup_config {
6479+
enabled = %v
6480+
%v
6481+
}`, enabled, retentionSetting)
6482+
6483+
finalBackupDescription := `""`
6484+
if enabled {
6485+
finalBackupDescription = `"Test FinalBackup Description"`
6486+
}
6487+
6488+
return fmt.Sprintf(`
6489+
resource "google_sql_database_instance" "instance" {
6490+
name = "tf-test-%d"
6491+
region = "us-central1"
6492+
database_version = "MYSQL_8_0"
6493+
deletion_protection = false
6494+
final_backup_description = %v
6495+
6496+
settings {
6497+
tier = "db-g1-small"
6498+
backup_configuration {
6499+
enabled = true
6500+
start_time = "00:00"
6501+
binary_log_enabled = true
6502+
transaction_log_retention_days = 2
6503+
backup_retention_settings {
6504+
retained_backups = 4
6505+
}
6506+
}
6507+
%v
6508+
}
6509+
}
6510+
`, masterID, finalBackupDescription, finalBackupConfig)
6511+
}
6512+
64406513
func testAccSqlDatabaseInstance_beforeBackup(context map[string]interface{}) string {
64416514
return acctest.Nprintf(`
64426515
resource "google_sql_database_instance" "instance" {

website/docs/r/sql_database_instance.html.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ includes an up-to-date reference of supported versions.
341341
or `terraform destroy` that would delete the instance will fail.
342342
When the field is set to false, deleting the instance is allowed.
343343

344+
* `final_backup_description` - (Optional) The description of final backup. Only set this field when `final_backup_config.enabled` is true.
345+
344346
~> **NOTE:** This flag only protects instances from deletion within Terraform. To protect your instances from accidental deletion across all surfaces (API, gcloud, Cloud Console and Terraform), use the API flag `settings.deletion_protection_enabled`.
345347

346348
* `restore_backup_context` - (optional) The context needed to restore the database to a backup run. This field will
@@ -409,6 +411,12 @@ The `settings` block supports:
409411

410412
* `retain_backups_on_delete` - (Optional) When this parameter is set to true, Cloud SQL retains backups of the instance even after the instance is deleted. The `ON_DEMAND` backup will be retained until customer deletes the backup or the project. The `AUTOMATED` backup will be retained based on the backups retention setting.
411413

414+
The optional `final_backup_config` subblock supports:
415+
416+
* `enabled` - (Optional) True if enabled final backup.
417+
418+
* `retention_days` - (Optional) The number of days we retain the final backup after instance deletion. The valid range is between 1 and 365. For instances managed by BackupDR, the valid range is between 1 day and 99 years.
419+
412420
The optional `settings.advanced_machine_features` subblock supports:
413421

414422
* `threads_per_core` - (Optional) The number of threads per core. The value of this flag can be 1 or 2. To disable SMT, set this flag to 1. Only available in Cloud SQL for SQL Server instances. See [smt](https://cloud.google.com/sql/docs/sqlserver/create-instance#smt-create-instance) for more details.

0 commit comments

Comments
 (0)