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 to ListResources API #36

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -2,7 +2,7 @@ GOVERSION := $(shell go version | cut -d ' ' -f 3 | cut -d '.' -f 2)

.PHONY: build check fmt lint test test-race vet test-cover-html help install proto
.DEFAULT_GOAL := build
PROTON_COMMIT := "cf2c230788bd298aa64c6e18dc79f0c0d1a9d076"
PROTON_COMMIT := "253aa80d6a65c575cdf4a6a1433867fd4d8092ca"

install:
@echo "Clean up imports..."
Expand Down
7 changes: 7 additions & 0 deletions core/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type Filter struct {
GroupID string
OrganizationID string
NamespaceID string
Limit int32
Page int32
}

type YAML struct {
Expand All @@ -59,3 +61,8 @@ type YAML struct {
ResourceType string `json:"resource_type" yaml:"resource_type"`
Actions map[string][]string `json:"actions" yaml:"actions"`
}

type PagedResources struct {
Count int32
Resources []Resource
}
11 changes: 9 additions & 2 deletions core/resource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,15 @@ func (s Service) Create(ctx context.Context, res Resource) (Resource, error) {
return newResource, nil
}

func (s Service) List(ctx context.Context, flt Filter) ([]Resource, error) {
return s.repository.List(ctx, flt)
func (s Service) List(ctx context.Context, flt Filter) (PagedResources, error) {
resources, err := s.repository.List(ctx, flt)
if err != nil {
return PagedResources{}, err
}
return PagedResources{
Count: int32(len(resources)),
Resources: resources,
}, nil
}

func (s Service) Update(ctx context.Context, id string, resource Resource) (Resource, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/action_service.go

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

2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/group_service.go

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

2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/namespace_service.go

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

2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/organization_service.go

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

2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/policy_service.go

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

2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/project_service.go

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

2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/relation_service.go

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

2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/relation_transformer.go

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

18 changes: 8 additions & 10 deletions internal/api/v1beta1/mocks/resource_service.go

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

2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/role_service.go

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

2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/rule_service.go

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

2 changes: 1 addition & 1 deletion internal/api/v1beta1/mocks/user_service.go

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

8 changes: 6 additions & 2 deletions internal/api/v1beta1/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

type ResourceService interface {
Get(ctx context.Context, id string) (resource.Resource, error)
List(ctx context.Context, flt resource.Filter) ([]resource.Resource, error)
List(ctx context.Context, flt resource.Filter) (resource.PagedResources, error)
Create(ctx context.Context, resource resource.Resource) (resource.Resource, error)
Update(ctx context.Context, id string, resource resource.Resource) (resource.Resource, error)
CheckAuthz(ctx context.Context, resource resource.Resource, action action.Action) (bool, error)
Expand All @@ -36,14 +36,17 @@ func (h Handler) ListResources(ctx context.Context, request *shieldv1beta1.ListR
OrganizationID: request.GetOrganizationId(),
ProjectID: request.GetProjectId(),
GroupID: request.GetGroupId(),
Limit: request.GetPageSize(),
Page: request.GetPageNum(),
}

resourcesList, err := h.resourceService.List(ctx, filters)
resourcesResp, err := h.resourceService.List(ctx, filters)
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
}

resourcesList := resourcesResp.Resources
for _, r := range resourcesList {
mabdh marked this conversation as resolved.
Show resolved Hide resolved
resourcePB, err := transformResourceToPB(r)
if err != nil {
Expand All @@ -54,6 +57,7 @@ func (h Handler) ListResources(ctx context.Context, request *shieldv1beta1.ListR
}

return &shieldv1beta1.ListResourcesResponse{
Count: resourcesResp.Count,
Resources: resources,
}, nil
}
Expand Down
16 changes: 12 additions & 4 deletions internal/api/v1beta1/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestHandler_ListResources(t *testing.T) {
{
name: "should return internal error if resource service return some error",
setup: func(rs *mocks.ResourceService) {
rs.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), resource.Filter{}).Return([]resource.Resource{}, errors.New("some error"))
rs.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), resource.Filter{}).Return(resource.PagedResources{}, errors.New("some error"))
},
request: &shieldv1beta1.ListResourcesRequest{},
want: nil,
Expand All @@ -71,12 +71,20 @@ func TestHandler_ListResources(t *testing.T) {
{
name: "should return resources if resource service return nil error",
setup: func(rs *mocks.ResourceService) {
rs.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), resource.Filter{}).Return([]resource.Resource{
testResource,
}, nil)
testResourceList := []resource.Resource{testResource}
rs.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), resource.Filter{}).Return(
resource.PagedResources{
Count: int32(len(testResourceList)),
Resources: testResourceList,
}, nil)
},
request: &shieldv1beta1.ListResourcesRequest{},
want: &shieldv1beta1.ListResourcesResponse{
Count: int32(
len([]*shieldv1beta1.Resource{
testResourcePB,
},
)),
Resources: []*shieldv1beta1.Resource{
testResourcePB,
},
Expand Down
2 changes: 1 addition & 1 deletion internal/proxy/hook/authz/mocks/relation_service.go

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

2 changes: 1 addition & 1 deletion internal/proxy/hook/authz/mocks/relation_transformer.go

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

2 changes: 1 addition & 1 deletion internal/proxy/hook/authz/mocks/resource_service.go

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

12 changes: 12 additions & 0 deletions internal/store/postgres/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ func (r ResourceRepository) Create(ctx context.Context, res resource.Resource) (
func (r ResourceRepository) List(ctx context.Context, flt resource.Filter) ([]resource.Resource, error) {
var fetchedResources []Resource

var defaultLimit int32 = 50
mabdh marked this conversation as resolved.
Show resolved Hide resolved
var defaultPage int32 = 1
if flt.Limit < 1 {
flt.Limit = defaultLimit
}
if flt.Page < 1 {
flt.Page = defaultPage
}

offset := (flt.Page - 1) * flt.Limit

sqlStatement := dialect.From(TABLE_RESOURCES)
if flt.ProjectID != "" {
sqlStatement = sqlStatement.Where(goqu.Ex{"project_id": flt.ProjectID})
Expand All @@ -97,6 +108,7 @@ func (r ResourceRepository) List(ctx context.Context, flt resource.Filter) ([]re
if flt.NamespaceID != "" {
sqlStatement = sqlStatement.Where(goqu.Ex{"namespace_id": flt.NamespaceID})
}
sqlStatement = sqlStatement.Limit(uint(flt.Limit)).Offset(uint(offset))
query, params, err := sqlStatement.ToSQL()
if err != nil {
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions proto/shield.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,16 @@ paths:
in: query
required: false
type: string
- name: page_size
in: query
required: false
type: integer
format: int32
- name: page_num
in: query
required: false
type: integer
format: int32
tags:
- Resource
post:
Expand Down Expand Up @@ -1235,6 +1245,9 @@ definitions:
items:
type: object
$ref: '#/definitions/Resource'
count:
type: integer
format: int32
ListRolesResponse:
type: object
properties:
Expand Down
Loading
Loading