-
Notifications
You must be signed in to change notification settings - Fork 716
/
Copy pathinit.go
295 lines (265 loc) · 8.33 KB
/
init.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
package mongodb
import (
"context"
"errors"
"time"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Enum for Database collections
const (
ChaosInfraCollection = iota
ChaosExperimentCollection
ChaosExperimentRunsCollection
ChaosHubCollection
ImageRegistryCollection
ServerConfigCollection
GitOpsCollection
UserCollection
ProjectCollection
EnvironmentCollection
ChaosProbeCollection
)
// MongoInterface requires a MongoClient that implements the Initialize method to create the Mongo DB client
// and a initAllCollection method to initialize all DB Collections
type MongoInterface interface {
Initialize(client *mongo.Client) *MongoClient
initAllCollection()
}
// MongoClient structure contains all the Database collections and the instance of the Database
type MongoClient struct {
Database *mongo.Database
ChaosInfraCollection *mongo.Collection
ChaosExperimentCollection *mongo.Collection
ChaosExperimentRunsCollection *mongo.Collection
ChaosHubCollection *mongo.Collection
ChaosServerConfigCollection *mongo.Collection
ImageRegistryCollection *mongo.Collection
ServerConfigCollection *mongo.Collection
GitOpsCollection *mongo.Collection
UserCollection *mongo.Collection
ProjectCollection *mongo.Collection
EnvironmentCollection *mongo.Collection
ChaosProbeCollection *mongo.Collection
}
var (
Client MongoInterface = &MongoClient{}
MgoClient *mongo.Client
Collections = map[int]string{
ChaosInfraCollection: "chaosInfrastructures",
ChaosExperimentCollection: "chaosExperiments",
ChaosExperimentRunsCollection: "chaosExperimentRuns",
ChaosProbeCollection: "chaosProbes",
ChaosHubCollection: "chaosHubs",
ImageRegistryCollection: "imageRegistry",
ServerConfigCollection: "serverConfig",
GitOpsCollection: "gitops",
UserCollection: "user",
ProjectCollection: "project",
EnvironmentCollection: "environment",
}
DbName = "litmus"
ConnectionTimeout = 20 * time.Second
backgroundContext = context.Background()
)
func MongoConnection() (*mongo.Client, error) {
var (
dbServer = utils.Config.DbServer
dbUser = utils.Config.DbUser
dbPassword = utils.Config.DbPassword
)
if dbServer == "" || dbUser == "" || dbPassword == "" {
return nil, errors.New("DB configuration failed")
}
credential := options.Credential{
Username: dbUser,
Password: dbPassword,
}
clientOptions := options.Client().ApplyURI(dbServer).SetAuth(credential)
client, err := mongo.Connect(backgroundContext, clientOptions)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(backgroundContext, ConnectionTimeout)
defer cancel()
// Check the connection
err = client.Ping(ctx, nil)
if err != nil {
return nil, err
}
logrus.Infof("connected to mongo")
return client, nil
}
// Initialize initializes database connection
func (m *MongoClient) Initialize(client *mongo.Client) *MongoClient {
m.Database = client.Database(DbName)
m.initAllCollection()
return m
}
// initAllCollection initializes all the database collections
func (m *MongoClient) initAllCollection() {
m.UserCollection = m.Database.Collection(Collections[UserCollection])
m.ProjectCollection = m.Database.Collection(Collections[ProjectCollection])
// Initialize chaos infra collection
err := m.Database.CreateCollection(context.TODO(), Collections[ChaosInfraCollection], nil)
if err != nil {
logrus.WithError(err).Error("failed to create chaosInfrastructures collection")
}
m.ChaosInfraCollection = m.Database.Collection(Collections[ChaosInfraCollection])
_, err = m.ChaosInfraCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
{
Keys: bson.M{
"infra_id": 1,
},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.M{
"name": 1,
},
},
})
if err != nil {
logrus.WithError(err).Error("failed to create indexes for chaosInfrastructures collection")
}
// Initialize chaos experiment collection
err = m.Database.CreateCollection(context.TODO(), Collections[ChaosExperimentCollection], nil)
if err != nil {
logrus.WithError(err).Error("failed to create chaosExperiments collection")
}
m.ChaosExperimentCollection = m.Database.Collection(Collections[ChaosExperimentCollection])
_, err = m.ChaosExperimentCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
{
Keys: bson.M{
"experiment_id": 1,
},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.M{
"name": 1,
},
},
})
if err != nil {
logrus.WithError(err).Error("failed to create indexes for chaosExperiments collection")
}
// Initialize chaos experiment runs collection
err = m.Database.CreateCollection(context.TODO(), Collections[ChaosExperimentRunsCollection], nil)
if err != nil {
logrus.WithError(err).Error("failed to create chaosExperimentRuns collection")
}
m.ChaosExperimentRunsCollection = m.Database.Collection(Collections[ChaosExperimentRunsCollection])
_, err = m.ChaosExperimentRunsCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
{
Keys: bson.M{
"experiment_run_id": 1,
},
},
})
if err != nil {
logrus.WithError(err).Fatal("failed to create indexes for chaosExperimentRuns collection")
}
// Initialize chaos hubs collection
err = m.Database.CreateCollection(context.TODO(), Collections[ChaosHubCollection], nil)
if err != nil {
logrus.WithError(err).Error("failed to create chaosHubs collection")
}
m.ChaosHubCollection = m.Database.Collection(Collections[ChaosHubCollection])
_, err = m.ChaosHubCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
{
Keys: bson.M{
"hub_id": 1,
},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.M{
"name": 1,
},
},
})
if err != nil {
logrus.WithError(err).Fatal("failed to create indexes for chaosHubs collection")
}
m.GitOpsCollection = m.Database.Collection(Collections[GitOpsCollection])
_, err = m.GitOpsCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
{
Keys: bson.M{
"project_id": 1,
},
Options: options.Index().SetUnique(true),
},
})
if err != nil {
logrus.WithError(err).Fatal("Error Creating Index for GitOps Collection")
}
m.ImageRegistryCollection = m.Database.Collection(Collections[ImageRegistryCollection])
_, err = m.ImageRegistryCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
{
Keys: bson.M{
"project_id": 1,
},
Options: options.Index().SetUnique(true),
},
})
if err != nil {
logrus.WithError(err).Fatal("Error Creating Index for Image Registry Collection")
}
m.ServerConfigCollection = m.Database.Collection(Collections[ServerConfigCollection])
_, err = m.ServerConfigCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
{
Keys: bson.M{
"key": 1,
},
Options: options.Index().SetUnique(true),
},
})
if err != nil {
logrus.WithError(err).Fatal("Error Creating Index for Server Config Collection")
}
m.EnvironmentCollection = m.Database.Collection(Collections[EnvironmentCollection])
_, err = m.EnvironmentCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
{
Keys: bson.M{
"environment_id": 1,
},
Options: options.Index().SetUnique(true).SetPartialFilterExpression(bson.D{{
"is_removed", false,
}}),
},
{
Keys: bson.M{
"name": 1,
},
},
})
if err != nil {
logrus.WithError(err).Fatal("failed to create indexes for environments collection")
}
// Initialize chaos probes collection
err = m.Database.CreateCollection(context.TODO(), Collections[ChaosProbeCollection], nil)
if err != nil {
logrus.WithError(err).Error("failed to create chaosProbes collection")
}
m.ChaosProbeCollection = m.Database.Collection(Collections[ChaosProbeCollection])
_, err = m.ChaosProbeCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
{
Keys: bson.M{
"name": 1,
},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{
{"project_id", 1},
},
},
})
if err != nil {
logrus.WithError(err).Error("failed to create indexes for chaosProbes collection")
}
}