Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_mssql_database - set short_term_retention_policy.backup_interval_in_hours to nil when elastic pool is hyperscale #27505

Merged
merged 5 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
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)
}
Loading