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_recovery_services_vault: update encryptionbefore update storageCfg #23618

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -258,6 +258,19 @@ func resourceRecoveryServicesVaultCreate(d *pluginsdk.ResourceData, meta interfa
return fmt.Errorf("creating %s: %+v", id.String(), err)
}

// `encryption` needs to be set before `cross_region_restore_enabled` is set. Or the service will return an error. "If CRR is enabled for the Vault, the storage state will be locked and it will interfere with further operations"
// recovery vault's encryption config cannot be set while creation, so a standalone update is required.
if _, ok := d.GetOk("encryption"); ok {
err = client.UpdateThenPoll(ctx, id, vaults.PatchVault{
Properties: &vaults.VaultProperties{
Encryption: expandEncryption(d),
},
})
if err != nil {
return fmt.Errorf("updating Recovery Service Encryption %s: %+v, but recovery vault was created, a manually import might be required", id.String(), err)
}
}

storageType := backupresourcestorageconfigsnoncrr.StorageType(d.Get("storage_mode_type").(string))
storageCfg := backupresourcestorageconfigsnoncrr.BackupResourceConfigResource{
Properties: &backupresourcestorageconfigsnoncrr.BackupResourceConfig{
Expand Down Expand Up @@ -305,18 +318,6 @@ func resourceRecoveryServicesVaultCreate(d *pluginsdk.ResourceData, meta interfa
return fmt.Errorf("creating %s: %+v", id, err)
}

// recovery vault's encryption config cannot be set while creation, so a standalone update is required.
if _, ok := d.GetOk("encryption"); ok {
err = client.UpdateThenPoll(ctx, id, vaults.PatchVault{
Properties: &vaults.VaultProperties{
Encryption: expandEncryption(d),
},
})
if err != nil {
return fmt.Errorf("updating Recovery Service Encryption %s: %+v, but recovery vault was created, a manually import might be required", id.String(), err)
}
}

// an update on the vault will reset the vault config to default, so we handle it at last.
enhancedSecurityState := backupresourcevaultconfigs.EnhancedSecurityStateEnabled
cfg := backupresourcevaultconfigs.BackupResourceVaultConfigResource{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,21 @@ func TestAccRecoveryServicesVault_crossRegionRestore(t *testing.T) {
})
}

func TestAccRecoveryServicesVault_CrossRegionRestoreWithEncryption(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_recovery_services_vault", "test")
r := RecoveryServicesVaultResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.crossRegionRestoreEnabledWithEncryption(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccRecoveryServicesVault_sku(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_recovery_services_vault", "test")
r := RecoveryServicesVaultResource{}
Expand Down Expand Up @@ -1169,6 +1184,95 @@ resource "azurerm_recovery_services_vault" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (RecoveryServicesVaultResource) crossRegionRestoreEnabledWithEncryption(data acceptance.TestData) string {

return fmt.Sprintf(`
provider "azurerm" {
features {
key_vault {
purge_soft_delete_on_destroy = true
purge_soft_deleted_keys_on_destroy = false
}
}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-recovery-%[1]d"
location = "%[2]s"
}

data "azurerm_client_config" "current" {}

resource "azurerm_key_vault" "test" {
name = "acctest-key-vault-%[3]s"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
enabled_for_disk_encryption = true
tenant_id = data.azurerm_client_config.current.tenant_id
soft_delete_retention_days = 7
purge_protection_enabled = false

sku_name = "standard"

access_policy {
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id

key_permissions = [
"Create",
"Decrypt",
"Encrypt",
"Delete",
"Get",
"List",
"Purge",
"UnwrapKey",
"WrapKey",
"Verify",
"GetRotationPolicy"
]
secret_permissions = [
"Set",
]
}
}

resource "azurerm_key_vault_key" "test" {
name = "acctest-key-vault-key"
key_vault_id = azurerm_key_vault.test.id
key_type = "RSA"
key_size = 2048

key_opts = [
"decrypt",
"encrypt",
"sign",
"unwrapKey",
"verify",
"wrapKey",
]
}

resource "azurerm_recovery_services_vault" "test" {
name = "acctest-Vault-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "Standard"

cross_region_restore_enabled = true

encryption {
key_id = azurerm_key_vault_key.test.id
infrastructure_encryption_enabled = false
}

identity {
type = "SystemAssigned"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func (RecoveryServicesVaultResource) storageModeTypeDefault(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
Loading