Skip to content

Commit

Permalink
Update the code architecture to Interface model for Environment pkg (#…
Browse files Browse the repository at this point in the history
…4159)

* refactor environment to interface model

Signed-off-by: Freedisch <[email protected]>

* fix lint

Signed-off-by: Freedisch <[email protected]>

* fix environments error chaos_inf

Signed-off-by: Freedisch <[email protected]>

* updated  environments_resolver

Signed-off-by: Freedisch <[email protected]>

* updated  environments_resolver

Signed-off-by: Freedisch <[email protected]>

* revert-deleted pre-commit/push

Signed-off-by: Magnim BATALE <[email protected]>

---------

Signed-off-by: Freedisch <[email protected]>
Signed-off-by: Magnim BATALE <[email protected]>
  • Loading branch information
Freedisch authored Sep 13, 2023
1 parent c4a617f commit 2cc7d4c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 36 deletions.
11 changes: 5 additions & 6 deletions chaoscenter/graphql/server/graph/environment.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/environment/handler"
"github.com/sirupsen/logrus"
)

Expand All @@ -23,7 +22,7 @@ func (r *mutationResolver) CreateEnvironment(ctx context.Context, projectID stri
if err != nil {
return nil, err
}
return handler.CreateEnvironment(ctx, projectID, request)
return r.environmentService.CreateEnvironment(ctx, projectID, request)
}

