Skip to content

Commit

Permalink
azurerm_mssql_database - add support for maintenance_configuration_…
Browse files Browse the repository at this point in the history
…name (#18247)
  • Loading branch information
richardfric authored Sep 9, 2022
1 parent 65fc056 commit 273d4a2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
27 changes: 27 additions & 0 deletions internal/services/mssql/mssql_database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v5.0/sql"
"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/go-azure-sdk/resource-manager/maintenance/2021-05-01/publicmaintenanceconfigurations"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down Expand Up @@ -162,6 +163,7 @@ func resourceMsSqlDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface
}
}

maintenanceConfigId := publicmaintenanceconfigurations.NewPublicMaintenanceConfigurationID(serverId.SubscriptionId, d.Get("maintenance_configuration_name").(string))
ledgerEnabled := d.Get("ledger_enabled").(bool)

// When databases are replicating, the primary cannot have a SKU belonging to a higher service tier than any of its
Expand Down Expand Up @@ -234,6 +236,7 @@ func resourceMsSqlDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface
SampleName: sql.SampleName(d.Get("sample_name").(string)),
RequestedBackupStorageRedundancy: sql.RequestedBackupStorageRedundancy(d.Get("storage_account_type").(string)),
ZoneRedundant: utils.Bool(d.Get("zone_redundant").(bool)),
MaintenanceConfigurationID: utils.String(maintenanceConfigId.ID()),
IsLedgerOn: utils.Bool(ledgerEnabled),
},

Expand Down Expand Up @@ -524,6 +527,11 @@ func resourceMsSqlDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) erro
if props.IsLedgerOn != nil {
ledgerEnabled = *props.IsLedgerOn
}
maintenanceConfigId, err := publicmaintenanceconfigurations.ParsePublicMaintenanceConfigurationID(*props.MaintenanceConfigurationID)
if err != nil {
return err
}
d.Set("maintenance_configuration_name", maintenanceConfigId.ResourceName)
d.Set("ledger_enabled", ledgerEnabled)
}

Expand Down Expand Up @@ -715,6 +723,17 @@ func expandMsSqlServerSecurityAlertPolicy(d *pluginsdk.ResourceData) sql.Databas
return policy
}

func resourceMsSqlDatabaseMaintenanceNames() []string {
return []string{"SQL_Default", "SQL_EastUS_DB_1", "SQL_EastUS2_DB_1", "SQL_SoutheastAsia_DB_1", "SQL_AustraliaEast_DB_1", "SQL_NorthEurope_DB_1", "SQL_SouthCentralUS_DB_1", "SQL_WestUS2_DB_1",
"SQL_UKSouth_DB_1", "SQL_WestEurope_DB_1", "SQL_EastUS_DB_2", "SQL_EastUS2_DB_2", "SQL_WestUS2_DB_2", "SQL_SoutheastAsia_DB_2", "SQL_AustraliaEast_DB_2", "SQL_NorthEurope_DB_2", "SQL_SouthCentralUS_DB_2",
"SQL_UKSouth_DB_2", "SQL_WestEurope_DB_2", "SQL_AustraliaSoutheast_DB_1", "SQL_BrazilSouth_DB_1", "SQL_CanadaCentral_DB_1", "SQL_CanadaEast_DB_1", "SQL_CentralUS_DB_1", "SQL_EastAsia_DB_1",
"SQL_FranceCentral_DB_1", "SQL_GermanyWestCentral_DB_1", "SQL_CentralIndia_DB_1", "SQL_SouthIndia_DB_1", "SQL_JapanEast_DB_1", "SQL_JapanWest_DB_1", "SQL_NorthCentralUS_DB_1", "SQL_UKWest_DB_1",
"SQL_WestUS_DB_1", "SQL_AustraliaSoutheast_DB_2", "SQL_BrazilSouth_DB_2", "SQL_CanadaCentral_DB_2", "SQL_CanadaEast_DB_2", "SQL_CentralUS_DB_2", "SQL_EastAsia_DB_2", "SQL_FranceCentral_DB_2",
"SQL_GermanyWestCentral_DB_2", "SQL_CentralIndia_DB_2", "SQL_SouthIndia_DB_2", "SQL_JapanEast_DB_2", "SQL_JapanWest_DB_2", "SQL_NorthCentralUS_DB_2", "SQL_UKWest_DB_2", "SQL_WestUS_DB_2",
"SQL_WestCentralUS_DB_1", "SQL_FranceSouth_DB_1", "SQL_WestCentralUS_DB_2", "SQL_FranceSouth_DB_2", "SQL_SwitzerlandNorth_DB_1", "SQL_SwitzerlandNorth_DB_2", "SQL_BrazilSoutheast_DB_1",
"SQL_UAENorth_DB_1", "SQL_BrazilSoutheast_DB_2", "SQL_UAENorth_DB_2"}
}

