From cff5cffc4ccb22f922bce8f4d6203701d3996f23 Mon Sep 17 00:00:00 2001 From: atoni <50518795+bosc0@users.noreply.github.com> Date: Thu, 10 Apr 2025 14:59:35 +0200 Subject: [PATCH] fix: return all conflicts instead of only the first one --- internal/provider/resource_usergroup.go | 2 +- internal/provider/usergroup_service.go | 36 ++++++++++++++----------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/internal/provider/resource_usergroup.go b/internal/provider/resource_usergroup.go index 806042e..ef4648c 100644 --- a/internal/provider/resource_usergroup.go +++ b/internal/provider/resource_usergroup.go @@ -158,7 +158,7 @@ func (r *UserGroupResource) ModifyPlan(ctx context.Context, req resource.ModifyP if err != nil { resp.Diagnostics.AddError( "Conflict", - fmt.Sprintf("PreventConflicts = true: %v", err), + fmt.Sprintf("PreventConflicts = true:\n%v", err), ) } } diff --git a/internal/provider/usergroup_service.go b/internal/provider/usergroup_service.go index cef55c1..f8addfa 100644 --- a/internal/provider/usergroup_service.go +++ b/internal/provider/usergroup_service.go @@ -143,25 +143,31 @@ func (s *userGroupServiceImpl) DeleteGroup(ctx context.Context, id string) error } func (s *userGroupServiceImpl) CheckConflicts(ctx context.Context, resourceID, name, handle string, includeDisabled bool) error { - existingByName, errNameLookup := s.queries.FindUserGroupByField(ctx, "name", name, includeDisabled) - if errNameLookup == nil { - if existingByName.ID != resourceID { - return fmt.Errorf("conflict: existing enabled group with same name\nAn enabled user group named %q already exists (ID: %s)", existingByName.Name, existingByName.ID) + conflict := func(field, val string) error { + existing, errLookup := s.queries.FindUserGroupByField(ctx, field, val, includeDisabled) + if errLookup != nil { + if !strings.Contains(errLookup.Error(), "no usergroup with "+field) { + return fmt.Errorf("error checking %s conflict\n%s", field, errLookup.Error()) + } + return nil } - } else if !strings.Contains(errNameLookup.Error(), "no usergroup with name") { - return fmt.Errorf("error checking name conflict\n%s", errNameLookup.Error()) - } - - existingByHandle, errHandleLookup := s.queries.FindUserGroupByField(ctx, "handle", handle, includeDisabled) - if errHandleLookup == nil { - if existingByHandle.ID != resourceID { - return fmt.Errorf("conflict: existing enabled group with same handle\nAn enabled user group with handle %q already exists (ID: %s)", existingByHandle.Handle, existingByHandle.ID) + if existing.ID != resourceID { + return fmt.Errorf("conflict: existing usergroup with same %s\nA usergroup with %s %q already exists (ID: %s)", field, field, val, existing.ID) } - } else if !strings.Contains(errHandleLookup.Error(), "no usergroup with handle") { - return fmt.Errorf("error checking handle conflict\n%s", errHandleLookup.Error()) + return nil } - return nil + var errs []string + if err := conflict("name", name); err != nil { + errs = append(errs, err.Error()) + } + if err := conflict("handle", handle); err != nil { + errs = append(errs, err.Error()) + } + if len(errs) == 0 { + return nil + } + return fmt.Errorf("%s", strings.Join(errs, "\n\n")) } func (s *userGroupServiceImpl) UpdateUserGroupMembership(ctx context.Context, groupID string, users []string) error {