Skip to content

Commit

Permalink
Just assume that the next page token is a string since we're parsing …
Browse files Browse the repository at this point in the history
…it from json
  • Loading branch information
jirwin committed Mar 7, 2024
1 parent afdb1fc commit ac0a7c1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
7 changes: 4 additions & 3 deletions pkg/connector/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"fmt"

"github.com/conductorone/baton-broadcom-sac/pkg/sac"
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
"github.com/conductorone/baton-sdk/pkg/annotations"
"github.com/conductorone/baton-sdk/pkg/pagination"
ent "github.com/conductorone/baton-sdk/pkg/types/entitlement"
grant "github.com/conductorone/baton-sdk/pkg/types/grant"
"github.com/conductorone/baton-sdk/pkg/types/grant"
rs "github.com/conductorone/baton-sdk/pkg/types/resource"

"github.com/conductorone/baton-broadcom-sac/pkg/sac"
)

type groupBuilder struct {
Expand Down Expand Up @@ -131,7 +132,7 @@ func (g *groupBuilder) Grants(ctx context.Context, resource *v2.Resource, pToken
}

if !paginationData.Last {
token, err = bag.NextToken(fmt.Sprintf("%v", paginationData.NextPage))
token, err = bag.NextToken(paginationData.NextPage)
if err != nil {
return nil, "", nil, err
}
Expand Down
59 changes: 21 additions & 38 deletions pkg/sac/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,27 @@ type AuthResponse struct {
}

type PaginationData struct {
First bool `json:"first"`
Last bool `json:"last"`
Size int `json:"size"`
TotalElements int `json:"totalElements"`
PerPage int `json:"perPage"`
NextPage interface{} `json:"nextPage"`
TotalPages int `json:"totalPages"`
Number int `json:"number"`
NumberOfElements int `json:"numberOfElements"`
First bool `json:"first"`
Last bool `json:"last"`
Size int `json:"size"`
TotalElements int `json:"totalElements"`
PerPage int `json:"perPage"`
NextPage string `json:"nextPage"`
TotalPages int `json:"totalPages"`
Number int `json:"number"`
NumberOfElements int `json:"numberOfElements"`
}

const applicationJSONHeader = "application/json"

// returns query params with pagination options with PageOffset.
func paginationQueryOffset(nextPage interface{}) (url.Values, error) {
func paginationQueryOffset(nextPage string) url.Values {
q := url.Values{}
if nextPage != nil {
switch np := nextPage.(type) {
case int:
q.Set("pageOffset", fmt.Sprintf("%d", np))
case string:
q.Set("pageOffset", np)
default:
return nil, fmt.Errorf("unexpected type for nextPage: %v", nextPage)
}
if nextPage != "" {
q.Set("pageOffset", nextPage)
}
q.Set("perPage", "50")
return q, nil
return q
}

// returns query params with pagination options with PageNumber.
Expand Down Expand Up @@ -130,18 +123,14 @@ func (c *Client) ListIdentityProviderIds(ctx context.Context) ([]string, error)
}

// ListUserPerProvider returns a list of users for the given identity provider id.
func (c *Client) ListUsersPerProvider(ctx context.Context, identityProviderId string, nextPage interface{}) ([]User, PaginationData, error) {
func (c *Client) ListUsersPerProvider(ctx context.Context, identityProviderId string, nextPage string) ([]User, PaginationData, error) {
url := fmt.Sprintf("%s/identities/%s/users", c.baseUrl, identityProviderId)
var res struct {
Content []User `json:"content"`
PaginationData
}

q, err := paginationQueryOffset(nextPage)
if err != nil {
return nil, PaginationData{}, err
}

q := paginationQueryOffset(nextPage)
if err := c.doRequest(ctx, url, &res, q); err != nil {
return nil, PaginationData{}, err
}
Expand All @@ -161,7 +150,7 @@ func (c *Client) ListAllUsers(ctx context.Context) ([]User, error) {
return nil, fmt.Errorf("error fetching identity providers: %w", err)
}
for _, identityProvider := range identityProviders {
var nextPage interface{}
var nextPage string
for {
users, paginationData, err := c.ListUsersPerProvider(ctx, identityProvider, nextPage)
if err != nil {
Expand All @@ -180,17 +169,14 @@ func (c *Client) ListAllUsers(ctx context.Context) ([]User, error) {
}

// ListGroups returns a list of groups for the given identity provider id.
func (c *Client) ListGroupsPerProvider(ctx context.Context, identityProviderId string, nextPage interface{}) ([]Group, PaginationData, error) {
func (c *Client) ListGroupsPerProvider(ctx context.Context, identityProviderId string, nextPage string) ([]Group, PaginationData, error) {
url := fmt.Sprintf("%s/identities/%s/groups", c.baseUrl, identityProviderId)
var res struct {
Content []Group `json:"content"`
PaginationData
}

q, err := paginationQueryOffset(nextPage)
if err != nil {
return nil, PaginationData{}, err
}
q := paginationQueryOffset(nextPage)

if err := c.doRequest(ctx, url, &res, q); err != nil {
return nil, PaginationData{}, err
Expand All @@ -211,7 +197,7 @@ func (c *Client) ListAllGroups(ctx context.Context) ([]Group, error) {
return nil, fmt.Errorf("error fetching identity providers: %w", err)
}
for _, identityProvider := range identityProviders {
var nextPage interface{}
var nextPage string
for {
groups, paginationData, err := c.ListGroupsPerProvider(ctx, identityProvider, nextPage)
if err != nil {
Expand All @@ -232,17 +218,14 @@ func (c *Client) ListAllGroups(ctx context.Context) ([]Group, error) {
}

// ListGroupUsers returns a list of users for the given identity provider id and group id.
func (c *Client) ListGroupMembers(ctx context.Context, identityProviderId string, groupId string, nextPage interface{}) ([]User, PaginationData, error) {
func (c *Client) ListGroupMembers(ctx context.Context, identityProviderId string, groupId string, nextPage string) ([]User, PaginationData, error) {
url := fmt.Sprintf("%s/identities/%s/groups/%s/users", c.baseUrl, identityProviderId, groupId)
var res struct {
Content []User `json:"content"`
PaginationData
}

q, err := paginationQueryOffset(nextPage)
if err != nil {
return nil, PaginationData{}, err
}
q := paginationQueryOffset(nextPage)

if err := c.doRequest(ctx, url, &res, q); err != nil {
return nil, PaginationData{}, err
Expand Down

0 comments on commit ac0a7c1

Please sign in to comment.