Skip to content

Commit

Permalink
azurerm_mssql_database - set `short_term_retention_policy.backup_in…
Browse files Browse the repository at this point in the history
…terval_in_hours` to nil when elastic pool is hyperscale (#27505)

* set intervalhours to nil if HS elastic pool is used

* linting

* fix typo

* remove extra nil check

* fix nil check
  • Loading branch information
catriona-m authored Oct 4, 2024
1 parent ef0d055 commit bb5f526
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
19 changes: 13 additions & 6 deletions internal/services/mssql/mssql_database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func resourceMsSqlDatabaseCreate(d *pluginsdk.ResourceData, meta interface{}) er
// NOTE: If the database is being added to an elastic pool, we need to GET the elastic pool and check
// if the 'enclave_type' matches. If they don't we need to raise an error stating that they must match.
elasticPoolId := d.Get("elastic_pool_id").(string)
elasticPoolSku := ""
if elasticPoolId != "" {
elasticId, err := commonids.ParseSqlElasticPoolID(elasticPoolId)
if err != nil {
Expand All @@ -288,12 +289,18 @@ func resourceMsSqlDatabaseCreate(d *pluginsdk.ResourceData, meta interface{}) er
return fmt.Errorf("retrieving %s: %s", elasticId, err)
}

if elasticPool.Model != nil && elasticPool.Model.Properties != nil && elasticPool.Model.Properties.PreferredEnclaveType != nil {
elasticEnclaveType := string(pointer.From(elasticPool.Model.Properties.PreferredEnclaveType))
databaseEnclaveType := string(enclaveType)
if elasticPool.Model != nil {
if elasticPool.Model.Properties != nil && elasticPool.Model.Properties.PreferredEnclaveType != nil {
elasticEnclaveType := string(pointer.From(elasticPool.Model.Properties.PreferredEnclaveType))
databaseEnclaveType := string(enclaveType)

if !strings.EqualFold(elasticEnclaveType, databaseEnclaveType) {
return fmt.Errorf("adding the %s with enclave type %q to the %s with enclave type %q is not supported. Before adding a database to an elastic pool please ensure that the 'enclave_type' is the same for both the database and the elastic pool", id, databaseEnclaveType, elasticId, elasticEnclaveType)
if !strings.EqualFold(elasticEnclaveType, databaseEnclaveType) {
return fmt.Errorf("adding the %s with enclave type %q to the %s with enclave type %q is not supported. Before adding a database to an elastic pool please ensure that the 'enclave_type' is the same for both the database and the elastic pool", id, databaseEnclaveType, elasticId, elasticEnclaveType)
}
}

if elasticPool.Model.Sku != nil {
elasticPoolSku = elasticPool.Model.Sku.Name
}
}
}
Expand Down Expand Up @@ -613,7 +620,7 @@ func resourceMsSqlDatabaseCreate(d *pluginsdk.ResourceData, meta interface{}) er
securityAlertPolicyPayload.Properties = shortTermSecurityAlertPolicyProps
}

if strings.HasPrefix(skuName, "HS") {
if strings.HasPrefix(skuName, "HS") || strings.HasPrefix(elasticPoolSku, "HS") {
securityAlertPolicyPayload.Properties.DiffBackupIntervalInHours = nil
}

Expand Down
47 changes: 47 additions & 0 deletions internal/services/mssql/mssql_database_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,21 @@ func TestAccMsSqlDatabase_namedReplicationZoneRedundant(t *testing.T) {
})
}

func TestAccMsSqlDatabase_elasticPoolHS(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_database", "test")
r := MsSqlDatabaseResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.elasticPoolHS(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (MsSqlDatabaseResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := commonids.ParseSqlDatabaseID(state.ID)
if err != nil {
Expand Down Expand Up @@ -2243,3 +2258,35 @@ resource "azurerm_mssql_database" "secondary" {
}
`, r.template(data), data.RandomInteger)
}

func (r MsSqlDatabaseResource) elasticPoolHS(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
resource "azurerm_mssql_elasticpool" "test" {
name = "acctest-pool-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
server_name = azurerm_mssql_server.test.name
sku {
name = "HS_Gen5"
tier = "Hyperscale"
family = "Gen5"
capacity = 4
}
per_database_settings {
min_capacity = 0.25
max_capacity = 4
}
}
resource "azurerm_mssql_database" "test" {
name = "acctest-db-%[2]d"
server_id = azurerm_mssql_server.test.id
elastic_pool_id = azurerm_mssql_elasticpool.test.id
sku_name = "ElasticPool"
}
`, r.template(data), data.RandomInteger)
}

0 comments on commit bb5f526

Please sign in to comment.