-
Notifications
You must be signed in to change notification settings - Fork 208
/
mongodb.go
328 lines (264 loc) · 7.87 KB
/
mongodb.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package mongodb
import (
"context"
"errors"
"fmt"
"strings"
"time"
"get.porter.sh/porter/pkg/portercontext"
"get.porter.sh/porter/pkg/storage/plugins"
"get.porter.sh/porter/pkg/tracing"
"github.com/davecgh/go-spew/spew"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
"go.opentelemetry.io/otel/attribute"
)
var (
_ plugins.StorageProtocol = &Store{}
ErrNotConnected = errors.New("cannot execute command against the mongodb plugin because the session is closed (or was never connected)")
)
// Store implements the Porter plugin.StoragePlugin interface for mongodb.
type Store struct {
*portercontext.Context
url string
database string
client *mongo.Client
timeout time.Duration
}
// NewStore creates a new storage engine that uses MongoDB.
func NewStore(c *portercontext.Context, cfg PluginConfig) *Store {
timeout := cfg.Timeout
if timeout <= 0 {
timeout = 10 // default to 10 seconds
}
return &Store{
Context: c,
url: cfg.URL,
timeout: time.Duration(timeout) * time.Second,
}
}
// Connect initializes the plugin for use.
// The plugin itself is responsible for ensuring it was called.
// Close is called automatically when the plugin is used by Porter.
func (s *Store) Connect(ctx context.Context) error {
if s.client != nil {
return nil
}
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
connStr, err := connstring.ParseAndValidate(s.url)
if err != nil {
// I'm not tracing additional information like the url since it is sensitive data
return span.Error(fmt.Errorf("invalid mongodb connection string"))
}
if connStr.Database == "" {
s.database = "porter"
} else {
s.database = strings.TrimSuffix(connStr.Database, "/")
}
span.SetAttributes(attribute.String("database", s.database))
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
client, err := mongo.Connect(cxt, options.Client().ApplyURI(s.url))
if err != nil {
return span.Error(err)
}
s.client = client
return nil
}
func (s *Store) Close() error {
if s.client != nil {
cxt, cancel := context.WithTimeout(context.Background(), s.timeout)
defer cancel()
s.client.Disconnect(cxt)
s.client = nil
}
return nil
}
// Ping the connected session to check if everything is okay.
func (s *Store) Ping(ctx context.Context) error {
if err := s.Connect(ctx); err != nil {
return err
}
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
return s.client.Ping(cxt, readpref.Primary())
}
func (s *Store) Aggregate(ctx context.Context, opts plugins.AggregateOptions) ([]bson.Raw, error) {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if err := s.Connect(ctx); err != nil {
return nil, err
}
// TODO(carolynvs): wrap each call with session.refresh on error and a single retry
c := s.getCollection(opts.Collection)
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
cur, err := c.Aggregate(cxt, opts.Pipeline)
if err != nil {
return nil, err
}
var results []bson.Raw
for cur.Next(cxt) {
results = append(results, cur.Current)
}
return results, err
}
// EnsureIndexes makes sure that the specified indexes exist and are
// defined appropriately.
func (s *Store) EnsureIndex(ctx context.Context, opts plugins.EnsureIndexOptions) error {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if err := s.Connect(ctx); err != nil {
return err
}
indices := make(map[string][]mongo.IndexModel, len(opts.Indices))
for _, index := range opts.Indices {
model := mongo.IndexModel{
Keys: index.Keys,
Options: options.Index(),
}
model.Options.SetUnique(index.Unique)
c, ok := indices[index.Collection]
if !ok {
c = make([]mongo.IndexModel, 0, 1)
}
c = append(c, model)
indices[index.Collection] = c
}
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
for collectionName, models := range indices {
c := s.getCollection(collectionName)
if _, err := c.Indexes().CreateMany(cxt, models); err != nil {
return span.Error(fmt.Errorf("invalid index specified: %v: %w", spew.Sdump(models), err))
}
}
return nil
}
func (s *Store) Count(ctx context.Context, opts plugins.CountOptions) (int64, error) {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if err := s.Connect(ctx); err != nil {
return 0, err
}
c := s.getCollection(opts.Collection)
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
count, err := c.CountDocuments(cxt, opts.Filter)
return count, span.Error(err)
}
func (s *Store) Find(ctx context.Context, opts plugins.FindOptions) ([]bson.Raw, error) {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if err := s.Connect(ctx); err != nil {
return nil, err
}
c := s.getCollection(opts.Collection)
findOpts, err := s.buildFindOptions(opts)
if err != nil {
return nil, span.Error(err)
}
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
cur, err := c.Find(cxt, opts.Filter, findOpts)
if err != nil {
return nil, span.Error(err)
}
defer cur.Close(ctx)
var results []bson.Raw
for cur.Next(cxt) {
results = append(results, cur.Current)
}
return results, span.Error(err)
}
func (s *Store) buildFindOptions(opts plugins.FindOptions) (*options.FindOptions, error) {
query := options.Find()
if opts.Select != nil {
query.SetProjection(opts.Select)
}
if opts.Limit > 0 {
query.SetLimit(opts.Limit)
}
if opts.Skip > 0 {
query.SetSkip(opts.Skip)
}
if opts.Sort != nil {
query.SetSort(opts.Sort)
}
return query, nil
}
func (s *Store) Insert(ctx context.Context, opts plugins.InsertOptions) error {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if err := s.Connect(ctx); err != nil {
return err
}
c := s.getCollection(opts.Collection)
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
docs := make([]interface{}, len(opts.Documents))
for i, doc := range opts.Documents {
docs[i] = doc
}
_, err := c.InsertMany(cxt, docs)
return span.Error(err)
}
func (s *Store) Patch(ctx context.Context, opts plugins.PatchOptions) error {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if err := s.Connect(ctx); err != nil {
return err
}
c := s.getCollection(opts.Collection)
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
_, err := c.UpdateOne(cxt, opts.QueryDocument, opts.Transformation)
return span.Error(err)
}
func (s *Store) Remove(ctx context.Context, opts plugins.RemoveOptions) error {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if err := s.Connect(ctx); err != nil {
return err
}
c := s.getCollection(opts.Collection)
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
if opts.All {
_, err := c.DeleteMany(ctx, opts.Filter)
return span.Error(err)
}
_, err := c.DeleteOne(cxt, opts.Filter)
return span.Error(err)
}
func (s *Store) Update(ctx context.Context, opts plugins.UpdateOptions) error {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if err := s.Connect(ctx); err != nil {
return err
}
c := s.getCollection(opts.Collection)
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
_, err := c.ReplaceOne(cxt, opts.Filter, opts.Document, &options.ReplaceOptions{Upsert: &opts.Upsert})
return span.Error(err)
}
func (s *Store) getCollection(collection string) *mongo.Collection {
return s.client.Database(s.database).Collection(collection)
}
// RemoveDatabase removes the current database.
func (s *Store) RemoveDatabase(ctx context.Context) error {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if err := s.Connect(ctx); err != nil {
return err
}
cxt, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
span.Info("Dropping database", attribute.String("database", s.database))
return s.client.Database(s.database).Drop(cxt)
}