Skip to content

Commit

Permalink
azurerm_mssql_database - support for property ledger_enabled (#16214
Browse files Browse the repository at this point in the history
)

* add new property ledger_enabled

* add ledger_enabled to docs

* linting

* add forcenew to ledger_enabled
  • Loading branch information
catriona-m authored Apr 6, 2022
1 parent 092b075 commit 184afa5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
20 changes: 18 additions & 2 deletions internal/services/mssql/mssql_database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{})),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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(),
}

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 @@ -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 {
Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions website/docs/r/mssql_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 184afa5

Please sign in to comment.