Skip to content

Commit

Permalink
updates to pagination logic to not request all roles at once vs reque…
Browse files Browse the repository at this point in the history
…sting all roles of a specific type - #551 (#552)
  • Loading branch information
Caleb Washburn authored Jun 5, 2024
1 parent f23d2b9 commit 428fe78
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 39 deletions.
92 changes: 57 additions & 35 deletions role/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (
"github.com/xchapter7x/lo"
)

const SPACE_AUDITOR string = "space_auditor"
const SPACE_DEVELOPER string = "space_developer"
const SPACE_MANAGER string = "space_manager"
const SPACE_SUPPORTER string = "space_supporter"

type DefaultManager struct {
RoleClient CFRoleClient
UserClient CFUserClient
Expand Down Expand Up @@ -49,33 +44,66 @@ func (m *DefaultManager) ClearRoles() {
}

func (m *DefaultManager) ListOrgRoles() ([]*resource.Role, error) {
roles, err := m.RoleClient.ListAll(context.Background(), &client.RoleListOptions{
Types: client.Filter{
Values: []string{resource.OrganizationRoleAuditor.String(),
resource.OrganizationRoleBillingManager.String(),
resource.OrganizationRoleManager.String(),
resource.OrganizationRoleUser.String()},
},
ListOptions: &client.ListOptions{
PerPage: 5000,
},
})

var rolesToReturn []*resource.Role
roles, err := m.listRolesForType(resource.OrganizationRoleAuditor.String())
if err != nil {
return nil, err
}
rolesToReturn = append(rolesToReturn, roles...)

roles, err = m.listRolesForType(resource.OrganizationRoleBillingManager.String())
if err != nil {
return nil, err
}
lo.G.Debugf("Found %d roles from %s API", len(roles), "organization")
err = m.checkResultsAllReturned("organization", roles)
rolesToReturn = append(rolesToReturn, roles...)

roles, err = m.listRolesForType(resource.OrganizationRoleManager.String())
if err != nil {
return nil, err
}
m.dumpV3Roles("organization", roles)
return roles, err
rolesToReturn = append(rolesToReturn, roles...)

roles, err = m.listRolesForType(resource.OrganizationRoleUser.String())
if err != nil {
return nil, err
}
rolesToReturn = append(rolesToReturn, roles...)
return rolesToReturn, err
}

func (m *DefaultManager) ListSpaceRoles() ([]*resource.Role, error) {
var rolesToReturn []*resource.Role
roles, err := m.listRolesForType(resource.SpaceRoleAuditor.String())
if err != nil {
return nil, err
}
rolesToReturn = append(rolesToReturn, roles...)

roles, err = m.listRolesForType(resource.SpaceRoleDeveloper.String())
if err != nil {
return nil, err
}
rolesToReturn = append(rolesToReturn, roles...)

roles, err = m.listRolesForType(resource.SpaceRoleManager.String())
if err != nil {
return nil, err
}
rolesToReturn = append(rolesToReturn, roles...)

roles, err = m.listRolesForType(resource.SpaceRoleSupporter.String())
if err != nil {
return nil, err
}
rolesToReturn = append(rolesToReturn, roles...)
return rolesToReturn, err
}

func (m *DefaultManager) listRolesForType(roleType string) ([]*resource.Role, error) {
roles, err := m.RoleClient.ListAll(context.Background(), &client.RoleListOptions{
Types: client.Filter{
Values: []string{SPACE_AUDITOR, SPACE_DEVELOPER, SPACE_MANAGER, SPACE_SUPPORTER},
Values: []string{roleType},
},
ListOptions: &client.ListOptions{
PerPage: 5000,
Expand All @@ -84,12 +112,12 @@ func (m *DefaultManager) ListSpaceRoles() ([]*resource.Role, error) {
if err != nil {
return nil, err
}
lo.G.Debugf("Found %d roles from %s API", len(roles), "space")
err = m.checkResultsAllReturned("space", roles)
lo.G.Debugf("Found %d roles from type %s", len(roles), roleType)
err = m.checkResultsAllReturned(roles)
if err != nil {
return nil, err
}
m.dumpV3Roles("space", roles)
m.dumpV3Roles(roles)
return roles, err
}

Expand Down Expand Up @@ -146,12 +174,6 @@ func (m *DefaultManager) InitializeOrgUserRolesMap() error {
if err != nil {
return err
}
lo.G.Debugf("Found %d roles from %s API", len(roles), "organization")
err = m.checkResultsAllReturned("organization", roles)
if err != nil {
return err
}
m.dumpV3Roles("organization", roles)
for _, role := range roles {
orgGUID := role.Relationships.Org.Data.GUID
user, err := m.getUserForGUID(role.Relationships.User.Data.GUID)
Expand Down Expand Up @@ -225,22 +247,22 @@ func (m *DefaultManager) dumpRolesUsers(entityType string, entityRoles map[strin
}
}

func (m *DefaultManager) dumpV3Roles(entityType string, roles []*resource.Role) {
func (m *DefaultManager) dumpV3Roles(roles []*resource.Role) {
level, logging := os.LookupEnv("LOG_LEVEL")
if logging && strings.EqualFold(level, "DEBUG") {
for _, role := range roles {
lo.G.Debugf("For entity [%s/%s] and role [%s] user guid [%s]", entityType, role.GUID, role.Type, role.Relationships.User.Data.GUID)
lo.G.Debugf("For role guid [%s] and role [%s] user guid [%s]", role.GUID, role.Type, role.Relationships.User.Data.GUID)
}
}
}

func (m *DefaultManager) checkResultsAllReturned(entityType string, roles []*resource.Role) error {
func (m *DefaultManager) checkResultsAllReturned(roles []*resource.Role) error {
tracker := make(map[string]*resource.Role)
for _, role := range roles {
if priorRole, ok := tracker[role.GUID]; !ok {
tracker[role.GUID] = role
} else {
return fmt.Errorf("role for type %s with GUID[%s] is returned multiple times, prior role [%s] and current role [%s]", entityType, role.GUID, asJson(priorRole), asJson(role))
return fmt.Errorf("role with GUID[%s] is returned multiple times, prior role [%s] and current role [%s]", role.GUID, asJson(priorRole), asJson(role))
}
}
return nil
Expand Down Expand Up @@ -288,7 +310,7 @@ func (m *DefaultManager) ListSpaceUsersByRole(spaceGUID string) (*RoleUsers, *Ro
return nil, nil, nil, nil, err
}
}
return m.getSpaceRole(spaceGUID, SPACE_MANAGER), m.getSpaceRole(spaceGUID, SPACE_DEVELOPER), m.getSpaceRole(spaceGUID, SPACE_AUDITOR), m.getSpaceRole(spaceGUID, SPACE_SUPPORTER), nil
return m.getSpaceRole(spaceGUID, resource.SpaceRoleManager.String()), m.getSpaceRole(spaceGUID, resource.SpaceRoleDeveloper.String()), m.getSpaceRole(spaceGUID, resource.SpaceRoleAuditor.String()), m.getSpaceRole(spaceGUID, resource.SpaceRoleSupporter.String()), nil
}

func (m *DefaultManager) getOrgRole(orgGUID, role string) *RoleUsers {
Expand Down
2 changes: 1 addition & 1 deletion role/manager_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ var _ = Describe("given RoleManager", func() {
}, nil)
err := roleManager.InitializeOrgUserRolesMap()
Expect(err).ShouldNot(HaveOccurred())
Expect(roleClient.ListAllCallCount()).To(Equal(1))
Expect(roleClient.ListAllCallCount()).To(Equal(4))
})
Context("RemoveOrgAuditor", func() {
It("should succeed", func() {
Expand Down
2 changes: 1 addition & 1 deletion role/manager_space_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var _ = Describe("given RoleManager", func() {
}, nil)
err := roleManager.InitializeSpaceUserRolesMap()
Expect(err).ShouldNot(HaveOccurred())
Expect(roleClient.ListAllCallCount()).To(Equal(1))
Expect(roleClient.ListAllCallCount()).To(Equal(4))
})
Context("RemoveSpaceAuditor", func() {
It("should succeed", func() {
Expand Down
4 changes: 2 additions & 2 deletions role/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var _ = Describe("given RoleManager", func() {
}, nil)
results, err := roleManager.ListSpaceRoles()
Expect(err).NotTo(HaveOccurred())
Expect(len(results)).To(Equal(1))
Expect(len(results)).To(Equal(4))
})
It("Should fail as duplicate guids returned", func() {
roleClient.ListAllReturns([]*resource.Role{
Expand All @@ -63,7 +63,7 @@ var _ = Describe("given RoleManager", func() {
}, nil)
results, err := roleManager.ListOrgRoles()
Expect(err).NotTo(HaveOccurred())
Expect(len(results)).To(Equal(1))
Expect(len(results)).To(Equal(4))
})
})
})
Expand Down

0 comments on commit 428fe78

Please sign in to comment.