Skip to content

Commit

Permalink
Refactor organization-related pagination method from 'Offset paginati…
Browse files Browse the repository at this point in the history
…on' to 'Checkpoint pagination'. (#965)

* Refactor organization-related pagination method from "Offset pagination" to "Checkpoint pagination".

* Update Get organizations in organizations.go to use Checkpoint Pagination.

* resolved issues

* Fix linting issues.

* Refactor removed unnecessary point references to members

* fixed reference

* Fixed Lint issues.

* Refactor flattenOrganizationForDataSource to use flattenOrganizationMembersSlice
  • Loading branch information
developerkunal authored May 27, 2024
1 parent 41ad9c8 commit e232fe1
Show file tree
Hide file tree
Showing 12 changed files with 6,987 additions and 3,438 deletions.
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

0 comments on commit e232fe1

Please sign in to comment.