From 6eb7bf10d7e89a6865f5780cdd7b9fa4bef13ba1 Mon Sep 17 00:00:00 2001 From: ilia-medvedev-codefresh Date: Mon, 27 May 2024 11:01:08 +0300 Subject: [PATCH] Fix: User data source times out on getting all users - add pagination (#146) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …return it time ## What ## Why ## Notes ## 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)._ --- codefresh/cfclient/user.go | 47 +++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/codefresh/cfclient/user.go b/codefresh/cfclient/user.go index 0a3c593..f4b4d36 100644 --- a/codefresh/cfclient/user.go +++ b/codefresh/cfclient/user.go @@ -2,7 +2,6 @@ package cfclient import ( "fmt" - "log" "strings" ) @@ -222,25 +221,41 @@ func (client *Client) DeleteUserAsAccountAdmin(accountId, userId string) error { func (client *Client) GetAllUsers() (*[]User, error) { - opts := RequestOptions{ - Path: "/admin/user", - Method: "GET", - } + limitPerQuery := 100 + bIsDone := false + nPageIndex := 1 - resp, err := client.RequestAPI(&opts) - if err != nil { - return nil, err - } + var allUsers []User - var users []User - respStr := string(resp) - log.Printf("[INFO] GetAllUsers resp: %s", respStr) - err = DecodeResponseInto(resp, &users) - if err != nil { - return nil, err + for !bIsDone { + var userPaginatedResp struct{Docs []User `json:"docs"`} + + opts := RequestOptions{ + Path: fmt.Sprintf("/admin/user?limit=%d&page=%d", limitPerQuery, nPageIndex), + Method: "GET", + } + + resp, err := client.RequestAPI(&opts) + + if err != nil { + return nil, err + } + + err = DecodeResponseInto(resp, &userPaginatedResp) + + if err != nil { + return nil, err + } + + if len(userPaginatedResp.Docs) > 0 { + allUsers = append(allUsers,userPaginatedResp.Docs...) + nPageIndex++ + } else { + bIsDone = true + } } - return &users, nil + return &allUsers, nil } func (client *Client) GetUserByID(userId string) (*User, error) {