Skip to content

Commit defbc8d

Browse files
authored
add final_backup_config field to google_sql_database_instance (#14891)
1 parent b0ae3d7 commit defbc8d

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
172172
Optional: true,
173173
Description: `Used to block Terraform from deleting a SQL Instance. Defaults to true.`,
174174
},
175+
"final_backup_description": {
176+
Type: schema.TypeString,
177+
Optional: true,
178+
Description: `The description of final backup if instance enable create final backup during instance deletion. `,
179+
180+
},
175181
"settings": {
176182
Type: schema.TypeList,
177183
Optional: true,
@@ -792,6 +798,28 @@ API (for read pools, effective_availability_type may differ from availability_ty
792798
Optional: true,
793799
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.`,
794800
},
801+
"final_backup_config": {
802+
Type: schema.TypeList,
803+
Optional: true,
804+
MaxItems: 1,
805+
Elem: &schema.Resource{
806+
Schema: map[string]*schema.Schema{
807+
"enabled": {
808+
Type: schema.TypeBool,
809+
Optional: true,
810+
Description: `When this parameter is set to true, the final backup is enabled for the instance`,
811+
},
812+
"retention_days": {
813+
Type: schema.TypeInt,
814+
Optional: true,
815+
ValidateFunc: validation.IntBetween(1, 36135),
816+
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).`,
817+
},
818+
},
819+
},
820+
Description: `Config used to determine the final backup settings for the instance`,
821+
822+
},
795823
},
796824
},
797825
Description: `The settings to use for the database. The configuration is detailed below.`,
@@ -1507,6 +1535,7 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}, databaseVersion
15071535
EnableGoogleMlIntegration: _settings["enable_google_ml_integration"].(bool),
15081536
EnableDataplexIntegration: _settings["enable_dataplex_integration"].(bool),
15091537
RetainBackupsOnDelete: _settings["retain_backups_on_delete"].(bool),
1538+
FinalBackupConfig: expandFinalBackupConfig(_settings["final_backup_config"].([]interface{})),
15101539
UserLabels: tpgresource.ConvertStringMap(_settings["user_labels"].(map[string]interface{})),
15111540
BackupConfiguration: expandBackupConfiguration(_settings["backup_configuration"].([]interface{})),
15121541
DatabaseFlags: expandDatabaseFlags(_settings["database_flags"].(*schema.Set).List()),
@@ -1749,6 +1778,19 @@ func expandBackupConfiguration(configured []interface{}) *sqladmin.BackupConfigu
17491778
}
17501779
}
17511780

