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_data_protection_backup_vault - support for immutability property #27859

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
)

func resourceDataProtectionBackupVault() *pluginsdk.Resource {
resource := &pluginsdk.Resource{
return &pluginsdk.Resource{
Create: resourceDataProtectionBackupVaultCreateUpdate,
Read: resourceDataProtectionBackupVaultRead,
Update: resourceDataProtectionBackupVaultCreateUpdate,
Expand Down Expand Up @@ -86,8 +86,6 @@
Optional: true,
},

"identity": commonschema.SystemAssignedIdentityOptional(),

"retention_duration_in_days": {
Type: pluginsdk.TypeFloat,
Optional: true,
Expand All @@ -102,19 +100,34 @@
ValidateFunc: validation.StringInSlice(backupvaults.PossibleValuesForSoftDeleteState(), false),
},

"immutability": {
Type: pluginsdk.TypeString,
Optional: true,
Default: false,
katbyte marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: validation.StringInSlice(backupvaults.PossibleValuesForImmutabilityState(), false),
katbyte marked this conversation as resolved.
Show resolved Hide resolved
},

"identity": commonschema.SystemAssignedIdentityOptional(),

"tags": tags.Schema(),
},

CustomizeDiff: pluginsdk.CustomDiffWithAll(
pluginsdk.ForceNewIfChange("soft_delete", func(ctx context.Context, old, new, meta interface{}) bool {
return old.(string) == string(backupvaults.SoftDeleteStateAlwaysOn) && new.(string) != string(backupvaults.SoftDeleteStateAlwaysOn)
}),

// Once `cross_region_restore_enabled` is enabled it cannot be disabled.
// Once `immutability` is enabled it cannot be disabled.
pluginsdk.ForceNewIfChange("cross_region_restore_enabled", func(ctx context.Context, old, new, meta interface{}) bool {
return old.(bool) && new.(bool) != old.(bool)
}),

// Once `cross_region_restore_enabled` is enabled it cannot be disabled.
pluginsdk.ForceNewIfChange("immutability", func(ctx context.Context, old, new, meta interface{}) bool {
return old.(string) == string(backupvaults.ImmutabilityStateLocked) && new.(string) != string(backupvaults.ImmutabilityStateLocked)
}),

pluginsdk.ForceNewIfChange("soft_delete", func(ctx context.Context, old, new, meta interface{}) bool {
return old.(string) == string(backupvaults.SoftDeleteStateAlwaysOn) && new.(string) != string(backupvaults.SoftDeleteStateAlwaysOn)
}),

pluginsdk.CustomizeDiffShim(func(ctx context.Context, d *pluginsdk.ResourceDiff, v interface{}) error {
redundancy := d.Get("redundancy").(string)
crossRegionRestore := d.GetRawConfig().AsValueMap()["cross_region_restore_enabled"]
Expand All @@ -126,8 +139,6 @@
}),
),
}

return resource
}

func resourceDataProtectionBackupVaultCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -158,22 +169,22 @@
return fmt.Errorf("expanding `identity`: %+v", err)
}

datastoreType := backupvaults.StorageSettingStoreTypes(d.Get("datastore_type").(string))
storageSettingType := backupvaults.StorageSettingTypes(d.Get("redundancy").(string))

