-
Notifications
You must be signed in to change notification settings - Fork 72
/
api_util.go
37 lines (29 loc) · 970 Bytes
/
api_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package rbacutil
import (
"context"
"github.com/redhat-developer/app-services-cli/pkg/api/rbac"
)
// FetchAllUsers retrieves and returns every user within the current user's organization with the applied filters
func FetchAllUsers(ctx context.Context, rbacAPI func() rbac.PrincipalAPI, queryParams ...rbac.QueryParam) ([]rbac.Principal, error) {
principals, resp, err := rbacAPI().GetPrincipals(ctx, rbac.WithIntQueryParam("limit", 0))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
totalUsers := principals.Meta.Count
// no need to re-fetch if the first call contains all items
if totalUsers <= len(principals.Data) {
return principals.Data, nil
}
queryParams = append(queryParams, rbac.WithIntQueryParam("limit", totalUsers))
principals, resp, err = rbacAPI().GetPrincipals(ctx, queryParams...)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
return principals.Data, nil
}