1781+
func expandFinalBackupConfig(configured []interface{}) *sqladmin.FinalBackupConfig{
1782+
if len(configured) == 0 || configured[0] == nil {
1783+
return nil
1784+
}
1785+
1786+
_finalBackupConfig := configured[0].(map[string]interface{})
1787+
return &sqladmin.FinalBackupConfig{
1788+
Enabled: _finalBackupConfig["enabled"].(bool),
1789+
RetentionDays: int64(_finalBackupConfig["retention_days"].(int)),
1790+
ForceSendFields: []string{"Enabled"},
1791+
}
1792+
}
1793+
17521794
func expandBackupRetentionSettings(configured interface{}) *sqladmin.BackupRetentionSettings {
17531795
l := configured.([]interface{})
17541796
if len(l) == 0 {
@@ -2377,9 +2419,13 @@ func resourceSqlDatabaseInstanceDelete(d *schema.ResourceData, meta interface{})
23772419
}
23782420

23792421
var op *sqladmin.Operation
2422+
finalBackupDescription := ""
2423+
if v, ok := d.GetOk("finalBackupDescription"); ok {
2424+
finalBackupDescription = v.(string)
2425+
}
23802426
err = transport_tpg.Retry(transport_tpg.RetryOptions{
23812427
RetryFunc: func() (rerr error) {
2382-
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Delete(project, d.Get("name").(string)).Do()
2428+
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Delete(project, d.Get("name").(string)).FinalBackupDescription(finalBackupDescription).Do()
23832429
if rerr != nil {
23842430
return rerr
23852431
}
@@ -2443,6 +2489,7 @@ func flattenSettings(settings *sqladmin.Settings, iType string, d *schema.Resour
24432489
"time_zone": settings.TimeZone,
24442490
"deletion_protection_enabled": settings.DeletionProtectionEnabled,
24452491
"retain_backups_on_delete": settings.RetainBackupsOnDelete,
2492+
"final_backup_config": settings.FinalBackupConfig,
24462493
}
24472494

24482495
if data["availability_type"] == "" {
@@ -2473,6 +2520,10 @@ func flattenSettings(settings *sqladmin.Settings, iType string, d *schema.Resour
24732520
data["backup_configuration"] = flattenBackupConfiguration(settings.BackupConfiguration)
24742521
}
24752522

2523+
if settings.FinalBackupConfig != nil {
2524+
data["final_backup_config"] = flattenFinalBackupConfig(settings.FinalBackupConfig)
2525+
}
2526+
24762527
if settings.DatabaseFlags != nil {
24772528
data["database_flags"] = flattenDatabaseFlags(settings.DatabaseFlags)
24782529
}
@@ -2557,6 +2608,15 @@ func flattenBackupConfiguration(backupConfiguration *sqladmin.BackupConfiguratio
25572608
return []map[string]interface{}{data}
25582609
}
25592610

2611+
func flattenFinalBackupConfig(finalBackupConfig *sqladmin.FinalBackupConfig) []map[string]interface{} {
2612+
data := map[string]interface{}{
2613+
"enabled": finalBackupConfig.Enabled,
2614+
"retention_days": finalBackupConfig.RetentionDays,
2615+
}
2616+
2617+
return []map[string]interface{}{data}
2618+
}
2619+
25602620
func flattenBackupRetentionSettings(b *sqladmin.BackupRetentionSettings) []map[string]interface{} {
25612621
if b == nil {
25622622
return nil

mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go.tmpl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,38 @@ func TestAccSqlDatabaseInstance_RetainBackupOnDelete(t *testing.T) {
17001700
})
17011701
}
17021702

1703+
func TestAccSqlDatabaseInstance_FinalBackupConfig(t *testing.T) {
1704+
t.Parallel()
1705+
1706+
masterID := acctest.RandInt(t)
1707+
1708+
acctest.VcrTest(t, resource.TestCase{
1709+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1710+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1711+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
1712+
Steps: []resource.TestStep{
1713+
{
1714+
Config: testGoogleSqlDatabaseInstance_FinalBackupConfig(masterID, true, 10),
1715+
},
1716+
{
1717+
ResourceName: "google_sql_database_instance.instance",
1718+
ImportState: true,
1719+
ImportStateVerify: true,
1720+
ImportStateVerifyIgnore: []string{"deletion_protection", "final_backup_description"},
1721+
},
1722+
{
1723+
Config: testGoogleSqlDatabaseInstance_FinalBackupConfig(masterID, false, -1),
1724+
},
1725+
{
1726+
ResourceName: "google_sql_database_instance.instance",
1727+
ImportState: true,
1728+
ImportStateVerify: true,
1729+
ImportStateVerifyIgnore: []string{"deletion_protection", "final_backup_description"},
1730+
},
1731+
},
1732+
})
1733+
}
1734+
17031735
func TestAccSqlDatabaseInstance_PointInTimeRecoveryEnabled(t *testing.T) {
17041736
t.Parallel()
17051737

@@ -6432,6 +6464,47 @@ resource "google_sql_database_instance" "instance" {
64326464
`, masterID, retainBackupOnDelete)
64336465
}
64346466

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

mmv1/third_party/terraform/website/docs/r/sql_database_instance.html.markdown

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

330+
* `final_backup_description` - (Optional) The description of final backup. Only set this field when `final_backup_config.enabled` is true.
331+
330332
~> **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`.
331333

332334
* `restore_backup_context` - (optional) The context needed to restore the database to a backup run. This field will
@@ -395,6 +397,12 @@ The `settings` block supports:
395397

396398
* `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.
397399

400+
The optional `final_backup_config` subblock supports:
401+
402+
* `enabled` - (Optional) True if enabled final backup.
403+
404+
* `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.
405+
398406
The optional `settings.advanced_machine_features` subblock supports:
399407

400408
* `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)