Skip to content

Commit 2fdda66

Browse files
Add Sql Server Timezone Update Support (GoogleCloudPlatform#11202)
1 parent 49eb7bf commit 2fdda66

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

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

+29-5
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,9 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
283283
},
284284
},
285285
"time_zone": {
286-
Type: schema.TypeString,
287-
ForceNew: true,
288-
Optional: true,
289-
Description: `The time_zone to be used by the database engine (supported only for SQL Server), in SQL Server timezone format.`,
286+
Type: schema.TypeString,
287+
Optional: true,
288+
Description: `The time_zone to be used by the database engine (supported only for SQL Server), in SQL Server timezone format.`,
290289
},
291290
"availability_type": {
292291
Type: schema.TypeString,
@@ -1979,7 +1978,32 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
19791978
}
19801979
}
19811980

1982-
return resourceSqlDatabaseInstanceRead(d, meta)
1981+
// Check if timezone is updated
1982+
if d.HasChange("settings.0.time_zone") {
1983+
timezone := d.Get("settings.0.time_zone").(string)
1984+
instance = &sqladmin.DatabaseInstance{Settings: &sqladmin.Settings{TimeZone: timezone}}
1985+
err = transport_tpg.Retry(transport_tpg.RetryOptions{
1986+
RetryFunc: func() (rerr error) {
1987+
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Patch(project, d.Get("name").(string), instance).Do()
1988+
return err
1989+
},
1990+
Timeout: d.Timeout(schema.TimeoutUpdate),
1991+
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.IsSqlOperationInProgressError},
1992+
})
1993+
if err != nil {
1994+
return fmt.Errorf("Error, failed to patch instance settings for %s: %s", instance.Name, err)
1995+
}
1996+
err = SqlAdminOperationWaitTime(config, op, project, "Patch Instance", userAgent, d.Timeout(schema.TimeoutUpdate))
1997+
if err != nil {
1998+
return err
1999+
}
2000+
err = resourceSqlDatabaseInstanceRead(d, meta)
2001+
if err != nil {
2002+
return err
2003+
}
2004+
}
2005+
2006+
return resourceSqlDatabaseInstanceRead(d, meta)
19832007
}
19842008

19852009
func maintenanceVersionDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {

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

+54
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,41 @@ func TestAccSqlDatabaseInstance_rootPasswordShouldBeUpdatable(t *testing.T) {
20762076
})
20772077
}
20782078

2079+
func TestAccSqlDatabaseInstance_SqlServerTimezoneUpdate(t *testing.T) {
2080+
t.Parallel()
2081+
2082+
instanceName := "tf-test-" + acctest.RandString(t, 10)
2083+
rootPassword := acctest.RandString(t, 15)
2084+
timezone := "Eastern Standard Time"
2085+
timezoneUpdate := "Pacific Standard Time"
2086+
2087+
acctest.VcrTest(t, resource.TestCase{
2088+
PreCheck: func() { acctest.AccTestPreCheck(t) },
2089+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
2090+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
2091+
Steps: []resource.TestStep{
2092+
{
2093+
Config: testGoogleSqlDatabaseInstance_SqlServerTimezone(instanceName, rootPassword, timezone),
2094+
},
2095+
{
2096+
ResourceName: "google_sql_database_instance.instance",
2097+
ImportState: true,
2098+
ImportStateVerify: true,
2099+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
2100+
},
2101+
{
2102+
Config: testGoogleSqlDatabaseInstance_SqlServerTimezone(instanceName, rootPassword, timezoneUpdate),
2103+
},
2104+
{
2105+
ResourceName: "google_sql_database_instance.instance",
2106+
ImportState: true,
2107+
ImportStateVerify: true,
2108+
ImportStateVerifyIgnore: []string{"deletion_protection", "root_password"},
2109+
},
2110+
},
2111+
})
2112+
}
2113+
20792114
func TestAccSqlDatabaseInstance_activationPolicy(t *testing.T) {
20802115
t.Parallel()
20812116

@@ -2684,6 +2719,25 @@ resource "google_sql_database_instance" "instance" {
26842719
}`, instanceName, tier, edition)
26852720
}
26862721

2722+
func testGoogleSqlDatabaseInstance_SqlServerTimezone(instance, rootPassword, timezone string) string {
2723+
return fmt.Sprintf(`
2724+
resource "google_sql_database_instance" "instance" {
2725+
name = "%s"
2726+
region = "us-central1"
2727+
database_version = "SQLSERVER_2017_STANDARD"
2728+
root_password = "%s"
2729+
deletion_protection = false
2730+
settings {
2731+
tier = "db-custom-1-3840"
2732+
ip_configuration {
2733+
ipv4_enabled = "true"
2734+
}
2735+
time_zone = "%s"
2736+
}
2737+
}
2738+
`, instance, rootPassword, timezone)
2739+
}
2740+
26872741
func testGoogleSqlDatabaseInstance_SqlServerAuditConfig(databaseName, rootPassword, bucketName, uploadInterval, retentionInterval string) string {
26882742
return fmt.Sprintf(`
26892743
resource "google_storage_bucket" "gs-bucket" {

0 commit comments

Comments
 (0)