From 0eabc5a1f9a2630441653f0e069a88c6ead66759 Mon Sep 17 00:00:00 2001 From: Elena Xin <39109137+sinbai@users.noreply.github.com> Date: Tue, 19 Sep 2023 16:08:49 +0800 Subject: [PATCH] `azurerm_mssql_elasticpool` : fix `license_type` can't be set to null when changing from vCore to DTU model issue (#23262) * fix issue 22311 * update code * update code --- .../mssql/mssql_elasticpool_resource.go | 14 +- .../mssql/mssql_elasticpool_resource_test.go | 140 +++++++++++++++++- .../docs/r/mssql_elasticpool.html.markdown | 2 - 3 files changed, 144 insertions(+), 12 deletions(-) diff --git a/internal/services/mssql/mssql_elasticpool_resource.go b/internal/services/mssql/mssql_elasticpool_resource.go index 2c02d2edfd99..b91613028e94 100644 --- a/internal/services/mssql/mssql_elasticpool_resource.go +++ b/internal/services/mssql/mssql_elasticpool_resource.go @@ -234,14 +234,6 @@ func resourceMsSqlElasticPoolCreateUpdate(d *pluginsdk.ResourceData, meta interf }, } - if _, ok := d.GetOk("license_type"); ok { - if sku.Tier != nil && (*sku.Tier == "GeneralPurpose" || *sku.Tier == "BusinessCritical" || *sku.Tier == "Hyperscale") { - elasticPool.ElasticPoolProperties.LicenseType = sql.ElasticPoolLicenseType(d.Get("license_type").(string)) - } else { - return fmt.Errorf("`license_type` can only be configured when `sku.0.tier` is set to `GeneralPurpose`, `Hyperscale` or `BusinessCritical`") - } - } - if d.HasChange("max_size_gb") { if v, ok := d.GetOk("max_size_gb"); ok { maxSizeBytes := v.(float64) * 1073741824 @@ -307,7 +299,11 @@ func resourceMsSqlElasticPoolRead(d *pluginsdk.ResourceData, meta interface{}) e } } d.Set("zone_redundant", properties.ZoneRedundant) - d.Set("license_type", string(properties.LicenseType)) + licenseType := string(sql.DatabaseLicenseTypeLicenseIncluded) + if string(properties.LicenseType) != "" { + licenseType = string(properties.LicenseType) + } + d.Set("license_type", licenseType) if err := d.Set("per_database_settings", flattenMsSqlElasticPoolPerDatabaseSettings(properties.PerDatabaseSettings)); err != nil { return fmt.Errorf("setting `per_database_settings`: %+v", err) diff --git a/internal/services/mssql/mssql_elasticpool_resource_test.go b/internal/services/mssql/mssql_elasticpool_resource_test.go index 38eaa1da8f23..5ed8e09c29ac 100644 --- a/internal/services/mssql/mssql_elasticpool_resource_test.go +++ b/internal/services/mssql/mssql_elasticpool_resource_test.go @@ -268,6 +268,42 @@ func TestAccMsSqlElasticPool_hyperScale(t *testing.T) { }) } +func TestAccMsSqlElasticPool_vCoreToStandardDTU(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_mssql_elasticpool", "test") + r := MsSqlElasticPoolResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.noLicenseType(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.updateToStandardDTU(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.licenseType(data, "LicenseIncluded"), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.updateToStandardDTU(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func (MsSqlElasticPoolResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := parse.ElasticPoolID(state.ID) if err != nil { @@ -330,6 +366,10 @@ func (r MsSqlElasticPoolResource) basicVCore(data acceptance.TestData) string { return r.templateVCore(data, "GP_Gen5", "GeneralPurpose", 4, "Gen5", 0.25, 4) } +func (r MsSqlElasticPoolResource) updateToStandardDTU(data acceptance.TestData) string { + return r.templateUpdateToDTU(data, "StandardPool", "Standard", 50, 50, 10, 50, false) +} + func (r MsSqlElasticPoolResource) fsv2VCore(data acceptance.TestData) string { return r.templateVCore(data, "GP_Fsv2", "GeneralPurpose", 8, "Fsv2", 0, 8) } @@ -405,6 +445,62 @@ resource "azurerm_mssql_elasticpool" "test" { `, data.RandomInteger, data.Locations.Primary, skuName, skuTier, skuCapacity, maxSizeGB, databaseSettingsMin, databaseSettingsMax, zoneRedundant, configName) } +func (MsSqlElasticPoolResource) templateUpdateToDTU(data acceptance.TestData, skuName string, skuTier string, skuCapacity int, maxSizeGB float64, databaseSettingsMin int, databaseSettingsMax int, zoneRedundant bool) string { + configName := "SQL_Default" + if skuTier != "Basic" { + switch data.Locations.Primary { + case "westeurope": + configName = "SQL_WestEurope_DB_2" + case "francecentral": + configName = "SQL_FranceCentral_DB_1" + default: + configName = "SQL_Default" + } + } + + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%[1]d" + location = "%[2]s" +} + +resource "azurerm_mssql_server" "test" { + name = "acctest%[1]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + version = "12.0" + administrator_login = "4dm1n157r470r" + administrator_login_password = "4-v3ry-53cr37-p455w0rd" +} + +resource "azurerm_mssql_elasticpool" "test" { + name = "acctest-pool-dtu-%[1]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + server_name = azurerm_mssql_server.test.name + max_size_gb = %.7[6]f + zone_redundant = %[9]t + + maintenance_configuration_name = "%[10]s" + + sku { + name = "%[3]s" + tier = "%[4]s" + capacity = %[5]d + } + + per_database_settings { + min_capacity = %[7]d + max_capacity = %[8]d + } +} +`, data.RandomInteger, data.Locations.Primary, skuName, skuTier, skuCapacity, maxSizeGB, databaseSettingsMin, databaseSettingsMax, zoneRedundant, configName) +} + func (MsSqlElasticPoolResource) templateVCore(data acceptance.TestData, skuName string, skuTier string, skuCapacity int, skuFamily string, databaseSettingsMin float64, databaseSettingsMax float64) string { return fmt.Sprintf(` provider "azurerm" { @@ -528,6 +624,49 @@ resource "azurerm_mssql_elasticpool" "test" { `, data.RandomInteger, data.Locations.Primary, skuName, skuTier, skuCapacity, skuFamily, databaseSettingsMin, databaseSettingsMax) } +func (MsSqlElasticPoolResource) noLicenseType(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%[1]d" + location = "%[2]s" +} + +resource "azurerm_mssql_server" "test" { + name = "acctest%[1]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + version = "12.0" + administrator_login = "4dm1n157r470r" + administrator_login_password = "4-v3ry-53cr37-p455w0rd" +} + +resource "azurerm_mssql_elasticpool" "test" { + name = "acctest-pool-dtu-%[1]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + server_name = azurerm_mssql_server.test.name + max_size_gb = 50 + zone_redundant = false + + sku { + name = "GP_Gen5" + tier = "GeneralPurpose" + capacity = 4 + family = "Gen5" + } + + per_database_settings { + min_capacity = 0 + max_capacity = 4 + } +} +`, data.RandomInteger, data.Locations.Primary) +} + func (MsSqlElasticPoolResource) licenseType(data acceptance.TestData, licenseType string) string { return fmt.Sprintf(` provider "azurerm" { @@ -568,7 +707,6 @@ resource "azurerm_mssql_elasticpool" "test" { min_capacity = 0 max_capacity = 4 } - } `, data.RandomInteger, data.Locations.Primary, licenseType) } diff --git a/website/docs/r/mssql_elasticpool.html.markdown b/website/docs/r/mssql_elasticpool.html.markdown index 005b560f1d32..dbcb7b82712b 100644 --- a/website/docs/r/mssql_elasticpool.html.markdown +++ b/website/docs/r/mssql_elasticpool.html.markdown @@ -79,8 +79,6 @@ The following arguments are supported: * `license_type` - (Optional) Specifies the license type applied to this database. Possible values are `LicenseIncluded` and `BasePrice`. --> **Note:** `license_type` can only be configured when `sku.0.tier` is set to `GeneralPurpose`, `Hyperscale` or `BusinessCritical` - --- The `sku` block supports the following: