/
operations.go
148 lines (121 loc) · 3.99 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package cluster
import (
"context"
"fmt"
"time"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
backgroundContext = context.Background()
)
// Operator is the model for cluster collection
type Operator struct {
operator mongodb.MongoOperator
}
// NewClusterOperator returns a new instance of Operator
func NewClusterOperator(mongodbOperator mongodb.MongoOperator) *Operator {
return &Operator{
operator: mongodbOperator,
}
}
// InsertCluster takes details of a cluster and inserts into the database collection
func (c *Operator) InsertCluster(cluster Cluster) error {
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
err := c.operator.Create(ctx, mongodb.ClusterCollection, cluster)
if err != nil {
return err
}
return nil
}
// GetCluster takes a clusterID to retrieve the cluster details from the database
func (c *Operator) GetCluster(clusterID string) (Cluster, error) {
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
query := bson.D{{"cluster_id", clusterID}}
var cluster Cluster
result, err := c.operator.Get(ctx, mongodb.ClusterCollection, query)
if err != nil {
return Cluster{}, fmt.Errorf("error in getting cluster data: %v", err)
}
err = result.Decode(&cluster)
if err != nil {
return Cluster{}, fmt.Errorf("error in decoding cluster data: %v", err)
}
return cluster, nil
}
// GetAgentDetails takes a agentName and projectID to retrieve the cluster details from the database
func (c *Operator) GetAgentDetails(ctx context.Context, clusterID string, projectID string) (Cluster, error) {
query := bson.D{
{"project_id", projectID},
{"cluster_id", clusterID},
}
var cluster Cluster
result, err := c.operator.Get(ctx, mongodb.ClusterCollection, query)
if err != nil {
return Cluster{}, fmt.Errorf("error in getting cluster data: %v", err)
}
err = result.Decode(&cluster)
if err != nil {
return Cluster{}, fmt.Errorf("error in decoding cluster data: %v", err)
}
return cluster, nil
}
// UpdateCluster takes query and update parameters to update the cluster details in the database
func (c *Operator) UpdateCluster(query bson.D, update bson.D) error {
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
_, err := c.operator.Update(ctx, mongodb.ClusterCollection, query, update)
if err != nil {
return err
}
return nil
}
// GetClusterWithProjectID takes projectID and clusterType parameters to retrieve the cluster details from the database
func (c *Operator) GetClusterWithProjectID(projectID string, clusterType *string) ([]*Cluster, error) {
var query bson.D
if clusterType == nil {
query = bson.D{
{"project_id", projectID},
{"is_removed", false},
}
} else {
query = bson.D{
{"project_id", projectID},
{"cluster_type", clusterType},
{"is_removed", false},
}
}
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
var clusters []*Cluster
results, err := c.operator.List(ctx, mongodb.ClusterCollection, query)
if err != nil {
return []*Cluster{}, err
}
err = results.All(ctx, &clusters)
if err != nil {
return []*Cluster{}, err
}
return clusters, nil
}
// ListClusters returns all the clusters matching the query
func (c *Operator) ListClusters(ctx context.Context, query bson.D) ([]*Cluster, error) {
var clusters []*Cluster
results, err := c.operator.List(ctx, mongodb.ClusterCollection, query)
if err != nil {
return []*Cluster{}, err
}
err = results.All(ctx, &clusters)
if err != nil {
return []*Cluster{}, err
}
return clusters, nil
}
// GetAggregateProjects takes a mongo pipeline to retrieve the project details from the database
func (c *Operator) GetAggregateProjects(ctx context.Context, pipeline mongo.Pipeline, opts *options.AggregateOptions) (*mongo.Cursor, error) {
results, err := c.operator.Aggregate(ctx, mongodb.ClusterCollection, pipeline, opts)
if err != nil {
return nil, err
}
return results, nil
}