From add8962ba9f9856c15a81223b98ac9629c297b4f Mon Sep 17 00:00:00 2001 From: Chris Dituri Date: Wed, 31 Jul 2024 13:24:09 -0500 Subject: [PATCH] Long Term Retention is not supported for SQL database with Free edition Fixes the deployment of SQL Database Free tier by skipping Long-Term and Short-Term Retention Policies, which are not supported on the Free tier. --- .../services/mssql/mssql_database_resource.go | 13 +++++---- .../mssql/mssql_database_resource_test.go | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/internal/services/mssql/mssql_database_resource.go b/internal/services/mssql/mssql_database_resource.go index 6b6075a10875..0e851a3fafab 100644 --- a/internal/services/mssql/mssql_database_resource.go +++ b/internal/services/mssql/mssql_database_resource.go @@ -1207,8 +1207,11 @@ func resourceMsSqlDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) erro // Determine whether the SKU is for SQL Data Warehouse isDwSku := strings.HasPrefix(strings.ToLower(skuName), "dw") - // DW SKUs do not currently support LRP and do not honour normal SRP operations - if !isDwSku { + // Determine whether the SKU is for SQL Database Free tier + isFreeSku := strings.EqualFold(skuName, "free") + + // DW SKUs and SQL Database Free tier do not currently support LRP and do not honour normal SRP operations + if !isDwSku && !isFreeSku { longTermPolicy, err := longTermRetentionClient.Get(ctx, pointer.From(id)) if err != nil { return fmt.Errorf("retrieving Long Term Retention Policies for %s: %+v", id, err) @@ -1231,7 +1234,7 @@ func resourceMsSqlDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) erro } } } else { - // DW SKUs need the retention policies to be empty for state consistency + // DW SKUs and SQL Database Free tier need the retention policies to be empty for state consistency emptySlice := make([]interface{}, 0) d.Set("long_term_retention_policy", emptySlice) d.Set("short_term_retention_policy", emptySlice) @@ -1246,9 +1249,9 @@ func resourceMsSqlDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) erro return fmt.Errorf("retrieving Geo Backup Policies for %s: %+v", id, err) } - // For Datawarehouse SKUs, set the geo-backup policy setting + // For Datawarehouse SKUs and SQL Database Free tier, set the geo-backup policy setting if geoPolicyModel := geoPoliciesResponse.Model; geoPolicyModel != nil { - if isDwSku && geoPolicyModel.Properties.State == geobackuppolicies.GeoBackupPolicyStateDisabled { + if (isDwSku || isFreeSku) && geoPolicyModel.Properties.State == geobackuppolicies.GeoBackupPolicyStateDisabled { geoBackupPolicy = false } } diff --git a/internal/services/mssql/mssql_database_resource_test.go b/internal/services/mssql/mssql_database_resource_test.go index 1bc2812af45a..9e1a318802a2 100644 --- a/internal/services/mssql/mssql_database_resource_test.go +++ b/internal/services/mssql/mssql_database_resource_test.go @@ -38,6 +38,21 @@ func TestAccMsSqlDatabase_basic(t *testing.T) { }) } +func TestAccMsSqlDatabase_free(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_mssql_database", "test") + r := MsSqlDatabaseResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.freeTier(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func TestAccMsSqlDatabase_requiresImport(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_mssql_database", "test") r := MsSqlDatabaseResource{} @@ -1042,6 +1057,18 @@ resource "azurerm_mssql_database" "test" { `, r.template(data), data.RandomInteger) } +func (r MsSqlDatabaseResource) freeTier(data acceptance.TestData) string { + return fmt.Sprintf(` +%[1]s + +resource "azurerm_mssql_database" "test" { + name = "acctest-db-%[2]d" + server_id = azurerm_mssql_server.test.id + sku_name = "Free" +} +`, r.template(data), data.RandomInteger) +} + func (r MsSqlDatabaseResource) requiresImport(data acceptance.TestData) string { return fmt.Sprintf(` %[1]s