Skip to content

Commit 6eb7bf1

Browse files
Fix: User data source times out on getting all users - add pagination (#146)
…return it time ## What ## Why ## Notes <!-- Add any notes here --> ## Checklist * [ ] _I have read [CONTRIBUTING.md](https://github.com/codefresh-io/terraform-provider-codefresh/blob/master/CONTRIBUTING.md)._ * [ ] _I have [allowed changes to my fork to be made](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)._ * [ ] _I have added tests, assuming new tests are warranted_. * [ ] _I understand that the `/test` comment will be ignored by the CI trigger [unless it is made by a repo admin or collaborator](https://codefresh.io/docs/docs/pipelines/triggers/git-triggers/#support-for-building-pull-requests-from-forks)._
1 parent 55e3ef2 commit 6eb7bf1

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

codefresh/cfclient/user.go

+31-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cfclient
22

33
import (
44
"fmt"
5-
"log"
65
"strings"
76
)
87

@@ -222,25 +221,41 @@ func (client *Client) DeleteUserAsAccountAdmin(accountId, userId string) error {
222221

223222
func (client *Client) GetAllUsers() (*[]User, error) {
224223

225-
opts := RequestOptions{
226-
Path: "/admin/user",
227-
Method: "GET",
228-
}
224+
limitPerQuery := 100
225+
bIsDone := false
226+
nPageIndex := 1
229227

230-
resp, err := client.RequestAPI(&opts)
231-
if err != nil {
232-
return nil, err
233-
}
228+
var allUsers []User
234229

235-
var users []User
236-
respStr := string(resp)
237-
log.Printf("[INFO] GetAllUsers resp: %s", respStr)
238-
err = DecodeResponseInto(resp, &users)
239-
if err != nil {
240-
return nil, err
230+
for !bIsDone {
231+
var userPaginatedResp struct{Docs []User `json:"docs"`}
232+
233+
opts := RequestOptions{
234+
Path: fmt.Sprintf("/admin/user?limit=%d&page=%d", limitPerQuery, nPageIndex),
235+
Method: "GET",
236+
}
237+
238+
resp, err := client.RequestAPI(&opts)
239+
240+
if err != nil {
241+
return nil, err
242+
}
243+
244+
err = DecodeResponseInto(resp, &userPaginatedResp)
245+
246+
if err != nil {
247+
return nil, err
248+
}
249+
250+
if len(userPaginatedResp.Docs) > 0 {
251+
allUsers = append(allUsers,userPaginatedResp.Docs...)
252+
nPageIndex++
253+
} else {
254+
bIsDone = true
255+
}
241256
}
242257

243-
return &users, nil
258+
return &allUsers, nil
244259
}
245260

246261
func (client *Client) GetUserByID(userId string) (*User, error) {

0 commit comments

Comments
 (0)