Skip to content

Commit

Permalink
Added terraform support to configure SMT in SQL Server instances (#7881)
Browse files Browse the repository at this point in the history
  • Loading branch information
dthaluru authored May 15, 2023
1 parent d505987 commit 21a919d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ func ResourceSqlDatabaseInstance() *schema.Resource {
Required: true,
Description: `The machine type to use. See tiers for more details and supported versions. Postgres supports only shared-core machine types, and custom machine types such as db-custom-2-13312. See the Custom Machine Type Documentation to learn about specifying custom machine types.`,
},
"advanced_machine_features": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"threads_per_core": {
Type: schema.TypeInt,
Optional: true,
Description: `The number of threads per physical core. Can be 1 or 2.`,
},
},
},
},
"activation_policy": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -1152,6 +1166,7 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}, databaseVersion
// Version is unset in Create but is set during update
SettingsVersion: int64(_settings["version"].(int)),
Tier: _settings["tier"].(string),
AdvancedMachineFeatures: expandSqlServerAdvancedMachineFeatures(_settings["advanced_machine_features"].([]interface{})),
ForceSendFields: []string{"StorageAutoResize"},
ActivationPolicy: _settings["activation_policy"].(string),
ActiveDirectoryConfig: expandActiveDirectoryConfig(_settings["active_directory_config"].([]interface{})),
Expand Down Expand Up @@ -1370,6 +1385,19 @@ func expandDenyMaintenancePeriod(configured []interface{}) []*sqladmin.DenyMaint

}


func expandSqlServerAdvancedMachineFeatures(configured interface{}) *sqladmin.AdvancedMachineFeatures {
l := configured.([]interface{})
if len(l) == 0 {
return nil
}

config := l[0].(map[string]interface{})
return &sqladmin.AdvancedMachineFeatures{
ThreadsPerCore: int64(config["threads_per_core"].(int)),
}
}

func expandSqlServerAuditConfig(configured interface{}) *sqladmin.SqlServerAuditConfig {
l := configured.([]interface{})
if len(l) == 0 {
Expand Down Expand Up @@ -1887,6 +1915,10 @@ func flattenSettings(settings *sqladmin.Settings) []map[string]interface{} {
data["password_validation_policy"] = flattenPasswordValidationPolicy(settings.PasswordValidationPolicy)
}

if settings.AdvancedMachineFeatures != nil {
data["advanced_machine_features"] = flattenSqlServerAdvancedMachineFeatures(settings.AdvancedMachineFeatures)
}

return []map[string]interface{}{data}
}

Expand Down Expand Up @@ -1944,6 +1976,17 @@ func flattenDenyMaintenancePeriod(denyMaintenancePeriod []*sqladmin.DenyMaintena
return flags
}

func flattenSqlServerAdvancedMachineFeatures(advancedMachineFeatures *sqladmin.AdvancedMachineFeatures) []map[string]interface{} {
if advancedMachineFeatures == nil {
return nil
}
return []map[string]interface{}{
{
"threads_per_core": advancedMachineFeatures.ThreadsPerCore,
},
}
}

func flattenSqlServerAuditConfig(sqlServerAuditConfig *sqladmin.SqlServerAuditConfig) []map[string]interface{} {
if sqlServerAuditConfig == nil {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,48 @@ func TestAccSqlDatabaseInstance_SqlServerAuditOptionalBucket(t *testing.T) {
})
}

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

databaseName := "tf-test-" + RandString(t, 10)
rootPassword := RandString(t, 15)

VcrTest(t, resource.TestCase{
PreCheck: func() { AccTestPreCheck(t) },
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlDatabaseInstance_Smt(databaseName, rootPassword, 1),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
},
{
Config: testGoogleSqlDatabaseInstance_Smt(databaseName, rootPassword, 2),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
},
{
Config: testGoogleSqlDatabaseInstance_NullSmt(databaseName, rootPassword),
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
},
},
})
}

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

Expand Down Expand Up @@ -2006,6 +2048,47 @@ resource "google_sql_database_instance" "instance" {
`, databaseName, rootPassword, retentionInterval, uploadInterval)
}

func testGoogleSqlDatabaseInstance_NullSmt(databaseName, rootPassword 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-8-53248"
ip_configuration {
ipv4_enabled = "true"
}
advanced_machine_features {
}
}
}
`, databaseName, rootPassword)
}

func testGoogleSqlDatabaseInstance_Smt(databaseName, rootPassword string, threadsPerCore int) 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-8-53248"
ip_configuration {
ipv4_enabled = "true"
}
advanced_machine_features {
threads_per_core = "%d"
}
}
}
`, databaseName, rootPassword, threadsPerCore)
}

func testGoogleSqlDatabaseInstance_Timezone(databaseName, rootPassword, timezone string) string {
return fmt.Sprintf(`
resource "google_sql_database_instance" "instance" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ The `settings` block supports:
for more details and supported versions. Postgres supports only shared-core machine types,
and custom machine types such as `db-custom-2-13312`. See the [Custom Machine Type Documentation](https://cloud.google.com/compute/docs/instances/creating-instance-with-custom-machine-type#create) to learn about specifying custom machine types.

The optional `settings.advanced_machine_features` subblock supports:

* `threads_per_core` - (Optional) The number of threads per core. The value of this flag can be 1 or 2. To disable SMT, set this flag to 1. Only available in Cloud SQL for SQL Server instances. See [smt](https://cloud.google.com/sql/docs/sqlserver/create-instance#smt-create-instance) for more details.

* `activation_policy` - (Optional) This specifies when the instance should be
active. Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`.

Expand Down

0 comments on commit 21a919d

Please sign in to comment.