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

azurerm_mssql_database - support for property ledger_enabled #16214

Merged
merged 4 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
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,11 @@ func resourceMsSqlDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface
}
}

if d.HasChange("ledger_enabled") && !d.IsNewResource() {
return fmt.Errorf("cannot change `ledger_enabled` after initial creation")
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
}
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 +254,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 +441,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 +519,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 +541,8 @@ 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)
ledgerEnabled = *props.IsLedgerOn
catriona-m marked this conversation as resolved.
Show resolved Hide resolved
d.Set("ledger_enabled", ledgerEnabled)
}

securityAlertPolicy, err := securityAlertPoliciesClient.Get(ctx, id.ResourceGroup, id.ServerName, id.Name)
Expand All @@ -546,7 +555,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 +1020,13 @@ func resourceMsSqlDatabaseSchema() map[string]*pluginsdk.Schema {
Optional: true,
Default: true,
},

"ledger_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Computed: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is defaulted to false in the api when we don't specify it so we should be able to do that here unless you saw something different!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mbfrahry! I did try adding the default here, but this needs to be computed to cover the case where ledger_enabled is set to true and then imported.

},

"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. This cannot be changed after the database has been created. Defaults to `false`.

* `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