Skip to content

Commit

Permalink
azurerm_mssql_database - HyperScale Skus now support `long_term_ret…
Browse files Browse the repository at this point in the history
…ention_policy` and `short_term_retention_policy` (#21166)
  • Loading branch information
mbfrahry authored Mar 29, 2023
1 parent 07d4d97 commit 546b865
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
7 changes: 7 additions & 0 deletions internal/services/mssql/helper/sql_retention_policies.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package helper

import (
"strconv"

"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v5.0/sql" // nolint: staticcheck
"github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -75,6 +77,11 @@ func ShortTermRetentionPolicySchema() *pluginsdk.Schema {
Optional: true,
ValidateFunc: validation.IntInSlice([]int{12, 24}),
Default: 12,
// HyperScale SKus can't set `backup_interval_in_hours so we'll ignore that value when it is 0 in the state file so we don't break the Default Value for existing users
DiffSuppressFunc: func(_, old, _ string, d *pluginsdk.ResourceData) bool {
oldInt, _ := strconv.Atoi(old)
return oldInt == 0
},
},
},
},
Expand Down
17 changes: 10 additions & 7 deletions internal/services/mssql/mssql_database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,14 @@ func resourceMsSqlDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface
return nil
}

if d.HasChange("long_term_retention_policy") && !ledgerEnabled {
if d.HasChange("long_term_retention_policy") {
v := d.Get("long_term_retention_policy")
longTermRetentionProps := helper.ExpandLongTermRetentionPolicy(v.([]interface{}))
if longTermRetentionProps != nil {
longTermRetentionPolicy := sql.LongTermRetentionPolicy{}

// hyper-scale SKU's do not support LRP currently
if !strings.HasPrefix(skuName.(string), "HS") && !strings.HasPrefix(skuName.(string), "DW") {
// DataWarehouse SKU's do not support LRP currently
if !strings.HasPrefix(skuName.(string), "DW") {
longTermRetentionPolicy.BaseLongTermRetentionPolicyProperties = longTermRetentionProps
}

Expand All @@ -472,9 +472,12 @@ func resourceMsSqlDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface
if backupShortTermPolicyProps != nil {
backupShortTermPolicy := sql.BackupShortTermRetentionPolicy{}

if !strings.HasPrefix(skuName.(string), "HS") && !strings.HasPrefix(skuName.(string), "DW") {
if !strings.HasPrefix(skuName.(string), "DW") {
backupShortTermPolicy.BackupShortTermRetentionPolicyProperties = backupShortTermPolicyProps
}
if strings.HasPrefix(skuName.(string), "HS") {
backupShortTermPolicy.BackupShortTermRetentionPolicyProperties.DiffBackupIntervalInHours = nil
}

shortTermRetentionFuture, err := shortTermRetentionClient.CreateOrUpdate(ctx, id.ResourceGroup, id.ServerName, id.Name, backupShortTermPolicy)
if err != nil {
Expand Down Expand Up @@ -575,8 +578,8 @@ func resourceMsSqlDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) erro

geoBackupPolicy := true

// Hyper Scale SKU's do not currently support LRP and do not honour normal SRP operations
if !strings.HasPrefix(skuName, "HS") && !strings.HasPrefix(skuName, "DW") && !ledgerEnabled {
// DW SKU's do not currently support LRP and do not honour normal SRP operations
if !strings.HasPrefix(skuName, "DW") {
longTermPolicy, err := longTermRetentionClient.Get(ctx, id.ResourceGroup, id.ServerName, id.Name)
if err != nil {
return fmt.Errorf("retrieving Long Term Retention Policies for %s: %+v", id, err)
Expand All @@ -594,7 +597,7 @@ func resourceMsSqlDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) erro
return fmt.Errorf("setting `short_term_retention_policy`: %+v", err)
}
} else {
// HS and DW SKUs need the retention policies zeroing for state consistency
// DW SKUs need the retention policies zeroing for state consistency
zero := make([]interface{}, 0)
d.Set("long_term_retention_policy", zero)
d.Set("short_term_retention_policy", zero)
Expand Down
76 changes: 76 additions & 0 deletions internal/services/mssql/mssql_database_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,32 @@ func TestAccMsSqlDatabase_HS(t *testing.T) {
})
}

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

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.hsWithRetentionPolicy(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("read_replica_count").HasValue("2"),
check.That(data.ResourceName).Key("sku_name").HasValue("HS_Gen5_2"),
),
},
data.ImportStep(),
{
Config: r.hsWithRetentionPolicyUpdate(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("read_replica_count").HasValue("4"),
check.That(data.ResourceName).Key("sku_name").HasValue("HS_Gen5_2"),
),
},
data.ImportStep(),
})
}

func TestAccMsSqlDatabase_createCopyMode(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_database", "copy")
r := MsSqlDatabaseResource{}
Expand Down Expand Up @@ -982,6 +1008,56 @@ resource "azurerm_mssql_database" "test" {
server_id = azurerm_mssql_server.test.id
read_replica_count = 4
sku_name = "HS_Gen5_2"
}
`, r.template(data), data.RandomInteger)
}

func (r MsSqlDatabaseResource) hsWithRetentionPolicy(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
read_replica_count = 2
sku_name = "HS_Gen5_2"
long_term_retention_policy {
weekly_retention = "P1W"
monthly_retention = "P1M"
yearly_retention = "P1Y"
week_of_year = 1
}
short_term_retention_policy {
retention_days = 10
}
}
`, r.template(data), data.RandomInteger)
}

func (r MsSqlDatabaseResource) hsWithRetentionPolicyUpdate(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
read_replica_count = 4
sku_name = "HS_Gen5_2"
long_term_retention_policy {
weekly_retention = "P1W"
monthly_retention = "P1M"
yearly_retention = "P1Y"
week_of_year = 2
}
short_term_retention_policy {
retention_days = 12
}
}
`, r.template(data), data.RandomInteger)
}
Expand Down

0 comments on commit 546b865

Please sign in to comment.