From 546b86507695d4f5f1218ab8342c446a5337c3a2 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Wed, 29 Mar 2023 10:18:38 -0700 Subject: [PATCH] `azurerm_mssql_database` - HyperScale Skus now support `long_term_retention_policy` and `short_term_retention_policy` (#21166) --- .../mssql/helper/sql_retention_policies.go | 7 ++ .../services/mssql/mssql_database_resource.go | 17 +++-- .../mssql/mssql_database_resource_test.go | 76 +++++++++++++++++++ 3 files changed, 93 insertions(+), 7 deletions(-) diff --git a/internal/services/mssql/helper/sql_retention_policies.go b/internal/services/mssql/helper/sql_retention_policies.go index d7fd09c13eb1..9794776f11de 100644 --- a/internal/services/mssql/helper/sql_retention_policies.go +++ b/internal/services/mssql/helper/sql_retention_policies.go @@ -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" @@ -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 + }, }, }, }, diff --git a/internal/services/mssql/mssql_database_resource.go b/internal/services/mssql/mssql_database_resource.go index 8518df8cc91c..438171abd742 100644 --- a/internal/services/mssql/mssql_database_resource.go +++ b/internal/services/mssql/mssql_database_resource.go @@ -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 } @@ -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 { @@ -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) @@ -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) diff --git a/internal/services/mssql/mssql_database_resource_test.go b/internal/services/mssql/mssql_database_resource_test.go index 94cf5c116c9b..c02f4e1c7a5b 100644 --- a/internal/services/mssql/mssql_database_resource_test.go +++ b/internal/services/mssql/mssql_database_resource_test.go @@ -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{} @@ -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) }