func (r *mutationResolver) UpdateEnvironment(ctx context.Context, projectID string, request *model.UpdateEnvironmentRequest) (string, error) {
Expand All @@ -38,7 +37,7 @@ func (r *mutationResolver) UpdateEnvironment(ctx context.Context, projectID stri
if err != nil {
return "", err
}
return handler.UpdateEnvironment(ctx, projectID, request)
return r.environmentService.UpdateEnvironment(ctx, projectID, request)
}

func (r *mutationResolver) DeleteEnvironment(ctx context.Context, projectID string, environmentID string) (string, error) {
Expand All @@ -53,7 +52,7 @@ func (r *mutationResolver) DeleteEnvironment(ctx context.Context, projectID stri
if err != nil {
return "", err
}
return handler.DeleteEnvironment(ctx, projectID, environmentID)
return r.environmentService.DeleteEnvironment(ctx, projectID, environmentID)
}

func (r *queryResolver) GetEnvironment(ctx context.Context, projectID string, environmentID string) (*model.Environment, error) {
Expand All @@ -68,7 +67,7 @@ func (r *queryResolver) GetEnvironment(ctx context.Context, projectID string, en
if err != nil {
return nil, err
}
return handler.GetEnvironment(projectID, environmentID)
return r.environmentService.GetEnvironment(projectID, environmentID)
}

func (r *queryResolver) ListEnvironments(ctx context.Context, projectID string, request *model.ListEnvironmentRequest) (*model.ListEnvironmentResponse, error) {
Expand All @@ -82,5 +81,5 @@ func (r *queryResolver) ListEnvironments(ctx context.Context, projectID string,
if err != nil {
return nil, err
}
return handler.ListEnvironments(projectID, request)
return r.environmentService.ListEnvironments(projectID, request)
}
8 changes: 7 additions & 1 deletion chaoscenter/graphql/server/graph/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment_run"
dbSchemaChaosHub "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub"
dbChaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
gitops2 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops"
image_registry2 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/image_registry"
envHandler "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/environment/handler"
gitops3 "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/gitops"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/image_registry"
)
Expand All @@ -37,6 +39,7 @@ type Resolver struct {
gitopsService gitops3.Service
chaosExperimentHandler handler.ChaosExperimentHandler
chaosExperimentRunHandler runHandler.ChaosExperimentRunHandler
environmentService envHandler.EnvironmentHandler
}

func NewConfig(mongodbOperator mongodb.MongoOperator) generated.Config {
Expand All @@ -47,14 +50,16 @@ func NewConfig(mongodbOperator mongodb.MongoOperator) generated.Config {
chaosExperimentRunOperator := chaos_experiment_run.NewChaosExperimentRunOperator(mongodbOperator)
gitopsOperator := gitops2.NewGitOpsOperator(mongodbOperator)
imageRegistryOperator := image_registry2.NewImageRegistryOperator(mongodbOperator)
EnvironmentOperator := environments.NewEnvironmentOperator(mongodbOperator)

//service
chaosHubService := chaoshub.NewService(chaosHubOperator)
chaosInfrastructureService := chaos_infrastructure.NewChaosInfrastructureService(chaosInfraOperator)
chaosInfrastructureService := chaos_infrastructure.NewChaosInfrastructureService(chaosInfraOperator, EnvironmentOperator)
chaosExperimentService := chaos_experiment2.NewChaosExperimentService(chaosExperimentOperator, chaosInfraOperator)
chaosExperimentRunService := chaos_experiment_run2.NewChaosExperimentRunService(chaosExperimentOperator, chaosInfraOperator, chaosExperimentRunOperator)
gitOpsService := gitops3.NewGitOpsService(gitopsOperator, chaosExperimentService, *chaosExperimentOperator)
imageRegistryService := image_registry.NewImageRegistryService(imageRegistryOperator)
environmentService := envHandler.NewEnvironmentService(EnvironmentOperator)

//handler
chaosExperimentHandler := handler.NewChaosExperimentHandler(chaosExperimentService, chaosExperimentRunService, chaosInfrastructureService, gitOpsService, chaosExperimentOperator, chaosExperimentRunOperator, mongodbOperator)
Expand All @@ -67,6 +72,7 @@ func NewConfig(mongodbOperator mongodb.MongoOperator) generated.Config {
chaosExperimentService: chaosExperimentService,
choasExperimentRunService: chaosExperimentRunService,
imageRegistryService: imageRegistryService,
environmentService: environmentService,
gitopsService: gitOpsService,
chaosExperimentHandler: *chaosExperimentHandler,
chaosExperimentRunHandler: *choasExperimentRunHandler,
Expand Down
10 changes: 6 additions & 4 deletions chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization"
store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/config"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
dbEnvironments "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/k8s"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -59,12 +59,14 @@ type Service interface {

type infraService struct {
infraOperator *dbChaosInfra.Operator
envOperator *dbEnvironments.Operator
}

// NewChaosInfrastructureService returns a new instance of Service
func NewChaosInfrastructureService(infraOperator *dbChaosInfra.Operator) Service {
func NewChaosInfrastructureService(infraOperator *dbChaosInfra.Operator, envOperator *dbEnvironments.Operator) Service {
return &infraService{
infraOperator: infraOperator,
envOperator: envOperator,
}
}

Expand Down Expand Up @@ -171,7 +173,7 @@ func (in *infraService) RegisterInfra(c context.Context, projectID string, input
{"infra_ids", infraID},
}},
}
err = environments.UpdateEnvironment(context.TODO(), envQuery, update)
err = in.envOperator.UpdateEnvironment(context.TODO(), envQuery, update)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -225,7 +227,7 @@ func (in *infraService) DeleteInfra(ctx context.Context, projectID string, infra
{"infra_ids", infra.InfraID},
}},
}
err = environments.UpdateEnvironment(context.TODO(), envQuery, updateQuery)
err = in.envOperator.UpdateEnvironment(context.TODO(), envQuery, updateQuery)
if err != nil {
return "", err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ 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 InsertEnvironment(ctx context.Context, environment Environment) error {
err := mongodb.Operator.Create(ctx, mongodb.EnvironmentCollection, environment)
func (e *Operator) InsertEnvironment(ctx context.Context, environment Environment) error {
err := e.operator.Create(ctx, mongodb.EnvironmentCollection, environment)
if err != nil {
return err
}
Expand All @@ -25,12 +36,12 @@ func InsertEnvironment(ctx context.Context, environment Environment) error {
}

// GetEnvironment takes a environmentID to retrieve the chaos_environment details from the database
func GetEnvironment(query bson.D) (Environment, error) {
func (e *Operator) GetEnvironment(query bson.D) (Environment, error) {
ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second)
defer cancel()

var environment Environment
result, err := mongodb.Operator.Get(ctx, mongodb.EnvironmentCollection, query)
result, err := e.operator.Get(ctx, mongodb.EnvironmentCollection, query)
err = result.Decode(&environment)
if err != nil {
return Environment{}, err
Expand All @@ -40,14 +51,14 @@ func GetEnvironment(query bson.D) (Environment, error) {
}

// GetEnvironmentDetails takes a environmentName and projectID to retrieve the chaos_environment details from the database
func GetEnvironmentDetails(ctx context.Context, environmentID string, projectID string) (Environment, error) {
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 := mongodb.Operator.Get(ctx, mongodb.EnvironmentCollection, query)
result, err := e.operator.Get(ctx, mongodb.EnvironmentCollection, query)
err = result.Decode(&environment)
if err != nil {
return Environment{}, err
Expand All @@ -57,8 +68,8 @@ func GetEnvironmentDetails(ctx context.Context, environmentID string, projectID
}

// UpdateEnvironment takes query and update parameters to update the chaos_environment details in the database
func UpdateEnvironment(ctx context.Context, query bson.D, update bson.D) error {
_, err := mongodb.Operator.UpdateMany(ctx, mongodb.EnvironmentCollection, query, update)
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
}
Expand All @@ -67,7 +78,7 @@ func UpdateEnvironment(ctx context.Context, query bson.D, update bson.D) error {
}

// GetEnvironmentWithProjectID takes projectID parameters to retrieve the chaos_environment details
func GetEnvironmentWithProjectID(projectID string) ([]*Environment, error) {
func (e *Operator) GetEnvironmentWithProjectID(projectID string) ([]*Environment, error) {
var query bson.D
query = bson.D{

Expand All @@ -93,9 +104,9 @@ func GetEnvironmentWithProjectID(projectID string) ([]*Environment, error) {
}

// GetEnvironments returns all the environments matching the query
func GetEnvironments(ctx context.Context, query bson.D) ([]Environment, error) {
func (e *Operator) GetEnvironments(ctx context.Context, query bson.D) ([]Environment, error) {
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
}
Expand All @@ -107,11 +118,11 @@ func GetEnvironments(ctx context.Context, query bson.D) ([]Environment, error) {
}

// GetAggregateEnvironments takes a mongo pipeline to retrieve the details from the database
func GetAggregateEnvironments(pipeline mongo.Pipeline) (*mongo.Cursor, error) {
func (e *Operator) GetAggregateEnvironments(pipeline mongo.Pipeline) (*mongo.Cursor, error) {
ctx, cancel := context.WithTimeout(backgroundContext, 10*time.Second)
defer cancel()

results, err := mongodb.Operator.Aggregate(ctx, mongodb.EnvironmentCollection, pipeline)
results, err := e.operator.Aggregate(ctx, mongodb.EnvironmentCollection, pipeline)
if err != nil {
return nil, err
}
Expand Down
43 changes: 31 additions & 12 deletions chaoscenter/graphql/server/pkg/environment/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,30 @@ import (
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
dbOperationsEnvironment "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/environments"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

func CreateEnvironment(ctx context.Context, projectID string, input *model.CreateEnvironmentRequest) (*model.Environment, error) {
type EnvironmentHandler interface {
CreateEnvironment(ctx context.Context, projectID string, input *model.CreateEnvironmentRequest) (*model.Environment, error)
UpdateEnvironment(ctx context.Context, projectID string, request *model.UpdateEnvironmentRequest) (string, error)
DeleteEnvironment(ctx context.Context, projectID string, environmentID string) (string, error)
GetEnvironment(projectID string, environmentID string) (*model.Environment, error)
ListEnvironments(projectID string, request *model.ListEnvironmentRequest) (*model.ListEnvironmentResponse, error)
}

type EnvironmentService struct {
EnvironmentOperator *dbOperationsEnvironment.Operator
}

func NewEnvironmentService(EnvironmentOperator *dbOperationsEnvironment.Operator) EnvironmentHandler {
return &EnvironmentService{
EnvironmentOperator: EnvironmentOperator,
}
}

func (e *EnvironmentService) CreateEnvironment(ctx context.Context, projectID string, input *model.CreateEnvironmentRequest) (*model.Environment, error) {

currentTime := time.Now()
if input.Tags == nil || len(input.Tags) == 0 {
Expand Down Expand Up @@ -52,7 +71,7 @@ func CreateEnvironment(ctx context.Context, projectID string, input *model.Creat
},
}

err = environments.InsertEnvironment(context.Background(), newEnv)
err = e.EnvironmentOperator.InsertEnvironment(context.Background(), newEnv)
if err != nil {
return &model.Environment{}, err
}
Expand All @@ -68,15 +87,15 @@ func CreateEnvironment(ctx context.Context, projectID string, input *model.Creat

}

func UpdateEnvironment(ctx context.Context, projectID string, request *model.UpdateEnvironmentRequest) (string, error) {
func (e *EnvironmentService) UpdateEnvironment(ctx context.Context, projectID string, request *model.UpdateEnvironmentRequest) (string, error) {

query := bson.D{
{"environment_id", request.EnvironmentID},
{"project_id", projectID},
{"is_removed", false},
}

_, err := environments.GetEnvironments(context.TODO(), query)
_, err := e.EnvironmentOperator.GetEnvironments(context.TODO(), query)
if err != nil {
return "couldn't update environment", err
}
Expand Down Expand Up @@ -124,14 +143,14 @@ func UpdateEnvironment(ctx context.Context, projectID string, request *model.Upd
})
}

err = environments.UpdateEnvironment(context.TODO(), query, updateQuery)
err = e.EnvironmentOperator.UpdateEnvironment(context.TODO(), query, updateQuery)
if err != nil {
return "couldn't update environment", err
}
return "environment updated successfully", nil
}

func DeleteEnvironment(ctx context.Context, projectID string, environmentID string) (string, error) {
func (e *EnvironmentService) DeleteEnvironment(ctx context.Context, projectID string, environmentID string) (string, error) {
currTime := time.Now().UnixMilli()
tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
Expand All @@ -144,7 +163,7 @@ func DeleteEnvironment(ctx context.Context, projectID string, environmentID stri
{"is_removed", false},
}

_, err = environments.GetEnvironment(query)
_, err = e.EnvironmentOperator.GetEnvironment(query)
if err != nil {
return "couldn't fetch environment details", err
}
Expand All @@ -156,21 +175,21 @@ func DeleteEnvironment(ctx context.Context, projectID string, environmentID stri
{"updated_by", username},
}},
}
err = environments.UpdateEnvironment(context.TODO(), query, update)
err = e.EnvironmentOperator.UpdateEnvironment(context.TODO(), query, update)
if err != nil {
return "couldn't delete environment", err
}
return "successfully deleted environment", nil
}

func GetEnvironment(projectID string, environmentID string) (*model.Environment, error) {
func (e *EnvironmentService) GetEnvironment(projectID string, environmentID string) (*model.Environment, error) {
query := bson.D{
{"environment_id", environmentID},
{"project_id", projectID},
{"is_removed", false},
}

env, err := environments.GetEnvironment(query)
env, err := e.EnvironmentOperator.GetEnvironment(query)
if err != nil {
return &model.Environment{}, err
}
Expand All @@ -192,7 +211,7 @@ func GetEnvironment(projectID string, environmentID string) (*model.Environment,

}

func ListEnvironments(projectID string, request *model.ListEnvironmentRequest) (*model.ListEnvironmentResponse, error) {
func (e *EnvironmentService) ListEnvironments(projectID string, request *model.ListEnvironmentRequest) (*model.ListEnvironmentResponse, error) {
var pipeline mongo.Pipeline

// Match with identifiers
Expand Down Expand Up @@ -342,7 +361,7 @@ func ListEnvironments(projectID string, request *model.ListEnvironmentRequest) (
}
pipeline = append(pipeline, facetStage)

cursor, err := environments.GetAggregateEnvironments(pipeline)
cursor, err := e.EnvironmentOperator.GetAggregateEnvironments(pipeline)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2cc7d4c

Please sign in to comment.