parameters := backupvaults.BackupVaultResource{
Location: pointer.To(location.Normalize(d.Get("location").(string))),
Properties: backupvaults.BackupVault{
StorageSettings: []backupvaults.StorageSetting{
{
DatastoreType: &datastoreType,
Type: &storageSettingType,
DatastoreType: pointer.To(backupvaults.StorageSettingStoreTypes(d.Get("datastore_type").(string))),
Type: pointer.To(backupvaults.StorageSettingTypes(d.Get("redundancy").(string))),
},
},
SecuritySettings: &backupvaults.SecuritySettings{
SoftDeleteSettings: &backupvaults.SoftDeleteSettings{
State: pointer.To(backupvaults.SoftDeleteState(d.Get("soft_delete").(string))),
},
ImmutabilitySettings: &backupvaults.ImmutabilitySettings{
State: pointer.To(backupvaults.ImmutabilityState(d.Get("immutability").(string))),
},
},
},
Identity: expandedIdentity,
Expand Down Expand Up @@ -229,16 +240,24 @@
if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))
props := model.Properties
if len(props.StorageSettings) > 0 {

if props.StorageSettings != nil && len(props.StorageSettings) > 0 {

Check failure on line 244 in internal/services/dataprotection/data_protection_backup_vault_resource.go

View workflow job for this annotation

GitHub Actions / golint

S1009: should omit nil check; len() for []github.com/hashicorp/go-azure-sdk/resource-manager/dataprotection/2024-04-01/backupvaults.StorageSetting is defined as zero (gosimple)
d.Set("datastore_type", string(pointer.From((props.StorageSettings)[0].DatastoreType)))
d.Set("redundancy", string(pointer.From((props.StorageSettings)[0].Type)))
}

if securitySetting := model.Properties.SecuritySettings; securitySetting != nil {
if immutability := securitySetting.ImmutabilitySettings; immutability != nil {
if immutability.State != nil {
katbyte marked this conversation as resolved.
Show resolved Hide resolved
d.Set("immutability", string(pointer.From(immutability.State)))
}
}
if softDelete := securitySetting.SoftDeleteSettings; softDelete != nil {
d.Set("soft_delete", string(pointer.From(softDelete.State)))
d.Set("retention_duration_in_days", pointer.From(softDelete.RetentionDurationInDays))
}
}

crossRegionStoreEnabled := false
if featureSetting := model.Properties.FeatureSettings; featureSetting != nil {
if crossRegionRestore := featureSetting.CrossRegionRestoreSettings; crossRegionRestore != nil {
Expand All @@ -247,7 +266,6 @@
}
}
}

d.Set("cross_region_restore_enabled", crossRegionStoreEnabled)

if err = d.Set("identity", flattenBackupVaultDppIdentityDetails(model.Identity)); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,15 @@ resource "azurerm_data_protection_backup_vault" "test" {
location = azurerm_resource_group.test.location
datastore_type = "VaultStore"
redundancy = "LocallyRedundant"

identity {
type = "SystemAssigned"
}

immutability = "Disabled"
soft_delete = "Off"
retention_duration_in_days = 14

tags = {
ENV = "Test"
}
Expand All @@ -264,11 +268,15 @@ resource "azurerm_data_protection_backup_vault" "test" {
location = azurerm_resource_group.test.location
datastore_type = "VaultStore"
redundancy = "LocallyRedundant"

identity {
type = "SystemAssigned"
}

immutability = "Locked"
soft_delete = "On"
retention_duration_in_days = 15

tags = {
ENV = "Test"
}
Expand All @@ -287,6 +295,7 @@ resource "azurerm_data_protection_backup_vault" "test" {
location = azurerm_resource_group.test.location
datastore_type = "VaultStore"
redundancy = "LocallyRedundant"

tags = {
ENV = "Test"
}
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/data_protection_backup_vault.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ The following arguments are supported:

-> **Note:** The `retention_duration_in_days` is the number of days for which deleted data is retained before being permanently deleted. Retention period till 14 days are free of cost, however, retention beyond 14 days may incur additional charges. The `retention_duration_in_days` is required when the `soft_delete` is set to `On`.

* `soft_delete` - (Optional) The state of soft delete for this Backup Vault. Possible values are `AlwaysOn`, `Off` and `On`. Defaults to `On`.
* `immutability` - (Optional) The state of immutability for this Backup Vault. Possible values are `Disabled`, `Locked`, and `Unlocked`. Defaults to `Disabled`. Changing this from `Locked` to anything else forces a new Backup Vault to be created.
Copy link
Member

Choose a reason for hiding this comment

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

This is more for if we want the naming to be better but I think Unlocked isn't very clear that it's actually enabling immutability. It'd be a bit more work on our end but what are your thoughts on changing Unlocked to Enabled?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think its best if we just keep it matching the API values even if the portal uses something else here despite it being confusing


* `soft_delete` - (Optional) The state of soft delete for this Backup Vault. Possible values are `AlwaysOn`, `Off`, and `On`. Defaults to `On`.

-> **Note:** Once the `soft_delete` is set to `AlwaysOn`, the setting cannot be changed.

Expand Down
Loading