-
Notifications
You must be signed in to change notification settings - Fork 127
/
config_bucket.go
79 lines (63 loc) · 2.09 KB
/
config_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
package evergreen
import (
"context"
"github.com/mongodb/anser/bsonutil"
"github.com/mongodb/grip"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
type BucketType string
const (
BucketTypeGridFS BucketType = "gridfs"
BucketTypeLocal BucketType = "local"
BucketTypeS3 BucketType = "s3"
)
func (b BucketType) validate() error {
switch b {
case BucketTypeGridFS, BucketTypeLocal, BucketTypeS3:
return nil
default:
return errors.Errorf("unrecognized bucket type '%s'", b)
}
}
// BucketsConfig represents the admin config section for interally-owned
// Evergreen data bucket storage.
type BucketsConfig struct {
LogBucket BucketConfig `bson:"log_bucket" json:"log_bucket" yaml:"log_bucket"`
// Credentials for accessing the buckets.
Credentials S3Credentials `bson:"credentials" json:"credentials" yaml:"credentials"`
}
var (
bucketsConfigLogBucketKey = bsonutil.MustHaveTag(BucketsConfig{}, "LogBucket")
bucketsConfigCredentialsKey = bsonutil.MustHaveTag(BucketsConfig{}, "Credentials")
)
// BucketConfig represents the admin config for an individual bucket.
type BucketConfig struct {
Name string `bson:"name" json:"name" yaml:"name"`
Type BucketType `bson:"type" json:"type" yaml:"type"`
DBName string `bson:"db_name" json:"db_name" yaml:"db_name"`
}
func (c *BucketConfig) validate() error {
if c.Type == "" {
c.Type = BucketTypeS3
}
catcher := grip.NewBasicCatcher()
catcher.Add(c.Type.validate())
catcher.NewWhen(c.Type == BucketTypeGridFS && c.DBName == "", "must specify DB name for GridFS bucket")
return catcher.Resolve()
}
func (*BucketsConfig) SectionId() string { return "buckets" }
func (c *BucketsConfig) Get(ctx context.Context) error {
return getConfigSection(ctx, c)
}
func (c *BucketsConfig) Set(ctx context.Context) error {
return errors.Wrapf(setConfigSection(ctx, c.SectionId(), bson.M{
"$set": bson.M{
bucketsConfigLogBucketKey: c.LogBucket,
bucketsConfigCredentialsKey: c.Credentials,
}}), "updating config section '%s'", c.SectionId(),
)
}
func (c *BucketsConfig) ValidateAndDefault() error {
return c.LogBucket.validate()
}