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 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
15 changes: 12 additions & 3 deletions internal/acctest/sweep/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ func Organizations() {
return err
}

var page int
var result *multierror.Error
var from string
options := []management.RequestOption{
management.Take(100),
}
for {
organizationList, err := api.Organization.List(ctx, management.Page(page))
if from != "" {
options = append(options, management.From(from))
}

organizationList, err := api.Organization.List(ctx, options...)
if err != nil {
return err
}
Expand All @@ -42,10 +49,12 @@ func Organizations() {
log.Printf("[DEBUG] ✗ %s", organization.GetName())
}
}

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

from = organizationList.Next
}

return result.ErrorOrNil()
Expand Down
30 changes: 20 additions & 10 deletions internal/auth0/organization/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,35 @@ func fetchAllOrganizationConnections(ctx context.Context, api *management.Manage
return foundConnections, nil
}

func fetchAllOrganizationMembers(ctx context.Context, api *management.Management, organizationID string) ([]string, error) {
foundMembers := make([]string, 0)
var page int
func fetchAllOrganizationMembers(
ctx context.Context,
api *management.Management,
organizationID string,
) ([]management.OrganizationMember, error) {
var foundMembers []management.OrganizationMember
var from string

options := []management.RequestOption{
management.Take(100),
management.IncludeFields("user_id"),
}

for {
members, err := api.Organization.Members(ctx, organizationID, management.Page(page), management.PerPage(100))
if err != nil {
return nil, err
if from != "" {
options = append(options, management.From(from))
}

for _, member := range members.Members {
foundMembers = append(foundMembers, member.GetUserID())
membersList, err := api.Organization.Members(ctx, organizationID, options...)
if err != nil {
return nil, err
}

if !members.HasNext() {
foundMembers = append(foundMembers, membersList.Members...)
if !membersList.HasNext() {
break
}

page++
from = membersList.Next
}

return foundMembers, nil
Expand Down
5 changes: 2 additions & 3 deletions internal/auth0/organization/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func flattenOrganizationForDataSource(
data *schema.ResourceData,
organization *management.Organization,
connections []*management.OrganizationConnection,
memberIDs []string,
members []management.OrganizationMember,
) error {
result := multierror.Append(
flattenOrganization(data, organization),
data.Set("connections", flattenOrganizationEnabledConnections(connections)),
data.Set("members", memberIDs),
data.Set("members", flattenOrganizationMembersSlice(members)),
)

return result.ErrorOrNil()
Expand Down Expand Up @@ -102,7 +102,6 @@ func flattenOrganizationMembersSlice(members []management.OrganizationMember) []
if len(members) == 0 {
return nil
}

flattenedMembers := make([]string, 0)
for _, member := range members {
flattenedMembers = append(flattenedMembers, member.GetUserID())
Expand Down
4 changes: 2 additions & 2 deletions internal/auth0/organization/resource_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func readOrganizationMember(ctx context.Context, data *schema.ResourceData, meta
}

userID := data.Get("user_id").(string)
for _, memberID := range members {
if memberID == userID {
for _, member := range members {
if member.GetUserID() == userID {
return nil
}
}
Expand Down
47 changes: 7 additions & 40 deletions internal/auth0/organization/resource_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"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 +50,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 +80,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
Loading
Loading