Skip to content

Commit

Permalink
Adding support for point_in_time_recovery_enabled flag in mssql ins…
Browse files Browse the repository at this point in the history
…tances (#7096) (#13454)

* Added validation for "type" in cloud_sql_user_resource for preventing user from setting "password" or "host" for CLOUD_IAM_USER and CLOUD_IAM_SERVICE_ACCOUNT user types.

* Removed validation and added documentation to prevent setting of host or password field for CLOUD_IAM_USER and CLOUD_IAM_SERVICE_ACCOUNT

* Updated the validation function to allow the setting of  flag for mssql instance, since the API supports it now. Updated documntation as well.

* Updated the validation function to allow the setting of  flag for mssql instance, since the API supports it now. Updated documntation as well.

* Added test function for checking support for point_in_time_recovery_enabled flag in Sql Server.

Signed-off-by: Modular Magician <[email protected]>

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jan 11, 2023
1 parent 2954557 commit 5a049d1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .changelog/7096.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
sql: Updated the ability to set `point_in_time_recovery_enabled` flag for `SQLSERVER` instance, since the API supports it now.
```
4 changes: 2 additions & 2 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,8 +891,8 @@ func privateNetworkCustomizeDiff(_ context.Context, d *schema.ResourceDiff, meta
func pitrPostgresOnlyCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
pitr := diff.Get("settings.0.backup_configuration.0.point_in_time_recovery_enabled").(bool)
dbVersion := diff.Get("database_version").(string)
if pitr && !strings.Contains(dbVersion, "POSTGRES") {
return fmt.Errorf("point_in_time_recovery_enabled is only available for Postgres. You may want to consider using binary_log_enabled instead.")
if pitr && (!strings.Contains(dbVersion, "POSTGRES") && !strings.Contains(dbVersion, "SQLSERVER")) {
return fmt.Errorf("point_in_time_recovery_enabled is only available for Postgres and SQL Server. You may want to consider using binary_log_enabled instead.")
}
return nil
}
Expand Down
49 changes: 41 additions & 8 deletions google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,22 +1085,54 @@ func TestAccSqlDatabaseInstance_PointInTimeRecoveryEnabled(t *testing.T) {
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlDatabaseInstance_PointInTimeRecoveryEnabled(masterID, true),
Config: testGoogleSqlDatabaseInstance_PointInTimeRecoveryEnabled(masterID, true, "POSTGRES_9_6"),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
},
{
Config: testGoogleSqlDatabaseInstance_PointInTimeRecoveryEnabled(masterID, false),
Config: testGoogleSqlDatabaseInstance_PointInTimeRecoveryEnabled(masterID, false, "POSTGRES_9_6"),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
},
},
})
}

func TestAccSqlDatabaseInstance_PointInTimeRecoveryEnabledForSqlServer(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_PointInTimeRecoveryEnabled(masterID, true, "SQLSERVER_2017_STANDARD"),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
},
{
Config: testGoogleSqlDatabaseInstance_PointInTimeRecoveryEnabled(masterID, false, "SQLSERVER_2017_STANDARD"),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
},
},
})
Expand Down Expand Up @@ -2547,23 +2579,24 @@ resource "google_sql_database_instance" "replica" {
}
`

func testGoogleSqlDatabaseInstance_PointInTimeRecoveryEnabled(masterID int, pointInTimeRecoveryEnabled bool) string {
func testGoogleSqlDatabaseInstance_PointInTimeRecoveryEnabled(masterID int, pointInTimeRecoveryEnabled bool, dbVersion string) string {
return fmt.Sprintf(`
resource "google_sql_database_instance" "instance" {
name = "tf-test-%d"
region = "us-central1"
database_version = "POSTGRES_9_6"
database_version = "%s"
deletion_protection = false
root_password = "rand-pwd-%d"
settings {
tier = "db-f1-micro"
tier = "db-custom-2-13312"
backup_configuration {
enabled = true
start_time = "00:00"
point_in_time_recovery_enabled = %t
}
}
}
`, masterID, pointInTimeRecoveryEnabled)
`, masterID, dbVersion, masterID, pointInTimeRecoveryEnabled)
}

func testGoogleSqlDatabaseInstance_BackupRetention(masterID int) string {
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/sql_database_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ The `settings` block supports:
instance, high availability (`REGIONAL`) or single zone (`ZONAL`).' For all instances, ensure that
`settings.backup_configuration.enabled` is set to `true`.
For MySQL instances, ensure that `settings.backup_configuration.binary_log_enabled` is set to `true`.
For Postgres instances, ensure that `settings.backup_configuration.point_in_time_recovery_enabled`
For Postgres and SQL Server instances, ensure that `settings.backup_configuration.point_in_time_recovery_enabled`
is set to `true`. Defaults to `ZONAL`.

* `collation` - (Optional) The name of server instance collation.
Expand Down Expand Up @@ -294,7 +294,7 @@ The optional `settings.backup_configuration` subblock supports:

* `start_time` - (Optional) `HH:MM` format time indicating when backup
configuration starts.
* `point_in_time_recovery_enabled` - (Optional) True if Point-in-time recovery is enabled. Will restart database if enabled after instance creation. Valid only for PostgreSQL instances.
* `point_in_time_recovery_enabled` - (Optional) True if Point-in-time recovery is enabled. Will restart database if enabled after instance creation. Valid only for PostgreSQL and SQL Server instances.

* `location` - (Optional) The region where the backup will be stored

Expand Down

0 comments on commit 5a049d1

Please sign in to comment.