Skip to content

Commit 17efc02

Browse files
authored
Merge pull request #19 from ConductorOne/jbernal/quickfix_limit
pagination fix
2 parents f969874 + d22f4e2 commit 17efc02

File tree

6 files changed

+79
-56
lines changed

6 files changed

+79
-56
lines changed

pkg/connector/client/auth0.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (c *Client) List(
9797
path string,
9898
target interface{},
9999
limit int,
100-
offset int,
100+
page int,
101101
) (
102102
*v2.RateLimitDescription,
103103
error,
@@ -108,7 +108,7 @@ func (c *Client) List(
108108
map[string]interface{}{
109109
// Note: `include_totals` changes the shape of the response
110110
"include_totals": true,
111-
"page": offset,
111+
"page": page,
112112
"per_page": limit,
113113
},
114114
&target,
@@ -126,7 +126,7 @@ func (c *Client) List(
126126
func (c *Client) GetUsers(
127127
ctx context.Context,
128128
limit int,
129-
offset int,
129+
page int,
130130
) (
131131
[]User,
132132
int,
@@ -139,7 +139,7 @@ func (c *Client) GetUsers(
139139
apiPathGetUsers,
140140
&target,
141141
limit,
142-
offset,
142+
page,
143143
)
144144
if err != nil {
145145
return nil, 0, rateLimitData, err
@@ -151,7 +151,7 @@ func (c *Client) GetUsers(
151151
func (c *Client) GetRoles(
152152
ctx context.Context,
153153
limit int,
154-
offset int,
154+
page int,
155155
) (
156156
[]Role,
157157
int,
@@ -164,7 +164,7 @@ func (c *Client) GetRoles(
164164
apiPathGetRoles,
165165
&target,
166166
limit,
167-
offset,
167+
page,
168168
)
169169
if err != nil {
170170
return nil, 0, rateLimitData, err
@@ -176,7 +176,7 @@ func (c *Client) GetRoles(
176176
func (c *Client) GetOrganizations(
177177
ctx context.Context,
178178
limit int,
179-
offset int,
179+
page int,
180180
) (
181181
[]Organization,
182182
int,
@@ -189,7 +189,7 @@ func (c *Client) GetOrganizations(
189189
apiPathGetOrganizations,
190190
&target,
191191
limit,
192-
offset,
192+
page,
193193
)
194194
if err != nil {
195195
return nil, 0, rateLimitData, err
@@ -202,7 +202,7 @@ func (c *Client) GetOrganizationMembers(
202202
ctx context.Context,
203203
organizationId string,
204204
limit int,
205-
offset int,
205+
page int,
206206
) (
207207
[]User,
208208
int,
@@ -215,7 +215,7 @@ func (c *Client) GetOrganizationMembers(
215215
fmt.Sprintf(apiPathOrganizationMembers, organizationId),
216216
&target,
217217
limit,
218-
offset,
218+
page,
219219
)
220220
if err != nil {
221221
return nil, 0, rateLimitData, err
@@ -228,7 +228,7 @@ func (c *Client) GetRoleUsers(
228228
ctx context.Context,
229229
roleId string,
230230
limit int,
231-
offset int,
231+
page int,
232232
) (
233233
[]User,
234234
int,
@@ -241,7 +241,7 @@ func (c *Client) GetRoleUsers(
241241
fmt.Sprintf(apiPathUsersForRole, roleId),
242242
&target,
243243
limit,
244-
offset,
244+
page,
245245
)
246246
if err != nil {
247247
return nil, 0, rateLimitData, err

pkg/connector/client/pagination.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ const PageSizeDefault = 100
1010

1111
type Pagination struct {
1212
PagingRequestId string `json:"pagingRequestId"`
13-
Offset int `json:"offset"`
13+
Page int `json:"page"`
1414
}
1515

16-
// ParsePaginationToken - takes as pagination token and returns offset, limit,
16+
// ParsePaginationToken - takes as pagination token and returns page, limit,
1717
// and `pagingRequestId` in that order.
1818
func ParsePaginationToken(pToken *pagination.Token) (
1919
int,
@@ -23,44 +23,44 @@ func ParsePaginationToken(pToken *pagination.Token) (
2323
) {
2424
var (
2525
limit = PageSizeDefault
26-
offset = 0
26+
page = 0
2727
pagingRequestId = ""
2828
)
2929

30-
if pToken != nil {
31-
if pToken.Size > 0 {
32-
limit = pToken.Size
33-
}
30+
if pToken == nil {
31+
return page, limit, pagingRequestId, nil
32+
}
33+
34+
if pToken.Size > 0 {
35+
limit = pToken.Size
36+
}
37+
38+
if pToken.Token == "" {
39+
return page, limit, pagingRequestId, nil
40+
}
3441

35-
if pToken.Token != "" {
36-
var parsed Pagination
37-
err := json.Unmarshal([]byte(pToken.Token), &parsed)
38-
if err != nil {
39-
return 0, 0, "", err
40-
}
41-
offset = parsed.Offset
42-
pagingRequestId = parsed.PagingRequestId
43-
}
42+
var parsed Pagination
43+
err := json.Unmarshal([]byte(pToken.Token), &parsed)
44+
if err != nil {
45+
return 0, 0, "", err
4446
}
45-
return offset, limit, pagingRequestId, nil
47+
48+
page = parsed.Page
49+
pagingRequestId = parsed.PagingRequestId
50+
return page, limit, pagingRequestId, nil
4651
}
4752

48-
// GetNextToken given a limit and offset that were used to fetch _this_ page of
53+
// GetNextToken given a limit and page that were used to fetch _this_ page of
4954
// data, and total number of resources, return the next pagination token as a
5055
// string.
5156
func GetNextToken(
52-
offset int,
57+
page int,
5358
limit int,
5459
total int,
5560
) string {
56-
nextOffset := offset + limit
57-
if nextOffset >= total {
58-
return ""
59-
}
60-
6161
bytes, err := json.Marshal(
6262
Pagination{
63-
Offset: nextOffset,
63+
Page: page + 1,
6464
},
6565
)
6666
if err != nil {

pkg/connector/organizations.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,21 @@ func (o *organizationBuilder) List(
6464
outputResources := make([]*v2.Resource, 0)
6565
var outputAnnotations annotations.Annotations
6666

67-
offset, limit, _, err := client.ParsePaginationToken(pToken)
67+
page, limit, _, err := client.ParsePaginationToken(pToken)
6868
if err != nil {
6969
return nil, "", nil, err
7070
}
7171

72-
organizations, total, ratelimitData, err := o.client.GetOrganizations(ctx, limit, offset)
72+
organizations, total, ratelimitData, err := o.client.GetOrganizations(ctx, limit, page)
7373
outputAnnotations.WithRateLimiting(ratelimitData)
7474
if err != nil {
7575
return nil, "", outputAnnotations, err
7676
}
7777

78+
if len(organizations) == 0 {
79+
return outputResources, "", outputAnnotations, nil
80+
}
81+
7882
for _, organization := range organizations {
7983
organizationResource0, err := organizationResource(organization, parentResourceID)
8084
if err != nil {
@@ -83,7 +87,7 @@ func (o *organizationBuilder) List(
8387
outputResources = append(outputResources, organizationResource0)
8488
}
8589

86-
nextToken := client.GetNextToken(offset, limit, total)
90+
nextToken := client.GetNextToken(page, limit, total)
8791

8892
return outputResources, nextToken, outputAnnotations, nil
8993
}
@@ -124,7 +128,7 @@ func (o *organizationBuilder) Grants(
124128
error,
125129
) {
126130
var outputAnnotations annotations.Annotations
127-
offset, limit, _, err := client.ParsePaginationToken(token)
131+
page, limit, _, err := client.ParsePaginationToken(token)
128132
if err != nil {
129133
return nil, "", nil, err
130134
}
@@ -133,13 +137,17 @@ func (o *organizationBuilder) Grants(
133137
ctx,
134138
resource.Id.Resource,
135139
limit,
136-
offset,
140+
page,
137141
)
138142
outputAnnotations.WithRateLimiting(ratelimitData)
139143
if err != nil {
140144
return nil, "", outputAnnotations, err
141145
}
142146

147+
if len(members) == 0 {
148+
return nil, "", outputAnnotations, nil
149+
}
150+
143151
var grants []*v2.Grant
144152
for _, member := range members {
145153
principalId, err := resourceSdk.NewResourceID(userResourceType, member.UserId)
@@ -154,7 +162,7 @@ func (o *organizationBuilder) Grants(
154162
grants = append(grants, nextGrant)
155163
}
156164

157-
nextToken := client.GetNextToken(offset, limit, total)
165+
nextToken := client.GetNextToken(page, limit, total)
158166

159167
return grants, nextToken, outputAnnotations, nil
160168
}

pkg/connector/roles.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,21 @@ func (o *roleBuilder) List(
6262
outputResources := make([]*v2.Resource, 0)
6363
var outputAnnotations annotations.Annotations
6464

65-
offset, limit, _, err := client.ParsePaginationToken(pToken)
65+
page, limit, _, err := client.ParsePaginationToken(pToken)
6666
if err != nil {
6767
return nil, "", nil, err
6868
}
6969

70-
roles, total, ratelimitData, err := o.client.GetRoles(ctx, limit, offset)
70+
roles, total, ratelimitData, err := o.client.GetRoles(ctx, limit, page)
7171
outputAnnotations.WithRateLimiting(ratelimitData)
7272
if err != nil {
7373
return nil, "", outputAnnotations, err
7474
}
7575

76+
if len(roles) == 0 {
77+
return outputResources, "", outputAnnotations, nil
78+
}
79+
7680
for _, role := range roles {
7781
roleResource0, err := roleResource(role, parentResourceID)
7882
if err != nil {
@@ -81,7 +85,7 @@ func (o *roleBuilder) List(
8185
outputResources = append(outputResources, roleResource0)
8286
}
8387

84-
nextToken := client.GetNextToken(offset, limit, total)
88+
nextToken := client.GetNextToken(page, limit, total)
8589

8690
return outputResources, nextToken, outputAnnotations, nil
8791
}
@@ -124,7 +128,7 @@ func (o *roleBuilder) Grants(
124128
error,
125129
) {
126130
var outputAnnotations annotations.Annotations
127-
offset, limit, _, err := client.ParsePaginationToken(token)
131+
page, limit, _, err := client.ParsePaginationToken(token)
128132
if err != nil {
129133
return nil, "", nil, err
130134
}
@@ -133,13 +137,17 @@ func (o *roleBuilder) Grants(
133137
ctx,
134138
resource.Id.Resource,
135139
limit,
136-
offset,
140+
page,
137141
)
138142
outputAnnotations.WithRateLimiting(ratelimitData)
139143
if err != nil {
140144
return nil, "", outputAnnotations, err
141145
}
142146

147+
if len(users) == 0 {
148+
return nil, "", outputAnnotations, nil
149+
}
150+
143151
var grants []*v2.Grant
144152
for _, user := range users {
145153
principalId, err := resourceSdk.NewResourceID(userResourceType, user.UserId)
@@ -154,7 +162,7 @@ func (o *roleBuilder) Grants(
154162
grants = append(grants, nextGrant)
155163
}
156164

157-
nextToken := client.GetNextToken(offset, limit, total)
165+
nextToken := client.GetNextToken(page, limit, total)
158166

159167
return grants, nextToken, outputAnnotations, nil
160168
}

pkg/connector/users.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,21 @@ func (o *userBuilder) List(
6666
outputResources := make([]*v2.Resource, 0)
6767
var outputAnnotations annotations.Annotations
6868

69-
offset, limit, _, err := client.ParsePaginationToken(pToken)
69+
page, limit, _, err := client.ParsePaginationToken(pToken)
7070
if err != nil {
7171
return nil, "", nil, err
7272
}
7373

74-
users, total, ratelimitData, err := o.client.GetUsers(ctx, limit, offset)
74+
users, total, ratelimitData, err := o.client.GetUsers(ctx, limit, page)
7575
outputAnnotations.WithRateLimiting(ratelimitData)
7676
if err != nil {
7777
return nil, "", outputAnnotations, err
7878
}
7979

80+
if len(users) == 0 {
81+
return outputResources, "", outputAnnotations, nil
82+
}
83+
8084
for _, user := range users {
8185
userResource0, err := userResource(user, parentResourceID)
8286
if err != nil {
@@ -87,7 +91,7 @@ func (o *userBuilder) List(
8791

8892
// TODO(marcos): it might never be possible to get a second page if we are limited to 1,000 results.
8993
// See https://auth0.com/docs/users/search/v3/view-search-results-by-page#limitation.
90-
nextToken := client.GetNextToken(offset, limit, total)
94+
nextToken := client.GetNextToken(page, limit, total)
9195

9296
return outputResources, nextToken, outputAnnotations, nil
9397
}

pkg/connector/users_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package connector
22

33
import (
44
"context"
5+
"encoding/json"
56
"testing"
67

78
"github.com/conductorone/baton-auth0/pkg/connector/client"
@@ -35,15 +36,17 @@ func TestUsersList(t *testing.T) {
3536
Token: "",
3637
Size: 1,
3738
}
38-
for {
39+
40+
for i := 0; i < 2; i++ {
3941
nextResources, nextToken, listAnnotations, err := c.List(ctx, nil, &pToken)
4042
resources = append(resources, nextResources...)
4143

4244
require.Nil(t, err)
4345
test.AssertNoRatelimitAnnotations(t, listAnnotations)
44-
if nextToken == "" {
45-
break
46-
}
46+
var token client.Pagination
47+
err = json.Unmarshal([]byte(nextToken), &token)
48+
require.Nil(t, err)
49+
require.Equal(t, token.Page, i+1)
4750

4851
pToken.Token = nextToken
4952
}

0 commit comments

Comments
 (0)