Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only set the page offset if we have a non-empty next page token #11

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 25 additions & 23 deletions pkg/sac/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,25 @@ 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 {
func paginationQueryOffset(nextPage string) url.Values {
q := url.Values{}
q.Set("pageOffset", fmt.Sprintf("%v", nextPage))
if nextPage != "" {
q.Set("pageOffset", nextPage)
}
q.Set("perPage", "50")
return q
}
Expand Down Expand Up @@ -100,9 +102,9 @@ func CreateBearerToken(ctx context.Context, username, password, tenant string) (
return res.AccessToken, nil
}

// ListIdentityProviderIds returns a list of identity provider ids.
func (c *Client) ListIdentityProviderIds(ctx context.Context) ([]string, error) {
var providerIds []string
// ListIdentityProviderIDs returns a list of identity provider ids.
func (c *Client) ListIdentityProviderIDs(ctx context.Context) ([]string, error) {
var providerIDs []string
providersUrl := fmt.Sprintf("%s/identities/settings/identity-providers", c.baseUrl)

q := url.Values{}
Expand All @@ -114,14 +116,14 @@ func (c *Client) ListIdentityProviderIds(ctx context.Context) ([]string, error)
}

for _, identityProvider := range res {
providerIds = append(providerIds, identityProvider.ID)
providerIDs = append(providerIDs, identityProvider.ID)
}

return providerIds, nil
return providerIDs, nil
}

// 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"`
Expand All @@ -143,12 +145,12 @@ func (c *Client) ListUsersPerProvider(ctx context.Context, identityProviderId st
// ListAllUsers returns a list of all users for all identity providers.
func (c *Client) ListAllUsers(ctx context.Context) ([]User, error) {
var allUsers []User
identityProviders, err := c.ListIdentityProviderIds(ctx)
identityProviders, err := c.ListIdentityProviderIDs(ctx)
if err != nil {
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 @@ -167,7 +169,7 @@ 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"`
Expand All @@ -190,12 +192,12 @@ func (c *Client) ListGroupsPerProvider(ctx context.Context, identityProviderId s
// ListAllGroups returns a list of all groups for all identity providers.
func (c *Client) ListAllGroups(ctx context.Context) ([]Group, error) {
var allGroups []Group
identityProviders, err := c.ListIdentityProviderIds(ctx)
identityProviders, err := c.ListIdentityProviderIDs(ctx)
if err != nil {
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 @@ -216,7 +218,7 @@ 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"`
Expand Down
Loading