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

feat: add pagination for list resources #77

Merged
merged 4 commits into from
Sep 26, 2023
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ COMMIT := $(shell git rev-parse --short HEAD)
TAG := "$(shell git rev-list --tags --max-count=1)"
VERSION := "$(shell git describe --tags ${TAG})-next"
BUILD_DIR=dist
PROTON_COMMIT := "5c19c2579d679e2a807dcde2fe4445f79233df91"
PROTON_COMMIT := "af4704d2f660ec3943bbd61c22b1bab51a74afbb"

.PHONY: all build clean test tidy vet proto setup format generate

Expand Down
1 change: 1 addition & 0 deletions api/handler/v1beta1/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type resourceService interface {
Get(context.Context, *domain.ResourceIdentifier) (*domain.Resource, error)
Delete(context.Context, string) error
BatchDelete(context.Context, []string) error
GetResourcesTotalCount(context.Context, domain.ListResourcesFilter) (int64, error)
}

//go:generate mockery --name=activityService --exported --with-expecter
Expand Down
54 changes: 54 additions & 0 deletions api/handler/v1beta1/mocks/resourceService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 44 additions & 10 deletions api/handler/v1beta1/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1beta1
import (
"context"
"errors"
"golang.org/x/sync/errgroup"
"strings"

guardianv1beta1 "github.com/goto/guardian/api/proto/gotocompany/guardian/v1beta1"
Expand All @@ -25,31 +26,64 @@ func (s *GRPCServer) ListResources(ctx context.Context, req *guardianv1beta1.Lis
}
}
}
resources, err := s.resourceService.Find(ctx, domain.ListResourcesFilter{
filter := domain.ListResourcesFilter{
IsDeleted: req.GetIsDeleted(),
ResourceType: req.GetType(),
ResourceURN: req.GetUrn(),
ProviderType: req.GetProviderType(),
ProviderURN: req.GetProviderUrn(),
Name: req.GetName(),
Details: details,
})
Size: req.GetSize(),
Offset: req.GetOffset(),
}

resources, total, err := s.listResources(ctx, filter)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get resource list: %v", err)
return nil, err
}

resourceProtos := []*guardianv1beta1.Resource{}
for _, r := range resources {
resourceProto, err := s.adapter.ToResourceProto(r)
return &guardianv1beta1.ListResourcesResponse{
Resources: resources,
Total: uint32(total),
}, nil
}

func (s *GRPCServer) listResources(ctx context.Context, filter domain.ListResourcesFilter) ([]*guardianv1beta1.Resource, int64, error) {
eg, ctx := errgroup.WithContext(ctx)
var resources []*domain.Resource
var total int64

eg.Go(func() error {
resourceRecords, err := s.resourceService.Find(ctx, filter)
if err != nil {
return status.Errorf(codes.Internal, "failed to get resource list: %s", err)
}
resources = resourceRecords
return nil
})
eg.Go(func() error {
totalRecord, err := s.resourceService.GetResourcesTotalCount(ctx, filter)
if err != nil {
return status.Errorf(codes.Internal, "failed to get resource total count: %s", err)
}
total = totalRecord
return nil
})

if err := eg.Wait(); err != nil {
return nil, 0, err
}
var resourceProtos []*guardianv1beta1.Resource
for i, r := range resources {
resourceProto, err := s.adapter.ToResourceProto(resources[i])
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to parse resource %v: %v", r.Name, err)
return nil, 0, status.Errorf(codes.Internal, "failed to parse resource %v: %v", r.Name, err)
}
resourceProtos = append(resourceProtos, resourceProto)
}

return &guardianv1beta1.ListResourcesResponse{
Resources: resourceProtos,
}, nil
return resourceProtos, total, nil
}

func (s *GRPCServer) GetResource(ctx context.Context, req *guardianv1beta1.GetResourceRequest) (*guardianv1beta1.GetResourceResponse, error) {
Expand Down
19 changes: 12 additions & 7 deletions api/handler/v1beta1/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ func (s *GrpcHandlersSuite) TestListResources() {
s.setup()
timeNow := time.Now()

ctx := context.Background()

req := &guardianv1beta1.ListResourcesRequest{
IsDeleted: true,
Type: "test-type",
Expand Down Expand Up @@ -85,9 +83,10 @@ func (s *GrpcHandlersSuite) TestListResources() {
Details: expectedDetails,
},
}
s.resourceService.EXPECT().Find(ctx, expectedFilters).Return(dummyResources, nil)

res, err := s.grpcServer.ListResources(ctx, req)
s.resourceService.EXPECT().Find(mock.AnythingOfType("*context.cancelCtx"), expectedFilters).Return(dummyResources, nil)
s.resourceService.EXPECT().GetResourcesTotalCount(mock.AnythingOfType("*context.cancelCtx"),
expectedFilters).Return(0, nil).Once()
res, err := s.grpcServer.ListResources(context.Background(), req)

s.NoError(err)
s.Equal(expectedResponse, res)
Expand All @@ -98,7 +97,10 @@ func (s *GrpcHandlersSuite) TestListResources() {
s.setup()

expectedError := errors.New("random error")
s.resourceService.EXPECT().Find(mock.AnythingOfType("*context.emptyCtx"), mock.Anything).Return(nil, expectedError).Once()
s.resourceService.EXPECT().Find(mock.AnythingOfType("*context.cancelCtx"),
mock.AnythingOfType("domain.ListResourcesFilter")).Return(nil, expectedError).Once()
s.resourceService.EXPECT().GetResourcesTotalCount(mock.AnythingOfType("*context.cancelCtx"),
mock.AnythingOfType("domain.ListResourcesFilter")).Return(0, nil).Once()

req := &guardianv1beta1.ListResourcesRequest{}
res, err := s.grpcServer.ListResources(context.Background(), req)
Expand All @@ -118,7 +120,10 @@ func (s *GrpcHandlersSuite) TestListResources() {
},
},
}
s.resourceService.EXPECT().Find(mock.AnythingOfType("*context.emptyCtx"), mock.Anything).Return(invalidResources, nil).Once()
s.resourceService.EXPECT().Find(mock.AnythingOfType("*context.cancelCtx"),
mock.AnythingOfType("domain.ListResourcesFilter")).Return(invalidResources, nil).Once()
s.resourceService.EXPECT().GetResourcesTotalCount(mock.AnythingOfType("*context.cancelCtx"),
mock.AnythingOfType("domain.ListResourcesFilter")).Return(0, nil).Once()

req := &guardianv1beta1.ListResourcesRequest{}
res, err := s.grpcServer.ListResources(context.Background(), req)
Expand Down
Loading
Loading