diff --git a/internal/services/mssql/mssql_database_resource.go b/internal/services/mssql/mssql_database_resource.go index 8f93af652b5a..f21eb138f4ca 100644 --- a/internal/services/mssql/mssql_database_resource.go +++ b/internal/services/mssql/mssql_database_resource.go @@ -179,6 +179,8 @@ func resourceMsSqlDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface } } + ledgerEnabled := d.Get("ledger_enabled").(bool) + // When databases are replicating, the primary cannot have a SKU belonging to a higher service tier than any of its // partner databases. To work around this, we'll try to identify any partner databases that are secondary to this // database, and where the new SKU tier for this database is going to be higher, first upgrade those databases to @@ -249,6 +251,7 @@ func resourceMsSqlDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface SampleName: sql.SampleName(d.Get("sample_name").(string)), RequestedBackupStorageRedundancy: expandMsSqlBackupStorageRedundancy(d.Get("storage_account_type").(string)), ZoneRedundant: utils.Bool(d.Get("zone_redundant").(bool)), + IsLedgerOn: utils.Bool(ledgerEnabled), }, Tags: tags.Expand(d.Get("tags").(map[string]interface{})), @@ -435,7 +438,7 @@ func resourceMsSqlDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface return nil } - if d.HasChange("long_term_retention_policy") { + if d.HasChange("long_term_retention_policy") && !ledgerEnabled { v := d.Get("long_term_retention_policy") longTermRetentionProps := helper.ExpandLongTermRetentionPolicy(v.([]interface{})) if longTermRetentionProps != nil { @@ -513,6 +516,7 @@ func resourceMsSqlDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) erro d.Set("server_id", serverId.ID()) skuName := "" + ledgerEnabled := false if props := resp.DatabaseProperties; props != nil { d.Set("auto_pause_delay_in_minutes", props.AutoPauseDelay) d.Set("collation", props.Collation) @@ -534,6 +538,10 @@ func resourceMsSqlDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) erro d.Set("sku_name", skuName) d.Set("storage_account_type", flattenMsSqlBackupStorageRedundancy(props.CurrentBackupStorageRedundancy)) d.Set("zone_redundant", props.ZoneRedundant) + if props.IsLedgerOn != nil { + ledgerEnabled = *props.IsLedgerOn + } + d.Set("ledger_enabled", ledgerEnabled) } securityAlertPolicy, err := securityAlertPoliciesClient.Get(ctx, id.ResourceGroup, id.ServerName, id.Name) @@ -546,7 +554,7 @@ 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") { + if !strings.HasPrefix(skuName, "HS") && !strings.HasPrefix(skuName, "DW") && !ledgerEnabled { 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) @@ -1011,6 +1019,14 @@ func resourceMsSqlDatabaseSchema() map[string]*pluginsdk.Schema { Optional: true, Default: true, }, + + "ledger_enabled": { + Type: pluginsdk.TypeBool, + Optional: true, + Computed: true, + ForceNew: true, + }, + "tags": tags.Schema(), } diff --git a/internal/services/mssql/mssql_database_resource_test.go b/internal/services/mssql/mssql_database_resource_test.go index bb6f38576e0b..c9a83260daa4 100644 --- a/internal/services/mssql/mssql_database_resource_test.go +++ b/internal/services/mssql/mssql_database_resource_test.go @@ -665,6 +665,21 @@ func TestAccMsSqlDatabase_errorOnDisabledEncryption(t *testing.T) { }) } +func TestAccMsSqlDatabase_ledgerEnabled(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_mssql_database", "test") + r := MsSqlDatabaseResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.ledgerEnabled(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 := parse.DatabaseID(state.ID) if err != nil { @@ -1479,3 +1494,15 @@ resource "azurerm_mssql_database" "test" { } `, r.template(data), data.RandomInteger) } + +func (r MsSqlDatabaseResource) ledgerEnabled(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 + ledger_enabled = true +} +`, r.template(data), data.RandomInteger) +} diff --git a/website/docs/r/mssql_database.html.markdown b/website/docs/r/mssql_database.html.markdown index 3533438de87c..08f727fe03ce 100644 --- a/website/docs/r/mssql_database.html.markdown +++ b/website/docs/r/mssql_database.html.markdown @@ -94,6 +94,8 @@ The following arguments are supported: ~> **Note:** `geo_backup_enabled` is only applicable for DataWarehouse SKUs (DW*). This setting is ignored for all other SKUs. +* `ledger_enabled` - (Optional) A boolean that specifies if this is a ledger database. Defaults to `false`. Changing this forces a new resource to be created. + * `license_type` - (Optional) Specifies the license type applied to this database. Possible values are `LicenseIncluded` and `BasePrice`. * `long_term_retention_policy` - (Optional) A `long_term_retention_policy` block as defined below.