Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQL Backup Retention fields #8582

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/4385.txt
Original file line number Diff line number Diff line change
@@ -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`
```
73 changes: 66 additions & 7 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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`,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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))

Expand Down
46 changes: 46 additions & 0 deletions google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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" {
Expand Down
11 changes: 11 additions & 0 deletions website/docs/r/sql_database_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down