diff --git a/.changelog/4385.txt b/.changelog/4385.txt new file mode 100644 index 00000000000..cc88c14b2ec --- /dev/null +++ b/.changelog/4385.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +sql: added `settings.0.backup_configuration.transaction_log_retention_days` and `settings.0.backup_configuration.transaction_log_retention_days` fields to `google_sql_database_instance` +``` diff --git a/google/resource_sql_database_instance.go b/google/resource_sql_database_instance.go index 727a45ea43f..3030062e0e1 100644 --- a/google/resource_sql_database_instance.go +++ b/google/resource_sql_database_instance.go @@ -44,6 +44,8 @@ var ( "settings.0.backup_configuration.0.start_time", "settings.0.backup_configuration.0.location", "settings.0.backup_configuration.0.point_in_time_recovery_enabled", + "settings.0.backup_configuration.0.backup_retention_settings", + "settings.0.backup_configuration.0.transaction_log_retention_days", } ipConfigurationKeys = []string{ @@ -110,7 +112,7 @@ func resourceSqlDatabaseInstance() *schema.Resource { Optional: true, Computed: true, ForceNew: true, - Description: `The region the instance will sit in. Note, Cloud SQL is not available in all regions - choose from one of the options listed here. A valid region must be provided to use this resource. If a region is not provided in the resource definition, the provider region will be used instead, but this will be an apply-time error for instances if the provider region is not supported with Cloud SQL. If you choose not to provide the region argument for this resource, make sure you understand this.`, + Description: `The region the instance will sit in. Note, Cloud SQL is not available in all regions. A valid region must be provided to use this resource. If a region is not provided in the resource definition, the provider region will be used instead, but this will be an apply-time error for instances if the provider region is not supported with Cloud SQL. If you choose not to provide the region argument for this resource, make sure you understand this.`, }, "deletion_protection": { Type: schema.TypeBool, @@ -204,6 +206,35 @@ settings.backup_configuration.binary_log_enabled are both set to true.`, AtLeastOneOf: backupConfigurationKeys, Description: `True if Point-in-time recovery is enabled.`, }, + "transaction_log_retention_days": { + Type: schema.TypeInt, + Computed: true, + Optional: true, + AtLeastOneOf: backupConfigurationKeys, + Description: `The number of days of transaction logs we retain for point in time restore, from 1-7.`, + }, + "backup_retention_settings": { + Type: schema.TypeList, + Optional: true, + AtLeastOneOf: backupConfigurationKeys, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "retained_backups": { + Type: schema.TypeInt, + Required: true, + Description: `Number of backups to retain.`, + }, + "retention_unit": { + Type: schema.TypeString, + Optional: true, + Default: "COUNT", + Description: `The unit that 'retainedBackups' represents. Defaults to COUNT`, + }, + }, + }, + }, }, }, }, @@ -1060,12 +1091,26 @@ func expandBackupConfiguration(configured []interface{}) *sqladmin.BackupConfigu _backupConfiguration := configured[0].(map[string]interface{}) return &sqladmin.BackupConfiguration{ - BinaryLogEnabled: _backupConfiguration["binary_log_enabled"].(bool), - Enabled: _backupConfiguration["enabled"].(bool), - StartTime: _backupConfiguration["start_time"].(string), - Location: _backupConfiguration["location"].(string), - PointInTimeRecoveryEnabled: _backupConfiguration["point_in_time_recovery_enabled"].(bool), - ForceSendFields: []string{"BinaryLogEnabled", "Enabled", "PointInTimeRecoveryEnabled"}, + BinaryLogEnabled: _backupConfiguration["binary_log_enabled"].(bool), + BackupRetentionSettings: expandBackupRetentionSettings(_backupConfiguration["backup_retention_settings"]), + Enabled: _backupConfiguration["enabled"].(bool), + StartTime: _backupConfiguration["start_time"].(string), + Location: _backupConfiguration["location"].(string), + TransactionLogRetentionDays: int64(_backupConfiguration["transaction_log_retention_days"].(int)), + PointInTimeRecoveryEnabled: _backupConfiguration["point_in_time_recovery_enabled"].(bool), + ForceSendFields: []string{"BinaryLogEnabled", "Enabled", "PointInTimeRecoveryEnabled"}, + } +} + +func expandBackupRetentionSettings(configured interface{}) *sqladmin.BackupRetentionSettings { + l := configured.([]interface{}) + if len(l) == 0 { + return nil + } + config := l[0].(map[string]interface{}) + return &sqladmin.BackupRetentionSettings{ + RetainedBackups: int64(config["retained_backups"].(int)), + RetentionUnit: config["retention_unit"].(string), } } @@ -1347,11 +1392,25 @@ func flattenBackupConfiguration(backupConfiguration *sqladmin.BackupConfiguratio "start_time": backupConfiguration.StartTime, "location": backupConfiguration.Location, "point_in_time_recovery_enabled": backupConfiguration.PointInTimeRecoveryEnabled, + "backup_retention_settings": flattenBackupRetentionSettings(backupConfiguration.BackupRetentionSettings), + "transaction_log_retention_days": backupConfiguration.TransactionLogRetentionDays, } return []map[string]interface{}{data} } +func flattenBackupRetentionSettings(b *sqladmin.BackupRetentionSettings) []map[string]interface{} { + if b == nil { + return nil + } + return []map[string]interface{}{ + { + "retained_backups": b.RetainedBackups, + "retention_unit": b.RetentionUnit, + }, + } +} + func flattenDatabaseFlags(databaseFlags []*sqladmin.DatabaseFlags) []map[string]interface{} { flags := make([]map[string]interface{}, 0, len(databaseFlags)) diff --git a/google/resource_sql_database_instance_test.go b/google/resource_sql_database_instance_test.go index 1be5c95f531..681b0d471cd 100644 --- a/google/resource_sql_database_instance_test.go +++ b/google/resource_sql_database_instance_test.go @@ -825,6 +825,29 @@ func testAccCheckGoogleSqlDatabaseRootUserDoesNotExist(t *testing.T, instance st } } +func TestAccSqlDatabaseInstance_BackupRetention(t *testing.T) { + t.Parallel() + + masterID := randInt(t) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testGoogleSqlDatabaseInstance_BackupRetention(masterID), + }, + { + ResourceName: "google_sql_database_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection"}, + }, + }, + }) +} + func TestAccSqlDatabaseInstance_PointInTimeRecoveryEnabled(t *testing.T) { t.Parallel() @@ -1319,6 +1342,29 @@ resource "google_sql_database_instance" "instance" { `, masterID, pointInTimeRecoveryEnabled) } +func testGoogleSqlDatabaseInstance_BackupRetention(masterID int) string { + return fmt.Sprintf(` +resource "google_sql_database_instance" "instance" { + name = "tf-test-%d" + region = "us-central1" + database_version = "MYSQL_8_0" + deletion_protection = false + settings { + tier = "db-f1-micro" + backup_configuration { + enabled = true + start_time = "00:00" + binary_log_enabled = true + transaction_log_retention_days = 2 + backup_retention_settings { + retained_backups = 4 + } + } + } +} +`, masterID) +} + func testAccSqlDatabaseInstance_beforeBackup(context map[string]interface{}) string { return Nprintf(` resource "google_sql_database_instance" "instance" { diff --git a/website/docs/r/sql_database_instance.html.markdown b/website/docs/r/sql_database_instance.html.markdown index 360ee527d3f..7c8833f87a2 100644 --- a/website/docs/r/sql_database_instance.html.markdown +++ b/website/docs/r/sql_database_instance.html.markdown @@ -303,6 +303,17 @@ The optional `settings.backup_configuration` subblock supports: * `location` - (Optional) The region where the backup will be stored +* `transaction_log_retention_days` - (Optional) The number of days of transaction logs we retain for point in time restore, from 1-7. + +* `backup_retention_settings` - (Optional) Backup retention settings. The configuration is detailed below. + +The optional `settings.backup_configuration.backup_retention_settings` subblock supports: + +* `retained_backups` - (Optional) Depending on the value of retention_unit, this is used to determine if a backup needs to be deleted. If retention_unit + is 'COUNT', we will retain this many backups. + +* `retention_unit` - (Optional) The unit that 'retained_backups' represents. Defaults to `COUNT`. + The optional `settings.ip_configuration` subblock supports: * `ipv4_enabled` - (Optional) Whether this Cloud SQL instance should be assigned