Skip to content

Commit

Permalink
Fix guard against funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jun 20, 2023
1 parent fc24db8 commit edff628
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
33 changes: 25 additions & 8 deletions internal/auth0/organization/resource_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,18 @@ func createOrganizationConnections(ctx context.Context, data *schema.ResourceDat
return diagnostics
}

var result *multierror.Error
for _, connection := range connectionsToAdd {
if err := api.Organization.AddConnection(organizationID, connection); err != nil {
result = multierror.Append(result, err)
if len(connectionsToAdd) > len(alreadyEnabledConnections.OrganizationConnections) {
var result *multierror.Error

for _, connection := range connectionsToAdd {
if err := api.Organization.AddConnection(organizationID, connection); err != nil {
result = multierror.Append(result, err)
}
}
}

if result.ErrorOrNil() != nil {
return diag.FromErr(result.ErrorOrNil())
if result.ErrorOrNil() != nil {
return diag.FromErr(result.ErrorOrNil())
}
}

return readOrganizationConnections(ctx, data, meta)
Expand Down Expand Up @@ -198,11 +201,25 @@ func guardAgainstErasingUnwantedConnections(
return nil
}

alreadyEnabledConnectionsIDs := make([]string, 0)
for _, conn := range alreadyEnabledConnections {
alreadyEnabledConnectionsIDs = append(alreadyEnabledConnectionsIDs, conn.GetConnectionID())
}

connectionIDsToAdd := make([]string, 0)
for _, conn := range connectionsToAdd {
connectionIDsToAdd = append(connectionIDsToAdd, conn.GetConnectionID())
}

if cmp.Equal(connectionIDsToAdd, alreadyEnabledConnectionsIDs) {
return nil
}

return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Organization with non empty enabled connections",
Detail: cmp.Diff(connectionsToAdd, alreadyEnabledConnections) +
Detail: cmp.Diff(connectionIDsToAdd, alreadyEnabledConnectionsIDs) +
fmt.Sprintf("\nThe organization already has enabled connections attached to it. "+
"Import the resource instead in order to proceed with the changes. "+
"Run: 'terraform import auth0_organization_connections.<given-name> %s'.", organizationID),
Expand Down
19 changes: 15 additions & 4 deletions internal/auth0/organization/resource_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ func createOrganizationMembers(ctx context.Context, data *schema.ResourceData, m
return diagnostics
}

if err := api.Organization.AddMembers(organizationID, membersToAdd); err != nil {
return diag.FromErr(err)
if len(membersToAdd) > len(alreadyMembers.Members) {
if err := api.Organization.AddMembers(organizationID, membersToAdd); err != nil {
return diag.FromErr(err)
}
}

return readOrganizationMembers(ctx, data, meta)
Expand Down Expand Up @@ -166,17 +168,26 @@ func deleteOrganizationMembers(_ context.Context, data *schema.ResourceData, met
func guardAgainstErasingUnwantedMembers(
organizationID string,
alreadyMembers []management.OrganizationMember,
membersToAdd []string,
memberIDsToAdd []string,
) diag.Diagnostics {
if len(alreadyMembers) == 0 {
return nil
}

alreadyMemberIDs := make([]string, 0)
for _, member := range alreadyMembers {
alreadyMemberIDs = append(alreadyMemberIDs, member.GetUserID())
}

if cmp.Equal(memberIDsToAdd, alreadyMemberIDs) {
return nil
}

return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Organization with non empty members",
Detail: cmp.Diff(membersToAdd, alreadyMembers) +
Detail: cmp.Diff(memberIDsToAdd, alreadyMemberIDs) +
fmt.Sprintf("\nThe organization already has members attached to it. "+
"Import the resource instead in order to proceed with the changes. "+
"Run: 'terraform import auth0_organization_members.<given-name> %s'.", organizationID),
Expand Down

0 comments on commit edff628

Please sign in to comment.