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
…) (#14604)

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed May 15, 2023
1 parent 8de298d commit 0c850e0
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/7881.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
sql: added `advanced_machine_features` field in `google_sql_database_instance`
```
42 changes: 42 additions & 0 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,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 @@ -1150,6 +1164,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 @@ -1368,6 +1383,18 @@ 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 @@ -1885,6 +1912,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 @@ -1941,6 +1972,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
83 changes: 83 additions & 0 deletions google/resource_sql_database_instance_test.go
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
4 changes: 4 additions & 0 deletions website/docs/r/sql_database_instance.html.markdown
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:

This comment has been minimized.

Copy link
@pdewilde

pdewilde Jun 5, 2023

#14813

This gets formatted so that all options below this appear to be within the advanced_machine_features subblock

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance#threads_per_core

This comment has been minimized.

Copy link
@pdewilde

pdewilde Jun 5, 2023

Putting them above this subblock should fix the issue


* `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 0c850e0

Please sign in to comment.