func resourceMsSqlDatabaseSchema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Expand Down Expand Up @@ -952,6 +971,14 @@ func resourceMsSqlDatabaseSchema() map[string]*pluginsdk.Schema {
Default: true,
},

"maintenance_configuration_name": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "SQL_Default",
ConflictsWith: []string{"elastic_pool_id"},
ValidateFunc: validation.StringInSlice(resourceMsSqlDatabaseMaintenanceNames(), false),
},

"ledger_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Expand Down
9 changes: 8 additions & 1 deletion internal/services/mssql/mssql_database_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,19 @@ func TestAccMsSqlDatabase_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_database", "test")
r := MsSqlDatabaseResource{}

maintenance_configuration_name := "SQL_Default"
if data.Locations.Primary == "westeurope" {
maintenance_configuration_name = "SQL_WestEurope_DB_2"
}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("collation").HasValue("SQL_AltDiction_CP850_CI_AI"),
check.That(data.ResourceName).Key("collation").HasValue("SQL_AltDiction_CP850_CI_AI"),
check.That(data.ResourceName).Key("license_type").HasValue("BasePrice"),
check.That(data.ResourceName).Key("maintenance_configuration_name").HasValue(maintenance_configuration_name),
check.That(data.ResourceName).Key("max_size_gb").HasValue("1"),
check.That(data.ResourceName).Key("sku_name").HasValue("GP_Gen5_2"),
check.That(data.ResourceName).Key("storage_account_type").HasValue("Local"),
Expand Down Expand Up @@ -746,6 +751,8 @@ resource "azurerm_mssql_database" "test" {
sample_name = "AdventureWorksLT"
sku_name = "GP_Gen5_2"
maintenance_configuration_name = azurerm_resource_group.test.location == "westeurope" ? "SQL_WestEurope_DB_2" : "SQL_Default"
storage_account_type = "Local"
tags = {
Expand Down
15 changes: 4 additions & 11 deletions internal/services/mssql/mssql_elasticpool_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,10 @@ func resourceMsSqlElasticPool() *pluginsdk.Resource {
},

"maintenance_configuration_name": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "SQL_Default",
ValidateFunc: validation.StringInSlice([]string{"SQL_Default", "SQL_EastUS_DB_1", "SQL_EastUS2_DB_1", "SQL_SoutheastAsia_DB_1", "SQL_AustraliaEast_DB_1", "SQL_NorthEurope_DB_1", "SQL_SouthCentralUS_DB_1",
"SQL_WestUS2_DB_1", "SQL_UKSouth_DB_1", "SQL_WestEurope_DB_1", "SQL_EastUS_DB_2", "SQL_EastUS2_DB_2", "SQL_WestUS2_DB_2", "SQL_SoutheastAsia_DB_2", "SQL_AustraliaEast_DB_2", "SQL_NorthEurope_DB_2",
"SQL_SouthCentralUS_DB_2", "SQL_UKSouth_DB_2", "SQL_WestEurope_DB_2", "SQL_AustraliaSoutheast_DB_1", "SQL_BrazilSouth_DB_1", "SQL_CanadaCentral_DB_1", "SQL_CanadaEast_DB_1", "SQL_CentralUS_DB_1",
"SQL_EastAsia_DB_1", "SQL_FranceCentral_DB_1", "SQL_GermanyWestCentral_DB_1", "SQL_CentralIndia_DB_1", "SQL_SouthIndia_DB_1", "SQL_JapanEast_DB_1", "SQL_JapanWest_DB_1", "SQL_NorthCentralUS_DB_1",
"SQL_UKWest_DB_1", "SQL_WestUS_DB_1", "SQL_AustraliaSoutheast_DB_2", "SQL_BrazilSouth_DB_2", "SQL_CanadaCentral_DB_2", "SQL_CanadaEast_DB_2", "SQL_CentralUS_DB_2", "SQL_EastAsia_DB_2",
"SQL_FranceCentral_DB_2", "SQL_GermanyWestCentral_DB_2", "SQL_CentralIndia_DB_2", "SQL_SouthIndia_DB_2", "SQL_JapanEast_DB_2", "SQL_JapanWest_DB_2", "SQL_NorthCentralUS_DB_2", "SQL_UKWest_DB_2",
"SQL_WestUS_DB_2", "SQL_WestCentralUS_DB_1", "SQL_FranceSouth_DB_1", "SQL_WestCentralUS_DB_2", "SQL_FranceSouth_DB_2", "SQL_SwitzerlandNorth_DB_1", "SQL_SwitzerlandNorth_DB_2", "SQL_BrazilSoutheast_DB_1",
"SQL_UAENorth_DB_1", "SQL_BrazilSoutheast_DB_2", "SQL_UAENorth_DB_2"}, false),
Type: pluginsdk.TypeString,
Optional: true,
Default: "SQL_Default",
ValidateFunc: validation.StringInSlice(resourceMsSqlDatabaseMaintenanceNames(), false),
},

"per_database_settings": {
Expand Down
6 changes: 6 additions & 0 deletions internal/services/mssql/mssql_elasticpool_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ func TestAccMsSqlElasticPool_standardDTU(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_elasticpool", "test")
r := MsSqlElasticPoolResource{}

maintenance_configuration_name := "SQL_Default"
if data.Locations.Primary == "westeurope" {
maintenance_configuration_name = "SQL_WestEurope_DB_2"
}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.standardDTU(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("maintenance_configuration_name").HasValue(maintenance_configuration_name),
),
},
data.ImportStep("max_size_gb"),
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/mssql_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ The following arguments are supported:

~> **Note:** `geo_backup_enabled` is only applicable for DataWarehouse SKUs (DW*). This setting is ignored for all other SKUs.

* `maintenance_configuration_name` - (Optional) The name of the Public Maintenance Configuration window to apply to the database. Valid values include `SQL_Default`, `SQL_EastUS_DB_1`, `SQL_EastUS2_DB_1`, `SQL_SoutheastAsia_DB_1`, `SQL_AustraliaEast_DB_1`, `SQL_NorthEurope_DB_1`, `SQL_SouthCentralUS_DB_1`, `SQL_WestUS2_DB_1`, `SQL_UKSouth_DB_1`, `SQL_WestEurope_DB_1`, `SQL_EastUS_DB_2`, `SQL_EastUS2_DB_2`, `SQL_WestUS2_DB_2`, `SQL_SoutheastAsia_DB_2`, `SQL_AustraliaEast_DB_2`, `SQL_NorthEurope_DB_2`, `SQL_SouthCentralUS_DB_2`, `SQL_UKSouth_DB_2`, `SQL_WestEurope_DB_2`, `SQL_AustraliaSoutheast_DB_1`, `SQL_BrazilSouth_DB_1`, `SQL_CanadaCentral_DB_1`, `SQL_CanadaEast_DB_1`, `SQL_CentralUS_DB_1`, `SQL_EastAsia_DB_1`, `SQL_FranceCentral_DB_1`, `SQL_GermanyWestCentral_DB_1`, `SQL_CentralIndia_DB_1`, `SQL_SouthIndia_DB_1`, `SQL_JapanEast_DB_1`, `SQL_JapanWest_DB_1`, `SQL_NorthCentralUS_DB_1`, `SQL_UKWest_DB_1`, `SQL_WestUS_DB_1`, `SQL_AustraliaSoutheast_DB_2`, `SQL_BrazilSouth_DB_2`, `SQL_CanadaCentral_DB_2`, `SQL_CanadaEast_DB_2`, `SQL_CentralUS_DB_2`, `SQL_EastAsia_DB_2`, `SQL_FranceCentral_DB_2`, `SQL_GermanyWestCentral_DB_2`, `SQL_CentralIndia_DB_2`, `SQL_SouthIndia_DB_2`, `SQL_JapanEast_DB_2`, `SQL_JapanWest_DB_2`, `SQL_NorthCentralUS_DB_2`, `SQL_UKWest_DB_2`, `SQL_WestUS_DB_2`, `SQL_WestCentralUS_DB_1`, `SQL_FranceSouth_DB_1`, `SQL_WestCentralUS_DB_2`, `SQL_FranceSouth_DB_2`, `SQL_SwitzerlandNorth_DB_1`, `SQL_SwitzerlandNorth_DB_2`, `SQL_BrazilSoutheast_DB_1`, `SQL_UAENorth_DB_1`, `SQL_BrazilSoutheast_DB_2`, `SQL_UAENorth_DB_2`. Defaults to `SQL_Default`.

* `ledger_enabled` - (Optional) A boolean that specifies if this is a ledger database. Defaults to `false`. Changing this forces a new resource to be created.

* `license_type` - (Optional) Specifies the license type applied to this database. Possible values are `LicenseIncluded` and `BasePrice`.
Expand Down

0 comments on commit 273d4a2

Please sign in to comment.