From b96ac5422480d7d7b5d3c0bad9e4f76fe8fe0d96 Mon Sep 17 00:00:00 2001 From: David Christofas Date: Thu, 20 Jan 2022 17:08:57 +0100 Subject: [PATCH] refactor ListStorageSpaces filter handling --- graph/pkg/service/v0/drives.go | 49 ++++++++++++++++------------------ 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/graph/pkg/service/v0/drives.go b/graph/pkg/service/v0/drives.go index 9b0c1745a..d534444e1 100644 --- a/graph/pkg/service/v0/drives.go +++ b/graph/pkg/service/v0/drives.go @@ -100,16 +100,7 @@ func (g Graph) GetSingleDrive(w http.ResponseWriter, r *http.Request) { g.logger.Info().Str("driveID", driveID).Msg("Calling GetSingleDrive") ctx := r.Context() - filters := []*storageprovider.ListStorageSpacesRequest_Filter{ - { - Type: storageprovider.ListStorageSpacesRequest_Filter_TYPE_ID, - Term: &storageprovider.ListStorageSpacesRequest_Filter_Id{ - Id: &storageprovider.StorageSpaceId{ - OpaqueId: driveID, - }, - }, - }, - } + filters := []*storageprovider.ListStorageSpacesRequest_Filter{listStorageSpacesIDFilter(driveID)} res, err := g.ListStorageSpacesWithFilters(ctx, filters) switch { case err != nil: @@ -710,23 +701,9 @@ func generateCs3Filters(request *godata.GoDataRequest) ([]*storageprovider.ListS if request.Query.Filter.Tree.Token.Value == "eq" { switch request.Query.Filter.Tree.Children[0].Token.Value { case "driveType": - filter1 := &storageprovider.ListStorageSpacesRequest_Filter{ - Type: storageprovider.ListStorageSpacesRequest_Filter_TYPE_SPACE_TYPE, - Term: &storageprovider.ListStorageSpacesRequest_Filter_SpaceType{ - SpaceType: strings.Trim(request.Query.Filter.Tree.Children[1].Token.Value, "'"), - }, - } - filters = append(filters, filter1) + filters = append(filters, listStorageSpacesTypeFilter(strings.Trim(request.Query.Filter.Tree.Children[1].Token.Value, "'"))) case "id": - filter2 := &storageprovider.ListStorageSpacesRequest_Filter{ - Type: storageprovider.ListStorageSpacesRequest_Filter_TYPE_ID, - Term: &storageprovider.ListStorageSpacesRequest_Filter_Id{ - Id: &storageprovider.StorageSpaceId{ - OpaqueId: strings.Trim(request.Query.Filter.Tree.Children[1].Token.Value, "'"), - }, - }, - } - filters = append(filters, filter2) + filters = append(filters, listStorageSpacesIDFilter(strings.Trim(request.Query.Filter.Tree.Children[1].Token.Value, "'"))) } } else { err := fmt.Errorf("unsupported filter operand: %s", request.Query.Filter.Tree.Token.Value) @@ -736,6 +713,26 @@ func generateCs3Filters(request *godata.GoDataRequest) ([]*storageprovider.ListS return filters, nil } +func listStorageSpacesIDFilter(id string) *storageprovider.ListStorageSpacesRequest_Filter { + return &storageprovider.ListStorageSpacesRequest_Filter{ + Type: storageprovider.ListStorageSpacesRequest_Filter_TYPE_ID, + Term: &storageprovider.ListStorageSpacesRequest_Filter_Id{ + Id: &storageprovider.StorageSpaceId{ + OpaqueId: id, + }, + }, + } +} + +func listStorageSpacesTypeFilter(spaceType string) *storageprovider.ListStorageSpacesRequest_Filter { + return &storageprovider.ListStorageSpacesRequest_Filter{ + Type: storageprovider.ListStorageSpacesRequest_Filter_TYPE_SPACE_TYPE, + Term: &storageprovider.ListStorageSpacesRequest_Filter_SpaceType{ + SpaceType: spaceType, + }, + } +} + func (g Graph) DeleteDrive(w http.ResponseWriter, r *http.Request) { driveID, err := url.PathUnescape(chi.URLParam(r, "driveID")) if err != nil {