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_disk_encryption_set - Support updating identity #23904

Merged
merged 1 commit into from
Nov 17, 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
17 changes: 17 additions & 0 deletions internal/services/compute/disk_encryption_set_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ func resourceDiskEncryptionSet() *pluginsdk.Resource {
Computed: true,
},
},

CustomizeDiff: pluginsdk.CustomDiffWithAll(
pluginsdk.ForceNewIfChange("identity.0.type", func(ctx context.Context, old, new, meta interface{}) bool {
// cannot change identity type from userAssigned to systemAssigned
return (old.(string) == string(identity.TypeUserAssigned) || old.(string) == string(identity.TypeSystemAssignedUserAssigned)) && (new.(string) == string(identity.TypeSystemAssigned))
}),
),
}
}

Expand Down Expand Up @@ -316,6 +323,16 @@ func resourceDiskEncryptionSetUpdate(d *pluginsdk.ResourceData, meta interface{}
}

update := diskencryptionsets.DiskEncryptionSetUpdate{}

if d.HasChange("identity") {
expandedIdentity, err := expandDiskEncryptionSetIdentity(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

update.Identity = expandedIdentity
}

if d.HasChange("tags") {
update.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
}
Expand Down
62 changes: 62 additions & 0 deletions internal/services/compute/disk_encryption_set_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ func TestAccDiskEncryptionSet_systemAssignedUserAssignedIdentity(t *testing.T) {
})
}

func TestAccDiskEncryptionSet_updateIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_disk_encryption_set", "test")
r := DiskEncryptionSetResource{}

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

func TestAccDiskEncryptionSet_withFederatedClientId(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_disk_encryption_set", "test")
r := DiskEncryptionSetResource{}
Expand Down Expand Up @@ -508,6 +530,46 @@ resource "azurerm_disk_encryption_set" "test" {
`, r.systemAssignedDependencies(data), data.RandomInteger)
}

func (r DiskEncryptionSetResource) systemAssignedIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_user_assigned_identity" "test" {
name = "acctestUAI-%[2]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_key_vault_access_policy" "user-assigned" {
key_vault_id = azurerm_key_vault.test.id
tenant_id = azurerm_user_assigned_identity.test.tenant_id
object_id = azurerm_user_assigned_identity.test.principal_id

key_permissions = [
"Get",
"WrapKey",
"UnwrapKey",
"GetRotationPolicy",
]

depends_on = [azurerm_key_vault_access_policy.service-principal]
}

resource "azurerm_disk_encryption_set" "test" {
name = "acctestDES-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
key_vault_key_id = azurerm_key_vault_key.test.id

identity {
type = "SystemAssigned"
}

depends_on = [azurerm_key_vault_access_policy.user-assigned]
}
`, r.dependencies(data, true), data.RandomInteger)
}

func (r DiskEncryptionSetResource) userAssignedIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand Down
Loading