-
Notifications
You must be signed in to change notification settings - Fork 124
/
project_parser_s3.go
91 lines (77 loc) · 2.8 KB
/
project_parser_s3.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
package model
import (
"bytes"
"context"
"io"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/pail"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
// ParserProjectS3Storage implements the ParserProjectStorage interface to
// access parser projects stored in S3.
type ParserProjectS3Storage struct {
bucket pail.Bucket
}
// NewParserProjectS3Storage sets up access to parser projects stored in S3.
func NewParserProjectS3Storage(ppConf evergreen.ParserProjectS3Config) (*ParserProjectS3Storage, error) {
b, err := pail.NewS3MultiPartBucket(pail.S3Options{
Name: ppConf.Bucket,
Prefix: ppConf.Prefix,
Region: endpoints.UsEast1RegionID,
})
if err != nil {
return nil, errors.Wrap(err, "setting up S3 multipart bucket")
}
return &ParserProjectS3Storage{bucket: b}, nil
}
// FindOneByID finds a parser project in S3 using its ID. If the context errors,
// it will return the context error.
func (s *ParserProjectS3Storage) FindOneByID(ctx context.Context, id string) (*ParserProject, error) {
r, err := s.bucket.Get(ctx, id)
if pail.IsKeyNotFoundError(err) {
return nil, nil
}
if err != nil {
return nil, errors.Wrapf(err, "getting parser project '%s'", id)
}
defer r.Close()
b, err := io.ReadAll(r)
if err != nil {
return nil, errors.Wrapf(err, "reading parser project '%s'", id)
}
var pp ParserProject
if err := bson.Unmarshal(b, &pp); err != nil {
return nil, errors.Wrapf(err, "unmarshalling parser project '%s' from BSON", id)
}
if pp.Functions == nil {
pp.Functions = map[string]*YAMLCommandSet{}
}
return &pp, nil
}
// FindOneByIDWithFields finds a parser project using its ID from S3 and returns
// the parser project with only the requested fields populated. This is not any
// more efficient than FindOneByID. If the context errors, it will return the
// context error.
func (s *ParserProjectS3Storage) FindOneByIDWithFields(ctx context.Context, id string, fields ...string) (*ParserProject, error) {
return s.FindOneByID(ctx, id)
}
// UpsertOne replaces a parser project if the parser project in S3 with the same
// ID already exists. If it does not exist yet, it inserts a new parser project.
func (s *ParserProjectS3Storage) UpsertOne(ctx context.Context, pp *ParserProject) error {
bsonPP, err := bson.Marshal(pp)
if err != nil {
return errors.Wrapf(err, "marshalling parser project '%s' to BSON", pp.Id)
}
return s.UpsertOneBSON(ctx, pp.Id, bsonPP)
}
// UpsertOneBSON upserts a parser project by its ID when has already been
// marshalled to BSON.
func (s *ParserProjectS3Storage) UpsertOneBSON(ctx context.Context, id string, bsonPP []byte) error {
r := bytes.NewBuffer(bsonPP)
if err := s.bucket.Put(ctx, id, r); err != nil {
return errors.Wrapf(err, "upserting BSON parser project '%s'", id)
}
return nil
}