-
Notifications
You must be signed in to change notification settings - Fork 17
/
bucket.go
161 lines (131 loc) · 4.49 KB
/
bucket.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package uplink
import (
"context"
"errors"
"time"
"github.com/zeebo/errs"
"storj.io/common/errs2"
"storj.io/common/rpc/rpcstatus"
"storj.io/uplink/private/metaclient"
)
// ErrBucketNameInvalid is returned when the bucket name is invalid.
var ErrBucketNameInvalid = errors.New("bucket name invalid")
// ErrBucketAlreadyExists is returned when the bucket already exists during creation.
var ErrBucketAlreadyExists = errors.New("bucket already exists")
// ErrBucketNotEmpty is returned when the bucket is not empty during deletion.
var ErrBucketNotEmpty = errors.New("bucket not empty")
// ErrBucketNotFound is returned when the bucket is not found.
var ErrBucketNotFound = errors.New("bucket not found")
// Bucket contains information about the bucket.
type Bucket struct {
Name string
Created time.Time
}
// StatBucket returns information about a bucket.
func (project *Project) StatBucket(ctx context.Context, bucket string) (info *Bucket, err error) {
defer mon.Task()(&ctx)(&err)
db, err := project.dialMetainfoDB(ctx)
if err != nil {
return nil, convertKnownErrors(err, bucket, "")
}
defer func() { err = errs.Combine(err, db.Close()) }()
b, err := db.GetBucket(ctx, bucket)
if err != nil {
return nil, convertKnownErrors(err, bucket, "")
}
return &Bucket{
Name: b.Name,
Created: b.Created,
}, nil
}
// CreateBucket creates a new bucket.
//
// When bucket already exists it returns a valid Bucket and ErrBucketExists.
func (project *Project) CreateBucket(ctx context.Context, bucket string) (created *Bucket, err error) {
defer mon.Task()(&ctx)(&err)
db, err := project.dialMetainfoDB(ctx)
if err != nil {
return nil, convertKnownErrors(err, bucket, "")
}
defer func() { err = errs.Combine(err, db.Close()) }()
b, err := db.CreateBucket(ctx, bucket)
if err != nil {
if metaclient.ErrNoBucket.Has(err) {
return nil, errwrapf("%w (%q)", ErrBucketNameInvalid, bucket)
}
if errs2.IsRPC(err, rpcstatus.AlreadyExists) {
// TODO: Ideally, the satellite should return the existing bucket when this error occurs.
existing, err := project.StatBucket(ctx, bucket)
if err != nil {
return existing, errs.Combine(errwrapf("%w (%q)", ErrBucketAlreadyExists, bucket), convertKnownErrors(err, bucket, ""))
}
return existing, errwrapf("%w (%q)", ErrBucketAlreadyExists, bucket)
}
if errs2.IsRPC(err, rpcstatus.InvalidArgument) {
return nil, errwrapf("%w (%q)", ErrBucketNameInvalid, bucket)
}
return nil, convertKnownErrors(err, bucket, "")
}
return &Bucket{
Name: b.Name,
Created: b.Created,
}, nil
}
// EnsureBucket ensures that a bucket exists or creates a new one.
//
// When bucket already exists it returns a valid Bucket and no error.
func (project *Project) EnsureBucket(ctx context.Context, bucket string) (ensured *Bucket, err error) {
defer mon.Task()(&ctx)(&err)
ensured, err = project.CreateBucket(ctx, bucket)
if err != nil && !errors.Is(err, ErrBucketAlreadyExists) {
return nil, convertKnownErrors(err, bucket, "")
}
return ensured, nil
}
// DeleteBucket deletes a bucket.
//
// When bucket is not empty it returns ErrBucketNotEmpty.
func (project *Project) DeleteBucket(ctx context.Context, bucket string) (deleted *Bucket, err error) {
defer mon.Task()(&ctx)(&err)
db, err := project.dialMetainfoDB(ctx)
if err != nil {
return nil, convertKnownErrors(err, bucket, "")
}
defer func() { err = errs.Combine(err, db.Close()) }()
existing, err := db.DeleteBucket(ctx, bucket, false)
if err != nil {
if errs2.IsRPC(err, rpcstatus.FailedPrecondition) {
return nil, errwrapf("%w (%q)", ErrBucketNotEmpty, bucket)
}
return nil, convertKnownErrors(err, bucket, "")
}
if len(existing.Name) == 0 {
return &Bucket{Name: bucket}, nil
}
return &Bucket{
Name: existing.Name,
Created: existing.Created,
}, nil
}
// DeleteBucketWithObjects deletes a bucket and all objects within that bucket.
func (project *Project) DeleteBucketWithObjects(ctx context.Context, bucket string) (deleted *Bucket, err error) {
defer mon.Task()(&ctx)(&err)
db, err := project.dialMetainfoDB(ctx)
if err != nil {
return nil, convertKnownErrors(err, bucket, "")
}
defer func() { err = errs.Combine(err, db.Close()) }()
existing, err := db.DeleteBucket(ctx, bucket, true)
if err != nil {
return nil, convertKnownErrors(err, bucket, "")
}
if len(existing.Name) == 0 {
return &Bucket{Name: bucket}, nil
}
return &Bucket{
Name: existing.Name,
Created: existing.Created,
}, nil
}