From 220920edb99cbe95c44f92fa7ab89cdf9d0b25be Mon Sep 17 00:00:00 2001 From: Michal Niewrzal Date: Wed, 22 Nov 2023 11:50:02 +0100 Subject: [PATCH] satellite/metainfo: return NotFound status for (Get|Set)BucketVersioning We need to return NotFound rpc status when bucket doesn't exist. Change-Id: I3abdd588e2af288618b05513884bc0c545bcbacf --- satellite/metainfo/endpoint_bucket.go | 6 +++++ satellite/metainfo/endpoint_bucket_test.go | 26 ++++++++++++++++++++++ satellite/satellitedb/bucketsdb.go | 10 +++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/satellite/metainfo/endpoint_bucket.go b/satellite/metainfo/endpoint_bucket.go index a118e08ec..adc4b3d30 100644 --- a/satellite/metainfo/endpoint_bucket.go +++ b/satellite/metainfo/endpoint_bucket.go @@ -104,6 +104,9 @@ func (endpoint *Endpoint) GetBucketVersioning(ctx context.Context, req *pb.GetBu versioning, err := endpoint.buckets.GetBucketVersioningState(ctx, req.GetName(), keyInfo.ProjectID) if err != nil { + if buckets.ErrBucketNotFound.Has(err) { + return nil, rpcstatus.Error(rpcstatus.NotFound, err.Error()) + } endpoint.log.Error("internal", zap.Error(err)) return nil, rpcstatus.Error(rpcstatus.Internal, "unable to get versioning state for the bucket") } @@ -138,6 +141,9 @@ func (endpoint *Endpoint) SetBucketVersioning(ctx context.Context, req *pb.SetBu err = endpoint.buckets.SuspendBucketVersioning(ctx, req.GetName(), keyInfo.ProjectID) } if err != nil { + if buckets.ErrBucketNotFound.Has(err) { + return nil, rpcstatus.Error(rpcstatus.NotFound, err.Error()) + } endpoint.log.Error("internal", zap.Error(err)) return nil, rpcstatus.Error(rpcstatus.Internal, "unable to enable versioning for the bucket") } diff --git a/satellite/metainfo/endpoint_bucket_test.go b/satellite/metainfo/endpoint_bucket_test.go index 5e784c07f..2296ce43e 100644 --- a/satellite/metainfo/endpoint_bucket_test.go +++ b/satellite/metainfo/endpoint_bucket_test.go @@ -379,6 +379,32 @@ func TestEnableSuspendBucketVersioning(t *testing.T) { return err } + _, err := satellite.API.Metainfo.Endpoint.GetBucketVersioning(ctx, &pb.GetBucketVersioningRequest{ + Header: &pb.RequestHeader{ + ApiKey: planet.Uplinks[0].APIKey[satellite.ID()].SerializeRaw(), + }, + Name: []byte("non-existing-bucket"), + }) + require.True(t, errs2.IsRPC(err, rpcstatus.NotFound)) + + _, err = satellite.API.Metainfo.Endpoint.SetBucketVersioning(ctx, &pb.SetBucketVersioningRequest{ + Header: &pb.RequestHeader{ + ApiKey: planet.Uplinks[0].APIKey[satellite.ID()].SerializeRaw(), + }, + Name: []byte("non-existing-bucket"), + Versioning: true, + }) + require.True(t, errs2.IsRPC(err, rpcstatus.NotFound)) + + _, err = satellite.API.Metainfo.Endpoint.SetBucketVersioning(ctx, &pb.SetBucketVersioningRequest{ + Header: &pb.RequestHeader{ + ApiKey: planet.Uplinks[0].APIKey[satellite.ID()].SerializeRaw(), + }, + Name: []byte("non-existing-bucket"), + Versioning: false, + }) + require.True(t, errs2.IsRPC(err, rpcstatus.NotFound)) + for _, tt := range []struct { name string initialVersioningState buckets.Versioning diff --git a/satellite/satellitedb/bucketsdb.go b/satellite/satellitedb/bucketsdb.go index 5dd043b2e..52bbcc18d 100644 --- a/satellite/satellitedb/bucketsdb.go +++ b/satellite/satellitedb/bucketsdb.go @@ -135,7 +135,10 @@ func (db *bucketsDB) EnableBucketVersioning(ctx context.Context, bucketName []by if err != nil { return buckets.ErrBucket.Wrap(err) } - if dbxBucket == nil || buckets.Versioning(dbxBucket.Versioning) != buckets.VersioningEnabled { + if dbxBucket == nil { + return buckets.ErrBucketNotFound.New("%s", bucketName) + } + if buckets.Versioning(dbxBucket.Versioning) != buckets.VersioningEnabled { return buckets.ErrBucket.New("cannot transition bucket versioning state to enabled") } return nil @@ -155,7 +158,10 @@ func (db *bucketsDB) SuspendBucketVersioning(ctx context.Context, bucketName []b if err != nil { return buckets.ErrBucket.Wrap(err) } - if dbxBucket == nil || buckets.Versioning(dbxBucket.Versioning) != buckets.VersioningSuspended { + if dbxBucket == nil { + return buckets.ErrBucketNotFound.New("%s", bucketName) + } + if buckets.Versioning(dbxBucket.Versioning) != buckets.VersioningSuspended { return buckets.ErrBucket.New("cannot transition bucket versioning state to suspended") } return nil