-
Notifications
You must be signed in to change notification settings - Fork 402
/
service.go
57 lines (46 loc) · 1.53 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package buckets
import (
"context"
"github.com/zeebo/errs"
"storj.io/storj/satellite/metabase"
)
var (
// ErrBucketNotEmpty is returned when a caller attempts to change placement constraints.
ErrBucketNotEmpty = errs.Class("bucket must be empty")
)
// NewService converts the provided db and metabase calls into a single DB interface.
func NewService(bucketsDB DB, metabase *metabase.DB) *Service {
return &Service{
DB: bucketsDB,
metabase: metabase,
}
}
// Service encapsulates operations around buckets.
type Service struct {
DB
metabase *metabase.DB
}
// UpdateBucket overrides the default UpdateBucket behaviour by adding a check against MetabaseDB to ensure the bucket
// is empty before attempting to change the placement constraint of a bucket. If the placement constraint is not being
// changed, then this additional check is skipped.
func (buckets *Service) UpdateBucket(ctx context.Context, bucket Bucket) (Bucket, error) {
current, err := buckets.GetBucket(ctx, []byte(bucket.Name), bucket.ProjectID)
if err != nil {
return Bucket{}, err
}
if current.Placement != bucket.Placement {
ok, err := buckets.metabase.BucketEmpty(ctx, metabase.BucketEmpty{
ProjectID: bucket.ProjectID,
BucketName: bucket.Name,
})
switch {
case err != nil:
return Bucket{}, err
case !ok:
return Bucket{}, ErrBucketNotEmpty.New("cannot modify placement constraint for non-empty bucket")
}
}
return buckets.DB.UpdateBucket(ctx, bucket)
}