|
| 1 | +// Copyright 2018-2026 CERN |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// In applying this license, CERN does not waive the privileges and immunities |
| 16 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 17 | +// or submit itself to any jurisdiction. |
| 18 | + |
| 19 | +package gateway |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + |
| 25 | + labels "github.com/cs3org/go-cs3apis/cs3/labels/v1beta1" |
| 26 | + rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" |
| 27 | + provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" |
| 28 | + "github.com/cs3org/reva/v3/pkg/appctx" |
| 29 | + "github.com/cs3org/reva/v3/pkg/rgrpc/status" |
| 30 | + "github.com/cs3org/reva/v3/pkg/rgrpc/todo/pool" |
| 31 | + "github.com/pkg/errors" |
| 32 | +) |
| 33 | + |
| 34 | +// resolveRef ensures the reference has a ResourceId by statting it if necessary. |
| 35 | +func (s *svc) resolveRef(ctx context.Context, ref *provider.Reference) (*provider.Reference, *rpc.Status, error) { |
| 36 | + if ref == nil { |
| 37 | + return nil, status.NewInvalidArg(ctx, "ref is required"), nil |
| 38 | + } |
| 39 | + if ref.GetResourceId() != nil { |
| 40 | + return ref, nil, nil |
| 41 | + } |
| 42 | + |
| 43 | + statRes, err := s.Stat(ctx, &provider.StatRequest{Ref: ref}) |
| 44 | + if err != nil { |
| 45 | + return nil, nil, errors.Wrap(err, "gateway: error statting ref") |
| 46 | + } |
| 47 | + if statRes.Status.Code != rpc.Code_CODE_OK { |
| 48 | + return nil, statRes.Status, nil |
| 49 | + } |
| 50 | + |
| 51 | + return &provider.Reference{ResourceId: statRes.Info.Id}, nil, nil |
| 52 | +} |
| 53 | + |
| 54 | +func labelMetadataKey(uid, label string) string { |
| 55 | + return fmt.Sprintf("reva.labels.%s.%s", uid, label) |
| 56 | +} |
| 57 | + |
| 58 | +func (s *svc) AddLabel(ctx context.Context, req *labels.AddLabelRequest) (*labels.AddLabelResponse, error) { |
| 59 | + ref, st, err := s.resolveRef(ctx, req.GetRef()) |
| 60 | + if err != nil { |
| 61 | + return &labels.AddLabelResponse{ |
| 62 | + Status: status.NewInternal(ctx, err, "error resolving ref"), |
| 63 | + }, nil |
| 64 | + } |
| 65 | + if st != nil { |
| 66 | + return &labels.AddLabelResponse{Status: st}, nil |
| 67 | + } |
| 68 | + req.Ref = ref |
| 69 | + |
| 70 | + u := appctx.ContextMustGetUser(ctx) |
| 71 | + |
| 72 | + // Set the label as metadata on the storage provider first. |
| 73 | + metaRes, err := s.SetArbitraryMetadata(ctx, &provider.SetArbitraryMetadataRequest{ |
| 74 | + Ref: ref, |
| 75 | + ArbitraryMetadata: &provider.ArbitraryMetadata{ |
| 76 | + Metadata: map[string]string{ |
| 77 | + labelMetadataKey(u.Id.OpaqueId, req.GetLabel()): "1", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + return &labels.AddLabelResponse{ |
| 83 | + Status: status.NewInternal(ctx, err, "error setting label metadata on storage provider"), |
| 84 | + }, nil |
| 85 | + } |
| 86 | + if metaRes.Status.Code != rpc.Code_CODE_OK { |
| 87 | + return &labels.AddLabelResponse{Status: metaRes.Status}, nil |
| 88 | + } |
| 89 | + |
| 90 | + // Store the label in the labels database. |
| 91 | + c, err := pool.GetLabelsClient(pool.Endpoint(s.c.LabelsEndpoint)) |
| 92 | + if err != nil { |
| 93 | + err = errors.Wrap(err, "gateway: error calling GetLabelsClient") |
| 94 | + return &labels.AddLabelResponse{ |
| 95 | + Status: status.NewInternal(ctx, err, "error getting labels client"), |
| 96 | + }, nil |
| 97 | + } |
| 98 | + |
| 99 | + res, err := c.AddLabel(ctx, req) |
| 100 | + if err != nil { |
| 101 | + return nil, errors.Wrap(err, "gateway: error calling AddLabel") |
| 102 | + } |
| 103 | + |
| 104 | + return res, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (s *svc) RemoveLabel(ctx context.Context, req *labels.RemoveLabelRequest) (*labels.RemoveLabelResponse, error) { |
| 108 | + ref, st, err := s.resolveRef(ctx, req.GetRef()) |
| 109 | + if err != nil { |
| 110 | + return &labels.RemoveLabelResponse{ |
| 111 | + Status: status.NewInternal(ctx, err, "error resolving ref"), |
| 112 | + }, nil |
| 113 | + } |
| 114 | + if st != nil { |
| 115 | + return &labels.RemoveLabelResponse{Status: st}, nil |
| 116 | + } |
| 117 | + req.Ref = ref |
| 118 | + |
| 119 | + u := appctx.ContextMustGetUser(ctx) |
| 120 | + |
| 121 | + // Remove the label metadata from the storage provider first. |
| 122 | + metaRes, err := s.UnsetArbitraryMetadata(ctx, &provider.UnsetArbitraryMetadataRequest{ |
| 123 | + Ref: ref, |
| 124 | + ArbitraryMetadataKeys: []string{labelMetadataKey(u.Id.OpaqueId, req.GetLabel())}, |
| 125 | + }) |
| 126 | + if err != nil { |
| 127 | + return &labels.RemoveLabelResponse{ |
| 128 | + Status: status.NewInternal(ctx, err, "error unsetting label metadata on storage provider"), |
| 129 | + }, nil |
| 130 | + } |
| 131 | + if metaRes.Status.Code != rpc.Code_CODE_OK { |
| 132 | + return &labels.RemoveLabelResponse{Status: metaRes.Status}, nil |
| 133 | + } |
| 134 | + |
| 135 | + // Remove the label from the labels database. |
| 136 | + c, err := pool.GetLabelsClient(pool.Endpoint(s.c.LabelsEndpoint)) |
| 137 | + if err != nil { |
| 138 | + err = errors.Wrap(err, "gateway: error calling GetLabelsClient") |
| 139 | + return &labels.RemoveLabelResponse{ |
| 140 | + Status: status.NewInternal(ctx, err, "error getting labels client"), |
| 141 | + }, nil |
| 142 | + } |
| 143 | + |
| 144 | + res, err := c.RemoveLabel(ctx, req) |
| 145 | + if err != nil { |
| 146 | + return nil, errors.Wrap(err, "gateway: error calling RemoveLabel") |
| 147 | + } |
| 148 | + |
| 149 | + return res, nil |
| 150 | +} |
| 151 | + |
| 152 | +func (s *svc) ListLabels(ctx context.Context, req *labels.ListLabelsRequest) (*labels.ListLabelsResponse, error) { |
| 153 | + c, err := pool.GetLabelsClient(pool.Endpoint(s.c.LabelsEndpoint)) |
| 154 | + if err != nil { |
| 155 | + err = errors.Wrap(err, "gateway: error calling GetLabelsClient") |
| 156 | + return &labels.ListLabelsResponse{ |
| 157 | + Status: status.NewInternal(ctx, err, "error getting labels client"), |
| 158 | + }, nil |
| 159 | + } |
| 160 | + |
| 161 | + res, err := c.ListLabels(ctx, req) |
| 162 | + if err != nil { |
| 163 | + return nil, errors.Wrap(err, "gateway: error calling ListLabels") |
| 164 | + } |
| 165 | + |
| 166 | + return res, nil |
| 167 | +} |
| 168 | + |
| 169 | +func (s *svc) ListResourcesForLabel(ctx context.Context, req *labels.ListResourcesForLabelRequest) (*labels.ListResourcesForLabelResponse, error) { |
| 170 | + c, err := pool.GetLabelsClient(pool.Endpoint(s.c.LabelsEndpoint)) |
| 171 | + if err != nil { |
| 172 | + err = errors.Wrap(err, "gateway: error calling GetLabelsClient") |
| 173 | + return &labels.ListResourcesForLabelResponse{ |
| 174 | + Status: status.NewInternal(ctx, err, "error getting labels client"), |
| 175 | + }, nil |
| 176 | + } |
| 177 | + |
| 178 | + res, err := c.ListResourcesForLabel(ctx, req) |
| 179 | + if err != nil { |
| 180 | + return nil, errors.Wrap(err, "gateway: error calling ListResourcesForLabel") |
| 181 | + } |
| 182 | + |
| 183 | + return res, nil |
| 184 | +} |
0 commit comments