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

Refactor organization-related pagination method from 'Offset pagination' to 'Checkpoint pagination'. #965

Merged
merged 12 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 6 additions & 3 deletions internal/acctest/sweep/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ func Organizations() {
return err
}

var page int
var result *multierror.Error
var from string

for {
organizationList, err := api.Organization.List(ctx, management.Page(page))
organizationList, err := api.Organization.List(ctx, management.From(from), management.Take(100))
developerkunal marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand All @@ -42,10 +43,12 @@ func Organizations() {
log.Printf("[DEBUG] ✗ %s", organization.GetName())
}
}

if !organizationList.HasNext() {
break
}
page++

from = organizationList.Next
}

return result.ErrorOrNil()
Expand Down
6 changes: 3 additions & 3 deletions internal/auth0/organization/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ func fetchAllOrganizationConnections(ctx context.Context, api *management.Manage

func fetchAllOrganizationMembers(ctx context.Context, api *management.Management, organizationID string) ([]string, error) {
developerkunal marked this conversation as resolved.
Show resolved Hide resolved
foundMembers := make([]string, 0)
var page int
var from string

for {
members, err := api.Organization.Members(ctx, organizationID, management.Page(page), management.PerPage(100))
members, err := api.Organization.Members(ctx, organizationID, management.From(from), management.Take(100), management.IncludeFields("user_id"))
if err != nil {
return nil, err
}
Expand All @@ -145,7 +145,7 @@ func fetchAllOrganizationMembers(ctx context.Context, api *management.Management
break
}

page++
from = members.Next
}

return foundMembers, nil
Expand Down
11 changes: 3 additions & 8 deletions internal/auth0/organization/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func flattenOrganizationMemberRole(data *schema.ResourceData, role management.Or
return result.ErrorOrNil()
}

func flattenOrganizationMembers(data *schema.ResourceData, members []management.OrganizationMember) error {
func flattenOrganizationMembers(data *schema.ResourceData, members []string) error {
result := multierror.Append(
data.Set("organization_id", data.Id()),
data.Set("members", flattenOrganizationMembersSlice(members)),
Expand All @@ -98,15 +98,10 @@ func flattenOrganizationMembers(data *schema.ResourceData, members []management.
return result.ErrorOrNil()
}

func flattenOrganizationMembersSlice(members []management.OrganizationMember) []string {
func flattenOrganizationMembersSlice(members []string) []string {
if len(members) == 0 {
return nil
}

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

return flattenedMembers
return members
}
56 changes: 8 additions & 48 deletions internal/auth0/organization/resource_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/auth0/go-auth0/management"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -49,26 +48,9 @@ func createOrganizationMembers(ctx context.Context, data *schema.ResourceData, m

organizationID := data.Get("organization_id").(string)

var alreadyMembers []management.OrganizationMember
var page int
for {
memberList, err := api.Organization.Members(
ctx,
organizationID,
management.Page(page),
management.PerPage(100),
)
if err != nil {
return diag.FromErr(internalError.HandleAPIError(data, err))
}

alreadyMembers = append(alreadyMembers, memberList.Members...)

if !memberList.HasNext() {
break
}

page++
alreadyMembers, err := fetchAllOrganizationMembers(ctx, api, organizationID)
if err != nil {
return diag.FromErr(internalError.HandleAPIError(data, err))
}

data.SetId(organizationID)
Expand Down Expand Up @@ -96,26 +78,9 @@ func createOrganizationMembers(ctx context.Context, data *schema.ResourceData, m
func readOrganizationMembers(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
api := meta.(*config.Config).GetAPI()

var members []management.OrganizationMember
var page int
for {
memberList, err := api.Organization.Members(
ctx,
data.Id(),
management.Page(page),
management.PerPage(100),
)
if err != nil {
return diag.FromErr(internalError.HandleAPIError(data, err))
}

members = append(members, memberList.Members...)

if !memberList.HasNext() {
break
}

page++
members, err := fetchAllOrganizationMembers(ctx, api, data.Id())
if err != nil {
return diag.FromErr(internalError.HandleAPIError(data, err))
}

return diag.FromErr(flattenOrganizationMembers(data, members))
Expand Down Expand Up @@ -173,18 +138,13 @@ func deleteOrganizationMembers(ctx context.Context, data *schema.ResourceData, m

func guardAgainstErasingUnwantedMembers(
organizationID string,
alreadyMembers []management.OrganizationMember,
alreadyMemberIDs []string,
memberIDsToAdd []string,
) diag.Diagnostics {
if len(alreadyMembers) == 0 {
if len(alreadyMemberIDs) == 0 {
return nil
}

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

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