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

azuread_group: fix for issue that prevents replacing a group when prevent_duplicate_names = true #980

Merged
merged 1 commit into from
Jan 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
2 changes: 1 addition & 1 deletion internal/services/groups/group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func groupResourceCustomizeDiff(ctx context.Context, diff *schema.ResourceDiff,

// Check for duplicate names
oldDisplayName, newDisplayName := diff.GetChange("display_name")
if diff.Get("prevent_duplicate_names").(bool) && tf.ValueIsNotEmptyOrUnknown(newDisplayName) &&
if tf.ValueIsNotEmptyOrUnknown(diff.Id()) && diff.Get("prevent_duplicate_names").(bool) && tf.ValueIsNotEmptyOrUnknown(newDisplayName) &&
(oldDisplayName.(string) == "" || oldDisplayName.(string) != newDisplayName.(string)) {
result, err := groupFindByName(ctx, client, newDisplayName.(string))
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions internal/services/groups/group_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func TestAccGroup_update(t *testing.T) {
r := GroupResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.unified(data),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -340,6 +347,27 @@ func TestAccGroup_preventDuplicateNamesFail(t *testing.T) {
})
}

func TestAccGroup_preventDuplicateNamesForceNew(t *testing.T) {
data := acceptance.BuildTestData(t, "azuread_group", "test")
r := GroupResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
{
Config: r.preventDuplicateNamesForceNew(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("display_name").HasValue(fmt.Sprintf("acctestGroup-%d", data.RandomInteger)),
),
},
data.ImportStep("prevent_duplicate_names"),
})
}

func TestAccGroup_provisioning(t *testing.T) {
data := acceptance.BuildTestData(t, "azuread_group", "test")
r := GroupResource{}
Expand Down Expand Up @@ -885,3 +913,15 @@ resource "azuread_group" "duplicate" {
}
`, r.basic(data))
}

func (GroupResource) preventDuplicateNamesForceNew(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azuread_group" "test" {
display_name = "acctestGroup-%[1]d"
security_enabled = true
prevent_duplicate_names = true

assignable_to_role = true
}
`, data.RandomInteger)
}