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

mssql - fix Long Term Retention not supported for SQL database with Free edition #26894

Merged
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
13 changes: 8 additions & 5 deletions internal/services/mssql/mssql_database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
}
}
Expand Down
27 changes: 27 additions & 0 deletions internal/services/mssql/mssql_database_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down
Loading