Add support for recursive listing (#885)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-01-25 19:37:40 -05:00
1 parent 1f9fcad1cd
commit 6cd97a4a6e
77 files changed
+2244 -1448

No files matched your search

@@ -12,6 +12,7 @@ import (
"github.com/hashicorp/boundary/internal/iam/store"
"github.com/hashicorp/boundary/internal/perms"
"github.com/hashicorp/boundary/internal/servers/controller/common"
"github.com/hashicorp/boundary/internal/servers/controller/common/scopeids"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
@@ -76,17 +77,24 @@ func (s Service) ListGroups(ctx context.Context, req *pbs.ListGroupsRequest) (*p
if authResults.Error != nil {
return nil, authResults.Error
}
gl, err := s.listFromRepo(ctx, req.GetScopeId())
scopeIds, scopeInfoMap, err := scopeids.GetScopeIds(
ctx, s.repoFn, authResults, req.GetScopeId(), req.GetRecursive())
if err != nil {
return nil, err
}
gl, err := s.listFromRepo(ctx, scopeIds)
if err != nil {
return nil, err
}
finalItems := make([]*pb.Group, 0, len(gl))
res := &perms.Resource{
ScopeId: authResults.Scope.Id,
Type: resource.Group,
Type: resource.Group,
}
for _, item := range gl {
item.Scope = authResults.Scope
item.Scope = scopeInfoMap[item.GetScopeId()]
res.ScopeId = item.Scope.Id
item.AuthorizedActions = authResults.FetchActionSetForId(ctx, item.Id, IdActions, auth.WithResource(res)).Strings()
if len(item.AuthorizedActions) > 0 {
finalItems = append(finalItems, item)
@@ -310,12 +318,12 @@ func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
return rows > 0, nil
}
func (s Service) listFromRepo(ctx context.Context, scopeId string) ([]*pb.Group, error) {
func (s Service) listFromRepo(ctx context.Context, scopeIds []string) ([]*pb.Group, error) {
repo, err := s.repoFn()
if err != nil {
return nil, err
}
gl, err := repo.ListGroups(ctx, scopeId)
gl, err := repo.ListGroups(ctx, scopeIds)
if err != nil {
return nil, fmt.Errorf("unable to list groups: %w", err)
}
@@ -217,6 +217,7 @@ func TestList(t *testing.T) {
oWithGroups, pNoGroups := iam.TestScopes(t, iamRepo)
var wantOrgGroups []*pb.Group
var wantProjGroups []*pb.Group
var totalGroups []*pb.Group
for i := 0; i < 10; i++ {
og := iam.TestGroup(t, conn, oWithGroups.GetPublicId())
wantOrgGroups = append(wantOrgGroups, &pb.Group{
@@ -228,6 +229,7 @@ func TestList(t *testing.T) {
Version: 1,
AuthorizedActions: []string{"read", "update", "delete", "add-members", "set-members", "remove-members"},
})
totalGroups = append(totalGroups, wantOrgGroups[i])
pg := iam.TestGroup(t, conn, pWithGroups.GetPublicId())
wantProjGroups = append(wantProjGroups, &pb.Group{
Id: pg.GetPublicId(),
@@ -238,33 +240,55 @@ func TestList(t *testing.T) {
Version: 1,
AuthorizedActions: []string{"read", "update", "delete", "add-members", "set-members", "remove-members"},
})
totalGroups = append(totalGroups, wantProjGroups[i])
}
cases := []struct {
name string
scopeId string
res *pbs.ListGroupsResponse
err error
name string
req *pbs.ListGroupsRequest
res *pbs.ListGroupsResponse
err error
}{
{
name: "List Many Group",
scopeId: oWithGroups.GetPublicId(),
res: &pbs.ListGroupsResponse{Items: wantOrgGroups},
name: "List Many Group",
req: &pbs.ListGroupsRequest{ScopeId: oWithGroups.GetPublicId()},
res: &pbs.ListGroupsResponse{Items: wantOrgGroups},
},
{
name: "List No Groups",
scopeId: oNoGroups.GetPublicId(),
res: &pbs.ListGroupsResponse{},
name: "List No Groups",
req: &pbs.ListGroupsRequest{ScopeId: oNoGroups.GetPublicId()},
res: &pbs.ListGroupsResponse{},
},
{
name: "List Many Project Group",
scopeId: pWithGroups.GetPublicId(),
res: &pbs.ListGroupsResponse{Items: wantProjGroups},
name: "List Many Project Group",
req: &pbs.ListGroupsRequest{ScopeId: pWithGroups.GetPublicId()},
res: &pbs.ListGroupsResponse{Items: wantProjGroups},
},
{
name: "List No Project Groups",
scopeId: pNoGroups.GetPublicId(),
res: &pbs.ListGroupsResponse{},
name: "List No Project Groups",
req: &pbs.ListGroupsRequest{ScopeId: pNoGroups.GetPublicId()},
res: &pbs.ListGroupsResponse{},
},
{
name: "List proj groups recursively",
req: &pbs.ListGroupsRequest{ScopeId: pWithGroups.GetPublicId(), Recursive: true},
res: &pbs.ListGroupsResponse{
Items: wantProjGroups,
},
},
{
name: "List org groups recursively",
req: &pbs.ListGroupsRequest{ScopeId: oNoGroups.GetPublicId(), Recursive: true},
res: &pbs.ListGroupsResponse{
Items: wantProjGroups,
},
},
{
name: "List global groups recursively",
req: &pbs.ListGroupsRequest{ScopeId: "global", Recursive: true},
res: &pbs.ListGroupsResponse{
Items: totalGroups,
},
},
}
for _, tc := range cases {
@@ -273,12 +297,12 @@ func TestList(t *testing.T) {
s, err := groups.NewService(repoFn)
require.NoError(err, "Couldn't create new group service.")
got, gErr := s.ListGroups(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), &pbs.ListGroupsRequest{ScopeId: tc.scopeId})
got, gErr := s.ListGroups(auth.DisabledAuthTestContext(auth.WithScopeId(tc.req.GetScopeId())), tc.req)
if tc.err != nil {
require.Error(gErr)
assert.True(errors.Is(gErr, tc.err), "ListGroups(%q) got error %v, wanted %v", tc.scopeId, gErr, tc.err)
assert.True(errors.Is(gErr, tc.err), "ListGroups(%q) got error %v, wanted %v", tc.req.GetScopeId(), gErr, tc.err)
}
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "ListGroups(%q) got response %q, wanted %q", tc.scopeId, got, tc.res)
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "ListGroups(%q) got response %q, wanted %q", tc.req.GetScopeId(), got, tc.res)
})
}
}