-
Notifications
You must be signed in to change notification settings - Fork 654
/
operations.go
131 lines (105 loc) · 3.62 KB
/
operations.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
package environments
import (
"context"
"time"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
var (
backgroundContext = context.Background()
)
type Operator struct {
operator mongodb.MongoOperator
}
// NewEnvironmentOperator retuurns a new instance of operator
func NewEnvironmentOperator(mongodbOperator mongodb.MongoOperator) *Operator {
return &Operator{
operator: mongodbOperator,
}
}
// InsertEnvironment takes details of a chaos_environment and inserts into the database collection
func (e *Operator) InsertEnvironment(ctx context.Context, environment Environment) error {
err := e.operator.Create(ctx, mongodb.EnvironmentCollection, environment)
if err != nil {
return err
}
return nil
}
// GetEnvironment takes a environmentID to retrieve the chaos_environment details from the database
func (e *Operator) GetEnvironment(query bson.D) (Environment, error) {
ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second)
defer cancel()
var environment Environment
result, err := e.operator.Get(ctx, mongodb.EnvironmentCollection, query)
err = result.Decode(&environment)
if err != nil {
return Environment{}, err
}
return environment, nil
}
// GetEnvironmentDetails takes a environmentName and projectID to retrieve the chaos_environment details from the database
func (e *Operator) GetEnvironmentDetails(ctx context.Context, environmentID string, projectID string) (Environment, error) {
query := bson.D{
{"project_id", projectID},
{"environment_id", environmentID},
}
var environment Environment
result, err := e.operator.Get(ctx, mongodb.EnvironmentCollection, query)
err = result.Decode(&environment)
if err != nil {
return Environment{}, err
}
return environment, nil
}
// UpdateEnvironment takes query and update parameters to update the chaos_environment details in the database
func (e *Operator) UpdateEnvironment(ctx context.Context, query bson.D, update bson.D) error {
_, err := e.operator.UpdateMany(ctx, mongodb.EnvironmentCollection, query, update)
if err != nil {
return err
}
return nil
}
// GetEnvironmentWithProjectID takes projectID parameters to retrieve the chaos_environment details
func (e *Operator) GetEnvironmentWithProjectID(projectID string) ([]*Environment, error) {
var query bson.D
query = bson.D{
{"project_id", projectID},
{"is_removed", false},
}
ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second)
defer cancel()
var environments []*Environment
results, err := mongodb.Operator.List(ctx, mongodb.EnvironmentCollection, query)
if err != nil {
return []*Environment{}, err
}
err = results.All(ctx, &environments)
if err != nil {
return []*Environment{}, err
}
return environments, nil
}
// GetEnvironments returns all the environments matching the query
func (e *Operator) GetEnvironments(ctx context.Context, query bson.D) ([]Environment, error) {
var environments []Environment
results, err := e.operator.List(ctx, mongodb.EnvironmentCollection, query)
if err != nil {
return []Environment{}, err
}
err = results.All(ctx, &environments)
if err != nil {
return []Environment{}, err
}
return environments, nil
}
// GetAggregateEnvironments takes a mongo pipeline to retrieve the details from the database
func (e *Operator) GetAggregateEnvironments(pipeline mongo.Pipeline) (*mongo.Cursor, error) {
ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second)
defer cancel()
results, err := e.operator.Aggregate(ctx, mongodb.EnvironmentCollection, pipeline)
if err != nil {
return nil, err
}
return results, nil
}