Skip to content

Commit

Permalink
fix #23375
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyeqf committed Oct 19, 2023
1 parent 7574d90 commit f846f2d
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 12 deletions.
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)
}

// `encription` 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"

Check failure on line 261 in internal/services/recoveryservices/recovery_services_vault_resource.go

View workflow job for this annotation

GitHub Actions / golint

`encription` is a misspelling of `encryption` (misspell)
// 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

0 comments on commit f846f2d

Please sign in to comment.