(Litmus-Portal) GQL server refractor for database and handlers (#2454)
* Adding datasource and dashboard APIs for analytics / monitoring. Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io> * Fixed formatting. Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io> * GraphQL server refractor Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io> * Refractor after review. Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io> * Minor fixes after review. Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io> * Sorted imports. Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io> * Reverting frontend changes. Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io> * code analysis fix. Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io> * minor fix Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io> * Changes after review. Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io> * Codacy fixes. Signed-off-by: ishangupta-ds <ishan.gupta@mayadata.io>
This commit is contained in:
parent
3ea8687872
commit
de0418fb2c
|
|
@ -2970,7 +2970,7 @@ func (ec *executionContext) introspectType(name string) (*introspection.Type, er
|
|||
}
|
||||
|
||||
var sources = []*ast.Source{
|
||||
&ast.Source{Name: "graph/analytics.graphqls", Input: `input DSInput {
|
||||
{Name: "graph/analytics.graphqls", Input: `input DSInput {
|
||||
ds_id: String
|
||||
ds_name: String!
|
||||
ds_type: String!
|
||||
|
|
@ -3146,7 +3146,7 @@ input deleteDSInput {
|
|||
force_delete: Boolean!
|
||||
ds_id: String!
|
||||
}`, BuiltIn: false},
|
||||
&ast.Source{Name: "graph/myhub.graphqls", Input: `enum AuthType {
|
||||
{Name: "graph/myhub.graphqls", Input: `enum AuthType {
|
||||
none
|
||||
basic
|
||||
token
|
||||
|
|
@ -3320,7 +3320,7 @@ input UpdateMyHub {
|
|||
SSHPublicKey: String
|
||||
}
|
||||
`, BuiltIn: false},
|
||||
&ast.Source{Name: "graph/project.graphqls", Input: `type Project {
|
||||
{Name: "graph/project.graphqls", Input: `type Project {
|
||||
id: ID!
|
||||
name: String!
|
||||
members: [Member!]!
|
||||
|
|
@ -3352,7 +3352,7 @@ enum MemberRole {
|
|||
Viewer
|
||||
}
|
||||
`, BuiltIn: false},
|
||||
&ast.Source{Name: "graph/schema.graphqls", Input: `# GraphQL schema example
|
||||
{Name: "graph/schema.graphqls", Input: `# GraphQL schema example
|
||||
#
|
||||
# https://gqlgen.com/getting-started/
|
||||
|
||||
|
|
@ -3710,7 +3710,7 @@ type Subscription {
|
|||
clusterConnect(clusterInfo: ClusterIdentity!): ClusterAction!
|
||||
}
|
||||
`, BuiltIn: false},
|
||||
&ast.Source{Name: "graph/usermanagement.graphqls", Input: `type User {
|
||||
{Name: "graph/usermanagement.graphqls", Input: `type User {
|
||||
id: ID!
|
||||
username: String!
|
||||
email: String
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
|
||||
"github.com/99designs/gqlgen/graphql"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/generated"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/authorization"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -12,34 +12,33 @@ import (
|
|||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jinzhu/copier"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/generated"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
analytics_handler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/analytics/handler"
|
||||
wf_handler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaos-workflow/handler"
|
||||
analyticsHandler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/analytics/handler"
|
||||
wfHandler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaos-workflow/handler"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"
|
||||
clusterHandler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster/handler"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/gitops/handler"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/graphql/mutations"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/graphql/queries"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/graphql/subscriptions"
|
||||
dbOperationsCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
|
||||
gitOpsHandler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/gitops/handler"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/myhub"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/myhub/myhub_ops"
|
||||
myHubOps "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/myhub/ops"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/project"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/usermanagement"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func (r *mutationResolver) UserClusterReg(ctx context.Context, clusterInput model.ClusterInput) (*model.ClusterRegResponse, error) {
|
||||
return mutations.ClusterRegister(clusterInput)
|
||||
return clusterHandler.ClusterRegister(clusterInput)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) CreateChaosWorkFlow(ctx context.Context, input model.ChaosWorkFlowInput) (*model.ChaosWorkFlowResponse, error) {
|
||||
return wf_handler.CreateChaosWorkflow(ctx, &input, store.Store)
|
||||
return wfHandler.CreateChaosWorkflow(ctx, &input, store.Store)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) ReRunChaosWorkFlow(ctx context.Context, workflowID string) (string, error) {
|
||||
return wf_handler.ReRunWorkflow(workflowID)
|
||||
return wfHandler.ReRunWorkflow(workflowID)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) CreateUser(ctx context.Context, user model.CreateUserInput) (*model.User, error) {
|
||||
|
|
@ -51,7 +50,7 @@ func (r *mutationResolver) UpdateUser(ctx context.Context, user model.UpdateUser
|
|||
}
|
||||
|
||||
func (r *mutationResolver) DeleteChaosWorkflow(ctx context.Context, workflowid string) (bool, error) {
|
||||
return wf_handler.DeleteWorkflow(ctx, workflowid, store.Store)
|
||||
return wfHandler.DeleteWorkflow(ctx, workflowid, store.Store)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) SendInvitation(ctx context.Context, member model.MemberInput) (*model.Member, error) {
|
||||
|
|
@ -71,19 +70,19 @@ func (r *mutationResolver) RemoveInvitation(ctx context.Context, member model.Me
|
|||
}
|
||||
|
||||
func (r *mutationResolver) ClusterConfirm(ctx context.Context, identity model.ClusterIdentity) (*model.ClusterConfirmResponse, error) {
|
||||
return mutations.ConfirmClusterRegistration(identity, *store.Store)
|
||||
return clusterHandler.ConfirmClusterRegistration(identity, *store.Store)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) NewClusterEvent(ctx context.Context, clusterEvent model.ClusterEventInput) (string, error) {
|
||||
return mutations.NewEvent(clusterEvent, *store.Store)
|
||||
return clusterHandler.NewEvent(clusterEvent, *store.Store)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) ChaosWorkflowRun(ctx context.Context, workflowData model.WorkflowRunInput) (string, error) {
|
||||
return mutations.WorkFlowRunHandler(workflowData, *store.Store)
|
||||
return wfHandler.WorkFlowRunHandler(workflowData, *store.Store)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) PodLog(ctx context.Context, log model.PodLog) (string, error) {
|
||||
return mutations.LogsHandler(log, *store.Store)
|
||||
return wfHandler.LogsHandler(log, *store.Store)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) AddMyHub(ctx context.Context, myhubInput model.CreateMyHub, projectID string) (*model.MyHub, error) {
|
||||
|
|
@ -99,15 +98,15 @@ func (r *mutationResolver) SyncHub(ctx context.Context, id string) ([]*model.MyH
|
|||
}
|
||||
|
||||
func (r *mutationResolver) UpdateChaosWorkflow(ctx context.Context, input *model.ChaosWorkFlowInput) (*model.ChaosWorkFlowResponse, error) {
|
||||
return wf_handler.UpdateWorkflow(ctx, input, store.Store)
|
||||
return wfHandler.UpdateWorkflow(ctx, input, store.Store)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) DeleteClusterReg(ctx context.Context, clusterID string) (string, error) {
|
||||
return mutations.DeleteCluster(clusterID, *store.Store)
|
||||
return clusterHandler.DeleteCluster(clusterID, *store.Store)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) GeneraterSSHKey(ctx context.Context) (*model.SSHKey, error) {
|
||||
publicKey, privateKey, err := myhub_ops.GenerateKeys()
|
||||
publicKey, privateKey, err := myHubOps.GenerateKeys()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -127,51 +126,51 @@ func (r *mutationResolver) DeleteMyHub(ctx context.Context, hubID string) (bool,
|
|||
}
|
||||
|
||||
func (r *mutationResolver) GitopsNotifer(ctx context.Context, clusterInfo model.ClusterIdentity, workflowID string) (string, error) {
|
||||
return handler.GitOpsNotificationHandler(ctx, clusterInfo, workflowID)
|
||||
return gitOpsHandler.GitOpsNotificationHandler(ctx, clusterInfo, workflowID)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) EnableGitOps(ctx context.Context, config model.GitConfig) (bool, error) {
|
||||
return handler.EnableGitOpsHandler(ctx, config)
|
||||
return gitOpsHandler.EnableGitOpsHandler(ctx, config)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) DisableGitOps(ctx context.Context, projectID string) (bool, error) {
|
||||
return handler.DisableGitOpsHandler(ctx, projectID)
|
||||
return gitOpsHandler.DisableGitOpsHandler(ctx, projectID)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) CreateDataSource(ctx context.Context, datasource *model.DSInput) (*model.DSResponse, error) {
|
||||
return analytics_handler.CreateDataSource(datasource)
|
||||
return analyticsHandler.CreateDataSource(datasource)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) CreateDashBoard(ctx context.Context, dashboard *model.CreateDBInput) (string, error) {
|
||||
return analytics_handler.CreateDashboard(dashboard)
|
||||
return analyticsHandler.CreateDashboard(dashboard)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) UpdateDataSource(ctx context.Context, datasource model.DSInput) (*model.DSResponse, error) {
|
||||
return analytics_handler.UpdateDataSource(datasource)
|
||||
return analyticsHandler.UpdateDataSource(datasource)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) UpdateDashboard(ctx context.Context, dashboard *model.UpdataDBInput) (string, error) {
|
||||
return analytics_handler.UpdateDashBoard(dashboard)
|
||||
return analyticsHandler.UpdateDashBoard(dashboard)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) UpdatePanel(ctx context.Context, panelInput []*model.Panel) (string, error) {
|
||||
return analytics_handler.UpdatePanel(panelInput)
|
||||
return analyticsHandler.UpdatePanel(panelInput)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) DeleteDashboard(ctx context.Context, dbID *string) (bool, error) {
|
||||
return analytics_handler.DeleteDashboard(dbID)
|
||||
return analyticsHandler.DeleteDashboard(dbID)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) DeleteDataSource(ctx context.Context, input model.DeleteDSInput) (bool, error) {
|
||||
return analytics_handler.DeleteDataSource(input)
|
||||
return analyticsHandler.DeleteDataSource(input)
|
||||
}
|
||||
|
||||
func (r *queryResolver) GetWorkFlowRuns(ctx context.Context, projectID string) ([]*model.WorkflowRun, error) {
|
||||
return wf_handler.QueryWorkflowRuns(projectID)
|
||||
return wfHandler.QueryWorkflowRuns(projectID)
|
||||
}
|
||||
|
||||
func (r *queryResolver) GetCluster(ctx context.Context, projectID string, clusterType *string) ([]*model.Cluster, error) {
|
||||
return queries.QueryGetClusters(projectID, clusterType)
|
||||
return clusterHandler.QueryGetClusters(projectID, clusterType)
|
||||
}
|
||||
|
||||
func (r *queryResolver) GetUser(ctx context.Context, username string) (*model.User, error) {
|
||||
|
|
@ -187,14 +186,14 @@ func (r *queryResolver) Users(ctx context.Context) ([]*model.User, error) {
|
|||
}
|
||||
|
||||
func (r *queryResolver) GetScheduledWorkflows(ctx context.Context, projectID string) ([]*model.ScheduledWorkflows, error) {
|
||||
return wf_handler.QueryWorkflows(projectID)
|
||||
return wfHandler.QueryWorkflows(projectID)
|
||||
}
|
||||
|
||||
func (r *queryResolver) ListWorkflow(ctx context.Context, projectID string, workflowIds []*string) ([]*model.Workflow, error) {
|
||||
if len(workflowIds) == 0 {
|
||||
return wf_handler.QueryListWorkflow(projectID)
|
||||
return wfHandler.QueryListWorkflow(projectID)
|
||||
} else {
|
||||
return wf_handler.QueryListWorkflowByIDs(workflowIds)
|
||||
return wfHandler.QueryListWorkflowByIDs(workflowIds)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -215,19 +214,19 @@ func (r *queryResolver) GetYAMLData(ctx context.Context, experimentInput model.E
|
|||
}
|
||||
|
||||
func (r *queryResolver) ListDataSource(ctx context.Context, projectID string) ([]*model.DSResponse, error) {
|
||||
return analytics_handler.QueryListDataSource(projectID)
|
||||
return analyticsHandler.QueryListDataSource(projectID)
|
||||
}
|
||||
|
||||
func (r *queryResolver) GetPromQuery(ctx context.Context, query *model.PromInput) ([]*model.PromResponse, error) {
|
||||
return analytics_handler.GetPromQuery(query)
|
||||
return analyticsHandler.GetPromQuery(query)
|
||||
}
|
||||
|
||||
func (r *queryResolver) ListDashboard(ctx context.Context, projectID string) ([]*model.ListDashboardReponse, error) {
|
||||
return analytics_handler.QueryListDashboard(projectID)
|
||||
return analyticsHandler.QueryListDashboard(projectID)
|
||||
}
|
||||
|
||||
func (r *queryResolver) GetGitOpsDetails(ctx context.Context, projectID string) (*model.GitConfigResponse, error) {
|
||||
return handler.GetGitOpsDetailsHandler(ctx, projectID)
|
||||
return gitOpsHandler.GetGitOpsDetailsHandler(ctx, projectID)
|
||||
}
|
||||
|
||||
func (r *subscriptionResolver) ClusterEventListener(ctx context.Context, projectID string) (<-chan *model.ClusterEvent, error) {
|
||||
|
|
@ -270,7 +269,7 @@ func (r *subscriptionResolver) GetPodLog(ctx context.Context, podDetails model.P
|
|||
log.Print("CLOSED LOG LISTENER", podDetails.ClusterID, podDetails.PodName)
|
||||
delete(store.Store.WorkflowLog, reqID.String())
|
||||
}()
|
||||
go queries.GetLogs(reqID.String(), podDetails, *store.Store)
|
||||
go wfHandler.GetLogs(reqID.String(), podDetails, *store.Store)
|
||||
return workflowLog, nil
|
||||
}
|
||||
|
||||
|
|
@ -297,7 +296,7 @@ func (r *subscriptionResolver) ClusterConnect(ctx context.Context, clusterInfo m
|
|||
newVerifiedCluster := model.Cluster{}
|
||||
copier.Copy(&newVerifiedCluster, &verifiedCluster)
|
||||
|
||||
subscriptions.SendClusterEvent("cluster-status", "Cluster Offline", "Cluster Disconnect", newVerifiedCluster, *store.Store)
|
||||
clusterHandler.SendClusterEvent("cluster-status", "Cluster Offline", "Cluster Disconnect", newVerifiedCluster, *store.Store)
|
||||
|
||||
store.Store.Mutex.Lock()
|
||||
delete(store.Store.ConnectedCluster, clusterInfo.ClusterID)
|
||||
|
|
@ -305,7 +304,7 @@ func (r *subscriptionResolver) ClusterConnect(ctx context.Context, clusterInfo m
|
|||
query := bson.D{{"cluster_id", clusterInfo.ClusterID}}
|
||||
update := bson.D{{"$set", bson.D{{"is_active", false}, {"updated_at", strconv.FormatInt(time.Now().Unix(), 10)}}}}
|
||||
|
||||
err = database.UpdateCluster(query, update)
|
||||
err = dbOperationsCluster.UpdateCluster(query, update)
|
||||
if err != nil {
|
||||
log.Print("Error", err)
|
||||
}
|
||||
|
|
@ -314,7 +313,7 @@ func (r *subscriptionResolver) ClusterConnect(ctx context.Context, clusterInfo m
|
|||
query := bson.D{{"cluster_id", clusterInfo.ClusterID}}
|
||||
update := bson.D{{"$set", bson.D{{"is_active", true}, {"updated_at", strconv.FormatInt(time.Now().Unix(), 10)}}}}
|
||||
|
||||
err = database.UpdateCluster(query, update)
|
||||
err = dbOperationsCluster.UpdateCluster(query, update)
|
||||
if err != nil {
|
||||
return clusterAction, err
|
||||
}
|
||||
|
|
@ -323,7 +322,7 @@ func (r *subscriptionResolver) ClusterConnect(ctx context.Context, clusterInfo m
|
|||
copier.Copy(&newVerifiedCluster, &verifiedCluster)
|
||||
|
||||
verifiedCluster.IsActive = true
|
||||
subscriptions.SendClusterEvent("cluster-status", "Cluster Live", "Cluster is Live and Connected", newVerifiedCluster, *store.Store)
|
||||
clusterHandler.SendClusterEvent("cluster-status", "Cluster Live", "Cluster is Live and Connected", newVerifiedCluster, *store.Store)
|
||||
return clusterAction, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,18 +3,20 @@ package handler
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/analytics"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/analytics/ops/prometheus"
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
database_operations "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/operations"
|
||||
dbSchema "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jinzhu/copier"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/analytics"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/analytics/ops/prometheus"
|
||||
dbOperationsAnalytics "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/analytics"
|
||||
dbSchemaAnalytics "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/analytics"
|
||||
dbOperationsCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
|
||||
)
|
||||
|
||||
func CreateDataSource(datasource *model.DSInput) (*model.DSResponse, error) {
|
||||
|
|
@ -23,7 +25,7 @@ func CreateDataSource(datasource *model.DSInput) (*model.DSResponse, error) {
|
|||
|
||||
if datasourceStatus == "Active" {
|
||||
|
||||
newDS := dbSchema.DataSource{
|
||||
newDS := dbSchemaAnalytics.DataSource{
|
||||
DsID: uuid.New().String(),
|
||||
DsName: datasource.DsName,
|
||||
DsType: datasource.DsType,
|
||||
|
|
@ -40,7 +42,7 @@ func CreateDataSource(datasource *model.DSInput) (*model.DSResponse, error) {
|
|||
UpdatedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
}
|
||||
|
||||
err := database_operations.InsertDataSource(newDS)
|
||||
err := dbOperationsAnalytics.InsertDataSource(newDS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -56,7 +58,7 @@ func CreateDataSource(datasource *model.DSInput) (*model.DSResponse, error) {
|
|||
|
||||
func CreateDashboard(dashboard *model.CreateDBInput) (string, error) {
|
||||
|
||||
newDashboard := dbSchema.DashBoard{
|
||||
newDashboard := dbSchemaAnalytics.DashBoard{
|
||||
DbID: uuid.New().String(),
|
||||
DbName: dashboard.DbName,
|
||||
DbType: dashboard.DbType,
|
||||
|
|
@ -72,8 +74,8 @@ func CreateDashboard(dashboard *model.CreateDBInput) (string, error) {
|
|||
}
|
||||
|
||||
var (
|
||||
newPanelGroups = make([]dbSchema.PanelGroup, len(dashboard.PanelGroups))
|
||||
newPanels []*dbSchema.Panel
|
||||
newPanelGroups = make([]dbSchemaAnalytics.PanelGroup, len(dashboard.PanelGroups))
|
||||
newPanels []*dbSchemaAnalytics.Panel
|
||||
)
|
||||
|
||||
for i, panel_group := range dashboard.PanelGroups {
|
||||
|
|
@ -83,13 +85,13 @@ func CreateDashboard(dashboard *model.CreateDBInput) (string, error) {
|
|||
newPanelGroups[i].PanelGroupName = panel_group.PanelGroupName
|
||||
|
||||
for _, panel := range panel_group.Panels {
|
||||
var newPromQueries []*dbSchema.PromQuery
|
||||
var newPromQueries []*dbSchemaAnalytics.PromQuery
|
||||
copier.Copy(&newPromQueries, &panel.PromQueries)
|
||||
|
||||
var newPanelOptions dbSchema.PanelOption
|
||||
var newPanelOptions dbSchemaAnalytics.PanelOption
|
||||
copier.Copy(&newPanelOptions, &panel.PanelOptions)
|
||||
|
||||
newPanel := dbSchema.Panel{
|
||||
newPanel := dbSchemaAnalytics.Panel{
|
||||
PanelID: uuid.New().String(),
|
||||
PanelOptions: &newPanelOptions,
|
||||
PanelName: panel.PanelName,
|
||||
|
|
@ -107,7 +109,7 @@ func CreateDashboard(dashboard *model.CreateDBInput) (string, error) {
|
|||
newPanels = append(newPanels, &newPanel)
|
||||
}
|
||||
}
|
||||
err := database_operations.InsertPanel(newPanels)
|
||||
err := dbOperationsAnalytics.InsertPanel(newPanels)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error on inserting panel data", err)
|
||||
}
|
||||
|
|
@ -115,7 +117,7 @@ func CreateDashboard(dashboard *model.CreateDBInput) (string, error) {
|
|||
|
||||
newDashboard.PanelGroups = newPanelGroups
|
||||
|
||||
err = database_operations.InsertDashBoard(newDashboard)
|
||||
err = dbOperationsAnalytics.InsertDashBoard(newDashboard)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error on inserting panel data", err)
|
||||
}
|
||||
|
|
@ -140,7 +142,7 @@ func UpdateDataSource(datasource model.DSInput) (*model.DSResponse, error) {
|
|||
{"query_timeout", datasource.QueryTimeout}, {"http_method", datasource.HTTPMethod},
|
||||
{"updated_at", timestamp}}}}
|
||||
|
||||
err := database_operations.UpdateDataSource(query, update)
|
||||
err := dbOperationsAnalytics.UpdateDataSource(query, update)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -175,7 +177,7 @@ func UpdateDashBoard(dashboard *model.UpdataDBInput) (string, error) {
|
|||
{"start_time", dashboard.StartTime}, {"refresh_rate", dashboard.RefreshRate},
|
||||
{"panel_groups", dashboard.PanelGroups}, {"updated_at", timestamp}}}}
|
||||
|
||||
err := database_operations.UpdateDashboard(query, update)
|
||||
err := dbOperationsAnalytics.UpdateDashboard(query, update)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -192,10 +194,10 @@ func UpdatePanel(panels []*model.Panel) (string, error) {
|
|||
return "", errors.New("DashBoard ID or Datasource is nil or empty")
|
||||
}
|
||||
|
||||
var newPanelOption dbSchema.PanelOption
|
||||
var newPanelOption dbSchemaAnalytics.PanelOption
|
||||
copier.Copy(&newPanelOption, &panel.PanelOptions)
|
||||
|
||||
var newPromQueries []dbSchema.PromQuery
|
||||
var newPromQueries []dbSchemaAnalytics.PromQuery
|
||||
copier.Copy(&newPromQueries, panel.PromQueries)
|
||||
|
||||
query := bson.D{{"panel_id", panel.PanelID}}
|
||||
|
|
@ -206,7 +208,7 @@ func UpdatePanel(panels []*model.Panel) (string, error) {
|
|||
{"y_axis_left", panel.YAxisLeft}, {"y_axis_right", panel.YAxisRight},
|
||||
{"x_axis_down", panel.XAxisDown}, {"unit", panel.Unit}}}}
|
||||
|
||||
err := database_operations.UpdatePanel(query, update)
|
||||
err := dbOperationsAnalytics.UpdatePanel(query, update)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -218,14 +220,14 @@ func UpdatePanel(panels []*model.Panel) (string, error) {
|
|||
func DeleteDashboard(db_id *string) (bool, error) {
|
||||
|
||||
dashboardQuery := bson.M{"db_id": db_id, "is_removed": false}
|
||||
dashboard, err := database_operations.GetDashboard(dashboardQuery)
|
||||
dashboard, err := dbOperationsAnalytics.GetDashboard(dashboardQuery)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to list dashboard, error: %v", err)
|
||||
}
|
||||
|
||||
for _, panelGroup := range dashboard.PanelGroups {
|
||||
listPanelQuery := bson.M{"panel_group_id": panelGroup.PanelGroupID, "is_removed": false}
|
||||
panels, err := database_operations.ListPanel(listPanelQuery)
|
||||
panels, err := dbOperationsAnalytics.ListPanel(listPanelQuery)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to list Panel, error: %v", err)
|
||||
}
|
||||
|
|
@ -236,7 +238,7 @@ func DeleteDashboard(db_id *string) (bool, error) {
|
|||
query := bson.D{{"panel_id", panel.PanelID}, {"is_removed", false}}
|
||||
update := bson.D{{"$set", bson.D{{"is_removed", true}, {"updated_at", time}}}}
|
||||
|
||||
err := database_operations.UpdatePanel(query, update)
|
||||
err := dbOperationsAnalytics.UpdatePanel(query, update)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to delete panel, error: %v", err)
|
||||
}
|
||||
|
|
@ -248,7 +250,7 @@ func DeleteDashboard(db_id *string) (bool, error) {
|
|||
query := bson.D{{"db_id", db_id}}
|
||||
update := bson.D{{"$set", bson.D{{"is_removed", true}, {"updated_at", time}}}}
|
||||
|
||||
err = database_operations.UpdateDashboard(query, update)
|
||||
err = dbOperationsAnalytics.UpdateDashboard(query, update)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to delete dashboard, error: %v", err)
|
||||
}
|
||||
|
|
@ -261,7 +263,7 @@ func DeleteDataSource(input model.DeleteDSInput) (bool, error) {
|
|||
time := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
|
||||
listDBQuery := bson.M{"ds_id": input.DsID, "is_removed": false}
|
||||
dashboards, err := database_operations.ListDashboard(listDBQuery)
|
||||
dashboards, err := dbOperationsAnalytics.ListDashboard(listDBQuery)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to list dashboard, error: %v", err)
|
||||
}
|
||||
|
|
@ -271,7 +273,7 @@ func DeleteDataSource(input model.DeleteDSInput) (bool, error) {
|
|||
|
||||
for _, panelGroup := range dashboard.PanelGroups {
|
||||
listPanelQuery := bson.M{"panel_group_id": panelGroup.PanelGroupID, "is_removed": false}
|
||||
panels, err := database_operations.ListPanel(listPanelQuery)
|
||||
panels, err := dbOperationsAnalytics.ListPanel(listPanelQuery)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to list Panel, error: %v", err)
|
||||
}
|
||||
|
|
@ -280,7 +282,7 @@ func DeleteDataSource(input model.DeleteDSInput) (bool, error) {
|
|||
query := bson.D{{"panel_id", panel.PanelID}, {"is_removed", false}}
|
||||
update := bson.D{{"$set", bson.D{{"is_removed", true}, {"updated_at", time}}}}
|
||||
|
||||
err := database_operations.UpdatePanel(query, update)
|
||||
err := dbOperationsAnalytics.UpdatePanel(query, update)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to delete panel, error: %v", err)
|
||||
}
|
||||
|
|
@ -289,7 +291,7 @@ func DeleteDataSource(input model.DeleteDSInput) (bool, error) {
|
|||
updateDBQuery := bson.D{{"db_id", dashboard.DbID}}
|
||||
update := bson.D{{"$set", bson.D{{"is_removed", true}, {"updated_at", time}}}}
|
||||
|
||||
err = database_operations.UpdateDashboard(updateDBQuery, update)
|
||||
err = dbOperationsAnalytics.UpdateDashboard(updateDBQuery, update)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to delete dashboard, error: %v", err)
|
||||
}
|
||||
|
|
@ -307,7 +309,7 @@ func DeleteDataSource(input model.DeleteDSInput) (bool, error) {
|
|||
updateDSQuery := bson.D{{"ds_id", input.DsID}}
|
||||
update := bson.D{{"$set", bson.D{{"is_removed", true}, {"updated_at", time}}}}
|
||||
|
||||
err = database_operations.UpdateDataSource(updateDSQuery, update)
|
||||
err = dbOperationsAnalytics.UpdateDataSource(updateDSQuery, update)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to delete datasource, error: %v", err)
|
||||
}
|
||||
|
|
@ -318,7 +320,7 @@ func DeleteDataSource(input model.DeleteDSInput) (bool, error) {
|
|||
func QueryListDataSource(projectID string) ([]*model.DSResponse, error) {
|
||||
query := bson.M{"project_id": projectID, "is_removed": false}
|
||||
|
||||
datasource, err := database_operations.ListDataSource(query)
|
||||
datasource, err := dbOperationsAnalytics.ListDataSource(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -359,7 +361,7 @@ func GetPromQuery(promInput *model.PromInput) ([]*model.PromResponse, error) {
|
|||
func QueryListDashboard(projectID string) ([]*model.ListDashboardReponse, error) {
|
||||
query := bson.M{"project_id": projectID, "is_removed": false}
|
||||
|
||||
dashboards, err := database_operations.ListDashboard(query)
|
||||
dashboards, err := dbOperationsAnalytics.ListDashboard(query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error on query from dashboard collection by projectid", err)
|
||||
}
|
||||
|
|
@ -368,7 +370,7 @@ func QueryListDashboard(projectID string) ([]*model.ListDashboardReponse, error)
|
|||
copier.Copy(&newListDashboard, &dashboards)
|
||||
|
||||
for _, dashboard := range newListDashboard {
|
||||
datasource, err := database_operations.GetDataSourceByID(dashboard.DsID)
|
||||
datasource, err := dbOperationsAnalytics.GetDataSourceByID(dashboard.DsID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error on querying from datasource collection", err)
|
||||
}
|
||||
|
|
@ -376,7 +378,7 @@ func QueryListDashboard(projectID string) ([]*model.ListDashboardReponse, error)
|
|||
dashboard.DsType = &datasource.DsType
|
||||
dashboard.DsName = &datasource.DsName
|
||||
|
||||
cluster, err := database.GetCluster(dashboard.ClusterID)
|
||||
cluster, err := dbOperationsCluster.GetCluster(dashboard.ClusterID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error on querying from cluster collection", err)
|
||||
}
|
||||
|
|
@ -385,7 +387,7 @@ func QueryListDashboard(projectID string) ([]*model.ListDashboardReponse, error)
|
|||
|
||||
for _, panelgroup := range dashboard.PanelGroups {
|
||||
query := bson.M{"panel_group_id": panelgroup.PanelGroupID, "is_removed": false}
|
||||
panels, err := database_operations.ListPanel(query)
|
||||
panels, err := dbOperationsAnalytics.ListPanel(query)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error on querying from promquery collection", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,10 @@ package prometheus
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/analytics"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/analytics"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
|
|||
|
|
@ -10,14 +10,16 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/prometheus/client_golang/api"
|
||||
apiV1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
||||
md "github.com/prometheus/common/model"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/analytics"
|
||||
"github.com/prometheus/client_golang/api"
|
||||
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
||||
md "github.com/prometheus/common/model"
|
||||
)
|
||||
|
||||
func CreateClient(url string) (apiv1.API, error) {
|
||||
// CreateClient creates a prometheus client from a URL
|
||||
func CreateClient(url string) (apiV1.API, error) {
|
||||
var DefaultRT = api.DefaultRoundTripper
|
||||
|
||||
cfg := api.Config{
|
||||
|
|
@ -30,9 +32,10 @@ func CreateClient(url string) (apiv1.API, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return apiv1.NewAPI(client), nil
|
||||
return apiV1.NewAPI(client), nil
|
||||
}
|
||||
|
||||
// Query is used to query prometheus using client
|
||||
func Query(prom analytics.PromQuery) (model.PromResponse, error) {
|
||||
client, err := CreateClient(prom.URL)
|
||||
if err != nil {
|
||||
|
|
@ -49,7 +52,7 @@ func Query(prom analytics.PromQuery) (model.PromResponse, error) {
|
|||
return model.PromResponse{}, err
|
||||
}
|
||||
|
||||
timeRange := apiv1.Range{
|
||||
timeRange := apiV1.Range{
|
||||
Start: time.Unix(startTime, 0).UTC(),
|
||||
End: time.Unix(endTime, 0).UTC(),
|
||||
Step: time.Duration(int64(prom.Minstep)) * time.Second,
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
var secret = os.Getenv("JWT_SECRET")
|
||||
|
||||
//UserValidateJWT validates the cluster jwt
|
||||
// UserValidateJWT validates the cluster jwt
|
||||
func UserValidateJWT(token string) (jwt.MapClaims, error) {
|
||||
tkn, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
|
||||
if ok := token.Method.Alg() == jwt.SigningMethodHS512.Alg(); !ok {
|
||||
|
|
|
|||
|
|
@ -2,21 +2,27 @@ package handler
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaos-workflow/ops"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
gitops_handler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/gitops/handler"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
dbOperationsCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
|
||||
dbOperationsWorkflow "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/workflow"
|
||||
dbSchemaWorkflow "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/workflow"
|
||||
gitOpsHandler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/gitops/handler"
|
||||
)
|
||||
|
||||
func CreateChaosWorkflow(ctx context.Context, input *model.ChaosWorkFlowInput, r *store.StateData) (*model.ChaosWorkFlowResponse, error) {
|
||||
|
|
@ -26,8 +32,8 @@ func CreateChaosWorkflow(ctx context.Context, input *model.ChaosWorkFlowInput, r
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// Gitops Update
|
||||
err = gitops_handler.UpsertWorkflowToGit(ctx, input)
|
||||
// GitOps Update
|
||||
err = gitOpsHandler.UpsertWorkflowToGit(ctx, input)
|
||||
if err != nil {
|
||||
log.Print("Error performing git push: ", err)
|
||||
return nil, err
|
||||
|
|
@ -50,7 +56,7 @@ func CreateChaosWorkflow(ctx context.Context, input *model.ChaosWorkFlowInput, r
|
|||
|
||||
func DeleteWorkflow(ctx context.Context, workflow_id string, r *store.StateData) (bool, error) {
|
||||
query := bson.D{{Key: "workflow_id", Value: workflow_id}}
|
||||
workflows, err := database.GetWorkflows(query)
|
||||
workflows, err := dbOperationsWorkflow.GetWorkflows(query)
|
||||
if len(workflows) == 0 {
|
||||
return false, errors.New("no such workflow found")
|
||||
}
|
||||
|
|
@ -61,8 +67,8 @@ func DeleteWorkflow(ctx context.Context, workflow_id string, r *store.StateData)
|
|||
WorkflowName: workflows[0].WorkflowName,
|
||||
}
|
||||
|
||||
//gitops delete
|
||||
err = gitops_handler.DeleteWorkflowFromGit(ctx, &wf)
|
||||
// gitOps delete
|
||||
err = gitOpsHandler.DeleteWorkflowFromGit(ctx, &wf)
|
||||
if err != nil {
|
||||
log.Print("Error performing git push: ", err)
|
||||
return false, err
|
||||
|
|
@ -83,8 +89,8 @@ func UpdateWorkflow(ctx context.Context, input *model.ChaosWorkFlowInput, r *sto
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// Gitops Update
|
||||
err = gitops_handler.UpsertWorkflowToGit(ctx, input)
|
||||
// GitOps Update
|
||||
err = gitOpsHandler.UpsertWorkflowToGit(ctx, input)
|
||||
if err != nil {
|
||||
log.Print("Error performing git push: ", err)
|
||||
return nil, err
|
||||
|
|
@ -105,16 +111,16 @@ func UpdateWorkflow(ctx context.Context, input *model.ChaosWorkFlowInput, r *sto
|
|||
}, nil
|
||||
}
|
||||
|
||||
//GetWorkflowRuns sends all the workflow runs for a project from the DB
|
||||
// GetWorkflowRuns sends all the workflow runs for a project from the DB
|
||||
func QueryWorkflowRuns(project_id string) ([]*model.WorkflowRun, error) {
|
||||
workflows, err := database.GetWorkflows(bson.D{{"project_id", project_id}})
|
||||
workflows, err := dbOperationsWorkflow.GetWorkflows(bson.D{{"project_id", project_id}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := []*model.WorkflowRun{}
|
||||
|
||||
for _, workflow := range workflows {
|
||||
cluster, err := database.GetCluster(workflow.ClusterID)
|
||||
cluster, err := dbOperationsCluster.GetCluster(workflow.ClusterID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -137,14 +143,14 @@ func QueryWorkflowRuns(project_id string) ([]*model.WorkflowRun, error) {
|
|||
}
|
||||
|
||||
func QueryWorkflows(project_id string) ([]*model.ScheduledWorkflows, error) {
|
||||
chaosWorkflows, err := database.GetWorkflows(bson.D{{"project_id", project_id}})
|
||||
chaosWorkflows, err := dbOperationsWorkflow.GetWorkflows(bson.D{{"project_id", project_id}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := []*model.ScheduledWorkflows{}
|
||||
for _, workflow := range chaosWorkflows {
|
||||
cluster, err := database.GetCluster(workflow.ClusterID)
|
||||
cluster, err := dbOperationsCluster.GetCluster(workflow.ClusterID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -176,7 +182,7 @@ func QueryWorkflows(project_id string) ([]*model.ScheduledWorkflows, error) {
|
|||
}
|
||||
|
||||
func QueryListWorkflow(project_id string) ([]*model.Workflow, error) {
|
||||
chaosWorkflows, err := database.GetWorkflows(bson.D{{"project_id", project_id}})
|
||||
chaosWorkflows, err := dbOperationsWorkflow.GetWorkflows(bson.D{{"project_id", project_id}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -184,7 +190,7 @@ func QueryListWorkflow(project_id string) ([]*model.Workflow, error) {
|
|||
result := []*model.Workflow{}
|
||||
for _, workflow := range chaosWorkflows {
|
||||
|
||||
cluster, err := database.GetCluster(workflow.ClusterID)
|
||||
cluster, err := dbOperationsCluster.GetCluster(workflow.ClusterID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -218,14 +224,14 @@ func QueryListWorkflow(project_id string) ([]*model.Workflow, error) {
|
|||
|
||||
func QueryListWorkflowByIDs(workflow_ids []*string) ([]*model.Workflow, error) {
|
||||
|
||||
chaosWorkflows, err := database.GetWorkflows(bson.D{{"workflow_id", bson.M{"$in": workflow_ids}}})
|
||||
chaosWorkflows, err := dbOperationsWorkflow.GetWorkflows(bson.D{{"workflow_id", bson.M{"$in": workflow_ids}}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := []*model.Workflow{}
|
||||
|
||||
for _, workflow := range chaosWorkflows {
|
||||
cluster, err := database.GetCluster(workflow.ClusterID)
|
||||
cluster, err := dbOperationsCluster.GetCluster(workflow.ClusterID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -258,10 +264,98 @@ func QueryListWorkflowByIDs(workflow_ids []*string) ([]*model.Workflow, error) {
|
|||
return result, nil
|
||||
}
|
||||
|
||||
//ReRunWorkflow sends workflow run request(single run workflow only) to agent on workflow re-run request
|
||||
// WorkFlowRunHandler Updates or Inserts a new Workflow Run into the DB
|
||||
func WorkFlowRunHandler(input model.WorkflowRunInput, r store.StateData) (string, error) {
|
||||
cluster, err := cluster.VerifyCluster(*input.ClusterID)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
// err = dbOperationsWorkflow.UpdateWorkflowRun(dbOperationsWorkflow.WorkflowRun(newWorkflowRun))
|
||||
count, err := dbOperationsWorkflow.UpdateWorkflowRun(input.WorkflowID, dbSchemaWorkflow.ChaosWorkflowRun{
|
||||
WorkflowRunID: input.WorkflowRunID,
|
||||
LastUpdated: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
ExecutionData: input.ExecutionData,
|
||||
Completed: input.Completed,
|
||||
})
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
return "Workflow Run Discarded[Duplicate Event]", nil
|
||||
}
|
||||
|
||||
ops.SendWorkflowEvent(model.WorkflowRun{
|
||||
ClusterID: cluster.ClusterID,
|
||||
ClusterName: cluster.ClusterName,
|
||||
ProjectID: cluster.ProjectID,
|
||||
LastUpdated: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
WorkflowRunID: input.WorkflowRunID,
|
||||
WorkflowName: input.WorkflowName,
|
||||
ExecutionData: input.ExecutionData,
|
||||
WorkflowID: input.WorkflowID,
|
||||
}, &r)
|
||||
|
||||
return "Workflow Run Accepted", nil
|
||||
}
|
||||
|
||||
// LogsHandler receives logs from the workflow-agent and publishes to frontend clients
|
||||
func LogsHandler(podLog model.PodLog, r store.StateData) (string, error) {
|
||||
_, err := cluster.VerifyCluster(*podLog.ClusterID)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return "", err
|
||||
}
|
||||
if reqChan, ok := r.WorkflowLog[podLog.RequestID]; ok {
|
||||
resp := model.PodLogResponse{
|
||||
PodName: podLog.PodName,
|
||||
WorkflowRunID: podLog.WorkflowRunID,
|
||||
PodType: podLog.PodType,
|
||||
Log: podLog.Log,
|
||||
}
|
||||
reqChan <- &resp
|
||||
close(reqChan)
|
||||
return "LOGS SENT SUCCESSFULLY", nil
|
||||
}
|
||||
return "LOG REQUEST CANCELLED", nil
|
||||
}
|
||||
|
||||
// GetLogs query is used to fetch the logs from the cluster
|
||||
func GetLogs(reqID string, pod model.PodLogRequest, r store.StateData) {
|
||||
data, err := json.Marshal(pod)
|
||||
if err != nil {
|
||||
log.Print("ERROR WHILE MARSHALLING POD DETAILS")
|
||||
}
|
||||
reqType := "logs"
|
||||
externalData := string(data)
|
||||
payload := model.ClusterAction{
|
||||
ProjectID: reqID,
|
||||
Action: &model.ActionPayload{
|
||||
RequestType: reqType,
|
||||
ExternalData: &externalData,
|
||||
},
|
||||
}
|
||||
if clusterChan, ok := r.ConnectedCluster[pod.ClusterID]; ok {
|
||||
clusterChan <- &payload
|
||||
} else if reqChan, ok := r.WorkflowLog[reqID]; ok {
|
||||
resp := model.PodLogResponse{
|
||||
PodName: pod.PodName,
|
||||
WorkflowRunID: pod.WorkflowRunID,
|
||||
PodType: pod.PodType,
|
||||
Log: "CLUSTER ERROR : CLUSTER NOT CONNECTED",
|
||||
}
|
||||
reqChan <- &resp
|
||||
close(reqChan)
|
||||
}
|
||||
}
|
||||
|
||||
// ReRunWorkflow sends workflow run request(single run workflow only) to agent on workflow re-run request
|
||||
func ReRunWorkflow(workflowID string) (string, error) {
|
||||
query := bson.D{{"workflow_id", workflowID}, {"isRemoved", false}}
|
||||
workflows, err := database.GetWorkflows(query)
|
||||
workflows, err := dbOperationsWorkflow.GetWorkflows(query)
|
||||
if err != nil {
|
||||
log.Print("Could not get workflow :", err)
|
||||
return "could not get workflow", err
|
||||
|
|
@ -288,14 +382,3 @@ func ReRunWorkflow(workflowID string) (string, error) {
|
|||
|
||||
return "Request for re-run acknowledged, workflowID: " + workflowID, nil
|
||||
}
|
||||
|
||||
//SendWorkflowEvent sends workflow events from the clusters to the appropriate users listening for the events
|
||||
func SendWorkflowEvent(wfRun model.WorkflowRun, r *store.StateData) {
|
||||
r.Mutex.Lock()
|
||||
if r.WorkflowEventPublish != nil {
|
||||
for _, observer := range r.WorkflowEventPublish[wfRun.ProjectID] {
|
||||
observer <- &wfRun
|
||||
}
|
||||
}
|
||||
r.Mutex.Unlock()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,30 +3,34 @@ package ops
|
|||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/ghodss/yaml"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
|
||||
"github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jinzhu/copier"
|
||||
chaostypes "github.com/litmuschaos/chaos-operator/pkg/apis/litmuschaos/v1alpha1"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/graphql"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/graphql/subscriptions"
|
||||
chaosTypes "github.com/litmuschaos/chaos-operator/pkg/apis/litmuschaos/v1alpha1"
|
||||
"github.com/tidwall/gjson"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
clusterOps "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"
|
||||
clusterHandler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster/handler"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
dbOperationsCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
|
||||
dbOperationsWorkflow "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/workflow"
|
||||
dbSchemaWorkflow "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/workflow"
|
||||
)
|
||||
|
||||
//ProcessWorkflow takes the workflow and processes it as required
|
||||
// ProcessWorkflow takes the workflow and processes it as required
|
||||
func ProcessWorkflow(workflow *model.ChaosWorkFlowInput) (*model.ChaosWorkFlowInput, error) {
|
||||
// security check for cluster access
|
||||
cluster, err := database.GetCluster(workflow.ClusterID)
|
||||
cluster, err := dbOperationsCluster.GetCluster(workflow.ClusterID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -84,7 +88,7 @@ func ProcessWorkflow(workflow *model.ChaosWorkFlowInput) (*model.ChaosWorkFlowIn
|
|||
data = strings.ReplaceAll(data, "{{", "")
|
||||
data = strings.ReplaceAll(data, "}}", "")
|
||||
|
||||
var meta chaostypes.ChaosEngine
|
||||
var meta chaosTypes.ChaosEngine
|
||||
err := yaml.Unmarshal([]byte(data), &meta)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to unmarshal chaosengine")
|
||||
|
|
@ -164,7 +168,7 @@ func ProcessWorkflow(workflow *model.ChaosWorkFlowInput) (*model.ChaosWorkFlowIn
|
|||
data = strings.ReplaceAll(data, "{{", "")
|
||||
data = strings.ReplaceAll(data, "}}", "")
|
||||
|
||||
var meta chaostypes.ChaosEngine
|
||||
var meta chaosTypes.ChaosEngine
|
||||
err = yaml.Unmarshal([]byte(data), &meta)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to unmarshal chaosengine")
|
||||
|
|
@ -217,14 +221,14 @@ func ProcessWorkflow(workflow *model.ChaosWorkFlowInput) (*model.ChaosWorkFlowIn
|
|||
return workflow, nil
|
||||
}
|
||||
|
||||
//ProcessWorkflowCreation creates new workflow entry and sends the workflow to the specific agent for execution
|
||||
// ProcessWorkflowCreation creates new workflow entry and sends the workflow to the specific agent for execution
|
||||
func ProcessWorkflowCreation(input *model.ChaosWorkFlowInput, r *store.StateData) error {
|
||||
var Weightages []*database.WeightagesInput
|
||||
var Weightages []*dbSchemaWorkflow.WeightagesInput
|
||||
if input.Weightages != nil {
|
||||
copier.Copy(&Weightages, &input.Weightages)
|
||||
}
|
||||
|
||||
newChaosWorkflow := database.ChaosWorkFlowInput{
|
||||
newChaosWorkflow := dbSchemaWorkflow.ChaosWorkFlowInput{
|
||||
WorkflowID: *input.WorkflowID,
|
||||
WorkflowManifest: input.WorkflowManifest,
|
||||
CronSyntax: input.CronSyntax,
|
||||
|
|
@ -236,11 +240,11 @@ func ProcessWorkflowCreation(input *model.ChaosWorkFlowInput, r *store.StateData
|
|||
Weightages: Weightages,
|
||||
CreatedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
UpdatedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
WorkflowRuns: []*database.WorkflowRun{},
|
||||
WorkflowRuns: []*dbSchemaWorkflow.ChaosWorkflowRun{},
|
||||
IsRemoved: false,
|
||||
}
|
||||
|
||||
err := database.InsertChaosWorkflow(newChaosWorkflow)
|
||||
err := dbOperationsWorkflow.InsertChaosWorkflow(newChaosWorkflow)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -252,9 +256,9 @@ func ProcessWorkflowCreation(input *model.ChaosWorkFlowInput, r *store.StateData
|
|||
return nil
|
||||
}
|
||||
|
||||
//ProcessWorkflowUpdate updates the workflow entry and sends update resource request to required agent
|
||||
// ProcessWorkflowUpdate updates the workflow entry and sends update resource request to required agent
|
||||
func ProcessWorkflowUpdate(workflow *model.ChaosWorkFlowInput, r *store.StateData) error {
|
||||
var Weightages []*database.WeightagesInput
|
||||
var Weightages []*dbSchemaWorkflow.WeightagesInput
|
||||
if workflow.Weightages != nil {
|
||||
copier.Copy(&Weightages, &workflow.Weightages)
|
||||
}
|
||||
|
|
@ -262,7 +266,7 @@ func ProcessWorkflowUpdate(workflow *model.ChaosWorkFlowInput, r *store.StateDat
|
|||
query := bson.D{{"workflow_id", workflow.WorkflowID}, {"project_id", workflow.ProjectID}}
|
||||
update := bson.D{{"$set", bson.D{{"workflow_manifest", workflow.WorkflowManifest}, {"cronSyntax", workflow.CronSyntax}, {"workflow_name", workflow.WorkflowName}, {"workflow_description", workflow.WorkflowDescription}, {"isCustomWorkflow", workflow.IsCustomWorkflow}, {"weightages", Weightages}, {"updated_at", strconv.FormatInt(time.Now().Unix(), 10)}}}}
|
||||
|
||||
err := database.UpdateChaosWorkflow(query, update)
|
||||
err := dbOperationsWorkflow.UpdateChaosWorkflow(query, update)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -273,16 +277,16 @@ func ProcessWorkflowUpdate(workflow *model.ChaosWorkFlowInput, r *store.StateDat
|
|||
return nil
|
||||
}
|
||||
|
||||
//ProcessWorkflowDelete deletes the workflow entry and sends delete resource request to required agent
|
||||
// ProcessWorkflowDelete deletes the workflow entry and sends delete resource request to required agent
|
||||
func ProcessWorkflowDelete(query bson.D, r *store.StateData) error {
|
||||
workflows, err := database.GetWorkflows(query)
|
||||
workflows, err := dbOperationsWorkflow.GetWorkflows(query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
update := bson.D{{"$set", bson.D{{"isRemoved", true}}}}
|
||||
|
||||
err = database.UpdateChaosWorkflow(query, update)
|
||||
err = dbOperationsWorkflow.UpdateChaosWorkflow(query, update)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -306,7 +310,7 @@ func SendWorkflowToSubscriber(workflow *model.ChaosWorkFlowInput, reqType string
|
|||
if workflowNamespace == "" {
|
||||
workflowNamespace = os.Getenv("AGENT_NAMESPACE")
|
||||
}
|
||||
subscriptions.SendRequestToSubscriber(graphql.SubscriberRequests{
|
||||
clusterHandler.SendRequestToSubscriber(clusterOps.SubscriberRequests{
|
||||
K8sManifest: workflow.WorkflowManifest,
|
||||
RequestType: reqType,
|
||||
ProjectID: workflow.ProjectID,
|
||||
|
|
@ -314,3 +318,14 @@ func SendWorkflowToSubscriber(workflow *model.ChaosWorkFlowInput, reqType string
|
|||
Namespace: workflowNamespace,
|
||||
}, *r)
|
||||
}
|
||||
|
||||
// SendWorkflowEvent sends workflow events from the clusters to the appropriate users listening for the events
|
||||
func SendWorkflowEvent(wfRun model.WorkflowRun, r *store.StateData) {
|
||||
r.Mutex.Lock()
|
||||
if r.WorkflowEventPublish != nil {
|
||||
for _, observer := range r.WorkflowEventPublish[wfRun.ProjectID] {
|
||||
observer <- &wfRun
|
||||
}
|
||||
}
|
||||
r.Mutex.Unlock()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,13 +3,14 @@ package cluster
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"os"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
|
||||
var secret = os.Getenv("JWT_SECRET")
|
||||
|
||||
//ClusterCreateJWT generates jwt used in cluster registration
|
||||
// ClusterCreateJWT generates jwt used in cluster registration
|
||||
func ClusterCreateJWT(id string) (string, error) {
|
||||
claims := jwt.MapClaims{}
|
||||
claims["cluster_id"] = id
|
||||
|
|
@ -23,7 +24,7 @@ func ClusterCreateJWT(id string) (string, error) {
|
|||
return tokenString, nil
|
||||
}
|
||||
|
||||
//ClusterValidateJWT validates the cluster jwt
|
||||
// ClusterValidateJWT validates the cluster jwt
|
||||
func ClusterValidateJWT(token string) (string, error) {
|
||||
tkn, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,238 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/pkg/errors"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
clusterOps "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
dbOperationsCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
|
||||
dbSchemaCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
|
||||
dbOperationsWorkflow "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/workflow"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/utils"
|
||||
)
|
||||
|
||||
// ClusterRegister creates an entry for a new cluster in DB and generates the url used to apply manifest
|
||||
func ClusterRegister(input model.ClusterInput) (*model.ClusterRegResponse, error) {
|
||||
clusterID := uuid.New().String()
|
||||
|
||||
token, err := clusterOps.ClusterCreateJWT(clusterID)
|
||||
if err != nil {
|
||||
return &model.ClusterRegResponse{}, err
|
||||
}
|
||||
|
||||
newCluster := dbSchemaCluster.Cluster{
|
||||
ClusterID: clusterID,
|
||||
ClusterName: input.ClusterName,
|
||||
Description: input.Description,
|
||||
ProjectID: input.ProjectID,
|
||||
AccessKey: utils.RandomString(32),
|
||||
ClusterType: input.ClusterType,
|
||||
PlatformName: input.PlatformName,
|
||||
AgentNamespace: input.AgentNamespace,
|
||||
Serviceaccount: input.Serviceaccount,
|
||||
AgentScope: input.AgentScope,
|
||||
AgentNsExists: input.AgentNsExists,
|
||||
AgentSaExists: input.AgentSaExists,
|
||||
CreatedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
UpdatedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
Token: token,
|
||||
IsRemoved: false,
|
||||
}
|
||||
|
||||
err = dbOperationsCluster.InsertCluster(newCluster)
|
||||
if err != nil {
|
||||
return &model.ClusterRegResponse{}, err
|
||||
}
|
||||
|
||||
log.Print("NEW CLUSTER REGISTERED : ID-", clusterID, " PID-", input.ProjectID)
|
||||
|
||||
return &model.ClusterRegResponse{
|
||||
ClusterID: newCluster.ClusterID,
|
||||
Token: token,
|
||||
ClusterName: newCluster.ClusterName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ConfirmClusterRegistration takes the cluster_id and access_key from the subscriber and validates it, if validated generates and sends new access_key
|
||||
func ConfirmClusterRegistration(identity model.ClusterIdentity, r store.StateData) (*model.ClusterConfirmResponse, error) {
|
||||
cluster, err := dbOperationsCluster.GetCluster(identity.ClusterID)
|
||||
if err != nil {
|
||||
return &model.ClusterConfirmResponse{IsClusterConfirmed: false}, err
|
||||
}
|
||||
|
||||
if cluster.AccessKey == identity.AccessKey {
|
||||
newKey := utils.RandomString(32)
|
||||
time := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
query := bson.D{{"cluster_id", identity.ClusterID}}
|
||||
update := bson.D{{"$unset", bson.D{{"token", ""}}}, {"$set", bson.D{{"access_key", newKey}, {"is_registered", true}, {"is_cluster_confirmed", true}, {"updated_at", time}}}}
|
||||
|
||||
err = dbOperationsCluster.UpdateCluster(query, update)
|
||||
if err != nil {
|
||||
return &model.ClusterConfirmResponse{IsClusterConfirmed: false}, err
|
||||
}
|
||||
|
||||
cluster.IsRegistered = true
|
||||
cluster.AccessKey = ""
|
||||
|
||||
newCluster := model.Cluster{}
|
||||
copier.Copy(&newCluster, &cluster)
|
||||
|
||||
log.Print("CLUSTER Confirmed : ID-", cluster.ClusterID, " PID-", cluster.ProjectID)
|
||||
SendClusterEvent("cluster-registration", "New Cluster", "New Cluster registration", newCluster, r)
|
||||
|
||||
return &model.ClusterConfirmResponse{IsClusterConfirmed: true, NewClusterKey: &newKey, ClusterID: &cluster.ClusterID}, err
|
||||
}
|
||||
return &model.ClusterConfirmResponse{IsClusterConfirmed: false}, err
|
||||
}
|
||||
|
||||
// NewEvent takes a event from a subscriber, validates identity and broadcasts the event to the users
|
||||
func NewEvent(clusterEvent model.ClusterEventInput, r store.StateData) (string, error) {
|
||||
cluster, err := dbOperationsCluster.GetCluster(clusterEvent.ClusterID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if cluster.AccessKey == clusterEvent.AccessKey && cluster.IsRegistered {
|
||||
log.Print("CLUSTER EVENT : ID-", cluster.ClusterID, " PID-", cluster.ProjectID)
|
||||
|
||||
newCluster := model.Cluster{}
|
||||
copier.Copy(&newCluster, &cluster)
|
||||
|
||||
SendClusterEvent("cluster-event", clusterEvent.EventName, clusterEvent.Description, newCluster, r)
|
||||
return "Event Published", nil
|
||||
}
|
||||
|
||||
return "", errors.New("ERROR WITH CLUSTER EVENT")
|
||||
}
|
||||
|
||||
// DeleteCluster takes clusterID and r parameters, deletes the cluster from the database and sends a request to the subscriber for clean-up
|
||||
func DeleteCluster(clusterID string, r store.StateData) (string, error) {
|
||||
time := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
|
||||
query := bson.D{{"cluster_id", clusterID}}
|
||||
update := bson.D{{"$set", bson.D{{"is_removed", true}, {"updated_at", time}}}}
|
||||
|
||||
err := dbOperationsCluster.UpdateCluster(query, update)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cluster, err := dbOperationsCluster.GetCluster(clusterID)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
requests := []string{
|
||||
`{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": {
|
||||
"name": "subscriber",
|
||||
"namespace": ` + *cluster.AgentNamespace + `
|
||||
}
|
||||
}`,
|
||||
`{
|
||||
"apiVersion": "v1",
|
||||
"kind": "ConfigMap",
|
||||
"metadata": {
|
||||
"name": "litmus-portal-config",
|
||||
"namespace": ` + *cluster.AgentNamespace + `
|
||||
}
|
||||
}`,
|
||||
}
|
||||
|
||||
for _, request := range requests {
|
||||
SendRequestToSubscriber(clusterOps.SubscriberRequests{
|
||||
K8sManifest: request,
|
||||
RequestType: "delete",
|
||||
ProjectID: cluster.ProjectID,
|
||||
ClusterID: clusterID,
|
||||
Namespace: *cluster.AgentNamespace,
|
||||
}, r)
|
||||
}
|
||||
|
||||
return "Successfully deleted cluster", nil
|
||||
}
|
||||
|
||||
// QueryGetClusters takes a projectID and clusterType to filter and return a list of clusters
|
||||
func QueryGetClusters(projectID string, clusterType *string) ([]*model.Cluster, error) {
|
||||
clusters, err := dbOperationsCluster.GetClusterWithProjectID(projectID, clusterType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newClusters := []*model.Cluster{}
|
||||
|
||||
for _, cluster := range clusters {
|
||||
var totalNoOfSchedules int
|
||||
|
||||
workflows, err := dbOperationsWorkflow.GetWorkflowsByClusterID(cluster.ClusterID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newCluster := model.Cluster{}
|
||||
copier.Copy(&newCluster, &cluster)
|
||||
newCluster.NoOfWorkflows = func(i int) *int { return &i }(len(workflows))
|
||||
for _, workflow := range workflows {
|
||||
totalNoOfSchedules = totalNoOfSchedules + len(workflow.WorkflowRuns)
|
||||
}
|
||||
|
||||
newCluster.NoOfSchedules = func(i int) *int { return &i }(totalNoOfSchedules)
|
||||
|
||||
newClusters = append(newClusters, &newCluster)
|
||||
}
|
||||
|
||||
return newClusters, nil
|
||||
}
|
||||
|
||||
// SendClusterEvent sends events from the clusters to the appropriate users listening for the events
|
||||
func SendClusterEvent(eventType, eventName, description string, cluster model.Cluster, r store.StateData) {
|
||||
newEvent := model.ClusterEvent{
|
||||
EventID: uuid.New().String(),
|
||||
EventType: eventType,
|
||||
EventName: eventName,
|
||||
Description: description,
|
||||
Cluster: &cluster,
|
||||
}
|
||||
r.Mutex.Lock()
|
||||
if r.ClusterEventPublish != nil {
|
||||
for _, observer := range r.ClusterEventPublish[cluster.ProjectID] {
|
||||
observer <- &newEvent
|
||||
}
|
||||
}
|
||||
r.Mutex.Unlock()
|
||||
}
|
||||
|
||||
// SendRequestToSubscriber sends events from the graphQL server to the subscribers listening for the requests
|
||||
func SendRequestToSubscriber(subscriberRequest clusterOps.SubscriberRequests, r store.StateData) {
|
||||
if os.Getenv("AGENT_SCOPE") == "cluster" {
|
||||
/*
|
||||
namespace = Obtain from WorkflowManifest or
|
||||
from frontend as a separate workflowNamespace field under ChaosWorkFlowInput model
|
||||
for CreateChaosWorkflow mutation to be passed to this function.
|
||||
*/
|
||||
}
|
||||
newAction := &model.ClusterAction{
|
||||
ProjectID: subscriberRequest.ProjectID,
|
||||
Action: &model.ActionPayload{
|
||||
K8sManifest: subscriberRequest.K8sManifest,
|
||||
Namespace: subscriberRequest.Namespace,
|
||||
RequestType: subscriberRequest.RequestType,
|
||||
},
|
||||
}
|
||||
|
||||
r.Mutex.Lock()
|
||||
|
||||
if observer, ok := r.ConnectedCluster[subscriberRequest.ClusterID]; ok {
|
||||
observer <- newAction
|
||||
}
|
||||
|
||||
r.Mutex.Unlock()
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package graphql
|
||||
package cluster
|
||||
|
||||
// SubscriberRequests contains the required configurable parameters for the requests sent to the subscriber
|
||||
type SubscriberRequests struct {
|
||||
RequestType string `json:"request_type"`
|
||||
K8sManifest string `json:"k8s_manifest"`
|
||||
|
|
@ -3,14 +3,15 @@ package cluster
|
|||
import (
|
||||
"errors"
|
||||
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
dbOperationsCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
|
||||
dbSchemaCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
)
|
||||
|
||||
//VerifyCluster utils function used to verify cluster identity
|
||||
func VerifyCluster(identity model.ClusterIdentity) (*database.Cluster, error) {
|
||||
cluster, err := database.GetCluster(identity.ClusterID)
|
||||
// VerifyCluster utils function used to verify cluster identity
|
||||
func VerifyCluster(identity model.ClusterIdentity) (*dbSchemaCluster.Cluster, error) {
|
||||
cluster, err := dbOperationsCluster.GetCluster(identity.ClusterID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import (
|
|||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
)
|
||||
|
||||
//Application state, contains channels and mutexes used for subscriptions
|
||||
// Application state, contains channels and mutexes used for subscriptions
|
||||
type StateData struct {
|
||||
ClusterEventPublish map[string][]chan *model.ClusterEvent
|
||||
ConnectedCluster map[string]chan *model.ClusterAction
|
||||
|
|
|
|||
|
|
@ -1,20 +1,21 @@
|
|||
package operations
|
||||
package analytics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
dbSchema "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
)
|
||||
|
||||
var (
|
||||
dataSourceCollection *mongo.Collection
|
||||
panelCollection *mongo.Collection
|
||||
dashBoardCollection *mongo.Collection
|
||||
backgroundContext = context.Background()
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
|
@ -23,7 +24,8 @@ func init() {
|
|||
dashBoardCollection = mongodb.Database.Collection("dashboard-collection")
|
||||
}
|
||||
|
||||
func InsertDataSource(datasource dbSchema.DataSource) error {
|
||||
// InsertDataSource takes details of a data source and inserts into the database collection
|
||||
func InsertDataSource(datasource DataSource) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
_, err := dataSourceCollection.InsertOne(ctx, datasource)
|
||||
|
|
@ -34,7 +36,8 @@ func InsertDataSource(datasource dbSchema.DataSource) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func InsertDashBoard(dashboard dbSchema.DashBoard) error {
|
||||
// InsertDashBoard takes details of a dashboard and inserts into the database collection
|
||||
func InsertDashBoard(dashboard DashBoard) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
_, err := dashBoardCollection.InsertOne(ctx, dashboard)
|
||||
|
|
@ -45,7 +48,8 @@ func InsertDashBoard(dashboard dbSchema.DashBoard) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func InsertPanel(panels []*dbSchema.Panel) error {
|
||||
// InsertPanel takes details of a dashboard panel and inserts into the database collection
|
||||
func InsertPanel(panels []*Panel) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
var newInterface []interface{}
|
||||
|
|
@ -61,62 +65,66 @@ func InsertPanel(panels []*dbSchema.Panel) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func ListDataSource(query bson.M) ([]*dbSchema.DataSource, error) {
|
||||
// ListDataSource takes a query parameter to retrieve the data source details from the database
|
||||
func ListDataSource(query bson.M) ([]*DataSource, error) {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
var datasources []*dbSchema.DataSource
|
||||
var datasources []*DataSource
|
||||
cursor, err := dataSourceCollection.Find(ctx, query)
|
||||
if err != nil {
|
||||
return []*dbSchema.DataSource{}, err
|
||||
return []*DataSource{}, err
|
||||
}
|
||||
|
||||
err = cursor.All(ctx, &datasources)
|
||||
if err != nil {
|
||||
return []*dbSchema.DataSource{}, err
|
||||
return []*DataSource{}, err
|
||||
}
|
||||
|
||||
return datasources, nil
|
||||
}
|
||||
|
||||
func ListDashboard(query bson.M) ([]*dbSchema.DashBoard, error) {
|
||||
// ListDashboard takes a query parameter to retrieve the dashboard details from the database
|
||||
func ListDashboard(query bson.M) ([]*DashBoard, error) {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
var dashboards []*dbSchema.DashBoard
|
||||
var dashboards []*DashBoard
|
||||
cursor, err := dashBoardCollection.Find(ctx, query)
|
||||
if err != nil {
|
||||
return []*dbSchema.DashBoard{}, err
|
||||
return []*DashBoard{}, err
|
||||
}
|
||||
|
||||
err = cursor.All(ctx, &dashboards)
|
||||
if err != nil {
|
||||
return []*dbSchema.DashBoard{}, err
|
||||
return []*DashBoard{}, err
|
||||
}
|
||||
|
||||
return dashboards, nil
|
||||
}
|
||||
|
||||
func ListPanel(query bson.M) ([]*dbSchema.Panel, error) {
|
||||
// ListPanel takes a query parameter to retrieve the dashboard panel details from the database
|
||||
func ListPanel(query bson.M) ([]*Panel, error) {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
var panels []*dbSchema.Panel
|
||||
var panels []*Panel
|
||||
cursor, err := panelCollection.Find(ctx, query)
|
||||
if err != nil {
|
||||
return []*dbSchema.Panel{}, err
|
||||
return []*Panel{}, err
|
||||
}
|
||||
|
||||
err = cursor.All(ctx, &panels)
|
||||
if err != nil {
|
||||
return []*dbSchema.Panel{}, err
|
||||
return []*Panel{}, err
|
||||
}
|
||||
|
||||
return panels, nil
|
||||
}
|
||||
|
||||
func GetDataSourceByID(ds_id string) (*dbSchema.DataSource, error) {
|
||||
// GetDataSourceByID takes a dsID parameter to retrieve the data source details from the database
|
||||
func GetDataSourceByID(dsID string) (*DataSource, error) {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
query := bson.M{"ds_id": ds_id}
|
||||
query := bson.M{"ds_id": dsID}
|
||||
|
||||
var datasource *dbSchema.DataSource
|
||||
var datasource *DataSource
|
||||
err := dataSourceCollection.FindOne(ctx, query).Decode(&datasource)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -125,6 +133,7 @@ func GetDataSourceByID(ds_id string) (*dbSchema.DataSource, error) {
|
|||
return datasource, nil
|
||||
}
|
||||
|
||||
// UpdateDataSource takes query and update parameters to update the data source details in the database
|
||||
func UpdateDataSource(query bson.D, update bson.D) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
|
|
@ -136,6 +145,7 @@ func UpdateDataSource(query bson.D, update bson.D) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// UpdateDashboard takes query and update parameters to update the dashboard details in the database
|
||||
func UpdateDashboard(query bson.D, update bson.D) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
|
|
@ -147,6 +157,7 @@ func UpdateDashboard(query bson.D, update bson.D) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// UpdatePanel takes query and update parameters to update the dashboard panel details in the database
|
||||
func UpdatePanel(query bson.D, update bson.D) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
|
|
@ -162,13 +173,14 @@ func UpdatePanel(query bson.D, update bson.D) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDashboard(query bson.M) (dbSchema.DashBoard, error) {
|
||||
// GetDashboard takes a query parameter to retrieve the dashboard details from the database
|
||||
func GetDashboard(query bson.M) (DashBoard, error) {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
var dashboard dbSchema.DashBoard
|
||||
var dashboard DashBoard
|
||||
err := dashBoardCollection.FindOne(ctx, query).Decode(&dashboard)
|
||||
if err != nil {
|
||||
return dbSchema.DashBoard{}, err
|
||||
return DashBoard{}, err
|
||||
}
|
||||
|
||||
return dashboard, nil
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package schema
|
||||
package analytics
|
||||
|
||||
// DataSource ...
|
||||
type DataSource struct {
|
||||
DsID string `bson:"ds_id"`
|
||||
DsName string `bson:"ds_name"`
|
||||
|
|
@ -18,6 +19,7 @@ type DataSource struct {
|
|||
IsRemoved bool `bson:"is_removed"`
|
||||
}
|
||||
|
||||
// DashBoard ...
|
||||
type DashBoard struct {
|
||||
DbID string `bson:"db_id"`
|
||||
DsID string `bson:"ds_id"`
|
||||
|
|
@ -34,11 +36,13 @@ type DashBoard struct {
|
|||
IsRemoved bool `bson:"is_removed"`
|
||||
}
|
||||
|
||||
// PanelGroup ...
|
||||
type PanelGroup struct {
|
||||
PanelGroupName string `bson:"panel_group_name"`
|
||||
PanelGroupID string `bson:"panel_group_id"`
|
||||
}
|
||||
|
||||
// Panel ...
|
||||
type Panel struct {
|
||||
PanelID string `bson:"panel_id"`
|
||||
PanelOptions *PanelOption `bson:"panel_options"`
|
||||
|
|
@ -54,12 +58,14 @@ type Panel struct {
|
|||
IsRemoved bool `bson:"is_removed"`
|
||||
}
|
||||
|
||||
// PanelOption ...
|
||||
type PanelOption struct {
|
||||
Points *bool `bson:"points"`
|
||||
Grids *bool `bson:"grids"`
|
||||
LeftAxis *bool `bson:"left_axis"`
|
||||
}
|
||||
|
||||
// PromQuery ...
|
||||
type PromQuery struct {
|
||||
Queryid string `bson:"queryid"`
|
||||
PromQueryName *string `bson:"prom_query_name"`
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
package cluster
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
)
|
||||
|
||||
var (
|
||||
clusterCollection *mongo.Collection
|
||||
backgroundContext = context.Background()
|
||||
err error
|
||||
)
|
||||
|
||||
func init() {
|
||||
clusterCollection = mongodb.Database.Collection("cluster-collection")
|
||||
}
|
||||
|
||||
// InsertCluster takes details of a cluster and inserts into the database collection
|
||||
func InsertCluster(cluster Cluster) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
_, err := clusterCollection.InsertOne(ctx, cluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCluster takes a clusterID to retrieve the cluster details from the database
|
||||
func GetCluster(clusterID string) (Cluster, error) {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
query := bson.M{"cluster_id": clusterID}
|
||||
|
||||
var cluster Cluster
|
||||
err = clusterCollection.FindOne(ctx, query).Decode(&cluster)
|
||||
if err != nil {
|
||||
return Cluster{}, err
|
||||
}
|
||||
|
||||
return cluster, nil
|
||||
}
|
||||
|
||||
// UpdateCluster takes query and update parameters to update the cluster details in the database
|
||||
func UpdateCluster(query bson.D, update bson.D) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
_, err := clusterCollection.UpdateOne(ctx, query, update)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetClusterWithProjectID takes projectID and clusterType parameters to retrieve the cluster details from the database
|
||||
func GetClusterWithProjectID(projectID string, clusterType *string) ([]*Cluster, error) {
|
||||
|
||||
var query bson.M
|
||||
if clusterType == nil {
|
||||
query = bson.M{"project_id": projectID, "is_removed": false}
|
||||
} else {
|
||||
query = bson.M{"project_id": projectID, "cluster_type": clusterType, "is_removed": false}
|
||||
}
|
||||
|
||||
fmt.Print(query)
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
var clusters []*Cluster
|
||||
|
||||
cursor, err := clusterCollection.Find(ctx, query)
|
||||
if err != nil {
|
||||
return []*Cluster{}, err
|
||||
}
|
||||
|
||||
err = cursor.All(ctx, &clusters)
|
||||
if err != nil {
|
||||
return []*Cluster{}, err
|
||||
}
|
||||
|
||||
return clusters, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package cluster
|
||||
|
||||
// Cluster contains the required fields to be stored in the database for a cluster
|
||||
type Cluster struct {
|
||||
ClusterID string `bson:"cluster_id"`
|
||||
ProjectID string `bson:"project_id"`
|
||||
ClusterName string `bson:"cluster_name"`
|
||||
Description *string `bson:"description"`
|
||||
PlatformName string `bson:"platform_name"`
|
||||
AgentNamespace *string `bson:"agent_namespace"`
|
||||
Serviceaccount *string `bson:"serviceaccount"`
|
||||
AgentScope string `bson:"agent_scope"`
|
||||
AgentNsExists *bool `bson:"agent_ns_exists"`
|
||||
AgentSaExists *bool `bson:"agent_sa_exists"`
|
||||
AccessKey string `bson:"access_key"`
|
||||
IsRegistered bool `bson:"is_registered"`
|
||||
IsClusterConfirmed bool `bson:"is_cluster_confirmed"`
|
||||
IsActive bool `bson:"is_active"`
|
||||
UpdatedAt string `bson:"updated_at"`
|
||||
CreatedAt string `bson:"created_at"`
|
||||
ClusterType string `bson:"cluster_type"`
|
||||
Token string `bson:"token"`
|
||||
IsRemoved bool `bson:"is_removed"`
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package operations
|
||||
package gitops
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
|
@ -6,11 +6,11 @@ import (
|
|||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
dbSchema "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -35,8 +35,8 @@ func init() {
|
|||
}
|
||||
}
|
||||
|
||||
//AddGitConfig inserts new git config for project
|
||||
func AddGitConfig(ctx context.Context, config *dbSchema.GitConfigDB) error {
|
||||
// AddGitConfig inserts new git config for project
|
||||
func AddGitConfig(ctx context.Context, config *GitConfigDB) error {
|
||||
ctx, cancel := context.WithTimeout(backgroundContext, timeout)
|
||||
defer cancel()
|
||||
_, err := gitOpsCollection.InsertOne(ctx, config)
|
||||
|
|
@ -46,12 +46,12 @@ func AddGitConfig(ctx context.Context, config *dbSchema.GitConfigDB) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//GetGitConfig retrieves git config using project id
|
||||
func GetGitConfig(ctx context.Context, projectID string) (*dbSchema.GitConfigDB, error) {
|
||||
// GetGitConfig retrieves git config using project id
|
||||
func GetGitConfig(ctx context.Context, projectID string) (*GitConfigDB, error) {
|
||||
ctx, cancel := context.WithTimeout(backgroundContext, timeout)
|
||||
defer cancel()
|
||||
query := bson.M{"project_id": projectID}
|
||||
var res dbSchema.GitConfigDB
|
||||
var res GitConfigDB
|
||||
err := gitOpsCollection.FindOne(ctx, query).Decode(&res)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
|
|
@ -63,8 +63,8 @@ func GetGitConfig(ctx context.Context, projectID string) (*dbSchema.GitConfigDB,
|
|||
return &res, nil
|
||||
}
|
||||
|
||||
//GetAllGitConfig retrieves all git configs from db
|
||||
func GetAllGitConfig(ctx context.Context) ([]dbSchema.GitConfigDB, error) {
|
||||
// GetAllGitConfig retrieves all git configs from db
|
||||
func GetAllGitConfig(ctx context.Context) ([]GitConfigDB, error) {
|
||||
ctx, cancel := context.WithTimeout(backgroundContext, timeout)
|
||||
defer cancel()
|
||||
query := bson.D{{}}
|
||||
|
|
@ -72,7 +72,7 @@ func GetAllGitConfig(ctx context.Context) ([]dbSchema.GitConfigDB, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var configs []dbSchema.GitConfigDB
|
||||
var configs []GitConfigDB
|
||||
err = cursor.All(ctx, &configs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -80,7 +80,7 @@ func GetAllGitConfig(ctx context.Context) ([]dbSchema.GitConfigDB, error) {
|
|||
return configs, nil
|
||||
}
|
||||
|
||||
//UpdateGitConfig update git config matching the query
|
||||
// UpdateGitConfig update git config matching the query
|
||||
func UpdateGitConfig(ctx context.Context, query bson.D, update bson.D) error {
|
||||
ctx, cancel := context.WithTimeout(backgroundContext, timeout)
|
||||
defer cancel()
|
||||
|
|
@ -96,7 +96,7 @@ func UpdateGitConfig(ctx context.Context, query bson.D, update bson.D) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//DeleteGitConfig removes git config corresponding to the given project id
|
||||
// DeleteGitConfig removes git config corresponding to the given project id
|
||||
func DeleteGitConfig(ctx context.Context, projectID string) error {
|
||||
ctx, cancel := context.WithTimeout(backgroundContext, timeout)
|
||||
defer cancel()
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
package schema
|
||||
package gitops
|
||||
|
||||
import "github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
|
||||
// GitConfigDB ...
|
||||
type GitConfigDB struct {
|
||||
ProjectID string `bson:"project_id"`
|
||||
RepositoryURL string `bson:"repo_url"`
|
||||
|
|
@ -14,6 +15,7 @@ type GitConfigDB struct {
|
|||
SSHPrivateKey *string `bson:"ssh_private_key"`
|
||||
}
|
||||
|
||||
// GetGitConfigDB ...
|
||||
func GetGitConfigDB(config model.GitConfig) GitConfigDB {
|
||||
return GitConfigDB{
|
||||
ProjectID: config.ProjectID,
|
||||
|
|
@ -6,81 +6,20 @@ import (
|
|||
"os"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var (
|
||||
collections = map[string]string{
|
||||
"Cluster": "cluster-collection",
|
||||
"User": "user",
|
||||
"Project": "project",
|
||||
"Workflow": "workflow-collection",
|
||||
}
|
||||
//Database ...
|
||||
// Database ...
|
||||
Database *mongo.Database
|
||||
dbName = "litmus"
|
||||
|
||||
//Collections ...
|
||||
clusterCollection *mongo.Collection
|
||||
workflowCollection *mongo.Collection
|
||||
backgroundContext = context.Background()
|
||||
err error
|
||||
backgroundContext = context.Background()
|
||||
err error
|
||||
)
|
||||
|
||||
type Cluster struct {
|
||||
ClusterID string `bson:"cluster_id"`
|
||||
ProjectID string `bson:"project_id"`
|
||||
ClusterName string `bson:"cluster_name"`
|
||||
Description *string `bson:"description"`
|
||||
PlatformName string `bson:"platform_name"`
|
||||
AgentNamespace *string `bson:"agent_namespace"`
|
||||
Serviceaccount *string `bson:"serviceaccount"`
|
||||
AgentScope string `bson:"agent_scope"`
|
||||
AgentNsExists *bool `bson:"agent_ns_exists"`
|
||||
AgentSaExists *bool `bson:"agent_sa_exists"`
|
||||
AccessKey string `bson:"access_key"`
|
||||
IsRegistered bool `bson:"is_registered"`
|
||||
IsClusterConfirmed bool `bson:"is_cluster_confirmed"`
|
||||
IsActive bool `bson:"is_active"`
|
||||
UpdatedAt string `bson:"updated_at"`
|
||||
CreatedAt string `bson:"created_at"`
|
||||
ClusterType string `bson:"cluster_type"`
|
||||
Token string `bson:"token"`
|
||||
IsRemoved bool `bson:"is_removed"`
|
||||
}
|
||||
|
||||
type ChaosWorkFlowInput struct {
|
||||
WorkflowID string `bson:"workflow_id"`
|
||||
WorkflowManifest string `bson:"workflow_manifest"`
|
||||
CronSyntax string `bson:"cronSyntax"`
|
||||
WorkflowName string `bson:"workflow_name"`
|
||||
WorkflowDescription string `bson:"workflow_description"`
|
||||
Weightages []*WeightagesInput `bson:"weightages"`
|
||||
IsCustomWorkflow bool `bson:"isCustomWorkflow"`
|
||||
UpdatedAt string `bson:"updated_at"`
|
||||
CreatedAt string `bson:"created_at"`
|
||||
ProjectID string `bson:"project_id"`
|
||||
ClusterID string `bson:"cluster_id"`
|
||||
WorkflowRuns []*WorkflowRun `bson:"workflow_runs"`
|
||||
IsRemoved bool `bson:"isRemoved"`
|
||||
}
|
||||
|
||||
type WorkflowRun struct {
|
||||
WorkflowRunID string `bson:"workflow_run_id"`
|
||||
LastUpdated string `bson:"last_updated"`
|
||||
ExecutionData string `bson:"execution_data"`
|
||||
Completed bool `bson:"completed"`
|
||||
}
|
||||
|
||||
type WeightagesInput struct {
|
||||
ExperimentName string `bson:"experiment_name"`
|
||||
Weightage int `bson:"weightage"`
|
||||
}
|
||||
|
||||
//init initializes database connection
|
||||
// init initializes database connection
|
||||
func init() {
|
||||
|
||||
var (
|
||||
|
|
@ -115,28 +54,4 @@ func init() {
|
|||
}
|
||||
|
||||
Database = client.Database(dbName)
|
||||
initAllCollections()
|
||||
|
||||
}
|
||||
|
||||
func initAllCollections() {
|
||||
clusterCollection = Database.Collection(collections["Cluster"])
|
||||
workflowCollection = Database.Collection(collections["Workflow"])
|
||||
_, err := workflowCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.M{
|
||||
"workflow_id": 1,
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.M{
|
||||
"workflow_name": 1,
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal("Error Creating Index for Workflow Collection: ", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
package operations
|
||||
package myhub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
dbSchema "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
)
|
||||
|
||||
var myhubCollection *mongo.Collection
|
||||
|
|
@ -17,8 +17,8 @@ func init() {
|
|||
myhubCollection = mongodb.Database.Collection("myhub")
|
||||
}
|
||||
|
||||
//CreateMyHub ...
|
||||
func CreateMyHub(ctx context.Context, myhub *dbSchema.MyHub) error {
|
||||
// CreateMyHub ...
|
||||
func CreateMyHub(ctx context.Context, myhub *MyHub) error {
|
||||
_, err := myhubCollection.InsertOne(ctx, myhub)
|
||||
if err != nil {
|
||||
log.Print("Error creating MyHub: ", err)
|
||||
|
|
@ -27,52 +27,53 @@ func CreateMyHub(ctx context.Context, myhub *dbSchema.MyHub) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//GetMyHubByProjectID ...
|
||||
func GetMyHubByProjectID(ctx context.Context, projectID string) ([]dbSchema.MyHub, error) {
|
||||
// GetMyHubByProjectID ...
|
||||
func GetMyHubByProjectID(ctx context.Context, projectID string) ([]MyHub, error) {
|
||||
query := bson.M{"project_id": projectID, "IsRemoved": false}
|
||||
cursor, err := myhubCollection.Find(ctx, query)
|
||||
if err != nil {
|
||||
log.Print("ERROR GETTING USERS : ", err)
|
||||
return []dbSchema.MyHub{}, err
|
||||
return []MyHub{}, err
|
||||
}
|
||||
var myhubs []dbSchema.MyHub
|
||||
var myhubs []MyHub
|
||||
err = cursor.All(ctx, &myhubs)
|
||||
if err != nil {
|
||||
log.Print("Error deserializing myhubs in myhub object : ", err)
|
||||
return []dbSchema.MyHub{}, err
|
||||
return []MyHub{}, err
|
||||
}
|
||||
return myhubs, nil
|
||||
}
|
||||
|
||||
//GetHubs ...
|
||||
func GetHubs(ctx context.Context) ([]dbSchema.MyHub, error) {
|
||||
// GetHubs ...
|
||||
func GetHubs(ctx context.Context) ([]MyHub, error) {
|
||||
// ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
query := bson.D{{}}
|
||||
cursor, err := myhubCollection.Find(ctx, query)
|
||||
if err != nil {
|
||||
log.Print("ERROR GETTING MYHUBS : ", err)
|
||||
return []dbSchema.MyHub{}, err
|
||||
return []MyHub{}, err
|
||||
}
|
||||
var MyHubs []dbSchema.MyHub
|
||||
var MyHubs []MyHub
|
||||
err = cursor.All(ctx, &MyHubs)
|
||||
if err != nil {
|
||||
log.Print("Error deserializing myhubs in the myhub object : ", err)
|
||||
return []dbSchema.MyHub{}, err
|
||||
return []MyHub{}, err
|
||||
}
|
||||
return MyHubs, nil
|
||||
}
|
||||
|
||||
//GetHubByID
|
||||
func GetHubByID(ctx context.Context, hubID string) (dbSchema.MyHub, error) {
|
||||
var myHub dbSchema.MyHub
|
||||
// GetHubByID ...
|
||||
func GetHubByID(ctx context.Context, hubID string) (MyHub, error) {
|
||||
var myHub MyHub
|
||||
err := myhubCollection.FindOne(ctx, bson.M{"myhub_id": hubID}).Decode(&myHub)
|
||||
if err != nil {
|
||||
return dbSchema.MyHub{}, err
|
||||
return MyHub{}, err
|
||||
}
|
||||
|
||||
return myHub, nil
|
||||
}
|
||||
|
||||
// UpdateMyHub ...
|
||||
func UpdateMyHub(ctx context.Context, query bson.D, update bson.D) error {
|
||||
updateResult, err := myhubCollection.UpdateOne(ctx, query, update)
|
||||
if err != nil {
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package schema
|
||||
package myhub
|
||||
|
||||
import "github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
|
||||
//MyHub ...
|
||||
// MyHub ...
|
||||
type MyHub struct {
|
||||
ID string `bson:"myhub_id"`
|
||||
ProjectID string `bson:"project_id"`
|
||||
|
|
@ -22,7 +22,7 @@ type MyHub struct {
|
|||
LastSyncedAt string `bson:"last_synced_at"`
|
||||
}
|
||||
|
||||
//GetOutputMyHub ...
|
||||
// GetOutputMyHub ...
|
||||
func (myhub *MyHub) GetOutputMyHub() *model.MyHub {
|
||||
|
||||
return &model.MyHub{
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
package operations
|
||||
package project
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
dbSchema "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
)
|
||||
|
||||
var projectCollection *mongo.Collection
|
||||
|
|
@ -20,7 +20,7 @@ func init() {
|
|||
}
|
||||
|
||||
// CreateProject ...
|
||||
func CreateProject(ctx context.Context, project *dbSchema.Project) error {
|
||||
func CreateProject(ctx context.Context, project *Project) error {
|
||||
// ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
_, err := projectCollection.InsertOne(ctx, project)
|
||||
if err != nil {
|
||||
|
|
@ -31,10 +31,10 @@ func CreateProject(ctx context.Context, project *dbSchema.Project) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//GetProject ...
|
||||
func GetProject(ctx context.Context, projectID string) (*dbSchema.Project, error) {
|
||||
// GetProject ...
|
||||
func GetProject(ctx context.Context, projectID string) (*Project, error) {
|
||||
// ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
var project *dbSchema.Project = new(dbSchema.Project)
|
||||
var project = new(Project)
|
||||
query := bson.M{"_id": projectID}
|
||||
err := projectCollection.FindOne(ctx, query).Decode(project)
|
||||
if err != nil {
|
||||
|
|
@ -45,11 +45,11 @@ func GetProject(ctx context.Context, projectID string) (*dbSchema.Project, error
|
|||
return project, err
|
||||
}
|
||||
|
||||
//GetProjectsByUserID ...
|
||||
func GetProjectsByUserID(ctx context.Context, userID string) ([]dbSchema.Project, error) {
|
||||
// GetProjectsByUserID ...
|
||||
func GetProjectsByUserID(ctx context.Context, userID string) ([]Project, error) {
|
||||
// ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
projects := []dbSchema.Project{}
|
||||
query := bson.M{"members": bson.M{"$elemMatch": bson.M{"user_id": userID, "invitation": bson.M{"$ne": dbSchema.DeclinedInvitation}}}}
|
||||
projects := []Project{}
|
||||
query := bson.M{"members": bson.M{"$elemMatch": bson.M{"user_id": userID, "invitation": bson.M{"$ne": DeclinedInvitation}}}}
|
||||
cursor, err := projectCollection.Find(ctx, query)
|
||||
if err != nil {
|
||||
log.Print("Error getting project with userID: ", userID, " error: ", err)
|
||||
|
|
@ -64,8 +64,8 @@ func GetProjectsByUserID(ctx context.Context, userID string) ([]dbSchema.Project
|
|||
return projects, err
|
||||
}
|
||||
|
||||
//AddMember ...
|
||||
func AddMember(ctx context.Context, projectID string, member *dbSchema.Member) error {
|
||||
// AddMember ...
|
||||
func AddMember(ctx context.Context, projectID string, member *Member) error {
|
||||
|
||||
query := bson.M{"_id": projectID}
|
||||
update := bson.M{"$push": bson.M{"members": member}}
|
||||
|
|
@ -77,13 +77,13 @@ func AddMember(ctx context.Context, projectID string, member *dbSchema.Member) e
|
|||
return nil
|
||||
}
|
||||
|
||||
//RemoveInvitation ...
|
||||
func RemoveInvitation(ctx context.Context, projectID string, userName string, invitation dbSchema.Invitation) error {
|
||||
// RemoveInvitation ...
|
||||
func RemoveInvitation(ctx context.Context, projectID string, userName string, invitation Invitation) error {
|
||||
query := bson.M{"_id": projectID}
|
||||
update := bson.M{"$pull": bson.M{"members": bson.M{"username": userName}}}
|
||||
_, err := projectCollection.UpdateOne(ctx, query, update)
|
||||
if err != nil {
|
||||
if invitation == dbSchema.AcceptedInvitation {
|
||||
if invitation == AcceptedInvitation {
|
||||
log.Print("Error Removing the member with username:", userName, "from project with project id: ", projectID, err)
|
||||
return err
|
||||
}
|
||||
|
|
@ -94,8 +94,8 @@ func RemoveInvitation(ctx context.Context, projectID string, userName string, in
|
|||
return nil
|
||||
}
|
||||
|
||||
//UpdateInvite ...
|
||||
func UpdateInvite(ctx context.Context, projectID, userName string, invitation dbSchema.Invitation, Role *model.MemberRole) error {
|
||||
// UpdateInvite ...
|
||||
func UpdateInvite(ctx context.Context, projectID, userName string, invitation Invitation, Role *model.MemberRole) error {
|
||||
options := options.Update().SetArrayFilters(options.ArrayFilters{
|
||||
Filters: []interface{}{
|
||||
bson.M{"elem.username": userName},
|
||||
|
|
@ -106,11 +106,11 @@ func UpdateInvite(ctx context.Context, projectID, userName string, invitation db
|
|||
var update bson.M
|
||||
|
||||
switch invitation {
|
||||
case dbSchema.PendingInvitation:
|
||||
case PendingInvitation:
|
||||
update = bson.M{"$set": bson.M{"members.$[elem].invitation": invitation, "members.$[elem].role": Role}}
|
||||
case dbSchema.DeclinedInvitation:
|
||||
case DeclinedInvitation:
|
||||
update = bson.M{"$set": bson.M{"members.$[elem].invitation": invitation}}
|
||||
case dbSchema.AcceptedInvitation:
|
||||
case AcceptedInvitation:
|
||||
update = bson.M{"$set": bson.M{"members.$[elem].invitation": invitation, "members.$[elem].joined_at": time.Now().Format(time.RFC1123Z)}}
|
||||
|
||||
}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package schema
|
||||
package project
|
||||
|
||||
import "github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
|
||||
//Project ...
|
||||
// Project ...
|
||||
type Project struct {
|
||||
ID string `bson:"_id"`
|
||||
Name string `bson:"name"`
|
||||
|
|
@ -13,7 +13,7 @@ type Project struct {
|
|||
RemovedAt string `bson:"removed_at"`
|
||||
}
|
||||
|
||||
//GetOutputProject ...
|
||||
// GetOutputProject ...
|
||||
func (project *Project) GetOutputProject() *model.Project {
|
||||
|
||||
return &model.Project{
|
||||
|
|
@ -27,7 +27,7 @@ func (project *Project) GetOutputProject() *model.Project {
|
|||
}
|
||||
}
|
||||
|
||||
//GetOutputMembers ...
|
||||
// GetOutputMembers ...
|
||||
func (project *Project) GetOutputMembers() []*model.Member {
|
||||
|
||||
outputMembers := []*model.Member{}
|
||||
|
|
@ -39,7 +39,7 @@ func (project *Project) GetOutputMembers() []*model.Member {
|
|||
return outputMembers
|
||||
}
|
||||
|
||||
//Member ...
|
||||
// Member ...
|
||||
type Member struct {
|
||||
UserID string `bson:"user_id"`
|
||||
UserName string `bson:"username"`
|
||||
|
|
@ -50,7 +50,7 @@ type Member struct {
|
|||
JoinedAt string `bson:"joined_at"`
|
||||
}
|
||||
|
||||
//GetOutputMember ...
|
||||
// GetOutputMember ...
|
||||
func (member *Member) GetOutputMember() *model.Member {
|
||||
|
||||
return &model.Member{
|
||||
|
|
@ -64,16 +64,16 @@ func (member *Member) GetOutputMember() *model.Member {
|
|||
}
|
||||
}
|
||||
|
||||
//Invitation ...
|
||||
// Invitation ...
|
||||
type Invitation string
|
||||
|
||||
const (
|
||||
//PendingInvitation ...
|
||||
// PendingInvitation ...
|
||||
PendingInvitation Invitation = "Pending"
|
||||
|
||||
//AcceptedInvitation ...
|
||||
// AcceptedInvitation ...
|
||||
AcceptedInvitation Invitation = "Accepted"
|
||||
|
||||
//DeclinedInvitation ...
|
||||
// DeclinedInvitation ...
|
||||
DeclinedInvitation Invitation = "Declined"
|
||||
)
|
||||
|
|
@ -1,24 +1,28 @@
|
|||
package operations
|
||||
package usermanagement
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
dbSchema "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
)
|
||||
|
||||
var userCollection *mongo.Collection
|
||||
var (
|
||||
userCollection *mongo.Collection
|
||||
projectCollection *mongo.Collection
|
||||
)
|
||||
|
||||
func init() {
|
||||
userCollection = mongodb.Database.Collection("user")
|
||||
projectCollection = mongodb.Database.Collection("project")
|
||||
}
|
||||
|
||||
// InsertUser ...
|
||||
func InsertUser(ctx context.Context, user *dbSchema.User) error {
|
||||
func InsertUser(ctx context.Context, user *User) error {
|
||||
// ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
_, err := userCollection.InsertOne(ctx, user)
|
||||
if err != nil {
|
||||
|
|
@ -29,10 +33,10 @@ func InsertUser(ctx context.Context, user *dbSchema.User) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//GetUserByUserName ...
|
||||
func GetUserByUserName(ctx context.Context, username string) (*dbSchema.User, error) {
|
||||
// GetUserByUserName ...
|
||||
func GetUserByUserName(ctx context.Context, username string) (*User, error) {
|
||||
// ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
var user *dbSchema.User = new(dbSchema.User)
|
||||
var user = new(User)
|
||||
query := bson.M{"username": username}
|
||||
err := userCollection.FindOne(ctx, query).Decode(user)
|
||||
if err != nil {
|
||||
|
|
@ -43,26 +47,26 @@ func GetUserByUserName(ctx context.Context, username string) (*dbSchema.User, er
|
|||
return user, err
|
||||
}
|
||||
|
||||
//GetUsers ...
|
||||
func GetUsers(ctx context.Context) ([]dbSchema.User, error) {
|
||||
// GetUsers ...
|
||||
func GetUsers(ctx context.Context) ([]User, error) {
|
||||
// ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
query := bson.D{{}}
|
||||
cursor, err := userCollection.Find(ctx, query)
|
||||
if err != nil {
|
||||
log.Print("ERROR GETTING USERS : ", err)
|
||||
return []dbSchema.User{}, err
|
||||
return []User{}, err
|
||||
}
|
||||
var users []dbSchema.User
|
||||
var users []User
|
||||
err = cursor.All(ctx, &users)
|
||||
if err != nil {
|
||||
log.Print("Error deserializing users in the user object : ", err)
|
||||
return []dbSchema.User{}, err
|
||||
return []User{}, err
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
//UpdateUser ...
|
||||
func UpdateUser(ctx context.Context, user *dbSchema.User) error {
|
||||
// UpdateUser ...
|
||||
func UpdateUser(ctx context.Context, user *User) error {
|
||||
|
||||
filter := bson.M{"_id": user.ID}
|
||||
update := bson.M{"$set": bson.M{"name": user.Name, "email": user.Email, "company_name": user.CompanyName, "updated_at": user.UpdatedAt}}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package schema
|
||||
package usermanagement
|
||||
|
||||
import "github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
|
||||
//User ...
|
||||
// User ...
|
||||
type User struct {
|
||||
ID string `bson:"_id"`
|
||||
Username string `bson:"username"`
|
||||
|
|
@ -17,7 +17,7 @@ type User struct {
|
|||
RemovedAt string `bson:"removed_at"`
|
||||
}
|
||||
|
||||
//GetOutputUser ...
|
||||
// GetOutputUser ...
|
||||
func (user User) GetOutputUser() *model.User {
|
||||
|
||||
return &model.User{
|
||||
|
|
@ -1,59 +1,56 @@
|
|||
package mongodb
|
||||
package workflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
)
|
||||
|
||||
func InsertCluster(cluster Cluster) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
_, err := clusterCollection.InsertOne(ctx, cluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var (
|
||||
workflowCollection *mongo.Collection
|
||||
backgroundContext = context.Background()
|
||||
)
|
||||
|
||||
return nil
|
||||
func init() {
|
||||
workflowCollection = mongodb.Database.Collection("workflow-collection")
|
||||
_, err := workflowCollection.Indexes().CreateMany(backgroundContext, []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.M{
|
||||
"workflow_id": 1,
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.M{
|
||||
"workflow_name": 1,
|
||||
},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal("Error Creating Index for Workflow Collection: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetCluster(cluster_id string) (Cluster, error) {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
query := bson.M{"cluster_id": cluster_id}
|
||||
|
||||
var cluster Cluster
|
||||
err = clusterCollection.FindOne(ctx, query).Decode(&cluster)
|
||||
if err != nil {
|
||||
return Cluster{}, err
|
||||
}
|
||||
|
||||
return cluster, nil
|
||||
}
|
||||
|
||||
func UpdateCluster(query bson.D, update bson.D) error {
|
||||
// UpdateWorkflowRun takes workflowID and wfRun parameters to update the workflow run details in the database
|
||||
func UpdateWorkflowRun(workflowID string, wfRun ChaosWorkflowRun) (int, error) {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
_, err := clusterCollection.UpdateOne(ctx, query, update)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateWorkflowRun(workflow_id string, wfRun WorkflowRun) (int, error) {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
count, err := workflowCollection.CountDocuments(ctx, bson.M{"workflow_id": workflow_id, "workflow_runs.workflow_run_id": wfRun.WorkflowRunID})
|
||||
count, err := workflowCollection.CountDocuments(ctx, bson.M{"workflow_id": workflowID, "workflow_runs.workflow_run_id": wfRun.WorkflowRunID})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
updateCount := 1
|
||||
if count == 0 {
|
||||
filter := bson.M{"workflow_id": workflow_id}
|
||||
filter := bson.M{"workflow_id": workflowID}
|
||||
update := bson.M{"$push": bson.M{"workflow_runs": wfRun}}
|
||||
updateResp, err := workflowCollection.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
|
|
@ -63,7 +60,7 @@ func UpdateWorkflowRun(workflow_id string, wfRun WorkflowRun) (int, error) {
|
|||
return 0, errors.New("workflow not found")
|
||||
}
|
||||
} else if count == 1 {
|
||||
filter := bson.M{"workflow_id": workflow_id, "workflow_runs.workflow_run_id": wfRun.WorkflowRunID, "workflow_runs.completed": false}
|
||||
filter := bson.M{"workflow_id": workflowID, "workflow_runs.workflow_run_id": wfRun.WorkflowRunID, "workflow_runs.completed": false}
|
||||
update := bson.M{"$set": bson.M{"workflow_runs.$.last_updated": wfRun.LastUpdated, "workflow_runs.$.execution_data": wfRun.ExecutionData, "workflow_runs.$.completed": wfRun.Completed}}
|
||||
updateResp, err := workflowCollection.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
|
|
@ -75,6 +72,7 @@ func UpdateWorkflowRun(workflow_id string, wfRun WorkflowRun) (int, error) {
|
|||
return updateCount, nil
|
||||
}
|
||||
|
||||
// GetWorkflows takes a query parameter to retrieve the workflow details from the database
|
||||
func GetWorkflows(query bson.D) ([]ChaosWorkFlowInput, error) {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
|
|
@ -92,8 +90,9 @@ func GetWorkflows(query bson.D) ([]ChaosWorkFlowInput, error) {
|
|||
return workflows, nil
|
||||
}
|
||||
|
||||
func GetWorkflowsByClusterID(cluster_id string) ([]ChaosWorkFlowInput, error) {
|
||||
query := bson.D{{"cluster_id", cluster_id}}
|
||||
// GetWorkflowsByClusterID takes a clusterID parameter to retrieve the workflow details from the database
|
||||
func GetWorkflowsByClusterID(clusterID string) ([]ChaosWorkFlowInput, error) {
|
||||
query := bson.D{{"cluster_id", clusterID}}
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
cursor, err := workflowCollection.Find(ctx, query)
|
||||
|
|
@ -109,32 +108,7 @@ func GetWorkflowsByClusterID(cluster_id string) ([]ChaosWorkFlowInput, error) {
|
|||
return workflows, nil
|
||||
}
|
||||
|
||||
func GetClusterWithProjectID(project_id string, cluster_type *string) ([]*Cluster, error) {
|
||||
|
||||
var query bson.M
|
||||
if cluster_type == nil {
|
||||
query = bson.M{"project_id": project_id, "is_removed": false}
|
||||
} else {
|
||||
query = bson.M{"project_id": project_id, "cluster_type": cluster_type, "is_removed": false}
|
||||
}
|
||||
|
||||
fmt.Print(query)
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
var clusters []*Cluster
|
||||
|
||||
cursor, err := clusterCollection.Find(ctx, query)
|
||||
if err != nil {
|
||||
return []*Cluster{}, err
|
||||
}
|
||||
|
||||
err = cursor.All(ctx, &clusters)
|
||||
if err != nil {
|
||||
return []*Cluster{}, err
|
||||
}
|
||||
|
||||
return clusters, nil
|
||||
}
|
||||
|
||||
// InsertChaosWorkflow takes details of a workflow and inserts into the database collection
|
||||
func InsertChaosWorkflow(chaosWorkflow ChaosWorkFlowInput) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
_, err := workflowCollection.InsertOne(ctx, chaosWorkflow)
|
||||
|
|
@ -145,6 +119,7 @@ func InsertChaosWorkflow(chaosWorkflow ChaosWorkFlowInput) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// UpdateChaosWorkflow takes query and update parameters to update the workflow details in the database
|
||||
func UpdateChaosWorkflow(query bson.D, update bson.D) error {
|
||||
ctx, _ := context.WithTimeout(backgroundContext, 10*time.Second)
|
||||
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package workflow
|
||||
|
||||
// ChaosWorkFlowInput contains the required fields to be stored in the database for a chaos workflow input
|
||||
type ChaosWorkFlowInput struct {
|
||||
WorkflowID string `bson:"workflow_id"`
|
||||
WorkflowManifest string `bson:"workflow_manifest"`
|
||||
CronSyntax string `bson:"cronSyntax"`
|
||||
WorkflowName string `bson:"workflow_name"`
|
||||
WorkflowDescription string `bson:"workflow_description"`
|
||||
Weightages []*WeightagesInput `bson:"weightages"`
|
||||
IsCustomWorkflow bool `bson:"isCustomWorkflow"`
|
||||
UpdatedAt string `bson:"updated_at"`
|
||||
CreatedAt string `bson:"created_at"`
|
||||
ProjectID string `bson:"project_id"`
|
||||
ClusterID string `bson:"cluster_id"`
|
||||
WorkflowRuns []*ChaosWorkflowRun `bson:"workflow_runs"`
|
||||
IsRemoved bool `bson:"isRemoved"`
|
||||
}
|
||||
|
||||
// ChaosWorkflowRun contains the required fields to be stored in the database for a workflow run
|
||||
type ChaosWorkflowRun struct {
|
||||
WorkflowRunID string `bson:"workflow_run_id"`
|
||||
LastUpdated string `bson:"last_updated"`
|
||||
ExecutionData string `bson:"execution_data"`
|
||||
Completed bool `bson:"completed"`
|
||||
}
|
||||
|
||||
// WeightagesInput contains the required fields to be stored in the database for a weightages input
|
||||
type WeightagesInput struct {
|
||||
ExperimentName string `bson:"experiment_name"`
|
||||
Weightage int `bson:"weightage"`
|
||||
}
|
||||
|
|
@ -1,15 +1,17 @@
|
|||
package file_handlers
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/k8s"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/types"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/utils"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"
|
||||
dbOperationsCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/k8s"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/types"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/utils"
|
||||
)
|
||||
|
||||
var subscriberConfiguration = &types.SubscriberConfigurationVars{
|
||||
|
|
@ -24,7 +26,7 @@ var subscriberConfiguration = &types.SubscriberConfigurationVars{
|
|||
ChaosRunnerImage: os.Getenv("LITMUS_CHAOS_RUNNER_IMAGE"),
|
||||
}
|
||||
|
||||
//FileHandler dynamically generates the manifest file and sends it as a response
|
||||
// FileHandler dynamically generates the manifest file and sends it as a response
|
||||
func FileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
vars = mux.Vars(r)
|
||||
|
|
@ -51,7 +53,7 @@ func GetManifest(token string) ([]byte, int, error) {
|
|||
return nil, 404, err
|
||||
}
|
||||
|
||||
reqCluster, err := database.GetCluster(id)
|
||||
reqCluster, err := dbOperationsCluster.GetCluster(id)
|
||||
if err != nil {
|
||||
return nil, 500, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,13 +7,13 @@ import (
|
|||
"sync"
|
||||
)
|
||||
|
||||
//GitMutexLock structure for the Git MutexLock
|
||||
// GitMutexLock structure for the Git MutexLock
|
||||
type GitMutexLock struct {
|
||||
mapMutex sync.Mutex
|
||||
gitMutex map[string]*sync.Mutex
|
||||
}
|
||||
|
||||
//Lock acquires a lock on particular project or repo for access
|
||||
// Lock acquires a lock on particular project or repo for access
|
||||
func (g *GitMutexLock) Lock(repo string, branch *string) {
|
||||
key := getKey(repo, branch)
|
||||
|
||||
|
|
@ -28,7 +28,7 @@ func (g *GitMutexLock) Lock(repo string, branch *string) {
|
|||
log.Print("Acquired LOCK : ", key)
|
||||
}
|
||||
|
||||
//Unlock releases the lock on particular project or repo
|
||||
// Unlock releases the lock on particular project or repo
|
||||
func (g *GitMutexLock) Unlock(repo string, branch *string) {
|
||||
key := getKey(repo, branch)
|
||||
g.mapMutex.Lock()
|
||||
|
|
@ -41,7 +41,7 @@ func (g *GitMutexLock) Unlock(repo string, branch *string) {
|
|||
log.Print("Release LOCK : ", key)
|
||||
}
|
||||
|
||||
//NewGitLock returns a instance of GitMutexLock
|
||||
// NewGitLock returns a instance of GitMutexLock
|
||||
func NewGitLock() GitMutexLock {
|
||||
return GitMutexLock{
|
||||
mapMutex: sync.Mutex{},
|
||||
|
|
|
|||
|
|
@ -20,20 +20,21 @@ import (
|
|||
"github.com/go-git/go-git/v5/plumbing/transport"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/authorization"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaos-workflow/ops"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/operations"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/tidwall/gjson"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
ssh2 "golang.org/x/crypto/ssh"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/authorization"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaos-workflow/ops"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
dbOperationsGitOps "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/gitops"
|
||||
dbSchemaGitOps "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/gitops"
|
||||
dbOperationsWorkflow "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/workflow"
|
||||
)
|
||||
|
||||
//GitConfig structure for the GitOps settings
|
||||
// GitConfig structure for the GitOps settings
|
||||
type GitConfig struct {
|
||||
ProjectID string
|
||||
RepositoryURL string
|
||||
|
|
@ -82,8 +83,8 @@ func GitUserFromContext(ctx context.Context) GitUser {
|
|||
}
|
||||
}
|
||||
|
||||
//GetGitOpsConfig is used for constructing the GitConfig from schema.GitConfigDB
|
||||
func GetGitOpsConfig(repoData schema.GitConfigDB) GitConfig {
|
||||
// GetGitOpsConfig is used for constructing the GitConfig from dbSchemaGitOps.GitConfigDB
|
||||
func GetGitOpsConfig(repoData dbSchemaGitOps.GitConfigDB) GitConfig {
|
||||
gitConfig := GitConfig{
|
||||
ProjectID: repoData.ProjectID,
|
||||
RepositoryURL: repoData.RepositoryURL,
|
||||
|
|
@ -101,7 +102,7 @@ func GetGitOpsConfig(repoData schema.GitConfigDB) GitConfig {
|
|||
return gitConfig
|
||||
}
|
||||
|
||||
//setupGitRepo helps clones and sets up the repo for gitops
|
||||
// setupGitRepo helps clones and sets up the repo for gitops
|
||||
func (c GitConfig) setupGitRepo(user GitUser) error {
|
||||
projectPath := c.LocalPath + "/" + ProjectDataPath + "/" + c.ProjectID
|
||||
|
||||
|
|
@ -142,7 +143,7 @@ func (c GitConfig) setupGitRepo(user GitUser) error {
|
|||
return c.GitPush()
|
||||
}
|
||||
|
||||
//GitClone clones the repo
|
||||
// GitClone clones the repo
|
||||
func (c GitConfig) GitClone() (*git.Repository, error) {
|
||||
// clean the local path
|
||||
os.RemoveAll(c.LocalPath)
|
||||
|
|
@ -161,7 +162,7 @@ func (c GitConfig) GitClone() (*git.Repository, error) {
|
|||
})
|
||||
}
|
||||
|
||||
//getAuthMethod returns the AuthMethod instance required for the current repo access [read/writes]
|
||||
// getAuthMethod returns the AuthMethod instance required for the current repo access [read/writes]
|
||||
func (c GitConfig) getAuthMethod() (transport.AuthMethod, error) {
|
||||
|
||||
switch c.AuthType {
|
||||
|
|
@ -194,7 +195,7 @@ func (c GitConfig) getAuthMethod() (transport.AuthMethod, error) {
|
|||
}
|
||||
}
|
||||
|
||||
//UnsafeGitPull executes git pull after a hard reset when uncommited changes are present in repo. Not safe.
|
||||
// UnsafeGitPull executes git pull after a hard reset when uncommited changes are present in repo. Not safe.
|
||||
func (c GitConfig) UnsafeGitPull() error {
|
||||
cleanStatus, err := c.GitGetStatus()
|
||||
if err != nil {
|
||||
|
|
@ -223,7 +224,7 @@ func (c GitConfig) GitGetStatus() (bool, error) {
|
|||
return status.IsClean(), nil
|
||||
}
|
||||
|
||||
//getRepositoryWorktreeReference returns the git.Repository and git.Worktree instanes for the repo
|
||||
// getRepositoryWorktreeReference returns the git.Repository and git.Worktree instanes for the repo
|
||||
func (c GitConfig) getRepositoryWorktreeReference() (*git.Repository, *git.Worktree, error) {
|
||||
repo, err := git.PlainOpen(c.LocalPath)
|
||||
if err != nil {
|
||||
|
|
@ -236,7 +237,7 @@ func (c GitConfig) getRepositoryWorktreeReference() (*git.Repository, *git.Workt
|
|||
return repo, workTree, nil
|
||||
}
|
||||
|
||||
//handlerForDirtyStatus calls relative functions if the GitGetStatus gives a clean status as a result
|
||||
// handlerForDirtyStatus calls relative functions if the GitGetStatus gives a clean status as a result
|
||||
func (c GitConfig) handlerForDirtyStatus() error {
|
||||
if err := c.GitHardReset(); err != nil {
|
||||
return err
|
||||
|
|
@ -244,7 +245,7 @@ func (c GitConfig) handlerForDirtyStatus() error {
|
|||
return c.GitPull()
|
||||
}
|
||||
|
||||
//GitHardReset executes "git reset --hard HEAD" in provided Repository Path
|
||||
// GitHardReset executes "git reset --hard HEAD" in provided Repository Path
|
||||
func (c GitConfig) GitHardReset() error {
|
||||
_, workTree, err := c.getRepositoryWorktreeReference()
|
||||
if err != nil {
|
||||
|
|
@ -256,7 +257,7 @@ func (c GitConfig) GitHardReset() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//GitPull updates the repository in provided Path
|
||||
// GitPull updates the repository in provided Path
|
||||
func (c GitConfig) GitPull() error {
|
||||
_, workTree, err := c.getRepositoryWorktreeReference()
|
||||
if err != nil {
|
||||
|
|
@ -278,7 +279,7 @@ func (c GitConfig) GitPull() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//GitCheckout changes the current active branch to specified branch in GitConfig
|
||||
// GitCheckout changes the current active branch to specified branch in GitConfig
|
||||
func (c GitConfig) GitCheckout() error {
|
||||
r, w, err := c.getRepositoryWorktreeReference()
|
||||
if err != nil {
|
||||
|
|
@ -296,7 +297,7 @@ func (c GitConfig) GitCheckout() error {
|
|||
})
|
||||
}
|
||||
|
||||
//GitPush pushes the current changes to remote set in GitConfig, always needs auth credentials
|
||||
// GitPush pushes the current changes to remote set in GitConfig, always needs auth credentials
|
||||
func (c GitConfig) GitPush() error {
|
||||
if c.AuthType == model.AuthTypeNone {
|
||||
return errors.New("cannot write/push without credentials, auth type = none")
|
||||
|
|
@ -321,7 +322,7 @@ func (c GitConfig) GitPush() error {
|
|||
return err
|
||||
}
|
||||
|
||||
//GitCommit saves the changes in the repo and commits them with the message provided
|
||||
// GitCommit saves the changes in the repo and commits them with the message provided
|
||||
func (c GitConfig) GitCommit(user GitUser, message string, deleteFile *string) (string, error) {
|
||||
_, w, err := c.getRepositoryWorktreeReference()
|
||||
if err != nil {
|
||||
|
|
@ -352,7 +353,7 @@ func (c GitConfig) GitCommit(user GitUser, message string, deleteFile *string) (
|
|||
return hash.String(), nil
|
||||
}
|
||||
|
||||
//GetChanges returns the LatestCommit and list of files changed(since previous LatestCommit) in the project directory mentioned in GitConfig
|
||||
// GetChanges returns the LatestCommit and list of files changed(since previous LatestCommit) in the project directory mentioned in GitConfig
|
||||
func (c GitConfig) GetChanges() (string, map[string]int, error) {
|
||||
path := ProjectDataPath + "/" + c.ProjectID + "/"
|
||||
|
||||
|
|
@ -414,7 +415,7 @@ func (c GitConfig) GetChanges() (string, map[string]int, error) {
|
|||
return c.LatestCommit, visited, nil
|
||||
}
|
||||
|
||||
//GetLatestCommitHash returns the latest commit hash in the local repo for the project directory
|
||||
// GetLatestCommitHash returns the latest commit hash in the local repo for the project directory
|
||||
func (c GitConfig) GetLatestCommitHash() (string, error) {
|
||||
path := ProjectDataPath + "/" + c.ProjectID + "/"
|
||||
r, _, err := c.getRepositoryWorktreeReference()
|
||||
|
|
@ -439,8 +440,8 @@ func (c GitConfig) GetLatestCommitHash() (string, error) {
|
|||
return commit.Hash.String(), nil
|
||||
}
|
||||
|
||||
//SetupGitOps clones and sets up the repo for gitops and returns the LatestCommit
|
||||
func SetupGitOps(user GitUser, config schema.GitConfigDB) (string, error) {
|
||||
// SetupGitOps clones and sets up the repo for gitops and returns the LatestCommit
|
||||
func SetupGitOps(user GitUser, config dbSchemaGitOps.GitConfigDB) (string, error) {
|
||||
gitConfig := GetGitOpsConfig(config)
|
||||
err := gitConfig.setupGitRepo(user)
|
||||
if err != nil {
|
||||
|
|
@ -453,7 +454,7 @@ func SetupGitOps(user GitUser, config schema.GitConfigDB) (string, error) {
|
|||
return commitHash, err
|
||||
}
|
||||
|
||||
//SyncDBToGit syncs the DB with the GitRepo for the project
|
||||
// SyncDBToGit syncs the DB with the GitRepo for the project
|
||||
func SyncDBToGit(ctx context.Context, config GitConfig) error {
|
||||
repositoryExists, err := PathExists(config.LocalPath)
|
||||
if err != nil {
|
||||
|
|
@ -480,7 +481,7 @@ func SyncDBToGit(ctx context.Context, config GitConfig) error {
|
|||
if !strings.HasSuffix(file, ".yaml") {
|
||||
continue
|
||||
}
|
||||
//check if file was deleted or not
|
||||
// check if file was deleted or not
|
||||
exists, err := PathExists(config.LocalPath + "/" + file)
|
||||
if err != nil {
|
||||
return errors.New("Error checking file in local repo : " + file + " | " + err.Error())
|
||||
|
|
@ -493,7 +494,7 @@ func SyncDBToGit(ctx context.Context, config GitConfig) error {
|
|||
}
|
||||
continue
|
||||
}
|
||||
//read changes [new additions/updates]
|
||||
// read changes [new additions/updates]
|
||||
data, err := ioutil.ReadFile(config.LocalPath + "/" + file)
|
||||
if err != nil {
|
||||
log.Print("Error reading data from git file : " + file + " | " + err.Error())
|
||||
|
|
@ -548,9 +549,9 @@ func SyncDBToGit(ctx context.Context, config GitConfig) error {
|
|||
if ctx == nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
err = operations.UpdateGitConfig(ctx, query, update)
|
||||
err = dbOperationsGitOps.UpdateGitConfig(ctx, query, update)
|
||||
} else {
|
||||
err = operations.UpdateGitConfig(ctx, query, update)
|
||||
err = dbOperationsGitOps.UpdateGitConfig(ctx, query, update)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -559,7 +560,7 @@ func SyncDBToGit(ctx context.Context, config GitConfig) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//createWorkflow helps in creating a new workflow during the SyncDBToGit operation
|
||||
// createWorkflow helps in creating a new workflow during the SyncDBToGit operation
|
||||
func createWorkflow(data, file string, config GitConfig) (bool, error) {
|
||||
_, fileName := filepath.Split(file)
|
||||
fileName = strings.Replace(fileName, ".yaml", "", -1)
|
||||
|
|
@ -607,7 +608,7 @@ func createWorkflow(data, file string, config GitConfig) (bool, error) {
|
|||
return true, nil
|
||||
}
|
||||
|
||||
//updateWorkflow helps in updating a existing workflow during the SyncDBToGit operation
|
||||
// updateWorkflow helps in updating a existing workflow during the SyncDBToGit operation
|
||||
func updateWorkflow(data, wfID, file string, config GitConfig) error {
|
||||
_, fileName := filepath.Split(file)
|
||||
fileName = strings.Replace(fileName, ".yaml", "", -1)
|
||||
|
|
@ -623,7 +624,7 @@ func updateWorkflow(data, wfID, file string, config GitConfig) error {
|
|||
return errors.New("file name doesn't match workflow name")
|
||||
}
|
||||
|
||||
workflow, err := mongodb.GetWorkflows(bson.D{{"workflow_id", wfID}, {"project_id", config.ProjectID}, {"isRemoved", false}})
|
||||
workflow, err := dbOperationsWorkflow.GetWorkflows(bson.D{{"workflow_id", wfID}, {"project_id", config.ProjectID}, {"isRemoved", false}})
|
||||
if len(workflow) == 0 {
|
||||
return errors.New("No such workflow found : " + wfID)
|
||||
}
|
||||
|
|
@ -653,7 +654,7 @@ func updateWorkflow(data, wfID, file string, config GitConfig) error {
|
|||
|
||||
}
|
||||
|
||||
//deleteWorkflow helps in deleting a workflow from DB during the SyncDBToGit operation
|
||||
// deleteWorkflow helps in deleting a workflow from DB during the SyncDBToGit operation
|
||||
func deleteWorkflow(file string, config GitConfig) error {
|
||||
_, fileName := filepath.Split(file)
|
||||
fileName = strings.Replace(fileName, ".yaml", "", -1)
|
||||
|
|
|
|||
|
|
@ -4,12 +4,6 @@ package handler
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaos-workflow/ops"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
|
|
@ -17,13 +11,23 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaos-workflow/ops"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
dbSchemaGitOps "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/gitops"
|
||||
dbOperationsProject "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/project"
|
||||
dbOperationsWorkflow "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/workflow"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/operations"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/gitops"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
dbOperationsGitOps "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/gitops"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/gitops"
|
||||
)
|
||||
|
||||
const timeout = time.Second * 5
|
||||
|
|
@ -33,8 +37,8 @@ var (
|
|||
backgroundContext = context.Background()
|
||||
)
|
||||
|
||||
//TODO add projectID and user permission validation
|
||||
//EnableGitOpsHandler enables gitops for a particular project
|
||||
// TODO add projectID and user permission validation
|
||||
// EnableGitOpsHandler enables gitops for a particular project
|
||||
func EnableGitOpsHandler(ctx context.Context, config model.GitConfig) (bool, error) {
|
||||
gitLock.Lock(config.ProjectID, nil)
|
||||
defer gitLock.Unlock(config.ProjectID, nil)
|
||||
|
|
@ -42,13 +46,13 @@ func EnableGitOpsHandler(ctx context.Context, config model.GitConfig) (bool, err
|
|||
gitLock.Lock(config.RepoURL, &config.Branch)
|
||||
defer gitLock.Unlock(config.RepoURL, &config.Branch)
|
||||
|
||||
_, err := operations.GetProject(ctx, config.ProjectID)
|
||||
_, err := dbOperationsProject.GetProject(ctx, config.ProjectID)
|
||||
if err != nil {
|
||||
return false, errors.New("Failed to setup GitOps : " + err.Error())
|
||||
}
|
||||
|
||||
log.Print("Enabling Gitops")
|
||||
gitDB := schema.GetGitConfigDB(config)
|
||||
gitDB := dbSchemaGitOps.GetGitConfigDB(config)
|
||||
|
||||
commit, err := gitops.SetupGitOps(gitops.GitUserFromContext(ctx), gitDB)
|
||||
if err != nil {
|
||||
|
|
@ -56,7 +60,7 @@ func EnableGitOpsHandler(ctx context.Context, config model.GitConfig) (bool, err
|
|||
}
|
||||
gitDB.LatestCommit = commit
|
||||
|
||||
err = operations.AddGitConfig(ctx, &gitDB)
|
||||
err = dbOperationsGitOps.AddGitConfig(ctx, &gitDB)
|
||||
if err != nil {
|
||||
return false, errors.New("Failed to enable GitOps in DB : " + err.Error())
|
||||
}
|
||||
|
|
@ -64,13 +68,13 @@ func EnableGitOpsHandler(ctx context.Context, config model.GitConfig) (bool, err
|
|||
return true, nil
|
||||
}
|
||||
|
||||
//DisableGitOpsHandler disables gitops for a specific project
|
||||
// DisableGitOpsHandler disables gitops for a specific project
|
||||
func DisableGitOpsHandler(ctx context.Context, projectID string) (bool, error) {
|
||||
gitLock.Lock(projectID, nil)
|
||||
defer gitLock.Unlock(projectID, nil)
|
||||
|
||||
log.Print("Disabling Gitops")
|
||||
err := operations.DeleteGitConfig(ctx, projectID)
|
||||
err := dbOperationsGitOps.DeleteGitConfig(ctx, projectID)
|
||||
if err != nil {
|
||||
return false, errors.New("Failed to delete git config from DB : " + err.Error())
|
||||
}
|
||||
|
|
@ -87,7 +91,7 @@ func DisableGitOpsHandler(ctx context.Context, projectID string) (bool, error) {
|
|||
func GetGitOpsDetailsHandler(ctx context.Context, projectID string) (*model.GitConfigResponse, error) {
|
||||
gitLock.Lock(projectID, nil)
|
||||
defer gitLock.Unlock(projectID, nil)
|
||||
config, err := operations.GetGitConfig(ctx, projectID)
|
||||
config, err := dbOperationsGitOps.GetGitConfig(ctx, projectID)
|
||||
if err != nil {
|
||||
return nil, errors.New("Cannot get Git Config from DB : " + err.Error())
|
||||
}
|
||||
|
|
@ -119,7 +123,7 @@ func GetGitOpsDetailsHandler(ctx context.Context, projectID string) (*model.GitC
|
|||
return &resp, nil
|
||||
}
|
||||
|
||||
//GitOpsNotificationHandler sends workflow run request(single run workflow only) to agent on gitops notification
|
||||
// GitOpsNotificationHandler sends workflow run request(single run workflow only) to agent on gitops notification
|
||||
func GitOpsNotificationHandler(ctx context.Context, clusterInfo model.ClusterIdentity, workflowID string) (string, error) {
|
||||
cInfo, err := cluster.VerifyCluster(clusterInfo)
|
||||
if err != nil {
|
||||
|
|
@ -128,7 +132,7 @@ func GitOpsNotificationHandler(ctx context.Context, clusterInfo model.ClusterIde
|
|||
}
|
||||
gitLock.Lock(cInfo.ProjectID, nil)
|
||||
defer gitLock.Unlock(cInfo.ProjectID, nil)
|
||||
config, err := operations.GetGitConfig(ctx, cInfo.ProjectID)
|
||||
config, err := dbOperationsGitOps.GetGitConfig(ctx, cInfo.ProjectID)
|
||||
if err != nil {
|
||||
return "", errors.New("Cannot get Git Config from DB : " + err.Error())
|
||||
}
|
||||
|
|
@ -136,7 +140,7 @@ func GitOpsNotificationHandler(ctx context.Context, clusterInfo model.ClusterIde
|
|||
return "Gitops Disabled", nil
|
||||
}
|
||||
query := bson.D{{"cluster_id", clusterInfo.ClusterID}, {"workflow_id", workflowID}, {"isRemoved", false}}
|
||||
workflows, err := mongodb.GetWorkflows(query)
|
||||
workflows, err := dbOperationsWorkflow.GetWorkflows(query)
|
||||
if err != nil {
|
||||
log.Print("Could not get workflow :", err)
|
||||
return "could not get workflow", err
|
||||
|
|
@ -164,11 +168,11 @@ func GitOpsNotificationHandler(ctx context.Context, clusterInfo model.ClusterIde
|
|||
return "Request Acknowledged for workflowID: " + workflowID, nil
|
||||
}
|
||||
|
||||
//UpsertWorkflowToGit adds/updates workflow to git
|
||||
// UpsertWorkflowToGit adds/updates workflow to git
|
||||
func UpsertWorkflowToGit(ctx context.Context, workflow *model.ChaosWorkFlowInput) error {
|
||||
gitLock.Lock(workflow.ProjectID, nil)
|
||||
defer gitLock.Unlock(workflow.ProjectID, nil)
|
||||
config, err := operations.GetGitConfig(ctx, workflow.ProjectID)
|
||||
config, err := dbOperationsGitOps.GetGitConfig(ctx, workflow.ProjectID)
|
||||
if err != nil {
|
||||
return errors.New("Cannot get Git Config from DB : " + err.Error())
|
||||
}
|
||||
|
|
@ -209,7 +213,7 @@ func UpsertWorkflowToGit(ctx context.Context, workflow *model.ChaosWorkFlowInput
|
|||
|
||||
query := bson.D{{"project_id", gitConfig.ProjectID}}
|
||||
update := bson.D{{"$set", bson.D{{"latest_commit", commit}}}}
|
||||
err = operations.UpdateGitConfig(ctx, query, update)
|
||||
err = dbOperationsGitOps.UpdateGitConfig(ctx, query, update)
|
||||
if err != nil {
|
||||
return errors.New("Failed to update git config : " + err.Error())
|
||||
}
|
||||
|
|
@ -217,13 +221,13 @@ func UpsertWorkflowToGit(ctx context.Context, workflow *model.ChaosWorkFlowInput
|
|||
return nil
|
||||
}
|
||||
|
||||
//DeleteWorkflowFromGit deletes workflow from git
|
||||
// DeleteWorkflowFromGit deletes workflow from git
|
||||
func DeleteWorkflowFromGit(ctx context.Context, workflow *model.ChaosWorkFlowInput) error {
|
||||
log.Print("Deleting Workflow...")
|
||||
gitLock.Lock(workflow.ProjectID, nil)
|
||||
defer gitLock.Unlock(workflow.ProjectID, nil)
|
||||
|
||||
config, err := operations.GetGitConfig(ctx, workflow.ProjectID)
|
||||
config, err := dbOperationsGitOps.GetGitConfig(ctx, workflow.ProjectID)
|
||||
if err != nil {
|
||||
return errors.New("Cannot get Git Config from DB : " + err.Error())
|
||||
}
|
||||
|
|
@ -268,7 +272,7 @@ func DeleteWorkflowFromGit(ctx context.Context, workflow *model.ChaosWorkFlowInp
|
|||
|
||||
query := bson.D{{"project_id", gitConfig.ProjectID}}
|
||||
update := bson.D{{"$set", bson.D{{"latest_commit", commit}}}}
|
||||
err = operations.UpdateGitConfig(ctx, query, update)
|
||||
err = dbOperationsGitOps.UpdateGitConfig(ctx, query, update)
|
||||
if err != nil {
|
||||
return errors.New("Failed to update git config : " + err.Error())
|
||||
}
|
||||
|
|
@ -276,8 +280,8 @@ func DeleteWorkflowFromGit(ctx context.Context, workflow *model.ChaosWorkFlowInp
|
|||
return nil
|
||||
}
|
||||
|
||||
//GitSyncHelper sync a particular repo with DB
|
||||
func GitSyncHelper(config schema.GitConfigDB, wg *sync.WaitGroup) {
|
||||
// GitSyncHelper sync a particular repo with DB
|
||||
func GitSyncHelper(config dbSchemaGitOps.GitConfigDB, wg *sync.WaitGroup) {
|
||||
if wg != nil {
|
||||
defer wg.Done()
|
||||
}
|
||||
|
|
@ -291,7 +295,7 @@ func GitSyncHelper(config schema.GitConfigDB, wg *sync.WaitGroup) {
|
|||
ctx, cancel := context.WithTimeout(backgroundContext, timeout)
|
||||
defer cancel()
|
||||
// get most recent data from db after acquiring lock
|
||||
conf, err := operations.GetGitConfig(ctx, config.ProjectID)
|
||||
conf, err := dbOperationsGitOps.GetGitConfig(ctx, config.ProjectID)
|
||||
if err != nil {
|
||||
log.Print("Repo Sync ERROR: ", config.ProjectID, err.Error())
|
||||
}
|
||||
|
|
@ -307,14 +311,14 @@ func GitSyncHelper(config schema.GitConfigDB, wg *sync.WaitGroup) {
|
|||
}
|
||||
}
|
||||
|
||||
//GitOpsSyncHandler syncs all repos in the DB
|
||||
// GitOpsSyncHandler syncs all repos in the DB
|
||||
func GitOpsSyncHandler(singleRun bool) {
|
||||
const syncGroupSize = 10
|
||||
const syncInterval = 2 * time.Minute
|
||||
for {
|
||||
ctx, cancel := context.WithTimeout(backgroundContext, timeout)
|
||||
log.Print("Running GitOps DB Sync...")
|
||||
configs, err := operations.GetAllGitConfig(ctx)
|
||||
configs, err := dbOperationsGitOps.GetAllGitConfig(ctx)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Print("Failed to get git configs from db : ", err)
|
||||
|
|
|
|||
|
|
@ -1,224 +0,0 @@
|
|||
package mutations
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaos-workflow/handler"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/graphql"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/graphql/subscriptions"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/utils"
|
||||
)
|
||||
|
||||
//ClusterRegister creates an entry for a new cluster in DB and generates the url used to apply manifest
|
||||
func ClusterRegister(input model.ClusterInput) (*model.ClusterRegResponse, error) {
|
||||
clusterID := uuid.New().String()
|
||||
|
||||
token, err := cluster.ClusterCreateJWT(clusterID)
|
||||
if err != nil {
|
||||
return &model.ClusterRegResponse{}, err
|
||||
}
|
||||
|
||||
newCluster := database.Cluster{
|
||||
ClusterID: clusterID,
|
||||
ClusterName: input.ClusterName,
|
||||
Description: input.Description,
|
||||
ProjectID: input.ProjectID,
|
||||
AccessKey: utils.RandomString(32),
|
||||
ClusterType: input.ClusterType,
|
||||
PlatformName: input.PlatformName,
|
||||
AgentNamespace: input.AgentNamespace,
|
||||
Serviceaccount: input.Serviceaccount,
|
||||
AgentScope: input.AgentScope,
|
||||
AgentNsExists: input.AgentNsExists,
|
||||
AgentSaExists: input.AgentSaExists,
|
||||
CreatedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
UpdatedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
Token: token,
|
||||
IsRemoved: false,
|
||||
}
|
||||
|
||||
err = database.InsertCluster(newCluster)
|
||||
if err != nil {
|
||||
return &model.ClusterRegResponse{}, err
|
||||
}
|
||||
|
||||
log.Print("NEW CLUSTER REGISTERED : ID-", clusterID, " PID-", input.ProjectID)
|
||||
|
||||
return &model.ClusterRegResponse{
|
||||
ClusterID: newCluster.ClusterID,
|
||||
Token: token,
|
||||
ClusterName: newCluster.ClusterName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
//ConfirmClusterRegistration takes the cluster_id and access_key from the subscriber and validates it, if validated generates and sends new access_key
|
||||
func ConfirmClusterRegistration(identity model.ClusterIdentity, r store.StateData) (*model.ClusterConfirmResponse, error) {
|
||||
cluster, err := database.GetCluster(identity.ClusterID)
|
||||
if err != nil {
|
||||
return &model.ClusterConfirmResponse{IsClusterConfirmed: false}, err
|
||||
}
|
||||
|
||||
if cluster.AccessKey == identity.AccessKey {
|
||||
newKey := utils.RandomString(32)
|
||||
time := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
query := bson.D{{"cluster_id", identity.ClusterID}}
|
||||
update := bson.D{{"$unset", bson.D{{"token", ""}}}, {"$set", bson.D{{"access_key", newKey}, {"is_registered", true}, {"is_cluster_confirmed", true}, {"updated_at", time}}}}
|
||||
|
||||
err = database.UpdateCluster(query, update)
|
||||
if err != nil {
|
||||
return &model.ClusterConfirmResponse{IsClusterConfirmed: false}, err
|
||||
}
|
||||
|
||||
cluster.IsRegistered = true
|
||||
cluster.AccessKey = ""
|
||||
|
||||
newCluster := model.Cluster{}
|
||||
copier.Copy(&newCluster, &cluster)
|
||||
|
||||
log.Print("CLUSTER Confirmed : ID-", cluster.ClusterID, " PID-", cluster.ProjectID)
|
||||
subscriptions.SendClusterEvent("cluster-registration", "New Cluster", "New Cluster registration", newCluster, r)
|
||||
|
||||
return &model.ClusterConfirmResponse{IsClusterConfirmed: true, NewClusterKey: &newKey, ClusterID: &cluster.ClusterID}, err
|
||||
}
|
||||
return &model.ClusterConfirmResponse{IsClusterConfirmed: false}, err
|
||||
}
|
||||
|
||||
//NewEvent takes a event from a subscriber, validates identity and broadcasts the event to the users
|
||||
func NewEvent(clusterEvent model.ClusterEventInput, r store.StateData) (string, error) {
|
||||
cluster, err := database.GetCluster(clusterEvent.ClusterID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if cluster.AccessKey == clusterEvent.AccessKey && cluster.IsRegistered {
|
||||
log.Print("CLUSTER EVENT : ID-", cluster.ClusterID, " PID-", cluster.ProjectID)
|
||||
|
||||
newCluster := model.Cluster{}
|
||||
copier.Copy(&newCluster, &cluster)
|
||||
|
||||
subscriptions.SendClusterEvent("cluster-event", clusterEvent.EventName, clusterEvent.Description, newCluster, r)
|
||||
return "Event Published", nil
|
||||
}
|
||||
|
||||
return "", errors.New("ERROR WITH CLUSTER EVENT")
|
||||
}
|
||||
|
||||
// WorkFlowRunHandler Updates or Inserts a new Workflow Run into the DB
|
||||
func WorkFlowRunHandler(input model.WorkflowRunInput, r store.StateData) (string, error) {
|
||||
cluster, err := cluster.VerifyCluster(*input.ClusterID)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
//err = database.UpdateWorkflowRun(database.WorkflowRun(newWorkflowRun))
|
||||
count, err := database.UpdateWorkflowRun(input.WorkflowID, database.WorkflowRun{
|
||||
WorkflowRunID: input.WorkflowRunID,
|
||||
LastUpdated: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
ExecutionData: input.ExecutionData,
|
||||
Completed: input.Completed,
|
||||
})
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
return "Workflow Run Discarded[Duplicate Event]", nil
|
||||
}
|
||||
|
||||
handler.SendWorkflowEvent(model.WorkflowRun{
|
||||
ClusterID: cluster.ClusterID,
|
||||
ClusterName: cluster.ClusterName,
|
||||
ProjectID: cluster.ProjectID,
|
||||
LastUpdated: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
WorkflowRunID: input.WorkflowRunID,
|
||||
WorkflowName: input.WorkflowName,
|
||||
ExecutionData: input.ExecutionData,
|
||||
WorkflowID: input.WorkflowID,
|
||||
}, &r)
|
||||
|
||||
return "Workflow Run Accepted", nil
|
||||
}
|
||||
|
||||
// LogsHandler receives logs from the workflow-agent and publishes to frontend clients
|
||||
func LogsHandler(podLog model.PodLog, r store.StateData) (string, error) {
|
||||
_, err := cluster.VerifyCluster(*podLog.ClusterID)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return "", err
|
||||
}
|
||||
if reqChan, ok := r.WorkflowLog[podLog.RequestID]; ok {
|
||||
resp := model.PodLogResponse{
|
||||
PodName: podLog.PodName,
|
||||
WorkflowRunID: podLog.WorkflowRunID,
|
||||
PodType: podLog.PodType,
|
||||
Log: podLog.Log,
|
||||
}
|
||||
reqChan <- &resp
|
||||
close(reqChan)
|
||||
return "LOGS SENT SUCCESSFULLY", nil
|
||||
}
|
||||
return "LOG REQUEST CANCELLED", nil
|
||||
}
|
||||
|
||||
func DeleteCluster(cluster_id string, r store.StateData) (string, error) {
|
||||
time := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
|
||||
query := bson.D{{"cluster_id", cluster_id}}
|
||||
update := bson.D{{"$set", bson.D{{"is_removed", true}, {"updated_at", time}}}}
|
||||
|
||||
err := database.UpdateCluster(query, update)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cluster, err := database.GetCluster(cluster_id)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
requests := []string{
|
||||
`{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": {
|
||||
"name": "subscriber",
|
||||
"namespace": ` + *cluster.AgentNamespace + `
|
||||
}
|
||||
}`,
|
||||
`{
|
||||
"apiVersion": "v1",
|
||||
"kind": "ConfigMap",
|
||||
"metadata": {
|
||||
"name": "litmus-portal-config",
|
||||
"namespace": ` + *cluster.AgentNamespace + `
|
||||
}
|
||||
}`,
|
||||
}
|
||||
|
||||
for _, request := range requests {
|
||||
subscriptions.SendRequestToSubscriber(graphql.SubscriberRequests{
|
||||
K8sManifest: request,
|
||||
RequestType: "delete",
|
||||
ProjectID: cluster.ProjectID,
|
||||
ClusterID: cluster_id,
|
||||
Namespace: *cluster.AgentNamespace,
|
||||
}, r)
|
||||
}
|
||||
|
||||
return "Successfully deleted cluster", nil
|
||||
}
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
package queries
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
)
|
||||
|
||||
//GetLogs query is used to fetch the logs from the cluster
|
||||
func GetLogs(reqID string, pod model.PodLogRequest, r store.StateData) {
|
||||
data, err := json.Marshal(pod)
|
||||
if err != nil {
|
||||
log.Print("ERROR WHILE MARSHALLING POD DETAILS")
|
||||
}
|
||||
reqType := "logs"
|
||||
externalData := string(data)
|
||||
payload := model.ClusterAction{
|
||||
ProjectID: reqID,
|
||||
Action: &model.ActionPayload{
|
||||
RequestType: reqType,
|
||||
ExternalData: &externalData,
|
||||
},
|
||||
}
|
||||
if clusterChan, ok := r.ConnectedCluster[pod.ClusterID]; ok {
|
||||
clusterChan <- &payload
|
||||
} else if reqChan, ok := r.WorkflowLog[reqID]; ok {
|
||||
resp := model.PodLogResponse{
|
||||
PodName: pod.PodName,
|
||||
WorkflowRunID: pod.WorkflowRunID,
|
||||
PodType: pod.PodType,
|
||||
Log: "CLUSTER ERROR : CLUSTER NOT CONNECTED",
|
||||
}
|
||||
reqChan <- &resp
|
||||
close(reqChan)
|
||||
}
|
||||
}
|
||||
|
||||
func QueryGetClusters(projectID string, clusterType *string) ([]*model.Cluster, error) {
|
||||
clusters, err := database.GetClusterWithProjectID(projectID, clusterType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newClusters := []*model.Cluster{}
|
||||
|
||||
for _, cluster := range clusters {
|
||||
var totalNoOfSchedules int
|
||||
|
||||
workflows, err := database.GetWorkflowsByClusterID(cluster.ClusterID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newCluster := model.Cluster{}
|
||||
copier.Copy(&newCluster, &cluster)
|
||||
newCluster.NoOfWorkflows = func(i int) *int { return &i }(len(workflows))
|
||||
for _, workflow := range workflows {
|
||||
totalNoOfSchedules = totalNoOfSchedules + len(workflow.WorkflowRuns)
|
||||
}
|
||||
|
||||
newCluster.NoOfSchedules = func(i int) *int { return &i }(totalNoOfSchedules)
|
||||
|
||||
newClusters = append(newClusters, &newCluster)
|
||||
}
|
||||
|
||||
return newClusters, nil
|
||||
}
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
package subscriptions
|
||||
|
||||
import (
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/graphql"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
|
||||
)
|
||||
|
||||
//SendClusterEvent sends events from the clusters to the appropriate users listening for the events
|
||||
func SendClusterEvent(eventType, eventName, description string, cluster model.Cluster, r store.StateData) {
|
||||
newEvent := model.ClusterEvent{
|
||||
EventID: uuid.New().String(),
|
||||
EventType: eventType,
|
||||
EventName: eventName,
|
||||
Description: description,
|
||||
Cluster: &cluster,
|
||||
}
|
||||
r.Mutex.Lock()
|
||||
if r.ClusterEventPublish != nil {
|
||||
for _, observer := range r.ClusterEventPublish[cluster.ProjectID] {
|
||||
observer <- &newEvent
|
||||
}
|
||||
}
|
||||
r.Mutex.Unlock()
|
||||
}
|
||||
|
||||
func SendRequestToSubscriber(subscriberRequest graphql.SubscriberRequests, r store.StateData) {
|
||||
if os.Getenv("AGENT_SCOPE") == "cluster" {
|
||||
/*
|
||||
namespace = Obtain from WorkflowManifest or
|
||||
from frontend as a separate workflowNamespace field under ChaosWorkFlowInput model
|
||||
for CreateChaosWorkflow mutation to be passed to this function.
|
||||
*/
|
||||
}
|
||||
newAction := &model.ClusterAction{
|
||||
ProjectID: subscriberRequest.ProjectID,
|
||||
Action: &model.ActionPayload{
|
||||
K8sManifest: subscriberRequest.K8sManifest,
|
||||
Namespace: subscriberRequest.Namespace,
|
||||
RequestType: subscriberRequest.RequestType,
|
||||
},
|
||||
}
|
||||
|
||||
r.Mutex.Lock()
|
||||
|
||||
if observer, ok := r.ConnectedCluster[subscriberRequest.ClusterID]; ok {
|
||||
observer <- newAction
|
||||
}
|
||||
|
||||
r.Mutex.Unlock()
|
||||
}
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
package k8s
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"k8s.io/client-go/discovery"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"os"
|
||||
)
|
||||
|
||||
func GetKubeConfig() (*rest.Config, error) {
|
||||
|
|
@ -28,7 +29,7 @@ func GetGenericK8sClient() (*kubernetes.Clientset, error) {
|
|||
return kubernetes.NewForConfig(config)
|
||||
}
|
||||
|
||||
//This function returns dynamic client and discovery client
|
||||
// This function returns dynamic client and discovery client
|
||||
func GetDynamicAndDiscoveryClient() (discovery.DiscoveryInterface, dynamic.Interface, error) {
|
||||
// returns a config object which uses the service account kubernetes gives to pods
|
||||
config, err := GetKubeConfig()
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import (
|
|||
"k8s.io/client-go/dynamic"
|
||||
"k8s.io/client-go/restmapper"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
memory "k8s.io/client-go/discovery/cached"
|
||||
)
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ func ClusterResource(manifest string, namespace string) (*unstructured.Unstructu
|
|||
dr = dynamicClient.Resource(mapping.Resource)
|
||||
}
|
||||
|
||||
response, err := dr.Create(obj, metav1.CreateOptions{})
|
||||
response, err := dr.Create(obj, metaV1.CreateOptions{})
|
||||
if errors.IsAlreadyExists(err) {
|
||||
// This doesnt ever happen even if it does already exist
|
||||
log.Print("Already exists")
|
||||
|
|
@ -84,11 +84,11 @@ func GetPortalEndpoint() (string, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
podList, _ := clientset.CoreV1().Pods(LitmusPortalNS).List(metav1.ListOptions{
|
||||
podList, _ := clientset.CoreV1().Pods(LitmusPortalNS).List(metaV1.ListOptions{
|
||||
LabelSelector: "component=litmusportal-server",
|
||||
})
|
||||
|
||||
svc, err := clientset.CoreV1().Services(LitmusPortalNS).Get("litmusportal-server-service", metav1.GetOptions{})
|
||||
svc, err := clientset.CoreV1().Services(LitmusPortalNS).Get("litmusportal-server-service", metaV1.GetOptions{})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -99,7 +99,7 @@ func GetPortalEndpoint() (string, error) {
|
|||
}
|
||||
}
|
||||
|
||||
nodeIP, err := clientset.CoreV1().Nodes().Get(podList.Items[0].Spec.NodeName, metav1.GetOptions{})
|
||||
nodeIP, err := clientset.CoreV1().Nodes().Get(podList.Items[0].Spec.NodeName, metaV1.GetOptions{})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
)
|
||||
|
||||
//Chart ...
|
||||
// Chart ...
|
||||
type Chart struct {
|
||||
APIVersion string `yaml:"apiVersion"`
|
||||
Kind string `yaml:"kind"`
|
||||
|
|
@ -20,26 +21,26 @@ type Chart struct {
|
|||
Experiments []Chart `yaml:"experiments"`
|
||||
}
|
||||
|
||||
//Maintainer ...
|
||||
// Maintainer ...
|
||||
type Maintainer struct {
|
||||
Name string
|
||||
Email string
|
||||
}
|
||||
|
||||
//Link ...
|
||||
// Link ...
|
||||
type Link struct {
|
||||
Name string
|
||||
URL string
|
||||
}
|
||||
|
||||
//Metadata ...
|
||||
// Metadata ...
|
||||
type Metadata struct {
|
||||
Name string `yaml:"name"`
|
||||
Version string `yaml:"version"`
|
||||
Annotations Annotation `yaml:"annotations"`
|
||||
}
|
||||
|
||||
//Annotation ...
|
||||
// Annotation ...
|
||||
type Annotation struct {
|
||||
Categories string `yaml:"categories"`
|
||||
Vendor string `yaml:"vendor"`
|
||||
|
|
@ -49,7 +50,7 @@ type Annotation struct {
|
|||
ChartDescription string `yaml:"chartDescription"`
|
||||
}
|
||||
|
||||
//Spec ...
|
||||
// Spec ...
|
||||
type Spec struct {
|
||||
DisplayName string `yaml:"displayName"`
|
||||
CategoryDescription string `yaml:"categoryDescription"`
|
||||
|
|
@ -67,7 +68,7 @@ type Spec struct {
|
|||
ChaosType string `yaml:"chaosType"`
|
||||
}
|
||||
|
||||
//PackageInformation ...
|
||||
// PackageInformation ...
|
||||
type PackageInformation struct {
|
||||
PackageName string `yaml:"packageName"`
|
||||
Experiments []struct {
|
||||
|
|
@ -77,15 +78,15 @@ type PackageInformation struct {
|
|||
} `yaml:"experiments"`
|
||||
}
|
||||
|
||||
//Charts ...
|
||||
// Charts ...
|
||||
type Charts []Chart
|
||||
|
||||
//default path for storing local clones
|
||||
// default path for storing local clones
|
||||
const (
|
||||
defaultPath = "/tmp/version/"
|
||||
)
|
||||
|
||||
//GetChartsPath is used to construct path for given chart.
|
||||
// GetChartsPath is used to construct path for given chart.
|
||||
func GetChartsPath(ctx context.Context, chartsInput model.CloningInput) string {
|
||||
ProjectID := chartsInput.ProjectID
|
||||
HubName := chartsInput.HubName
|
||||
|
|
@ -93,7 +94,7 @@ func GetChartsPath(ctx context.Context, chartsInput model.CloningInput) string {
|
|||
return ChartsPath
|
||||
}
|
||||
|
||||
//GetExperimentChartsVersionYamlPath is used to construct path for given chartsversion.yaml.
|
||||
// GetExperimentChartsVersionYamlPath is used to construct path for given chartsversion.yaml.
|
||||
func GetExperimentChartsVersionYamlPath(ctx context.Context, experimentInput model.ExperimentInput) string {
|
||||
ProjectID := experimentInput.ProjectID
|
||||
HubName := experimentInput.HubName
|
||||
|
|
@ -103,7 +104,7 @@ func GetExperimentChartsVersionYamlPath(ctx context.Context, experimentInput mod
|
|||
return ExperimentPath
|
||||
}
|
||||
|
||||
//GetExperimentYAMLPath is used to construct path for given experiment/engine.
|
||||
// GetExperimentYAMLPath is used to construct path for given experiment/engine.
|
||||
func GetExperimentYAMLPath(ctx context.Context, experimentInput model.ExperimentInput) string {
|
||||
ProjectID := experimentInput.ProjectID
|
||||
HubName := experimentInput.HubName
|
||||
|
|
@ -114,7 +115,7 @@ func GetExperimentYAMLPath(ctx context.Context, experimentInput model.Experiment
|
|||
return ExperimentYAMLPath
|
||||
}
|
||||
|
||||
//GetChartsData is used to get details of charts like experiments.
|
||||
// GetChartsData is used to get details of charts like experiments.
|
||||
func GetChartsData(ChartsPath string) ([]*model.Chart, error) {
|
||||
var AllChartsDetails Charts
|
||||
Charts, err := ioutil.ReadDir(ChartsPath)
|
||||
|
|
@ -134,7 +135,7 @@ func GetChartsData(ChartsPath string) ([]*model.Chart, error) {
|
|||
return data1, nil
|
||||
}
|
||||
|
||||
//GetExperimentData is used for getting details of selected Experiment path
|
||||
// GetExperimentData is used for getting details of selected Experiment path
|
||||
func GetExperimentData(experimentFilePath string) (*model.Chart, error) {
|
||||
data, _ := ReadExperimentFile(experimentFilePath)
|
||||
e, _ := json.Marshal(data)
|
||||
|
|
@ -143,7 +144,7 @@ func GetExperimentData(experimentFilePath string) (*model.Chart, error) {
|
|||
return data1, nil
|
||||
}
|
||||
|
||||
//ReadExperimentFile is used for reading a experiment file from given path
|
||||
// ReadExperimentFile is used for reading a experiment file from given path
|
||||
func ReadExperimentFile(path string) (Chart, error) {
|
||||
var experiment Chart
|
||||
experimentFile, err := ioutil.ReadFile(path)
|
||||
|
|
@ -157,7 +158,7 @@ func ReadExperimentFile(path string) (Chart, error) {
|
|||
return experiment, nil
|
||||
}
|
||||
|
||||
//ReadExperimentYAMLFile is used for reading a experiment/engine file from given path
|
||||
// ReadExperimentYAMLFile is used for reading a experiment/engine file from given path
|
||||
func ReadExperimentYAMLFile(path string) (string, error) {
|
||||
var s string
|
||||
YAMLData, err := ioutil.ReadFile(path)
|
||||
|
|
|
|||
|
|
@ -4,28 +4,30 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/operations"
|
||||
dbSchema "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/myhub/handler"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/myhub/myhub_ops"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/jinzhu/copier"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
dbOperationsMyHub "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/myhub"
|
||||
dbSchemaMyHub "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/myhub"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/myhub/handler"
|
||||
myHubOps "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/myhub/ops"
|
||||
)
|
||||
|
||||
const (
|
||||
timeInterval = 6 * time.Hour
|
||||
)
|
||||
|
||||
//AddMyHub is used for Adding a new MyHub
|
||||
// AddMyHub is used for Adding a new MyHub
|
||||
func AddMyHub(ctx context.Context, myhub model.CreateMyHub, projectID string) (*model.MyHub, error) {
|
||||
|
||||
IsExist, err := IsMyHubAvailable(ctx, myhub.HubName, projectID)
|
||||
|
|
@ -49,15 +51,15 @@ func AddMyHub(ctx context.Context, myhub model.CreateMyHub, projectID string) (*
|
|||
SSHPrivateKey: myhub.SSHPrivateKey,
|
||||
}
|
||||
|
||||
//Cloning the repository at a path from myhub link structure.
|
||||
err = myhub_ops.GitClone(cloneHub)
|
||||
// Cloning the repository at a path from myhub link structure.
|
||||
err = myHubOps.GitClone(cloneHub)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//Initialize a UID for new Hub.
|
||||
// Initialize a UID for new Hub.
|
||||
uuid := uuid.New()
|
||||
newHub := &dbSchema.MyHub{
|
||||
newHub := &dbSchemaMyHub.MyHub{
|
||||
ID: uuid.String(),
|
||||
ProjectID: projectID,
|
||||
RepoURL: myhub.RepoURL,
|
||||
|
|
@ -76,8 +78,8 @@ func AddMyHub(ctx context.Context, myhub model.CreateMyHub, projectID string) (*
|
|||
LastSyncedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
}
|
||||
|
||||
//Adding the new hub into database with the given username.
|
||||
err = database.CreateMyHub(ctx, newHub)
|
||||
// Adding the new hub into database with the given username.
|
||||
err = dbOperationsMyHub.CreateMyHub(ctx, newHub)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return nil, err
|
||||
|
|
@ -86,7 +88,7 @@ func AddMyHub(ctx context.Context, myhub model.CreateMyHub, projectID string) (*
|
|||
return newHub.GetOutputMyHub(), nil
|
||||
}
|
||||
|
||||
//SaveMyHub is used for Adding a new MyHub
|
||||
// SaveMyHub is used for Adding a new MyHub
|
||||
func SaveMyHub(ctx context.Context, myhub model.CreateMyHub, projectID string) (*model.MyHub, error) {
|
||||
|
||||
IsExist, err := IsMyHubAvailable(ctx, myhub.HubName, projectID)
|
||||
|
|
@ -97,9 +99,9 @@ func SaveMyHub(ctx context.Context, myhub model.CreateMyHub, projectID string) (
|
|||
return nil, errors.New("HubName Already exists")
|
||||
}
|
||||
|
||||
//Initialize a UID for new Hub.
|
||||
// Initialize a UID for new Hub.
|
||||
uuid := uuid.New()
|
||||
newHub := &dbSchema.MyHub{
|
||||
newHub := &dbSchemaMyHub.MyHub{
|
||||
ID: uuid.String(),
|
||||
ProjectID: projectID,
|
||||
RepoURL: myhub.RepoURL,
|
||||
|
|
@ -118,8 +120,8 @@ func SaveMyHub(ctx context.Context, myhub model.CreateMyHub, projectID string) (
|
|||
LastSyncedAt: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
}
|
||||
|
||||
//Adding the new hub into database with the given username without cloning.
|
||||
err = database.CreateMyHub(ctx, newHub)
|
||||
// Adding the new hub into database with the given username without cloning.
|
||||
err = dbOperationsMyHub.CreateMyHub(ctx, newHub)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return nil, err
|
||||
|
|
@ -128,10 +130,10 @@ func SaveMyHub(ctx context.Context, myhub model.CreateMyHub, projectID string) (
|
|||
return newHub.GetOutputMyHub(), nil
|
||||
}
|
||||
|
||||
//HubStatus returns the array of hubdetails with their current status.
|
||||
// HubStatus returns the array of hubdetails with their current status.
|
||||
func HubStatus(ctx context.Context, projectID string) ([]*model.MyHubStatus, error) {
|
||||
|
||||
allHubs, err := database.GetMyHubByProjectID(ctx, projectID)
|
||||
allHubs, err := dbOperationsMyHub.GetMyHubByProjectID(ctx, projectID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -180,9 +182,9 @@ func HubStatus(ctx context.Context, projectID string) ([]*model.MyHubStatus, err
|
|||
|
||||
}
|
||||
|
||||
//IsMyHubAvailable is used for checking if hub already exist or not
|
||||
// IsMyHubAvailable is used for checking if hub already exist or not
|
||||
func IsMyHubAvailable(ctx context.Context, hubname string, projectID string) (bool, error) {
|
||||
myhubs, err := database.GetMyHubByProjectID(ctx, projectID)
|
||||
myhubs, err := dbOperationsMyHub.GetMyHubByProjectID(ctx, projectID)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
|
|
@ -195,11 +197,11 @@ func IsMyHubAvailable(ctx context.Context, hubname string, projectID string) (bo
|
|||
return false, nil
|
||||
}
|
||||
|
||||
//GetCharts is responsible for getting the charts details
|
||||
// GetCharts is responsible for getting the charts details
|
||||
func GetCharts(ctx context.Context, hubName string, projectID string) ([]*model.Chart, error) {
|
||||
|
||||
chartsInput := model.CloningInput{}
|
||||
myhubs, err := database.GetMyHubByProjectID(ctx, projectID)
|
||||
myhubs, err := dbOperationsMyHub.GetMyHubByProjectID(ctx, projectID)
|
||||
for _, n := range myhubs {
|
||||
if n.HubName == hubName {
|
||||
chartsInput = model.CloningInput{
|
||||
|
|
@ -214,7 +216,7 @@ func GetCharts(ctx context.Context, hubName string, projectID string) ([]*model.
|
|||
ChartsPath := handler.GetChartsPath(ctx, chartsInput)
|
||||
ChartsData, err := handler.GetChartsData(ChartsPath)
|
||||
if err != nil {
|
||||
err = myhub_ops.GitClone(chartsInput)
|
||||
err = myHubOps.GitClone(chartsInput)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -227,7 +229,7 @@ func GetCharts(ctx context.Context, hubName string, projectID string) ([]*model.
|
|||
return ChartsData, nil
|
||||
}
|
||||
|
||||
//GetExperiment is used for getting details of a given experiment using chartserviceversion.yaml.
|
||||
// GetExperiment is used for getting details of a given experiment using chartserviceversion.yaml.
|
||||
func GetExperiment(ctx context.Context, experimentInput model.ExperimentInput) (*model.Chart, error) {
|
||||
|
||||
ExperimentPath := handler.GetExperimentChartsVersionYamlPath(ctx, experimentInput)
|
||||
|
|
@ -239,9 +241,9 @@ func GetExperiment(ctx context.Context, experimentInput model.ExperimentInput) (
|
|||
return ExperimentData, nil
|
||||
}
|
||||
|
||||
//SyncHub is used for syncing the hub again if some not present or some error happens.
|
||||
// SyncHub is used for syncing the hub again if some not present or some error happens.
|
||||
func SyncHub(ctx context.Context, hubID string) ([]*model.MyHubStatus, error) {
|
||||
myhub, err := database.GetHubByID(ctx, hubID)
|
||||
myhub, err := dbOperationsMyHub.GetHubByID(ctx, hubID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -263,12 +265,12 @@ func SyncHub(ctx context.Context, hubID string) ([]*model.MyHubStatus, error) {
|
|||
query := bson.D{{"myhub_id", hubID}, {"IsRemoved", false}}
|
||||
update := bson.D{{"$set", bson.D{{"last_synced_at", time}}}}
|
||||
|
||||
err = myhub_ops.GitSyncHandlerForProjects(syncHubInput)
|
||||
err = myHubOps.GitSyncHandlerForProjects(syncHubInput)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//Updating the last_synced_at time using hubID
|
||||
err = database.UpdateMyHub(ctx, query, update)
|
||||
// Updating the last_synced_at time using hubID
|
||||
err = dbOperationsMyHub.UpdateMyHub(ctx, query, update)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return nil, err
|
||||
|
|
@ -286,10 +288,10 @@ func GetYAMLData(ctx context.Context, experimentInput model.ExperimentInput) (st
|
|||
return YAMLData, nil
|
||||
}
|
||||
|
||||
//GetAllHubs ...
|
||||
// GetAllHubs ...
|
||||
func GetAllHubs(ctx context.Context) ([]*model.MyHub, error) {
|
||||
|
||||
myhubs, err := database.GetHubs(ctx)
|
||||
myhubs, err := dbOperationsMyHub.GetHubs(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -317,7 +319,7 @@ func UpdateMyHub(ctx context.Context, myhub model.UpdateMyHub, projectID string)
|
|||
SSHPrivateKey: myhub.SSHPrivateKey,
|
||||
}
|
||||
|
||||
prevMyHub, err := database.GetHubByID(ctx, myhub.ID)
|
||||
prevMyHub, err := dbOperationsMyHub.GetHubByID(ctx, myhub.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -333,12 +335,12 @@ func UpdateMyHub(ctx context.Context, myhub model.UpdateMyHub, projectID string)
|
|||
// Syncing/Cloning the repository at a path from myhub link structure.
|
||||
if prevMyHub.RepoURL != myhub.RepoURL || prevMyHub.RepoBranch != myhub.RepoBranch || prevMyHub.IsPrivate != myhub.IsPrivate || prevMyHub.AuthType != myhub.AuthType.String() {
|
||||
fmt.Println(myhub.AuthType.String())
|
||||
err := myhub_ops.GitClone(cloneHub)
|
||||
err := myHubOps.GitClone(cloneHub)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
err := myhub_ops.GitSyncHandlerForProjects(cloneHub)
|
||||
err := myHubOps.GitSyncHandlerForProjects(cloneHub)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -352,8 +354,8 @@ func UpdateMyHub(ctx context.Context, myhub model.UpdateMyHub, projectID string)
|
|||
{"Token", myhub.Token}, {"UserName", myhub.UserName}, {"Password", myhub.Password},
|
||||
{"SSHPrivateKey", myhub.SSHPrivateKey}, {"SSHPublicKey", myhub.SSHPublicKey}, {"updated_at", time}}}}
|
||||
|
||||
//Updating the new hub into database with the given username.
|
||||
err = database.UpdateMyHub(ctx, query, update)
|
||||
// Updating the new hub into database with the given username.
|
||||
err = dbOperationsMyHub.UpdateMyHub(ctx, query, update)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return nil, err
|
||||
|
|
@ -371,7 +373,7 @@ func DeleteMyHub(ctx context.Context, hubID string) (bool, error) {
|
|||
query := bson.D{{"myhub_id", hubID}}
|
||||
update := bson.D{{"$set", bson.D{{"IsRemoved", true}, {"updated_at", strconv.FormatInt(time.Now().Unix(), 10)}}}}
|
||||
|
||||
err := database.UpdateMyHub(ctx, query, update)
|
||||
err := dbOperationsMyHub.UpdateMyHub(ctx, query, update)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return false, err
|
||||
|
|
@ -395,10 +397,10 @@ var GetIconHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Reques
|
|||
io.Copy(w, img)
|
||||
})
|
||||
|
||||
//RecurringHubSync is used for syncing
|
||||
// RecurringHubSync is used for syncing
|
||||
func RecurringHubSync() {
|
||||
for {
|
||||
//Started Syncing of hubs
|
||||
// Started Syncing of hubs
|
||||
myhubs, _ := GetAllHubs(nil)
|
||||
|
||||
for _, myhub := range myhubs {
|
||||
|
|
@ -416,10 +418,10 @@ func RecurringHubSync() {
|
|||
SSHPrivateKey: myhub.SSHPrivateKey,
|
||||
}
|
||||
|
||||
myhub_ops.GitSyncHandlerForProjects(chartsInput)
|
||||
myHubOps.GitSyncHandlerForProjects(chartsInput)
|
||||
}
|
||||
|
||||
//Syncing Completed
|
||||
// Syncing Completed
|
||||
time.Sleep(timeInterval)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,23 @@
|
|||
package myhub_ops
|
||||
package myhubOps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
ssh2 "golang.org/x/crypto/ssh"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/transport"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gopkg.in/src-d/go-git.v4"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
)
|
||||
|
||||
//MyHubConfig ...
|
||||
// MyHubConfig ...
|
||||
type MyHubConfig struct {
|
||||
ProjectID string
|
||||
RepositoryURL string
|
||||
|
|
@ -36,13 +38,13 @@ const (
|
|||
defaultPath = "/tmp/version/"
|
||||
)
|
||||
|
||||
//GetClonePath is used to construct path for Repository.
|
||||
// GetClonePath is used to construct path for Repository.
|
||||
func GetClonePath(c MyHubConfig) string {
|
||||
RepoPath := defaultPath + c.ProjectID + "/" + c.HubName
|
||||
return RepoPath
|
||||
}
|
||||
|
||||
//GitConfigConstruct is used for constructing the gitconfig
|
||||
// GitConfigConstruct is used for constructing the gitconfig
|
||||
func GitConfigConstruct(repoData model.CloningInput) MyHubConfig {
|
||||
gitConfig := MyHubConfig{
|
||||
ProjectID: repoData.ProjectID,
|
||||
|
|
@ -61,7 +63,7 @@ func GitConfigConstruct(repoData model.CloningInput) MyHubConfig {
|
|||
return gitConfig
|
||||
}
|
||||
|
||||
//GitClone Trigger is reponsible for setting off the go routine for git-op
|
||||
// GitClone Trigger is reponsible for setting off the go routine for git-op
|
||||
func GitClone(repoData model.CloningInput) error {
|
||||
gitConfig := GitConfigConstruct(repoData)
|
||||
if repoData.IsPrivate {
|
||||
|
|
@ -78,11 +80,11 @@ func GitClone(repoData model.CloningInput) error {
|
|||
}
|
||||
|
||||
}
|
||||
//Successfully Cloned
|
||||
// Successfully Cloned
|
||||
return nil
|
||||
}
|
||||
|
||||
//getChaosChartVersion is responsible for plain cloning the repository
|
||||
// getChaosChartVersion is responsible for plain cloning the repository
|
||||
func (c MyHubConfig) getChaosChartRepo() (string, error) {
|
||||
ClonePath := GetClonePath(c)
|
||||
os.RemoveAll(ClonePath)
|
||||
|
|
@ -93,7 +95,7 @@ func (c MyHubConfig) getChaosChartRepo() (string, error) {
|
|||
return c.Branch, err
|
||||
}
|
||||
|
||||
//getPrivateChaosChartVersion is responsible for plain cloning the private repository
|
||||
// getPrivateChaosChartVersion is responsible for plain cloning the private repository
|
||||
func (c MyHubConfig) getPrivateChaosChartRepo() (string, error) {
|
||||
ClonePath := GetClonePath(c)
|
||||
os.RemoveAll(ClonePath)
|
||||
|
|
@ -112,14 +114,14 @@ func (c MyHubConfig) getPrivateChaosChartRepo() (string, error) {
|
|||
return c.Branch, err
|
||||
}
|
||||
|
||||
//GitSyncHandlerForProjects ...
|
||||
// GitSyncHandlerForProjects ...
|
||||
func GitSyncHandlerForProjects(repoData model.CloningInput) error {
|
||||
gitConfig := GitConfigConstruct(repoData)
|
||||
if err := gitConfig.chaosChartSyncHandler(); err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
//Repository syncing completed
|
||||
// Repository syncing completed
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package myhub_ops
|
||||
package myhubOps
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
|
|
@ -9,33 +9,36 @@ import (
|
|||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/myhub"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/operations"
|
||||
dbSchema "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
dbOperationsProject "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/project"
|
||||
dbSchemaProject "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/project"
|
||||
dbOperationsUserManagement "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/usermanagement"
|
||||
dbSchemaUserManagement "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/usermanagement"
|
||||
)
|
||||
|
||||
//CreateProjectWithUser ...
|
||||
func CreateProjectWithUser(ctx context.Context, projectName string, user *dbSchema.User) (*model.Project, error) {
|
||||
// CreateProjectWithUser ...
|
||||
func CreateProjectWithUser(ctx context.Context, projectName string, user *dbSchemaUserManagement.User) (*model.Project, error) {
|
||||
|
||||
uuid := uuid.New()
|
||||
newProject := &dbSchema.Project{
|
||||
newProject := &dbSchemaProject.Project{
|
||||
ID: uuid.String(),
|
||||
Name: projectName,
|
||||
Members: []*dbSchema.Member{
|
||||
Members: []*dbSchemaProject.Member{
|
||||
{
|
||||
UserID: user.ID,
|
||||
UserName: user.Username,
|
||||
Name: *user.Name,
|
||||
Email: *user.Email,
|
||||
Role: model.MemberRoleOwner,
|
||||
Invitation: dbSchema.AcceptedInvitation,
|
||||
Invitation: dbSchemaProject.AcceptedInvitation,
|
||||
JoinedAt: time.Now().Format(time.RFC1123Z),
|
||||
},
|
||||
},
|
||||
CreatedAt: time.Now().String(),
|
||||
}
|
||||
|
||||
err := database.CreateProject(ctx, newProject)
|
||||
err := dbOperationsProject.CreateProject(ctx, newProject)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return nil, err
|
||||
|
|
@ -53,18 +56,18 @@ func CreateProjectWithUser(ctx context.Context, projectName string, user *dbSche
|
|||
return newProject.GetOutputProject(), nil
|
||||
}
|
||||
|
||||
//GetProject ...
|
||||
// GetProject ...
|
||||
func GetProject(ctx context.Context, projectID string) (*model.Project, error) {
|
||||
project, err := database.GetProject(ctx, projectID)
|
||||
project, err := dbOperationsProject.GetProject(ctx, projectID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return project.GetOutputProject(), nil
|
||||
}
|
||||
|
||||
//GetProjectsByUserID ...
|
||||
// GetProjectsByUserID ...
|
||||
func GetProjectsByUserID(ctx context.Context, userID string) ([]*model.Project, error) {
|
||||
projects, err := database.GetProjectsByUserID(ctx, userID)
|
||||
projects, err := dbOperationsProject.GetProjectsByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -76,7 +79,7 @@ func GetProjectsByUserID(ctx context.Context, userID string) ([]*model.Project,
|
|||
return outputProjects, nil
|
||||
}
|
||||
|
||||
//SendInvitation ...
|
||||
// SendInvitation ...
|
||||
func SendInvitation(ctx context.Context, member model.MemberInput) (*model.Member, error) {
|
||||
|
||||
invitation, err := getInvitation(ctx, member)
|
||||
|
|
@ -84,32 +87,32 @@ func SendInvitation(ctx context.Context, member model.MemberInput) (*model.Membe
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if invitation == dbSchema.AcceptedInvitation {
|
||||
if invitation == dbSchemaProject.AcceptedInvitation {
|
||||
return nil, errors.New("This user is already a member of this project")
|
||||
} else if invitation == dbSchema.PendingInvitation || invitation == dbSchema.DeclinedInvitation {
|
||||
err = database.UpdateInvite(ctx, member.ProjectID, member.UserName, dbSchema.PendingInvitation, member.Role)
|
||||
} else if invitation == dbSchemaProject.PendingInvitation || invitation == dbSchemaProject.DeclinedInvitation {
|
||||
err = dbOperationsProject.UpdateInvite(ctx, member.ProjectID, member.UserName, dbSchemaProject.PendingInvitation, member.Role)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user, err := database.GetUserByUserName(ctx, member.UserName)
|
||||
user, err := dbOperationsUserManagement.GetUserByUserName(ctx, member.UserName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newMember := &dbSchema.Member{
|
||||
newMember := &dbSchemaProject.Member{
|
||||
UserID: user.ID,
|
||||
UserName: user.Username,
|
||||
Name: *user.Name,
|
||||
Email: *user.Email,
|
||||
Role: *member.Role,
|
||||
Invitation: dbSchema.PendingInvitation,
|
||||
Invitation: dbSchemaProject.PendingInvitation,
|
||||
}
|
||||
|
||||
err = database.AddMember(ctx, member.ProjectID, newMember)
|
||||
err = dbOperationsProject.AddMember(ctx, member.ProjectID, newMember)
|
||||
return newMember.GetOutputMember(), err
|
||||
}
|
||||
|
||||
//AcceptInvitation ...
|
||||
// AcceptInvitation ...
|
||||
func AcceptInvitation(ctx context.Context, member model.MemberInput) (string, error) {
|
||||
|
||||
invitation, err := getInvitation(ctx, member)
|
||||
|
|
@ -117,22 +120,22 @@ func AcceptInvitation(ctx context.Context, member model.MemberInput) (string, er
|
|||
return "Unsuccessful", err
|
||||
}
|
||||
|
||||
if invitation == dbSchema.AcceptedInvitation {
|
||||
if invitation == dbSchemaProject.AcceptedInvitation {
|
||||
return "Unsuccessful", errors.New("You are already a member of this project")
|
||||
} else if invitation == dbSchema.PendingInvitation {
|
||||
err := database.UpdateInvite(ctx, member.ProjectID, member.UserName, dbSchema.AcceptedInvitation, nil)
|
||||
} else if invitation == dbSchemaProject.PendingInvitation {
|
||||
err := dbOperationsProject.UpdateInvite(ctx, member.ProjectID, member.UserName, dbSchemaProject.AcceptedInvitation, nil)
|
||||
if err != nil {
|
||||
return "Unsuccessful", err
|
||||
}
|
||||
return "Successfull", nil
|
||||
} else if invitation == dbSchema.DeclinedInvitation {
|
||||
} else if invitation == dbSchemaProject.DeclinedInvitation {
|
||||
return "Unsuccessful", errors.New("You have already declined the request")
|
||||
}
|
||||
|
||||
return "Unsuccessful", errors.New("No invitation is present to accept")
|
||||
}
|
||||
|
||||
//DeclineInvitation ...
|
||||
// DeclineInvitation ...
|
||||
func DeclineInvitation(ctx context.Context, member model.MemberInput) (string, error) {
|
||||
|
||||
invitation, err := getInvitation(ctx, member)
|
||||
|
|
@ -140,25 +143,25 @@ func DeclineInvitation(ctx context.Context, member model.MemberInput) (string, e
|
|||
return "Unsuccessful", err
|
||||
}
|
||||
|
||||
if invitation == dbSchema.AcceptedInvitation {
|
||||
if invitation == dbSchemaProject.AcceptedInvitation {
|
||||
return "Unsuccessful", errors.New("You are already a member of this project")
|
||||
} else if invitation == dbSchema.PendingInvitation {
|
||||
err := database.UpdateInvite(ctx, member.ProjectID, member.UserName, dbSchema.DeclinedInvitation, nil)
|
||||
} else if invitation == dbSchemaProject.PendingInvitation {
|
||||
err := dbOperationsProject.UpdateInvite(ctx, member.ProjectID, member.UserName, dbSchemaProject.DeclinedInvitation, nil)
|
||||
if err != nil {
|
||||
return "Unsuccessful", err
|
||||
}
|
||||
return "Successfull", nil
|
||||
} else if invitation == dbSchema.DeclinedInvitation {
|
||||
} else if invitation == dbSchemaProject.DeclinedInvitation {
|
||||
return "Unsuccessful", errors.New("You have already declined the request")
|
||||
}
|
||||
|
||||
return "Unsuccessful", errors.New("No invitation is present to decline")
|
||||
}
|
||||
|
||||
//getInvitation
|
||||
func getInvitation(ctx context.Context, member model.MemberInput) (dbSchema.Invitation, error) {
|
||||
// getInvitation
|
||||
func getInvitation(ctx context.Context, member model.MemberInput) (dbSchemaProject.Invitation, error) {
|
||||
|
||||
project, err := database.GetProject(ctx, member.ProjectID)
|
||||
project, err := dbOperationsProject.GetProject(ctx, member.ProjectID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -172,15 +175,15 @@ func getInvitation(ctx context.Context, member model.MemberInput) (dbSchema.Invi
|
|||
return "", nil
|
||||
}
|
||||
|
||||
//RemoveInvitation ...
|
||||
// RemoveInvitation ...
|
||||
func RemoveInvitation(ctx context.Context, member model.MemberInput) (string, error) {
|
||||
|
||||
invitation, err := getInvitation(ctx, member)
|
||||
if err != nil {
|
||||
return "Unsuccessful", err
|
||||
}
|
||||
if invitation == dbSchema.AcceptedInvitation || invitation == dbSchema.PendingInvitation {
|
||||
er := database.RemoveInvitation(ctx, member.ProjectID, member.UserName, invitation)
|
||||
if invitation == dbSchemaProject.AcceptedInvitation || invitation == dbSchemaProject.PendingInvitation {
|
||||
er := dbOperationsProject.RemoveInvitation(ctx, member.ProjectID, member.UserName, invitation)
|
||||
if er != nil {
|
||||
return "Unsuccessful", er
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
package self_deployer
|
||||
|
||||
import (
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/file_handlers"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/k8s"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
clusterHandler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster/handler"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/file_handlers"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/k8s"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/graphql/mutations"
|
||||
)
|
||||
|
||||
// StartDeployer registers a new internal self-cluster and starts the deployer
|
||||
|
|
@ -29,7 +30,7 @@ func StartDeployer(projectID string) {
|
|||
AgentNamespace: &deployerNamespace,
|
||||
}
|
||||
|
||||
resp, err := mutations.ClusterRegister(clusterInput)
|
||||
resp, err := clusterHandler.ClusterRegister(clusterInput)
|
||||
if err != nil {
|
||||
log.Print("SELF CLUSTER REG FAILED[DB-REG] : ", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package types
|
||||
|
||||
//SubscriberConfigurationVars contains the required configurable parameters for subscriber installation
|
||||
// SubscriberConfigurationVars contains the required configurable parameters for subscriber installation
|
||||
type SubscriberConfigurationVars struct {
|
||||
AgentNamespace string
|
||||
AgentScope string
|
||||
|
|
|
|||
|
|
@ -8,17 +8,19 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
self_deployer "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/self-deployer"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
selfDeployer "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/self-deployer"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/operations"
|
||||
dbSchema "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/schema"
|
||||
dbOperationsUserManagement "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/usermanagement"
|
||||
dbSchemaUserManagement "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/usermanagement"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/project"
|
||||
)
|
||||
|
||||
//CreateUser ...
|
||||
// CreateUser ...
|
||||
func CreateUser(ctx context.Context, user model.CreateUserInput) (*model.User, error) {
|
||||
|
||||
var (
|
||||
|
|
@ -32,7 +34,7 @@ func CreateUser(ctx context.Context, user model.CreateUserInput) (*model.User, e
|
|||
return outputUser, errors.New("User already exists")
|
||||
}
|
||||
|
||||
newUser := &dbSchema.User{
|
||||
newUser := &dbSchemaUserManagement.User{
|
||||
ID: uuid.String(),
|
||||
Username: user.Username,
|
||||
Email: user.Email,
|
||||
|
|
@ -41,7 +43,7 @@ func CreateUser(ctx context.Context, user model.CreateUserInput) (*model.User, e
|
|||
CreatedAt: time.Now().Format(time.RFC1123Z),
|
||||
}
|
||||
|
||||
err = database.InsertUser(ctx, newUser)
|
||||
err = dbOperationsUserManagement.InsertUser(ctx, newUser)
|
||||
if err != nil {
|
||||
log.Print("ERROR", err)
|
||||
return nil, err
|
||||
|
|
@ -57,16 +59,16 @@ func CreateUser(ctx context.Context, user model.CreateUserInput) (*model.User, e
|
|||
|
||||
if strings.ToLower(self_cluster) == "true" && strings.ToLower(outputUser.Username) == "admin" {
|
||||
log.Print("Starting self deployer")
|
||||
go self_deployer.StartDeployer(project.ID)
|
||||
go selfDeployer.StartDeployer(project.ID)
|
||||
}
|
||||
|
||||
return outputUser, nil
|
||||
}
|
||||
|
||||
//GetUser ...
|
||||
// GetUser ...
|
||||
func GetUser(ctx context.Context, username string) (*model.User, error) {
|
||||
|
||||
user, err := database.GetUserByUserName(ctx, username)
|
||||
user, err := dbOperationsUserManagement.GetUserByUserName(ctx, username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -81,10 +83,10 @@ func GetUser(ctx context.Context, username string) (*model.User, error) {
|
|||
return outputUser, nil
|
||||
}
|
||||
|
||||
//GetUsers ...
|
||||
// GetUsers ...
|
||||
func GetUsers(ctx context.Context) ([]*model.User, error) {
|
||||
|
||||
users, err := database.GetUsers(ctx)
|
||||
users, err := dbOperationsUserManagement.GetUsers(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -97,17 +99,17 @@ func GetUsers(ctx context.Context) ([]*model.User, error) {
|
|||
return outputUsers, nil
|
||||
}
|
||||
|
||||
//UpdateUser ...
|
||||
// UpdateUser ...
|
||||
func UpdateUser(ctx context.Context, user model.UpdateUserInput) (string, error) {
|
||||
|
||||
dbUser := &dbSchema.User{
|
||||
dbUser := &dbSchemaUserManagement.User{
|
||||
ID: user.ID,
|
||||
Email: user.Email,
|
||||
CompanyName: user.CompanyName,
|
||||
Name: user.Name,
|
||||
UpdatedAt: time.Now().Format(time.RFC1123Z),
|
||||
}
|
||||
err := database.UpdateUser(ctx, dbUser)
|
||||
err := dbOperationsUserManagement.UpdateUser(ctx, dbUser)
|
||||
if err != nil {
|
||||
return "Updating user aborted", err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,13 +13,14 @@ import (
|
|||
"github.com/99designs/gqlgen/graphql/playground"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/rs/cors"
|
||||
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/generated"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/authorization"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/file_handlers"
|
||||
gitops_handler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/gitops/handler"
|
||||
gitOpsHandler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/gitops/handler"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/myhub"
|
||||
"github.com/rs/cors"
|
||||
)
|
||||
|
||||
const defaultPort = "8080"
|
||||
|
|
@ -63,10 +64,10 @@ func main() {
|
|||
AllowCredentials: true,
|
||||
}).Handler)
|
||||
|
||||
gitops_handler.GitOpsSyncHandler(true) // sync all previous existing repos before start
|
||||
gitOpsHandler.GitOpsSyncHandler(true) // sync all previous existing repos before start
|
||||
|
||||
go myhub.RecurringHubSync() //go routine for syncing hubs for all users
|
||||
go gitops_handler.GitOpsSyncHandler(false) // routine to sync git repos for gitops
|
||||
go myhub.RecurringHubSync() // go routine for syncing hubs for all users
|
||||
go gitOpsHandler.GitOpsSyncHandler(false) // routine to sync git repos for gitOps
|
||||
|
||||
router.Handle("/", playground.Handler("GraphQL playground", "/query"))
|
||||
router.Handle("/query", authorization.Middleware(srv))
|
||||
|
|
|
|||
|
|
@ -8,18 +8,18 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
database "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb"
|
||||
dbSchemaCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/types"
|
||||
)
|
||||
|
||||
//WriteHeaders adds important headers to API responses
|
||||
// WriteHeaders adds important headers to API responses
|
||||
func WriteHeaders(w *http.ResponseWriter, statusCode int) {
|
||||
(*w).Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
(*w).Header().Set("Access-Control-Allow-Origin", "*")
|
||||
(*w).WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
//RandomString generates random strings, can be used to create ids or random secrets
|
||||
// RandomString generates random strings, can be used to create ids or random secrets
|
||||
func RandomString(n int) string {
|
||||
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
|
||||
s := make([]rune, n)
|
||||
|
|
@ -29,8 +29,8 @@ func RandomString(n int) string {
|
|||
return string(s)
|
||||
}
|
||||
|
||||
//ManifestParser parses manifests yaml and generates dynamic manifest with specified keys
|
||||
func ManifestParser(cluster database.Cluster, rootPath string, subscriberConfig *types.SubscriberConfigurationVars) ([]byte, error) {
|
||||
// ManifestParser parses manifests yaml and generates dynamic manifest with specified keys
|
||||
func ManifestParser(cluster dbSchemaCluster.Cluster, rootPath string, subscriberConfig *types.SubscriberConfigurationVars) ([]byte, error) {
|
||||
var (
|
||||
generatedYAML []string
|
||||
defaultState = false
|
||||
|
|
|
|||
Loading…
Reference in New Issue