Skip to content

Commit

Permalink
Add Sql Server Timezone Update Support (#11202)
Browse files Browse the repository at this point in the history
  • Loading branch information
stameishiGoogle committed Sep 26, 2024
1 parent 49eb7bf commit 2fdda66
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,9 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
},
},
"time_zone": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Description: `The time_zone to be used by the database engine (supported only for SQL Server), in SQL Server timezone format.`,
Type: schema.TypeString,
Optional: true,
Description: `The time_zone to be used by the database engine (supported only for SQL Server), in SQL Server timezone format.`,
},
"availability_type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -1979,7 +1978,32 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
}
}

return resourceSqlDatabaseInstanceRead(d, meta)
// Check if timezone is updated
if d.HasChange("settings.0.time_zone") {
timezone := d.Get("settings.0.time_zone").(string)
instance = &sqladmin.DatabaseInstance{Settings: &sqladmin.Settings{TimeZone: timezone}}
err = transport_tpg.Retry(transport_tpg.RetryOptions{
RetryFunc: func() (rerr error) {
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Patch(project, d.Get("name").(string), instance).Do()
return err
},
Timeout: d.Timeout(schema.TimeoutUpdate),
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.IsSqlOperationInProgressError},
})
if err != nil {
return fmt.Errorf("Error, failed to patch instance settings for %s: %s", instance.Name, err)
}
err = SqlAdminOperationWaitTime(config, op, project, "Patch Instance", userAgent, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
err = resourceSqlDatabaseInstanceRead(d, meta)
if err != nil {
return err
}
}

return resourceSqlDatabaseInstanceRead(d, meta)
}

func maintenanceVersionDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2076,6 +2076,41 @@ func TestAccSqlDatabaseInstance_rootPasswordShouldBeUpdatable(t *testing.T) {
})
}

func TestAccSqlDatabaseInstance_SqlServerTimezoneUpdate(t *testing.T) {
t.Parallel()

instanceName := "tf-test-" + acctest.RandString(t, 10)
rootPassword := acctest.RandString(t, 15)
timezone := "Eastern Standard Time"
timezoneUpdate := "Pacific Standard Time"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlDatabaseInstance_SqlServerTimezone(instanceName, rootPassword, timezone),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
},
{
Config: testGoogleSqlDatabaseInstance_SqlServerTimezone(instanceName, rootPassword, timezoneUpdate),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
},
},
})
}

func TestAccSqlDatabaseInstance_activationPolicy(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2684,6 +2719,25 @@ resource "google_sql_database_instance" "instance" {
}`, instanceName, tier, edition)
}

func testGoogleSqlDatabaseInstance_SqlServerTimezone(instance, rootPassword, timezone string) string {
return fmt.Sprintf(`
resource "google_sql_database_instance" "instance" {
name = "%s"
region = "us-central1"
database_version = "SQLSERVER_2017_STANDARD"
root_password = "%s"
deletion_protection = false
settings {
tier = "db-custom-1-3840"
ip_configuration {
ipv4_enabled = "true"
}
time_zone = "%s"
}
}
`, instance, rootPassword, timezone)
}

func testGoogleSqlDatabaseInstance_SqlServerAuditConfig(databaseName, rootPassword, bucketName, uploadInterval, retentionInterval string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "gs-bucket" {
Expand Down

0 comments on commit 2fdda66

Please sign in to comment.