diff --git a/chaoscenter/authentication/go.sum b/chaoscenter/authentication/go.sum index e05409a07f6..c8fd6dbd7fa 100644 --- a/chaoscenter/authentication/go.sum +++ b/chaoscenter/authentication/go.sum @@ -131,6 +131,8 @@ golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn5 golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= +golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub/operations.go index faa8c1faf06..ca5550ab6a1 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub/operations.go @@ -92,7 +92,7 @@ func (c *Operator) UpdateChaosHub(ctx context.Context, query bson.D, update bson // GetAggregateChaosHubs takes a mongo pipeline to retrieve the project details from the database func (c *Operator) GetAggregateChaosHubs(ctx context.Context, pipeline mongo.Pipeline) (*mongo.Cursor, error) { - results, err := mongodb.Operator.Aggregate(ctx, mongodb.ChaosHubCollection, pipeline) + results, err := c.operator.Aggregate(ctx, mongodb.ChaosHubCollection, pipeline) if err != nil { return nil, fmt.Errorf("error on getting the chaos hubs : %v", err) } diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/config/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/config/operations.go index 7033b82273b..6fe31724ce9 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/config/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/config/operations.go @@ -9,9 +9,19 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) +type Operator struct { + operator mongodb.MongoOperator +} + +func NewConfigOperator(mongodbOperator mongodb.MongoOperator) *Operator { + return &Operator{ + operator: mongodbOperator, + } +} + // CreateConfig creates a new server config with unique key -func CreateConfig(ctx context.Context, config *ServerConfig) error { - err := mongodb.Operator.Create(ctx, mongodb.ServerConfigCollection, config) +func (o *Operator) CreateConfig(ctx context.Context, config *ServerConfig) error { + err := o.operator.Create(ctx, mongodb.ServerConfigCollection, config) if err != nil { return err } @@ -19,11 +29,11 @@ func CreateConfig(ctx context.Context, config *ServerConfig) error { } // GetConfig returns the requested server config -func GetConfig(ctx context.Context, key string) (*ServerConfig, error) { +func (o *Operator) GetConfig(ctx context.Context, key string) (*ServerConfig, error) { query := bson.D{ {"key", key}, } - results, err := mongodb.Operator.Get(ctx, mongodb.ServerConfigCollection, query) + results, err := o.operator.Get(ctx, mongodb.ServerConfigCollection, query) if err != nil { return nil, err } @@ -39,14 +49,14 @@ func GetConfig(ctx context.Context, key string) (*ServerConfig, error) { } // UpdateConfig updates the required server config -func UpdateConfig(ctx context.Context, key string, value interface{}) error { +func (o *Operator) UpdateConfig(ctx context.Context, key string, value interface{}) error { query := bson.D{ {"key", key}, } update := bson.D{{"$set", bson.D{{ "value", value}}, }} - _, err := mongodb.Operator.Update(ctx, mongodb.ServerConfigCollection, query, update) + _, err := o.operator.Update(ctx, mongodb.ServerConfigCollection, query, update) if err != nil { return err } diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/environments/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/environments/operations.go index 6cff68e1451..72f98abb9bc 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/environments/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/environments/operations.go @@ -90,7 +90,7 @@ func (e *Operator) GetEnvironmentWithProjectID(projectID string) ([]*Environment defer cancel() var environments []*Environment - results, err := mongodb.Operator.List(ctx, mongodb.EnvironmentCollection, query) + results, err := e.operator.List(ctx, mongodb.EnvironmentCollection, query) if err != nil { return []*Environment{}, err } diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/operations.go index cecd1395f13..c5ad23a12d7 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/operations.go @@ -33,11 +33,6 @@ type MongoOperations struct { MongoClient *MongoClient } -var ( - // Operator contains all the CRUD operations of the mongo database - Operator MongoOperator = &MongoOperations{} -) - func NewMongoOperations(mongoClient *MongoClient) *MongoOperations { return &MongoOperations{ MongoClient: mongoClient, diff --git a/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go b/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go index 1860d3dae1f..d9b11c999c9 100644 --- a/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go +++ b/chaoscenter/graphql/server/pkg/database/mongodb/probe/operations.go @@ -10,13 +10,13 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) -// Operator is the model for probe collection +// Operator is the model for probe operations and collection type Operator struct { operator mongodb.MongoOperator } -// NewChaosProbeOperator returns a new instance of Operator -func NewChaosProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator { +// NewProbeOperator returns a new instance of Operator +func NewProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator { return &Operator{ operator: mongodbOperator, } diff --git a/chaoscenter/graphql/server/pkg/handlers/readiness_handler.go b/chaoscenter/graphql/server/pkg/handlers/readiness_handler.go index 98ce0220058..1d143961d17 100644 --- a/chaoscenter/graphql/server/pkg/handlers/readiness_handler.go +++ b/chaoscenter/graphql/server/pkg/handlers/readiness_handler.go @@ -9,6 +9,19 @@ import ( "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb" ) +// Operator encapsulates the MongoDB operations +type Operator struct { + mongoOperator mongodb.MongoOperator +} + +// NewOperator returns a new instance of Operator +func NewOperator(mongoOperator mongodb.MongoOperator) *Operator { + return &Operator{ + mongoOperator: mongoOperator, + } +} + +// ReadinessAPIStatus represents the readiness status of the API type ReadinessAPIStatus struct { DataBase string `json:"database"` Collections string `json:"collections"` @@ -20,14 +33,14 @@ func contains(s []string, str string) bool { return true } } - return false } -func ReadinessHandler() gin.HandlerFunc { +// ReadinessHandler returns a handler function for readiness checks +func (r *Operator) ReadinessHandler() gin.HandlerFunc { return func(c *gin.Context) { var dbFlag = "up" - dbs, err := mongodb.Operator.ListDataBase(context.Background(), mongodb.MgoClient) + dbs, err := r.mongoOperator.ListDataBase(context.Background(), mongodb.MgoClient) if err != nil { dbFlag = "down" } diff --git a/chaoscenter/graphql/server/pkg/probe/handler/handler.go b/chaoscenter/graphql/server/pkg/probe/handler/handler.go index 193b81147d2..c6158a0874a 100644 --- a/chaoscenter/graphql/server/pkg/probe/handler/handler.go +++ b/chaoscenter/graphql/server/pkg/probe/handler/handler.go @@ -46,6 +46,18 @@ type probeService struct { probeOperator *dbSchemaProbe.Operator } +type Operator struct { + operator mongodb.MongoOperator +} + +// NewProbeOperator returns a new instance of Operator +func NewProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator { + return &Operator{ + operator: mongodbOperator, + } +} + +// NewProbeService returns a new instance of probeService func NewProbeService(probeOperator *dbSchemaProbe.Operator) Service { return &probeService{ probeOperator: probeOperator, @@ -405,7 +417,7 @@ func GetProbeExecutionHistoryInExperimentRuns(projectID string, probeName string pipeline = append(pipeline, matchIdentifierStage) // Call aggregation on pipeline - experimentRunOperator := dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodb.Operator) + experimentRunOperator := dbChaosExperimentRun.NewChaosExperimentRunOperator(NewProbeOperator.operator) expRunCursor, err := experimentRunOperator.GetAggregateExperimentRuns(pipeline) if err != nil { return nil, errors.New("DB aggregate stage error: " + err.Error()) diff --git a/chaoscenter/graphql/server/server.go b/chaoscenter/graphql/server/server.go index 78e8b00ba9a..eb5b32ac778 100644 --- a/chaoscenter/graphql/server/server.go +++ b/chaoscenter/graphql/server/server.go @@ -90,7 +90,6 @@ func main() { mongoClient := mongodb.Client.Initialize(mongodb.MgoClient) var mongodbOperator mongodb.MongoOperator = mongodb.NewMongoOperations(mongoClient) - mongodb.Operator = mongodbOperator if err := validateVersion(); err != nil { log.Fatal(err)