Skip to content

Commit

Permalink
add filters when getting spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Oct 25, 2023
1 parent 35e8ee8 commit 94e2767
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion internal/grpc/services/spacesregistry/spacesregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *service) CreateStorageSpace(ctx context.Context, req *provider.CreateSt

func (s *service) ListStorageSpaces(ctx context.Context, req *provider.ListStorageSpacesRequest) (*provider.ListStorageSpacesResponse, error) {
user := appctx.ContextMustGetUser(ctx)
spaces, err := s.spaces.ListSpaces(ctx, user)
spaces, err := s.spaces.ListSpaces(ctx, user, req.Filters)
if err != nil {
return &provider.ListStorageSpacesResponse{
Status: status.NewInternal(ctx, err, "error listing storage spaces"),
Expand Down
9 changes: 0 additions & 9 deletions pkg/spaces/filters.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package spaces

import (
"slices"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
providerpb "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/golang/protobuf/proto"
)

type ListStorageSpaceFilter struct {
Expand Down Expand Up @@ -65,9 +62,3 @@ func (f ListStorageSpaceFilter) ByUser(user *userpb.UserId) ListStorageSpaceFilt
func (f ListStorageSpaceFilter) List() []*providerpb.ListStorageSpacesRequest_Filter {
return f.filters
}

func (f ListStorageSpaceFilter) Match(filter *providerpb.ListStorageSpacesRequest_Filter) bool {
return slices.ContainsFunc(f.filters, func(f *providerpb.ListStorageSpacesRequest_Filter) bool {
return proto.Equal(f, filter)
})
}
34 changes: 27 additions & 7 deletions pkg/spaces/manager/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"slices"

"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/spaces"
"github.com/cs3org/reva/pkg/spaces/manager/registry"
"github.com/cs3org/reva/pkg/storage/utils/templates"
Expand Down Expand Up @@ -80,18 +81,37 @@ func (s *service) StoreSpace(ctx context.Context, owner *userpb.UserId, path, na
return errors.New("not yet implemented")
}

func (s *service) ListSpaces(ctx context.Context, user *userpb.User) ([]*provider.StorageSpace, error) {
func (s *service) listSpacesByType(ctx context.Context, user *userpb.User, spaceType spaces.SpaceType) []*provider.StorageSpace {
sp := []*provider.StorageSpace{}

// home space
if space := s.userSpace(ctx, user); space != nil {
sp = append(sp, space)
if spaceType == spaces.SpaceTypeHome {
if space := s.userSpace(ctx, user); space != nil {
sp = append(sp, space)
}
} else if spaceType == spaces.SpaceTypeProject {
projects := s.projectSpaces(ctx, user)
sp = append(sp, projects...)
}

// project spaces
projects := s.projectSpaces(ctx, user)
sp = append(sp, projects...)
return sp
}

func (s *service) ListSpaces(ctx context.Context, user *userpb.User, filters []*provider.ListStorageSpacesRequest_Filter) ([]*provider.StorageSpace, error) {
sp := []*provider.StorageSpace{}
if len(filters) == 0 {
sp = s.listSpacesByType(ctx, user, spaces.SpaceTypeHome)
sp = append(sp, s.listSpacesByType(ctx, user, spaces.SpaceTypeProject)...)
return sp, nil
}

for _, filter := range filters {
switch filter.Type {
case provider.ListStorageSpacesRequest_Filter_TYPE_SPACE_TYPE:
sp = append(sp, s.listSpacesByType(ctx, user, spaces.SpaceType(filter.Term.(*provider.ListStorageSpacesRequest_Filter_SpaceType).SpaceType))...)
default:
return nil, errtypes.NotSupported("filter not supported")
}
}
return sp, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/spaces/manager/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestListSpaces(t *testing.T) {
t.Fatalf("got unexpected error creating new memory spaces provider: %+v", err)
}

got, err := s.ListSpaces(context.Background(), tt.user)
got, err := s.ListSpaces(context.Background(), tt.user, nil)
if err != nil {
t.Fatalf("got unexpected error getting list of spaces: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/spaces/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
// Manager is the interface that stores the spaces.
type Manager interface {
StoreSpace(ctx context.Context, owner *userpb.UserId, path, name string, quota *provider.Quota) error
ListSpaces(ctx context.Context, user *userpb.User) ([]*provider.StorageSpace, error)
ListSpaces(ctx context.Context, user *userpb.User, filters []*provider.ListStorageSpacesRequest_Filter) ([]*provider.StorageSpace, error)
UpdateSpace(ctx context.Context, space *provider.StorageSpace) error
DeleteSpace(ctx context.Context, spaceID *provider.StorageSpaceId) error
}
Expand Down

0 comments on commit 94e2767

Please sign in to comment.