From 62c3445245a00c05a7e5939a43c289371b032e43 Mon Sep 17 00:00:00 2001 From: nagesh bansal Date: Sun, 30 Jul 2023 18:17:47 +0530 Subject: [PATCH] Update api directory str Signed-off-by: nagesh bansal --- pkg/apis/environment.go | 75 ------- pkg/apis/environment/environment.go | 93 +++++++++ pkg/apis/environment/query.go | 21 ++ pkg/apis/environment/types.go | 43 ++++ pkg/apis/{ => experiment}/experiment.go | 230 +++------------------ pkg/apis/experiment/query.go | 77 +++++++ pkg/apis/experiment/types.go | 113 ++++++++++ pkg/apis/{ => infrastructure}/infra.go | 177 +--------------- pkg/apis/infrastructure/query.go | 33 +++ pkg/apis/infrastructure/types.go | 68 ++++++ pkg/cmd/config/setAccount.go | 5 +- pkg/cmd/connect/connect.go | 2 +- pkg/cmd/connect/infra.go | 36 ++-- pkg/cmd/create/create.go | 10 +- pkg/cmd/{connect => create}/environment.go | 30 +-- pkg/cmd/create/experiment.go | 147 +++++++++++++ pkg/cmd/create/workflow.go | 141 ------------- pkg/cmd/delete/delete.go | 2 +- pkg/cmd/delete/experiment.go | 3 +- pkg/cmd/describe/describe.go | 2 +- pkg/cmd/describe/experiment.go | 4 +- pkg/cmd/disconnect/disconnect.go | 2 +- pkg/cmd/disconnect/infra.go | 3 +- pkg/cmd/get/experimentruns.go | 10 +- pkg/cmd/get/experiments.go | 4 +- pkg/cmd/get/infra.go | 8 +- pkg/cmd/run/experiment.go | 7 +- pkg/cmd/run/run.go | 2 +- pkg/cmd/save/experiment.go | 32 +-- pkg/cmd/save/save.go | 2 +- pkg/{infra => ops}/ops.go | 12 +- pkg/{infra => ops}/platform.go | 2 +- 32 files changed, 709 insertions(+), 687 deletions(-) delete mode 100644 pkg/apis/environment.go create mode 100644 pkg/apis/environment/environment.go create mode 100644 pkg/apis/environment/query.go create mode 100644 pkg/apis/environment/types.go rename pkg/apis/{ => experiment}/experiment.go (58%) create mode 100644 pkg/apis/experiment/query.go create mode 100644 pkg/apis/experiment/types.go rename pkg/apis/{ => infrastructure}/infra.go (53%) create mode 100644 pkg/apis/infrastructure/query.go create mode 100644 pkg/apis/infrastructure/types.go rename pkg/cmd/{connect => create}/environment.go (83%) create mode 100644 pkg/cmd/create/experiment.go delete mode 100644 pkg/cmd/create/workflow.go rename pkg/{infra => ops}/ops.go (96%) rename pkg/{infra => ops}/platform.go (99%) diff --git a/pkg/apis/environment.go b/pkg/apis/environment.go deleted file mode 100644 index 26bab104..00000000 --- a/pkg/apis/environment.go +++ /dev/null @@ -1,75 +0,0 @@ -package apis - -import ( - "encoding/json" - "errors" - models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - "github.com/litmuschaos/litmusctl/pkg/types" - "github.com/litmuschaos/litmusctl/pkg/utils" - "io/ioutil" - "net/http" -) - -type CreateEnvironmentGQLRequest struct { - Query string `json:"query"` - Variables struct { - ProjectId string `json:"projectID"` - Request models.CreateEnvironmentRequest `json:"request"` - } `json:"variables"` -} - -type EnvironmentConnectionData struct { - Data EnvironmentData `json:"data"` - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` -} - -type EnvironmentData struct { - EnvironmentDetails models.Environment `json:"connectEnvironment"` -} - -// ConnectEnvironment connects the Infra with the given details -func ConnectEnvironment(pid string, request models.CreateEnvironmentRequest, cred types.Credentials) (EnvironmentConnectionData, error) { - var gqlReq CreateEnvironmentGQLRequest - gqlReq.Query = `mutation createEnvironment($projectID: ID!, $request: CreateEnvironmentRequest!) { - createEnvironment( - projectID: $projectID - request: $request - ) { - environmentID - name - } - } - ` - gqlReq.Variables.ProjectId = pid - gqlReq.Variables.Request = request - - query, err := json.Marshal(gqlReq) - resp, err := SendRequest(SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, query, string(types.Post)) - if err != nil { - return EnvironmentConnectionData{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error()) - } - - bodyBytes, err := ioutil.ReadAll(resp.Body) - defer resp.Body.Close() - if err != nil { - return EnvironmentConnectionData{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error()) - } - - if resp.StatusCode == http.StatusOK { - var connectEnvironment EnvironmentConnectionData - err = json.Unmarshal(bodyBytes, &connectEnvironment) - if err != nil { - return EnvironmentConnectionData{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error()) - } - - if len(connectEnvironment.Errors) > 0 { - return EnvironmentConnectionData{}, errors.New(connectEnvironment.Errors[0].Message) - } - return connectEnvironment, nil - } else { - return EnvironmentConnectionData{}, err - } -} diff --git a/pkg/apis/environment/environment.go b/pkg/apis/environment/environment.go new file mode 100644 index 00000000..d4b1e6e6 --- /dev/null +++ b/pkg/apis/environment/environment.go @@ -0,0 +1,93 @@ +package environment + +import ( + "encoding/json" + "errors" + models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmusctl/pkg/apis" + "github.com/litmuschaos/litmusctl/pkg/types" + "github.com/litmuschaos/litmusctl/pkg/utils" + "io/ioutil" + "net/http" +) + +// ConnectEnvironment connects the Infra with the given details +func ConnectEnvironment(pid string, request models.CreateEnvironmentRequest, cred types.Credentials) (CreateEnvironmentResponse, error) { + var gqlReq CreateEnvironmentGQLRequest + gqlReq.Query = CreateEnvironmentQuery + gqlReq.Variables.ProjectId = pid + gqlReq.Variables.Request = request + + query, err := json.Marshal(gqlReq) + resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, query, string(types.Post)) + if err != nil { + return CreateEnvironmentResponse{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error()) + } + + bodyBytes, err := ioutil.ReadAll(resp.Body) + defer resp.Body.Close() + if err != nil { + return CreateEnvironmentResponse{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error()) + } + + if resp.StatusCode == http.StatusOK { + var connectEnvironment CreateEnvironmentResponse + err = json.Unmarshal(bodyBytes, &connectEnvironment) + if err != nil { + return CreateEnvironmentResponse{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error()) + } + + if len(connectEnvironment.Errors) > 0 { + return CreateEnvironmentResponse{}, errors.New(connectEnvironment.Errors[0].Message) + } + return connectEnvironment, nil + } else { + return CreateEnvironmentResponse{}, err + } +} + +func GetEnvironmentList(pid string, cred types.Credentials) (ListEnvironmentData, error) { + var err error + var gqlReq CreateEnvironmentListGQLRequest + gqlReq.Query = ListEnvironmentQuery + + gqlReq.Variables.Request = models.ListEnvironmentRequest{} + gqlReq.Variables.ProjectID = pid + query, err := json.Marshal(gqlReq) + if err != nil { + return ListEnvironmentData{}, err + } + + resp, err := apis.SendRequest( + apis.SendRequestParams{ + Endpoint: cred.Endpoint + utils.GQLAPIPath, + Token: cred.Token, + }, + query, + string(types.Post), + ) + + if err != nil { + return ListEnvironmentData{}, errors.New("Error in Getting Chaos Environment List: " + err.Error()) + } + + bodyBytes, err := ioutil.ReadAll(resp.Body) + defer resp.Body.Close() + if err != nil { + return ListEnvironmentData{}, errors.New("Error in Getting Chaos Environment List: " + err.Error()) + } + + if resp.StatusCode == http.StatusOK { + var listEnvironment ListEnvironmentData + err = json.Unmarshal(bodyBytes, &listEnvironment) + if err != nil { + return ListEnvironmentData{}, errors.New("Error in Getting Chaos Environment List: " + err.Error()) + } + if len(listEnvironment.Errors) > 0 { + return ListEnvironmentData{}, errors.New(listEnvironment.Errors[0].Message) + } + return listEnvironment, nil + } else { + return ListEnvironmentData{}, err + } +} diff --git a/pkg/apis/environment/query.go b/pkg/apis/environment/query.go new file mode 100644 index 00000000..8e0f77fd --- /dev/null +++ b/pkg/apis/environment/query.go @@ -0,0 +1,21 @@ +package environment + +const ( + CreateEnvironmentQuery = `mutation createEnvironment($projectID: ID!, $request: CreateEnvironmentRequest!) { + createEnvironment( + projectID: $projectID + request: $request + ) { + environmentID + name + } + } + ` + ListEnvironmentQuery = `query listEnvironments($projectID: ID!, $request: ListEnvironmentRequest) { + listEnvironments(projectID: $projectID,request: $request){ + environments { + environmentID + } + } + }` +) diff --git a/pkg/apis/environment/types.go b/pkg/apis/environment/types.go new file mode 100644 index 00000000..0bba5e90 --- /dev/null +++ b/pkg/apis/environment/types.go @@ -0,0 +1,43 @@ +package environment + +import models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + +type CreateEnvironmentGQLRequest struct { + Query string `json:"query"` + Variables struct { + ProjectId string `json:"projectID"` + Request models.CreateEnvironmentRequest `json:"request"` + } `json:"variables"` +} + +type CreateEnvironmentResponse struct { + Data CreateEnvironmentData `json:"data"` + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` +} + +type CreateEnvironmentData struct { + EnvironmentDetails models.Environment `json:"createEnvironment"` +} + +type ListEnvironmentData struct { + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` + Data EnvironmentsList `json:"data"` +} + +type EnvironmentsList struct { + ListEnvironmentDetails models.ListEnvironmentResponse `json:"listEnvironments"` +} + +type CreateEnvironmentListGQLRequest struct { + Query string `json:"query"` + Variables struct { + ProjectID string `json:"projectID"` + Request models.ListEnvironmentRequest `json:"request"` + } +} diff --git a/pkg/apis/experiment.go b/pkg/apis/experiment/experiment.go similarity index 58% rename from pkg/apis/experiment.go rename to pkg/apis/experiment/experiment.go index ecf5c2fe..56e1e5b6 100644 --- a/pkg/apis/experiment.go +++ b/pkg/apis/experiment/experiment.go @@ -13,62 +13,26 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -package apis +package experiment import ( "encoding/json" "errors" - "fmt" - "io/ioutil" - "net/http" - "strings" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmusctl/pkg/apis" types "github.com/litmuschaos/litmusctl/pkg/types" "github.com/litmuschaos/litmusctl/pkg/utils" + "io/ioutil" + "net/http" ) -type SaveExperimentData struct { - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` - Data SavedExperimentDetails `json:"data"` -} - -type SavedExperimentDetails struct { - Message string `json:"saveChaosExperiment"` -} - -type SaveChaosExperimentGraphQLRequest struct { - Query string `json:"query"` - Variables struct { - ProjectID string `json:"projectID"` - SaveChaosExperimentRequest model.SaveChaosExperimentRequest `json:"request"` - } `json:"variables"` -} - -type RunExperimentData struct { - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` - Data RunExperimentDetails `json:"data"` -} - -type RunExperimentDetails struct { - RunExperimentResponse model.RunChaosExperimentResponse `json:"runChaosExperiment"` -} - // CreateExperiment sends GraphQL API request for creating a Experiment func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest, cred types.Credentials) (RunExperimentData, error) { // Query to Save the Experiment var gqlReq SaveChaosExperimentGraphQLRequest - gqlReq.Query = `mutation saveChaosExperiment($projectID: ID!, $request: SaveChaosExperimentRequest!) { - saveChaosExperiment(projectID: $projectID, request: $request) - }` + gqlReq.Query = SaveExperimentQuery gqlReq.Variables.ProjectID = pid gqlReq.Variables.SaveChaosExperimentRequest = requestData @@ -77,8 +41,8 @@ func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest, return RunExperimentData{}, err } - resp, err := SendRequest( - SendRequestParams{ + resp, err := apis.SendRequest( + apis.SendRequestParams{ Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token, }, @@ -110,16 +74,13 @@ func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest, return RunExperimentData{}, errors.New(savedExperiment.Errors[0].Message) } - if strings.Contains(savedExperiment.Data.Message, "experiment saved successfully") { - fmt.Print("\nπŸš€ Chaos Experiment successfully Saved πŸŽ‰") - } } else { return RunExperimentData{}, errors.New("graphql schema error") } // Query to Run the Chaos Experiment runQuery := `{"query":"mutation{ \n runChaosExperiment(experimentID: \"` + requestData.ID + `\", projectID: \"` + pid + `\"){\n notifyID \n}}"}` - resp, err = SendRequest(SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(runQuery), string(types.Post)) + resp, err = apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(runQuery), string(types.Post)) if err != nil { return RunExperimentData{}, errors.New("Error in Running Chaos Experiment: " + err.Error()) @@ -152,9 +113,7 @@ func SaveExperiment(pid string, requestData model.SaveChaosExperimentRequest, cr // Query to Save the Experiment var gqlReq SaveChaosExperimentGraphQLRequest - gqlReq.Query = `mutation saveChaosExperiment($projectID: ID!, $request: SaveChaosExperimentRequest!) { - saveChaosExperiment(projectID: $projectID, request: $request) - }` + gqlReq.Query = SaveExperimentQuery gqlReq.Variables.ProjectID = pid gqlReq.Variables.SaveChaosExperimentRequest = requestData @@ -163,8 +122,8 @@ func SaveExperiment(pid string, requestData model.SaveChaosExperimentRequest, cr return SaveExperimentData{}, err } - resp, err := SendRequest( - SendRequestParams{ + resp, err := apis.SendRequest( + apis.SendRequestParams{ Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token, }, @@ -195,10 +154,6 @@ func SaveExperiment(pid string, requestData model.SaveChaosExperimentRequest, cr if len(savedExperiment.Errors) > 0 { return SaveExperimentData{}, errors.New(savedExperiment.Errors[0].Message) } - - if strings.Contains(savedExperiment.Data.Message, "experiment saved successfully") { - fmt.Print("\nπŸš€ Chaos Experiment successfully Saved πŸŽ‰") - } return savedExperiment, nil } else { @@ -211,7 +166,7 @@ func RunExperiment(pid string, eid string, cred types.Credentials) (RunExperimen var err error runQuery := `{"query":"mutation{ \n runChaosExperiment(experimentID: \"` + eid + `\", projectID: \"` + pid + `\"){\n notifyID \n}}"}` - resp, err := SendRequest(SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(runQuery), string(types.Post)) + resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(runQuery), string(types.Post)) if err != nil { return RunExperimentData{}, errors.New("Error in Running Chaos Experiment: " + err.Error()) @@ -239,62 +194,13 @@ func RunExperiment(pid string, eid string, cred types.Credentials) (RunExperimen } } -type ExperimentListData struct { - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` - Data ExperimentList `json:"data"` -} - -type ExperimentList struct { - ListExperimentDetails model.ListExperimentResponse `json:"listExperiment"` -} - -type GetChaosExperimentsGraphQLRequest struct { - Query string `json:"query"` - Variables struct { - GetChaosExperimentRequest model.ListExperimentRequest `json:"request"` - ProjectID string `json:"projectID"` - } `json:"variables"` -} - // GetExperimentList sends GraphQL API request for fetching a list of experiments. func GetExperimentList(pid string, in model.ListExperimentRequest, cred types.Credentials) (ExperimentListData, error) { var gqlReq GetChaosExperimentsGraphQLRequest var err error - gqlReq.Query = `query listExperiment($projectID: ID!, $request: ListExperimentRequest!) { - listExperiment(projectID: $projectID, request: $request) { - totalNoOfExperiments - experiments { - experimentID - experimentManifest - cronSyntax - name - description - weightages { - faultName - weightage - } - isCustomExperiment - updatedAt - createdAt - infra { - projectID - name - infraID - infraType - } - isRemoved - updatedBy{ - username - email - } - } - } -}` + gqlReq.Query = ListExperimentQuery gqlReq.Variables.GetChaosExperimentRequest = in gqlReq.Variables.ProjectID = pid @@ -303,8 +209,8 @@ func GetExperimentList(pid string, in model.ListExperimentRequest, cred types.Cr return ExperimentListData{}, err } - resp, err := SendRequest( - SendRequestParams{ + resp, err := apis.SendRequest( + apis.SendRequestParams{ Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token, }, @@ -338,59 +244,13 @@ func GetExperimentList(pid string, in model.ListExperimentRequest, cred types.Cr } } -type ExperimentRunListData struct { - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` - Data ExperimentRunsList `json:"data"` -} - -type ExperimentRunsList struct { - ListExperimentRunDetails model.ListExperimentRunResponse `json:"listExperimentRun"` -} - -type GetChaosExperimentRunGraphQLRequest struct { - Query string `json:"query"` - Variables struct { - ProjectID string `json:"projectID"` - GetChaosExperimentRunRequest model.ListExperimentRunRequest `json:"request"` - } `json:"variables"` -} - // GetExperimentRunsList sends GraphQL API request for fetching a list of experiment runs. func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred types.Credentials) (ExperimentRunListData, error) { var gqlReq GetChaosExperimentRunGraphQLRequest var err error - gqlReq.Query = `query listExperimentRuns($projectID: ID!, $request: ListExperimentRunRequest!) { - listExperimentRun(projectID: $projectID, request: $request) { - totalNoOfExperimentRuns - experimentRuns { - experimentRunID - experimentID - experimentName - infra { - name - projectID - infraID - infraType - } - isRemoved - updatedAt - phase - resiliencyScore - faultsPassed - faultsFailed - faultsAwaited - faultsStopped - faultsNa - totalFaults - executionData - } - } - }` + gqlReq.Query = ListExperimentRunsQuery gqlReq.Variables.ProjectID = pid gqlReq.Variables.GetChaosExperimentRunRequest = in @@ -399,8 +259,8 @@ func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred t return ExperimentRunListData{}, err } - resp, err := SendRequest( - SendRequestParams{ + resp, err := apis.SendRequest( + apis.SendRequestParams{ Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token, }, @@ -434,40 +294,13 @@ func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred t } } -type DeleteChaosExperimentData struct { - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` - Data DeleteChaosExperimentDetails `json:"data"` -} - -type DeleteChaosExperimentDetails struct { - IsDeleted bool `json:"deleteChaosExperiment"` -} - -type DeleteChaosExperimentGraphQLRequest struct { - Query string `json:"query"` - Variables struct { - ProjectID string `json:"projectID"` - ExperimentID *string `json:"experimentID"` - ExperimentRunID *string `json:"experimentRunID"` - } `json:"variables"` -} - // DeleteChaosExperiment sends GraphQL API request for deleting a given Chaos Experiment. func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Credentials) (DeleteChaosExperimentData, error) { var gqlReq DeleteChaosExperimentGraphQLRequest var err error - gqlReq.Query = `mutation deleteChaosExperiment($projectID: ID!, $experimentID: String!, $experimentRunID: String) { - deleteChaosExperiment( - projectID: $projectID - experimentID: $experimentID - experimentRunID: $experimentRunID - ) - }` + gqlReq.Query = DeleteExperimentQuery gqlReq.Variables.ProjectID = projectID gqlReq.Variables.ExperimentID = experimentID //var experiment_run_id string = "" @@ -478,8 +311,8 @@ func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Cr return DeleteChaosExperimentData{}, err } - resp, err := SendRequest( - SendRequestParams{ + resp, err := apis.SendRequest( + apis.SendRequestParams{ Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token, }, @@ -513,28 +346,11 @@ func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Cr } } -type ServerVersionResponse struct { - Data ServerVersionData `json:"data"` - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` -} - -type ServerVersionData struct { - GetServerVersion GetServerVersionData `json:"getServerVersion"` -} - -type GetServerVersionData struct { - Key string `json:"key"` - Value string `json:"value"` -} - // GetServerVersion fetches the GQL server version func GetServerVersion(endpoint string) (ServerVersionResponse, error) { query := `{"query":"query{\n getServerVersion{\n key value\n }\n}"}` - resp, err := SendRequest( - SendRequestParams{ + resp, err := apis.SendRequest( + apis.SendRequestParams{ Endpoint: endpoint + utils.GQLAPIPath, }, []byte(query), diff --git a/pkg/apis/experiment/query.go b/pkg/apis/experiment/query.go new file mode 100644 index 00000000..325f43be --- /dev/null +++ b/pkg/apis/experiment/query.go @@ -0,0 +1,77 @@ +package experiment + +const ( + SaveExperimentQuery = `mutation saveChaosExperiment($projectID: ID!, $request: SaveChaosExperimentRequest!) { + saveChaosExperiment(projectID: $projectID, request: $request) + }` + + ListExperimentQuery = `query listExperiment($projectID: ID!, $request: ListExperimentRequest!) { + listExperiment(projectID: $projectID, request: $request) { + totalNoOfExperiments + experiments { + experimentID + experimentManifest + cronSyntax + name + description + weightages { + faultName + weightage + } + isCustomExperiment + updatedAt + createdAt + infra { + projectID + name + infraID + infraType + } + isRemoved + updatedBy{ + username + email + } + } + } + }` + + ListExperimentRunsQuery = `query listExperimentRuns($projectID: ID!, $request: ListExperimentRunRequest!) { + listExperimentRun(projectID: $projectID, request: $request) { + totalNoOfExperimentRuns + experimentRuns { + experimentRunID + experimentID + experimentName + infra { + name + projectID + infraID + infraType + } + isRemoved + updatedAt + updatedBy{ + username + email + } + phase + resiliencyScore + faultsPassed + faultsFailed + faultsAwaited + faultsStopped + faultsNa + totalFaults + executionData + } + } + }` + DeleteExperimentQuery = `mutation deleteChaosExperiment($projectID: ID!, $experimentID: String!, $experimentRunID: String) { + deleteChaosExperiment( + projectID: $projectID + experimentID: $experimentID + experimentRunID: $experimentRunID + ) + }` +) diff --git a/pkg/apis/experiment/types.go b/pkg/apis/experiment/types.go new file mode 100644 index 00000000..901f1fbf --- /dev/null +++ b/pkg/apis/experiment/types.go @@ -0,0 +1,113 @@ +package experiment + +import "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + +type SaveExperimentData struct { + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` + Data SavedExperimentDetails `json:"data"` +} + +type SavedExperimentDetails struct { + Message string `json:"saveChaosExperiment"` +} + +type SaveChaosExperimentGraphQLRequest struct { + Query string `json:"query"` + Variables struct { + ProjectID string `json:"projectID"` + SaveChaosExperimentRequest model.SaveChaosExperimentRequest `json:"request"` + } `json:"variables"` +} + +type RunExperimentData struct { + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` + Data RunExperimentDetails `json:"data"` +} + +type RunExperimentDetails struct { + RunExperimentResponse model.RunChaosExperimentResponse `json:"runChaosExperiment"` +} + +type ExperimentListData struct { + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` + Data ExperimentList `json:"data"` +} + +type ExperimentList struct { + ListExperimentDetails model.ListExperimentResponse `json:"listExperiment"` +} + +type GetChaosExperimentsGraphQLRequest struct { + Query string `json:"query"` + Variables struct { + GetChaosExperimentRequest model.ListExperimentRequest `json:"request"` + ProjectID string `json:"projectID"` + } `json:"variables"` +} + +type ExperimentRunListData struct { + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` + Data ExperimentRunsList `json:"data"` +} + +type ExperimentRunsList struct { + ListExperimentRunDetails model.ListExperimentRunResponse `json:"listExperimentRun"` +} + +type GetChaosExperimentRunGraphQLRequest struct { + Query string `json:"query"` + Variables struct { + ProjectID string `json:"projectID"` + GetChaosExperimentRunRequest model.ListExperimentRunRequest `json:"request"` + } `json:"variables"` +} + +type DeleteChaosExperimentData struct { + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` + Data DeleteChaosExperimentDetails `json:"data"` +} + +type DeleteChaosExperimentDetails struct { + IsDeleted bool `json:"deleteChaosExperiment"` +} + +type DeleteChaosExperimentGraphQLRequest struct { + Query string `json:"query"` + Variables struct { + ProjectID string `json:"projectID"` + ExperimentID *string `json:"experimentID"` + ExperimentRunID *string `json:"experimentRunID"` + } `json:"variables"` +} + +type ServerVersionResponse struct { + Data ServerVersionData `json:"data"` + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` +} + +type ServerVersionData struct { + GetServerVersion GetServerVersionData `json:"getServerVersion"` +} + +type GetServerVersionData struct { + Key string `json:"key"` + Value string `json:"value"` +} diff --git a/pkg/apis/infra.go b/pkg/apis/infrastructure/infra.go similarity index 53% rename from pkg/apis/infra.go rename to pkg/apis/infrastructure/infra.go index bd2869b9..e1cd1081 100644 --- a/pkg/apis/infra.go +++ b/pkg/apis/infrastructure/infra.go @@ -13,11 +13,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -package apis +package infrastructure import ( "encoding/json" "errors" + "github.com/litmuschaos/litmusctl/pkg/apis" "github.com/litmuschaos/litmusctl/pkg/types" "io/ioutil" "net/http" @@ -26,39 +27,10 @@ import ( "github.com/litmuschaos/litmusctl/pkg/utils" ) -type InfraData struct { - Data InfraList `json:"data"` - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` -} - -type InfraList struct { - ListInfraDetails models.ListInfraResponse `json:"listInfras"` -} - -type ListInfraGraphQLRequest struct { - Query string `json:"query"` - Variables struct { - ProjectID string `json:"projectID"` - ListInfraRequest models.ListInfraRequest `json:"request"` - } `json:"variables"` -} - // GetInfraList lists the Chaos Infrastructure connected to the specified project func GetInfraList(c types.Credentials, pid string, request models.ListInfraRequest) (InfraData, error) { var gplReq ListInfraGraphQLRequest - gplReq.Query = `query listInfras($projectID: ID!, $request: ListInfraRequest!){ - listInfras(projectID: $projectID, request: $request){ - totalNoOfInfras - infras { - infraID - name - isActive - } - } - }` + gplReq.Query = ListInfraQuery gplReq.Variables.ProjectID = pid gplReq.Variables.ListInfraRequest = request @@ -66,7 +38,7 @@ func GetInfraList(c types.Credentials, pid string, request models.ListInfraReque if err != nil { return InfraData{}, err } - resp, err := SendRequest(SendRequestParams{Endpoint: c.Endpoint + utils.GQLAPIPath, Token: c.Token}, query, string(types.Post)) + resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: c.Endpoint + utils.GQLAPIPath, Token: c.Token}, query, string(types.Post)) if err != nil { return InfraData{}, err } @@ -94,45 +66,10 @@ func GetInfraList(c types.Credentials, pid string, request models.ListInfraReque } } -type Errors struct { - Message string `json:"message"` - Path []string `json:"path"` -} - -type RegisterInfraGqlRequest struct { - Query string `json:"query"` - Variables struct { - ProjectId string `json:"projectID"` - RegisterInfraRequest models.RegisterInfraRequest `json:"request"` - } `json:"variables"` -} - -type InfraConnectionData struct { - Data RegisterInfra `json:"data"` - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` -} - -type RegisterInfra struct { - RegisterInfraDetails models.RegisterInfraResponse `json:"registerInfra"` -} - // ConnectInfra connects the Infra with the given details func ConnectInfra(infra types.Infra, cred types.Credentials) (InfraConnectionData, error) { var gqlReq RegisterInfraGqlRequest - gqlReq.Query = `mutation registerInfra($projectID: ID!, $request: RegisterInfraRequest!) { - registerInfra( - projectID: $projectID - request: $request - ) { - infraID - name - token - } - } - ` + gqlReq.Query = RegisterInfraQuery gqlReq.Variables.ProjectId = infra.ProjectId gqlReq.Variables.RegisterInfraRequest = CreateRegisterInfraRequest(infra) @@ -157,7 +94,7 @@ func ConnectInfra(infra types.Infra, cred types.Credentials) (InfraConnectionDat } query, err := json.Marshal(gqlReq) - resp, err := SendRequest(SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, query, string(types.Post)) + resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, query, string(types.Post)) if err != nil { return InfraConnectionData{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error()) } @@ -206,38 +143,13 @@ func CreateRegisterInfraRequest(infra types.Infra) (request models.RegisterInfra } } -type DisconnectInfraData struct { - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` - Data DisconnectInfraDetails `json:"data"` -} - -type DisconnectInfraDetails struct { - Message string `json:"deleteInfra"` -} - -type DisconnectInfraGraphQLRequest struct { - Query string `json:"query"` - Variables struct { - ProjectID string `json:"projectID"` - InfraID string `json:"infraID"` - } `json:"variables"` -} - // DisconnectInfra sends GraphQL API request for disconnecting Chaos Infra(s). func DisconnectInfra(projectID string, infraID string, cred types.Credentials) (DisconnectInfraData, error) { var gqlReq DisconnectInfraGraphQLRequest var err error - gqlReq.Query = `mutation deleteInfra($projectID: ID!, $infraID: String!) { - deleteInfra( - projectID: $projectID - infraID: $infraID - ) - }` + gqlReq.Query = DisconnectInfraQuery gqlReq.Variables.ProjectID = projectID gqlReq.Variables.InfraID = infraID @@ -246,8 +158,8 @@ func DisconnectInfra(projectID string, infraID string, cred types.Credentials) ( return DisconnectInfraData{}, err } - resp, err := SendRequest( - SendRequestParams{ + resp, err := apis.SendRequest( + apis.SendRequestParams{ Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token, }, @@ -280,74 +192,3 @@ func DisconnectInfra(projectID string, infraID string, cred types.Credentials) ( return DisconnectInfraData{}, err } } - -type ListEnvironmentData struct { - Errors []struct { - Message string `json:"message"` - Path []string `json:"path"` - } `json:"errors"` - Data EnvironmentsList `json:"data"` -} - -type EnvironmentsList struct { - ListEnvironmentDetails models.ListEnvironmentResponse `json:"listEnvironments"` -} -type EnvironmentListGQLRequest struct { - Query string `json:"query"` - Variables struct { - ProjectID string `json:"projectID"` - Request models.ListEnvironmentRequest `json:"request"` - } -} - -func GetEnvironmentList(pid string, cred types.Credentials) (ListEnvironmentData, error) { - var err error - var gqlReq EnvironmentListGQLRequest - gqlReq.Query = `query listEnvironments($projectID: ID!, $request: ListEnvironmentRequest) { - listEnvironments(projectID: $projectID,request: $request){ - environments { - environmentID - } - } - }` - - gqlReq.Variables.Request = models.ListEnvironmentRequest{} - gqlReq.Variables.ProjectID = pid - query, err := json.Marshal(gqlReq) - if err != nil { - return ListEnvironmentData{}, err - } - - resp, err := SendRequest( - SendRequestParams{ - Endpoint: cred.Endpoint + utils.GQLAPIPath, - Token: cred.Token, - }, - query, - string(types.Post), - ) - - if err != nil { - return ListEnvironmentData{}, errors.New("Error in Getting Chaos Environment List: " + err.Error()) - } - - bodyBytes, err := ioutil.ReadAll(resp.Body) - defer resp.Body.Close() - if err != nil { - return ListEnvironmentData{}, errors.New("Error in Getting Chaos Environment List: " + err.Error()) - } - - if resp.StatusCode == http.StatusOK { - var listEnvironment ListEnvironmentData - err = json.Unmarshal(bodyBytes, &listEnvironment) - if err != nil { - return ListEnvironmentData{}, errors.New("Error in Getting Chaos Environment List: " + err.Error()) - } - if len(listEnvironment.Errors) > 0 { - return ListEnvironmentData{}, errors.New(listEnvironment.Errors[0].Message) - } - return listEnvironment, nil - } else { - return ListEnvironmentData{}, err - } -} diff --git a/pkg/apis/infrastructure/query.go b/pkg/apis/infrastructure/query.go new file mode 100644 index 00000000..277f07bd --- /dev/null +++ b/pkg/apis/infrastructure/query.go @@ -0,0 +1,33 @@ +package infrastructure + +const ( + DisconnectInfraQuery = `mutation deleteInfra($projectID: ID!, $infraID: String!) { + deleteInfra( + projectID: $projectID + infraID: $infraID + ) + }` + + RegisterInfraQuery = `mutation registerInfra($projectID: ID!, $request: RegisterInfraRequest!) { + registerInfra( + projectID: $projectID + request: $request + ) { + infraID + name + token + } + } + ` + ListInfraQuery = `query listInfras($projectID: ID!, $request: ListInfraRequest!){ + listInfras(projectID: $projectID, request: $request){ + totalNoOfInfras + infras { + infraID + name + isActive + environmentID + } + } + }` +) diff --git a/pkg/apis/infrastructure/types.go b/pkg/apis/infrastructure/types.go new file mode 100644 index 00000000..e8dc6b9a --- /dev/null +++ b/pkg/apis/infrastructure/types.go @@ -0,0 +1,68 @@ +package infrastructure + +import models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + +type InfraData struct { + Data InfraList `json:"data"` + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` +} + +type InfraList struct { + ListInfraDetails models.ListInfraResponse `json:"listInfras"` +} + +type ListInfraGraphQLRequest struct { + Query string `json:"query"` + Variables struct { + ProjectID string `json:"projectID"` + ListInfraRequest models.ListInfraRequest `json:"request"` + } `json:"variables"` +} + +type Errors struct { + Message string `json:"message"` + Path []string `json:"path"` +} + +type RegisterInfraGqlRequest struct { + Query string `json:"query"` + Variables struct { + ProjectId string `json:"projectID"` + RegisterInfraRequest models.RegisterInfraRequest `json:"request"` + } `json:"variables"` +} + +type InfraConnectionData struct { + Data RegisterInfra `json:"data"` + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` +} + +type RegisterInfra struct { + RegisterInfraDetails models.RegisterInfraResponse `json:"registerInfra"` +} + +type DisconnectInfraData struct { + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` + Data DisconnectInfraDetails `json:"data"` +} + +type DisconnectInfraDetails struct { + Message string `json:"deleteInfra"` +} + +type DisconnectInfraGraphQLRequest struct { + Query string `json:"query"` + Variables struct { + ProjectID string `json:"projectID"` + InfraID string `json:"infraID"` + } `json:"variables"` +} diff --git a/pkg/cmd/config/setAccount.go b/pkg/cmd/config/setAccount.go index ac033b36..1be8f5d4 100644 --- a/pkg/cmd/config/setAccount.go +++ b/pkg/cmd/config/setAccount.go @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -17,6 +17,7 @@ package config import ( "fmt" + "github.com/litmuschaos/litmusctl/pkg/apis/experiment" "net/url" "os" "strings" @@ -158,7 +159,7 @@ var setAccountCmd = &cobra.Command{ } utils.White_B.Printf("\naccount.username/%s configured", claims["username"].(string)) - serverResp, err := apis.GetServerVersion(authInput.Endpoint) + serverResp, err := experiment.GetServerVersion(authInput.Endpoint) var isCompatible bool if err != nil { utils.Red.Println("\nError: ", err) diff --git a/pkg/cmd/connect/connect.go b/pkg/cmd/connect/connect.go index 2f883317..96dfa6e4 100644 --- a/pkg/cmd/connect/connect.go +++ b/pkg/cmd/connect/connect.go @@ -22,7 +22,7 @@ import ( // connectCmd represents the connect command var ConnectCmd = &cobra.Command{ Use: "connect", - Short: `Connect resources for LitmusChaos infra plane. + Short: `Connect resources for LitmusChaos Execution plane. Examples: #connect a Chaos Infrastructure litmusctl connect chaos-infra --name="new-chaos-infra" --non-interactive diff --git a/pkg/cmd/connect/infra.go b/pkg/cmd/connect/infra.go index c60c4989..200b04a8 100644 --- a/pkg/cmd/connect/infra.go +++ b/pkg/cmd/connect/infra.go @@ -18,11 +18,13 @@ package connect import ( "fmt" models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmusctl/pkg/apis/environment" + "github.com/litmuschaos/litmusctl/pkg/apis/infrastructure" "os" "github.com/litmuschaos/litmusctl/pkg/apis" - "github.com/litmuschaos/litmusctl/pkg/infra" "github.com/litmuschaos/litmusctl/pkg/k8s" + "github.com/litmuschaos/litmusctl/pkg/ops" "github.com/litmuschaos/litmusctl/pkg/types" "github.com/litmuschaos/litmusctl/pkg/utils" @@ -78,7 +80,7 @@ var infraCmd = &cobra.Command{ if !projectExists { utils.White_B.Print("Creating a random project...") - newInfra.ProjectId = infra.CreateRandomProject(credentials) + newInfra.ProjectId = ops.CreateRandomProject(credentials) } } @@ -139,10 +141,6 @@ var infraCmd = &cobra.Command{ utils.PrintError(err) if toleration != "" { - //var tolerations []types.Toleration - //err := json.Unmarshal([]byte(toleration), &tolerations) - //utils.PrintError(err) - // newInfra.Tolerations = toleration } @@ -169,9 +167,9 @@ var infraCmd = &cobra.Command{ // Check if user has sufficient permissions based on mode utils.White_B.Print("\nπŸƒ Running prerequisites check....") - infra.ValidateSAPermissions(newInfra.Namespace, newInfra.Mode, &kubeconfig) + ops.ValidateSAPermissions(newInfra.Namespace, newInfra.Mode, &kubeconfig) var ListInfraRequest = models.ListInfraRequest{} - infras, err := apis.GetInfraList(credentials, newInfra.ProjectId, ListInfraRequest) + infras, err := infrastructure.GetInfraList(credentials, newInfra.ProjectId, ListInfraRequest) utils.PrintError(err) // Duplicate Infra check @@ -184,10 +182,10 @@ var infraCmd = &cobra.Command{ } if isInfraExist { - infra.PrintExistingInfra(infras) + ops.PrintExistingInfra(infras) os.Exit(1) } - envIDs, err := apis.GetEnvironmentList(newInfra.ProjectId, credentials) + envIDs, err := environment.GetEnvironmentList(newInfra.ProjectId, credentials) utils.PrintError(err) // Check if Environment exists @@ -201,7 +199,7 @@ var infraCmd = &cobra.Command{ } if !isEnvExist { utils.Red.Println("\nChaos Environment with the given ID doesn't exists.") - infra.PrintExistingEnvironments(envIDs) + ops.PrintExistingEnvironments(envIDs) utils.White_B.Println("\n❗ Please enter a name from the List.") os.Exit(1) } @@ -212,28 +210,28 @@ var infraCmd = &cobra.Command{ if newInfra.ProjectId == "" { // Fetch project id - newInfra.ProjectId = infra.GetProjectID(userDetails) + newInfra.ProjectId = ops.GetProjectID(userDetails) } - modeType := infra.GetModeType() + modeType := ops.GetModeType() // Check if user has sufficient permissions based on mode utils.White_B.Print("\nπŸƒ Running prerequisites check....") - infra.ValidateSAPermissions(newInfra.Namespace, modeType, &kubeconfig) - newInfra, err = infra.GetInfraDetails(modeType, newInfra.ProjectId, credentials, &kubeconfig) + ops.ValidateSAPermissions(newInfra.Namespace, modeType, &kubeconfig) + newInfra, err = ops.GetInfraDetails(modeType, newInfra.ProjectId, credentials, &kubeconfig) utils.PrintError(err) newInfra.ServiceAccount, newInfra.SAExists = k8s.ValidSA(newInfra.Namespace, &kubeconfig) newInfra.Mode = modeType } - infra.Summary(newInfra, &kubeconfig) + ops.Summary(newInfra, &kubeconfig) if !nonInteractive { - infra.ConfirmInstallation() + ops.ConfirmInstallation() } - infra, err := apis.ConnectInfra(newInfra, credentials) + infra, err := infrastructure.ConnectInfra(newInfra, credentials) if err != nil { utils.Red.Println("\n❌ Chaos Infra connection failed: " + err.Error() + "\n") os.Exit(1) @@ -248,7 +246,7 @@ var infraCmd = &cobra.Command{ utils.White_B.Print("Applying YAML:\n", path) // Print error message in case Data field is null in response - if (infra.Data == apis.RegisterInfra{}) { + if (infra.Data == infrastructure.RegisterInfra{}) { utils.White_B.Print("\n🚫 Chaos newInfra connection failed: " + infra.Errors[0].Message + "\n") os.Exit(1) } diff --git a/pkg/cmd/create/create.go b/pkg/cmd/create/create.go index 08d1ab81..dc2a5cf4 100644 --- a/pkg/cmd/create/create.go +++ b/pkg/cmd/create/create.go @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -22,14 +22,16 @@ import ( // createCmd represents the create command var CreateCmd = &cobra.Command{ Use: "create", - Short: `Create resources for LitmusChaos agent plane. + Short: `Create resources for LitmusChaos Execution plane. Examples: #create a project litmusctl create project --name new-proj - #create a Chaos Scenario from a file - litmusctl create chaos-scenario -f chaos-scenario.yaml --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --chaos-delegate-id="d861b650-1549-4574-b2ba-ab754058dd04" + #create a Chaos Experiment from a file + litmusctl create chaos-experiment -f chaos-experiment.yaml --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --chaos-infra-id="d861b650-1549-4574-b2ba-ab754058dd04" + #create a Chaos Environment + litmusctl create chaos-environment --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --name="new-chaos-environment" Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag `, } diff --git a/pkg/cmd/connect/environment.go b/pkg/cmd/create/environment.go similarity index 83% rename from pkg/cmd/connect/environment.go rename to pkg/cmd/create/environment.go index cd3e01f6..08f7750c 100644 --- a/pkg/cmd/connect/environment.go +++ b/pkg/cmd/create/environment.go @@ -13,13 +13,14 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -package connect +package create import ( "fmt" models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" "github.com/litmuschaos/litmusctl/pkg/apis" - "github.com/litmuschaos/litmusctl/pkg/infra" + "github.com/litmuschaos/litmusctl/pkg/apis/environment" + "github.com/litmuschaos/litmusctl/pkg/ops" "github.com/litmuschaos/litmusctl/pkg/utils" "github.com/spf13/cobra" "os" @@ -30,11 +31,9 @@ var environmentCmd = &cobra.Command{ Use: "chaos-environment", Short: `Create an Environment. Example(s): - #connect a Chaos infra - litmusctl connect chaos-infra --name="new-chaos-infra" --non-interactive - #connect a Chaos infra within a project - litmusctl connect chaos-infra --name="new-chaos-infra" --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --non-interactive + #create a Chaos Environment + litmusctl create chaos-environment --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --name="new-chaos-environment" Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag `, @@ -92,7 +91,7 @@ var environmentCmd = &cobra.Command{ } newEnvironment.Type = models.EnvironmentType(envType) - envs, err := apis.GetEnvironmentList(pid, credentials) + envs, err := environment.GetEnvironmentList(pid, credentials) utils.PrintError(err) // Generate EnvironmentID from Environment Name @@ -109,8 +108,8 @@ var environmentCmd = &cobra.Command{ } } if isEnvExist { - utils.Red.Println("\nChaos Environment with the given ID already exists. try a different name") - infra.PrintExistingEnvironments(envs) + utils.Red.Println("\nChaos Environment with the given ID already exists, try with a different name") + ops.PrintExistingEnvironments(envs) os.Exit(1) } @@ -134,24 +133,25 @@ var environmentCmd = &cobra.Command{ os.Exit(1) } - environment, err := apis.ConnectEnvironment(pid, newEnvironment, credentials) + newEnv, err := environment.ConnectEnvironment(pid, newEnvironment, credentials) if err != nil { utils.Red.Println("\n❌ Chaos Environment connection failed: " + err.Error() + "\n") os.Exit(1) } - // Print error message in case Data field is null in response - //if (environment.Data == }) { - // utils.White_B.Print("\n🚫 Chaos newInfra connection failed: " + environment.Errors[0].Message + "\n") + //Print error message in case Data field is null in response + //if (newEnv.Data == environment.CreateEnvironmentData{}) { + // utils.White_B.Print("\n🚫 Chaos newInfra connection failed: " + newEnv.Errors[0].Message + "\n") // os.Exit(1) //} - utils.White_B.Println("\nπŸš€ Chaos New Environment Created successful!! πŸŽ‰" + environment.Data.EnvironmentDetails.EnvironmentID) + utils.White_B.Println("\nπŸš€ New Chaos Environment Created successful!! πŸŽ‰") + utils.White_B.Println("EnvironmentID: " + newEnv.Data.EnvironmentDetails.EnvironmentID) }, } func init() { - ConnectCmd.AddCommand(environmentCmd) + CreateCmd.AddCommand(environmentCmd) environmentCmd.Flags().String("project-id", "", "Set the project-id to install Chaos infra for the particular project. To see the projects, apply litmusctl get projects") environmentCmd.Flags().String("type", "NON_PROD", "Set the installation mode for the kind of Chaos infra | Supported=cluster/namespace") environmentCmd.Flags().String("name", "", "Set the Chaos infra name") diff --git a/pkg/cmd/create/experiment.go b/pkg/cmd/create/experiment.go new file mode 100644 index 00000000..bdad4518 --- /dev/null +++ b/pkg/cmd/create/experiment.go @@ -0,0 +1,147 @@ +/* +Copyright Β© 2021 The LitmusChaos Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package create + +import ( + "fmt" + models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmusctl/pkg/apis/experiment" + "os" + "strings" + //"time" + + //"github.com/gorhill/cronexpr" + "github.com/litmuschaos/litmusctl/pkg/apis" + "github.com/litmuschaos/litmusctl/pkg/utils" + + "github.com/spf13/cobra" +) + +// workflowCmd represents the project command +var workflowCmd = &cobra.Command{ + Use: "chaos-experiment", + Short: `Create a Chaos Experiment + Example: + #create a Chaos Experiment + litmusctl create chaos-experiment -f chaos-experiment.yaml --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --chaos-infra-id="1c9c5801-8789-4ac9-bf5f-32649b707a5c" + + Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag + `, + Run: func(cmd *cobra.Command, args []string) { + + // Fetch user credentials + credentials, err := utils.GetCredentials(cmd) + utils.PrintError(err) + + var chaosExperimentRequest models.SaveChaosExperimentRequest + + workflowManifest, err := cmd.Flags().GetString("file") + utils.PrintError(err) + + pid, err := cmd.Flags().GetString("project-id") + utils.PrintError(err) + + // Handle blank input for project ID + if pid == "" { + utils.White_B.Print("\nEnter the Project ID: ") + fmt.Scanln(&pid) + + if pid == "" { + utils.Red.Println("β›” Project ID can't be empty!!") + os.Exit(1) + } + } + + chaosExperimentRequest.InfraID, err = cmd.Flags().GetString("chaos-infra-id") + utils.PrintError(err) + + // Handle blank input for Chaos Infra ID + if chaosExperimentRequest.InfraID == "" { + utils.White_B.Print("\nEnter the Chaos Infra ID: ") + fmt.Scanln(&chaosExperimentRequest.InfraID) + + if chaosExperimentRequest.InfraID == "" { + utils.Red.Println("β›” Chaos Infra ID can't be empty!!") + os.Exit(1) + } + } + + chaosExperimentRequest.Description, err = cmd.Flags().GetString("description") + utils.PrintError(err) + if chaosExperimentRequest.Description == "" { + utils.White_B.Print("\nExperiment Description: ") + fmt.Scanln(&chaosExperimentRequest.Description) + } + + // Perform authorization + userDetails, err := apis.GetProjectDetails(credentials) + utils.PrintError(err) + var editAccess = false + var project apis.Project + for _, p := range userDetails.Data.Projects { + if p.ID == pid { + project = p + } + } + for _, member := range project.Members { + if (member.UserID == userDetails.Data.ID) && (member.Role == "Owner" || member.Role == "Editor") { + editAccess = true + } + } + if !editAccess { + utils.Red.Println("β›” User doesn't have edit access to the project!!") + os.Exit(1) + } + + // Parse workflow manifest and populate chaosWorkFlowInput + err = utils.ParseExperimentManifest(workflowManifest, &chaosExperimentRequest) + if err != nil { + utils.Red.Println("❌ Error parsing Chaos Experiment manifest: " + err.Error()) + os.Exit(1) + } + // Generate ExperimentID from ExperimentName + chaosExperimentRequest.ID = utils.GenerateNameID(chaosExperimentRequest.Name) + // Make API call + createExperiment, err := experiment.CreateExperiment(pid, chaosExperimentRequest, credentials) + if err != nil { + if (createExperiment.Data == experiment.RunExperimentDetails{}) { + if strings.Contains(err.Error(), "multiple write errors") { + utils.Red.Println("\n❌ Chaos Experiment/" + chaosExperimentRequest.Name + " already exists") + os.Exit(1) + } + if strings.Contains(err.Error(), "multiple run errors") { + utils.Red.Println("\n❌ Chaos Experiment already exists") + os.Exit(1) + } + } else { + utils.White_B.Print("\n❌ Chaos Experiment/" + chaosExperimentRequest.Name + " failed to be created: " + err.Error()) + os.Exit(1) + } + } + + //Successful creation + utils.White_B.Println("\nπŸš€ Chaos Experiment successfully createdπŸŽ‰") + }, +} + +func init() { + CreateCmd.AddCommand(workflowCmd) + + workflowCmd.Flags().String("project-id", "", "Set the project-id to create Chaos Experiment for the particular project. To see the projects, apply litmusctl get projects") + workflowCmd.Flags().String("chaos-infra-id", "", "Set the chaos-delegate-id to create Chaos Experiment for the particular Chaos Infrastructure. To see the Chaos Infrastructures, apply litmusctl get chaos-infra") + workflowCmd.Flags().StringP("file", "f", "", "The manifest file for the Chaos Experiment") + workflowCmd.Flags().StringP("description", "d", "", "The Description for the Chaos Experiment") +} diff --git a/pkg/cmd/create/workflow.go b/pkg/cmd/create/workflow.go deleted file mode 100644 index c54de784..00000000 --- a/pkg/cmd/create/workflow.go +++ /dev/null @@ -1,141 +0,0 @@ -/* -Copyright Β© 2021 The LitmusChaos Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -package create - -import ( - "fmt" - "os" - "strings" - "time" - - "github.com/gorhill/cronexpr" - "github.com/litmuschaos/litmusctl/pkg/apis" - "github.com/litmuschaos/litmusctl/pkg/utils" - - "github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model" - - "github.com/spf13/cobra" -) - -// workflowCmd represents the project command -var workflowCmd = &cobra.Command{ - Use: "chaos-scenario", - Short: `Create a Chaos Scenario - Example: - #create a Chaos Scenario - litmusctl create chaos-scenario -f chaos-scenario.yaml --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --chaos-delegate-id="1c9c5801-8789-4ac9-bf5f-32649b707a5c" - - Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag - `, - Run: func(cmd *cobra.Command, args []string) { - - // Fetch user credentials - credentials, err := utils.GetCredentials(cmd) - utils.PrintError(err) - - var chaosWorkFlowRequest model.ChaosWorkFlowRequest - - workflowManifest, err := cmd.Flags().GetString("file") - utils.PrintError(err) - - chaosWorkFlowRequest.ProjectID, err = cmd.Flags().GetString("project-id") - utils.PrintError(err) - - // Handle blank input for project ID - if chaosWorkFlowRequest.ProjectID == "" { - utils.White_B.Print("\nEnter the Project ID: ") - fmt.Scanln(&chaosWorkFlowRequest.ProjectID) - - if chaosWorkFlowRequest.ProjectID == "" { - utils.Red.Println("β›” Project ID can't be empty!!") - os.Exit(1) - } - } - - chaosWorkFlowRequest.ClusterID, err = cmd.Flags().GetString("chaos-delegate-id") - utils.PrintError(err) - - // Handle blank input for Chaos Delegate ID - if chaosWorkFlowRequest.ClusterID == "" { - utils.White_B.Print("\nEnter the Chaos Delegate ID: ") - fmt.Scanln(&chaosWorkFlowRequest.ClusterID) - - if chaosWorkFlowRequest.ClusterID == "" { - utils.Red.Println("β›” Chaos Delegate ID can't be empty!!") - os.Exit(1) - } - } - - // Perform authorization - userDetails, err := apis.GetProjectDetails(credentials) - utils.PrintError(err) - var editAccess = false - var project apis.Project - for _, p := range userDetails.Data.Projects { - if p.ID == chaosWorkFlowRequest.ProjectID { - project = p - } - } - for _, member := range project.Members { - if (member.UserID == userDetails.Data.ID) && (member.Role == "Owner" || member.Role == "Editor") { - editAccess = true - } - } - if !editAccess { - utils.Red.Println("β›” User doesn't have edit access to the project!!") - os.Exit(1) - } - - // Parse workflow manifest and populate chaosWorkFlowInput - err = utils.ParseWorkflowManifest(workflowManifest, &chaosWorkFlowRequest) - if err != nil { - utils.Red.Println("❌ Error parsing Chaos Scenario manifest: " + err.Error()) - os.Exit(1) - } - - // Make API call - createdWorkflow, err := apis.CreateWorkflow(chaosWorkFlowRequest, credentials) - if err != nil { - if (createdWorkflow.Data == apis.CreatedChaosWorkflow{}) { - if strings.Contains(err.Error(), "multiple write errors") { - utils.Red.Println("\n❌ Chaos Scenario/" + chaosWorkFlowRequest.WorkflowName + " already exists") - os.Exit(1) - } else { - utils.White_B.Print("\n❌ Chaos Scenario/" + chaosWorkFlowRequest.WorkflowName + " failed to be created: " + err.Error()) - os.Exit(1) - } - } - } - - // Successful creation - utils.White_B.Println("\nπŸš€ Chaos Scenario/" + createdWorkflow.Data.CreateChaosWorkflow.WorkflowName + " successfully created πŸŽ‰") - if createdWorkflow.Data.CreateChaosWorkflow.CronSyntax == "" { - utils.White_B.Println("\nThe next run of this Chaos Scenario will be scheduled immediately.") - } else { - utils.White_B.Println( - "\nThe next run of this Chaos Scenario will be scheduled at " + - cronexpr.MustParse(createdWorkflow.Data.CreateChaosWorkflow.CronSyntax).Next(time.Now()).Format("January 2nd 2006, 03:04:05 pm")) - } - }, -} - -func init() { - CreateCmd.AddCommand(workflowCmd) - - workflowCmd.Flags().String("project-id", "", "Set the project-id to create Chaos Scenario for the particular project. To see the projects, apply litmusctl get projects") - workflowCmd.Flags().String("chaos-delegate-id", "", "Set the chaos-delegate-id to create Chaos Scenario for the particular Chaos Delegate. To see the Chaos Delegates, apply litmusctl get chaos-delegates") - workflowCmd.Flags().StringP("file", "f", "", "The manifest file for the Chaos Scenario") -} diff --git a/pkg/cmd/delete/delete.go b/pkg/cmd/delete/delete.go index 16a44d21..00578978 100644 --- a/pkg/cmd/delete/delete.go +++ b/pkg/cmd/delete/delete.go @@ -22,7 +22,7 @@ import ( // deleteCmd represents the delete command var DeleteCmd = &cobra.Command{ Use: "delete", - Short: `Delete resources for LitmusChaos agent plane. + Short: `Delete resources for LitmusChaos Execution plane. Examples: #delete a Chaos Experiment litmusctl delete chaos-experiment c520650e-7cb6-474c-b0f0-4df07b2b025b --project-id=c520650e-7cb6-474c-b0f0-4df07b2b025b diff --git a/pkg/cmd/delete/experiment.go b/pkg/cmd/delete/experiment.go index 8341400c..2dd07131 100644 --- a/pkg/cmd/delete/experiment.go +++ b/pkg/cmd/delete/experiment.go @@ -17,6 +17,7 @@ package delete import ( "fmt" + "github.com/litmuschaos/litmusctl/pkg/apis/experiment" "os" "github.com/litmuschaos/litmusctl/pkg/apis" @@ -90,7 +91,7 @@ var experimentCmd = &cobra.Command{ } // Make API call - deleteExperiment, err := apis.DeleteChaosExperiment(projectID, &experimentID, credentials) + deleteExperiment, err := experiment.DeleteChaosExperiment(projectID, &experimentID, credentials) if err != nil { utils.Red.Println("\n❌ Error in deleting Chaos Experiment: ", err.Error()) os.Exit(1) diff --git a/pkg/cmd/describe/describe.go b/pkg/cmd/describe/describe.go index 5a0e6025..24713d48 100644 --- a/pkg/cmd/describe/describe.go +++ b/pkg/cmd/describe/describe.go @@ -22,7 +22,7 @@ import ( // DescribeCmd represents the describe command var DescribeCmd = &cobra.Command{ Use: "describe", - Short: `Describe resources for LitmusChaos agent plane. + Short: `Describe resources for LitmusChaos Execution plane. Examples: #describe a Chaos Experiment litmusctl describe chaos-experiment d861b650-1549-4574-b2ba-ab754058dd04 --project-id="d861b650-1549-4574-b2ba-ab754058dd04" diff --git a/pkg/cmd/describe/experiment.go b/pkg/cmd/describe/experiment.go index 9e0c51db..0d3936dc 100644 --- a/pkg/cmd/describe/experiment.go +++ b/pkg/cmd/describe/experiment.go @@ -17,10 +17,10 @@ package describe import ( "fmt" + "github.com/litmuschaos/litmusctl/pkg/apis/experiment" "os" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - "github.com/litmuschaos/litmusctl/pkg/apis" "github.com/litmuschaos/litmusctl/pkg/utils" "github.com/spf13/cobra" "sigs.k8s.io/yaml" @@ -66,7 +66,7 @@ var experimentCmd = &cobra.Command{ describeExperimentRequest.ExperimentIDs = append(describeExperimentRequest.ExperimentIDs, &experimentID) - experiment, err := apis.GetExperimentList(pid, describeExperimentRequest, credentials) + experiment, err := experiment.GetExperimentList(pid, describeExperimentRequest, credentials) utils.PrintError(err) if len(experiment.Data.ListExperimentDetails.Experiments) == 0 { diff --git a/pkg/cmd/disconnect/disconnect.go b/pkg/cmd/disconnect/disconnect.go index 33824b56..b77807d8 100644 --- a/pkg/cmd/disconnect/disconnect.go +++ b/pkg/cmd/disconnect/disconnect.go @@ -22,7 +22,7 @@ import ( // disconnectCmd represents the disconnect command var DisconnectCmd = &cobra.Command{ Use: "disconnect", - Short: `Disconnect resources for LitmusChaos infra plane. + Short: `Disconnect resources for LitmusChaos Execution plane. Examples: #disconnect a Chaos Infra litmusctl disconnect chaos-infra c520650e-7cb6-474c-b0f0-4df07b2b025b --project-id=c520650e-7cb6-474c-b0f0-4df07b2b025b diff --git a/pkg/cmd/disconnect/infra.go b/pkg/cmd/disconnect/infra.go index e1ef83d3..927cce88 100644 --- a/pkg/cmd/disconnect/infra.go +++ b/pkg/cmd/disconnect/infra.go @@ -17,6 +17,7 @@ package disconnect import ( "fmt" + "github.com/litmuschaos/litmusctl/pkg/apis/infrastructure" "os" "strings" @@ -92,7 +93,7 @@ var infraCmd = &cobra.Command{ // Make API call //var agentIDs []*string //agentIDs = append(agentIDs, &infraID) - disconnectedInfra, err := apis.DisconnectInfra(projectID, infraID, credentials) + disconnectedInfra, err := infrastructure.DisconnectInfra(projectID, infraID, credentials) if err != nil { utils.Red.Println("\n❌ Error in disconnecting Chaos Infrastructure: ", err.Error()) os.Exit(1) diff --git a/pkg/cmd/get/experimentruns.go b/pkg/cmd/get/experimentruns.go index 104a2d35..7c1fee25 100644 --- a/pkg/cmd/get/experimentruns.go +++ b/pkg/cmd/get/experimentruns.go @@ -17,20 +17,20 @@ package get import ( "fmt" + "github.com/litmuschaos/litmusctl/pkg/apis/experiment" "os" "strconv" "text/tabwriter" "time" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - "github.com/litmuschaos/litmusctl/pkg/apis" "github.com/litmuschaos/litmusctl/pkg/utils" "github.com/spf13/cobra" ) // experimentRunsCmd represents the Chaos Experiments runs command var experimentRunsCmd = &cobra.Command{ - Use: "chaos-experiments-runs", + Use: "chaos-experiment-runs", Short: "Display list of Chaos Experiments runs within the project", Long: `Display list of Chaos Experiments runs within the project`, Run: func(cmd *cobra.Command, args []string) { @@ -59,7 +59,7 @@ var experimentRunsCmd = &cobra.Command{ listExperimentRunsRequest.Pagination.Limit, _ = cmd.Flags().GetInt("count") } - experimentRuns, err := apis.GetExperimentRunsList(projectID, listExperimentRunsRequest, credentials) + experimentRuns, err := experiment.GetExperimentRunsList(projectID, listExperimentRunsRequest, credentials) utils.PrintError(err) output, err := cmd.Flags().GetString("output") @@ -75,7 +75,7 @@ var experimentRunsCmd = &cobra.Command{ case "": writer := tabwriter.NewWriter(os.Stdout, 4, 8, 1, '\t', 0) - utils.White_B.Fprintln(writer, "CHAOS EXPERIMENT RUN ID\tSTATUS\tRESILIENCY SCORE\tCHAOS EXPERIMENT ID\tCHAOS EXPERIMENT NAME\tTARGET CHAOS INFRA\tUPDATED AT\tEXECUTED DATA") + utils.White_B.Fprintln(writer, "CHAOS EXPERIMENT RUN ID\tSTATUS\tRESILIENCY SCORE\tCHAOS EXPERIMENT ID\tCHAOS EXPERIMENT NAME\tTARGET CHAOS INFRA\tUPDATED AT\tUPDATED BY") for _, experimentRun := range experimentRuns.Data.ListExperimentRunDetails.ExperimentRuns { @@ -106,7 +106,7 @@ var experimentRunsCmd = &cobra.Command{ func init() { GetCmd.AddCommand(experimentRunsCmd) - experimentRunsCmd.Flags().String("project-id", "", "Set the project-id to list Chaos Experimentss from the particular project. To see the projects, apply litmusctl get projects") + experimentRunsCmd.Flags().String("project-id", "", "Set the project-id to list Chaos Experiments from the particular project. To see the projects, apply litmusctl get projects") experimentRunsCmd.Flags().Int("count", 30, "Set the count of Chaos Experiments runs to display. Default value is 30") experimentRunsCmd.Flags().BoolP("all", "A", false, "Set to true to display all Chaos Experiments runs") diff --git a/pkg/cmd/get/experiments.go b/pkg/cmd/get/experiments.go index 08b6d4b7..c1612d7d 100644 --- a/pkg/cmd/get/experiments.go +++ b/pkg/cmd/get/experiments.go @@ -17,13 +17,13 @@ package get import ( "fmt" + "github.com/litmuschaos/litmusctl/pkg/apis/experiment" "os" "text/tabwriter" "time" "github.com/gorhill/cronexpr" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - "github.com/litmuschaos/litmusctl/pkg/apis" "github.com/litmuschaos/litmusctl/pkg/utils" "github.com/spf13/cobra" ) @@ -63,7 +63,7 @@ var experimentsCmd = &cobra.Command{ utils.PrintError(err) listExperimentRequest.Filter.InfraName = &infraName - experiments, err := apis.GetExperimentList(pid, listExperimentRequest, credentials) + experiments, err := experiment.GetExperimentList(pid, listExperimentRequest, credentials) utils.PrintError(err) output, err := cmd.Flags().GetString("output") diff --git a/pkg/cmd/get/infra.go b/pkg/cmd/get/infra.go index f2870482..d2aa97b8 100644 --- a/pkg/cmd/get/infra.go +++ b/pkg/cmd/get/infra.go @@ -18,10 +18,10 @@ package get import ( "fmt" models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmusctl/pkg/apis/infrastructure" "os" "text/tabwriter" - "github.com/litmuschaos/litmusctl/pkg/apis" "github.com/litmuschaos/litmusctl/pkg/utils" "github.com/spf13/cobra" ) @@ -48,7 +48,7 @@ var InfraCmd = &cobra.Command{ } } - infras, err := apis.GetInfraList(credentials, projectID, models.ListInfraRequest{}) + infras, err := infrastructure.GetInfraList(credentials, projectID, models.ListInfraRequest{}) utils.PrintError(err) output, err := cmd.Flags().GetString("output") @@ -64,7 +64,7 @@ var InfraCmd = &cobra.Command{ case "": writer := tabwriter.NewWriter(os.Stdout, 4, 8, 1, '\t', 0) - utils.White_B.Fprintln(writer, "CHAOS INFRASTRUCTURE ID \tCHAOS INFRASTRUCTURE NAME\tSTATUS\t") + utils.White_B.Fprintln(writer, "CHAOS INFRASTRUCTURE ID \tCHAOS INFRASTRUCTURE NAME\tSTATUS\tCHAOS ENVIRONMENT ID\t") for _, infra := range infras.Data.ListInfraDetails.Infras { var status string @@ -80,7 +80,7 @@ var InfraCmd = &cobra.Command{ //} else { // isRegistered = "NOT REGISTERED" //} - utils.White.Fprintln(writer, infra.InfraID+"\t"+infra.Name+"\t"+status+"\t") + utils.White.Fprintln(writer, infra.InfraID+"\t"+infra.Name+"\t"+status+"\t"+infra.EnvironmentID+"\t") //+isRegistered+"\t" } writer.Flush() diff --git a/pkg/cmd/run/experiment.go b/pkg/cmd/run/experiment.go index 95b0d048..3beacb37 100644 --- a/pkg/cmd/run/experiment.go +++ b/pkg/cmd/run/experiment.go @@ -18,6 +18,7 @@ package run import ( "fmt" "github.com/litmuschaos/litmusctl/pkg/apis" + "github.com/litmuschaos/litmusctl/pkg/apis/experiment" "github.com/litmuschaos/litmusctl/pkg/utils" "github.com/spf13/cobra" "os" @@ -89,9 +90,9 @@ var experimentCmd = &cobra.Command{ } // Make API call - runExperiment, err := apis.RunExperiment(pid, eid, credentials) + runExperiment, err := experiment.RunExperiment(pid, eid, credentials) if err != nil { - if (runExperiment.Data == apis.RunExperimentDetails{}) { + if (runExperiment.Data == experiment.RunExperimentDetails{}) { if strings.Contains(err.Error(), "multiple run errors") { utils.Red.Println("\n❌ Chaos Experiment already exists") os.Exit(1) @@ -103,7 +104,7 @@ var experimentCmd = &cobra.Command{ } //Successful run - utils.White_B.Println("\nπŸš€ Chaos Experiment successfully running πŸŽ‰") + utils.White_B.Println("\nπŸš€ Chaos Experiment running successfully πŸŽ‰") }, } diff --git a/pkg/cmd/run/run.go b/pkg/cmd/run/run.go index d82def85..2506fdb8 100644 --- a/pkg/cmd/run/run.go +++ b/pkg/cmd/run/run.go @@ -7,7 +7,7 @@ import ( // createCmd represents the create command var RunCmd = &cobra.Command{ Use: "run", - Short: `Runs Experiment for LitmusChaos agent plane. + Short: `Runs Experiment for LitmusChaos Execution plane. Examples: #Run a Chaos Experiment diff --git a/pkg/cmd/save/experiment.go b/pkg/cmd/save/experiment.go index 470e696a..c2022d2c 100644 --- a/pkg/cmd/save/experiment.go +++ b/pkg/cmd/save/experiment.go @@ -18,6 +18,7 @@ package save import ( "fmt" models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmusctl/pkg/apis/experiment" "os" "strings" //"time" @@ -78,27 +79,6 @@ var experimentCmd = &cobra.Command{ } } - //chaosExperimentRequest.ID, err = cmd.Flags().GetString("experiment-id") - //utils.PrintError(err) - // - ////Handle blank input for Chaos EnvironmentID - //if chaosExperimentRequest.ID == "" { - // utils.White_B.Print("\nEnter the Chaos Experiment ID: ") - // fmt.Scanln(&chaosExperimentRequest.ID) - // - // if chaosExperimentRequest.ID == "" { - // utils.Red.Println("β›” Chaos Experiment ID can't be empty!!") - // os.Exit(1) - // } - //} - - chaosExperimentRequest.Name, err = cmd.Flags().GetString("name") - utils.PrintError(err) - if chaosExperimentRequest.Name == "" { - utils.White_B.Print("\nExperiment Name: ") - fmt.Scanln(&chaosExperimentRequest.Name) - } - chaosExperimentRequest.Description, err = cmd.Flags().GetString("description") utils.PrintError(err) if chaosExperimentRequest.Description == "" { @@ -132,11 +112,13 @@ var experimentCmd = &cobra.Command{ utils.Red.Println("❌ Error parsing Chaos Experiment manifest: " + err.Error()) os.Exit(1) } + + // Generate ExperimentID from the ExperimentName chaosExperimentRequest.ID = utils.GenerateNameID(chaosExperimentRequest.Name) // Make API call - saveExperiment, err := apis.SaveExperiment(pid, chaosExperimentRequest, credentials) + saveExperiment, err := experiment.SaveExperiment(pid, chaosExperimentRequest, credentials) if err != nil { - if (saveExperiment.Data == apis.SavedExperimentDetails{}) { + if (saveExperiment.Data == experiment.SavedExperimentDetails{}) { if strings.Contains(err.Error(), "multiple write errors") { utils.Red.Println("\n❌ Chaos Experiment/" + chaosExperimentRequest.Name + " already exists") os.Exit(1) @@ -148,7 +130,7 @@ var experimentCmd = &cobra.Command{ } //Successful creation - utils.White_B.Println("\nπŸš€ Chaos Experiment/" + chaosExperimentRequest.Name + " successfully created πŸŽ‰") + utils.White_B.Println("\nπŸš€ Chaos Experiment/" + chaosExperimentRequest.Name + " successfully saved πŸŽ‰") }, } @@ -157,8 +139,6 @@ func init() { experimentCmd.Flags().String("project-id", "", "Set the project-id to create Chaos Experiment for the particular project. To see the projects, apply litmusctl get projects") experimentCmd.Flags().String("chaos-infra-id", "", "Set the chaos-infra-id to create Chaos Experiment for the particular Chaos Infrastructure. To see the Chaos Infrastructures, apply litmusctl get chaos-infra") - experimentCmd.Flags().String("experiment-id", "", "Set the environment-id to create Chaos Experiment for the particular Chaos Infrastructure. To see the Chaos Infrastructures, apply litmusctl get chaos-infra") experimentCmd.Flags().StringP("file", "f", "", "The manifest file for the Chaos Experiment") - experimentCmd.Flags().StringP("name", "n", "", "The Name for the Chaos Experiment") experimentCmd.Flags().StringP("description", "d", "", "The Description for the Chaos Experiment") } diff --git a/pkg/cmd/save/save.go b/pkg/cmd/save/save.go index 6c092f4b..16d276fd 100644 --- a/pkg/cmd/save/save.go +++ b/pkg/cmd/save/save.go @@ -22,7 +22,7 @@ import ( // createCmd represents the create command var SaveCmd = &cobra.Command{ Use: "save", - Short: `Save Experiment for LitmusChaos agent plane. + Short: `Save Experiment for LitmusChaos Execution plane. Examples: #Save a Chaos Experiment from a file diff --git a/pkg/infra/ops.go b/pkg/ops/ops.go similarity index 96% rename from pkg/infra/ops.go rename to pkg/ops/ops.go index d74c6aca..53fcd61e 100644 --- a/pkg/infra/ops.go +++ b/pkg/ops/ops.go @@ -13,11 +13,13 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -package infra +package ops import ( "fmt" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmusctl/pkg/apis/environment" + "github.com/litmuschaos/litmusctl/pkg/apis/infrastructure" "os" "strconv" "strings" @@ -28,7 +30,7 @@ import ( "github.com/litmuschaos/litmusctl/pkg/utils" ) -func PrintExistingInfra(infra apis.InfraData) { +func PrintExistingInfra(infra infrastructure.InfraData) { utils.Red.Println("\nChaos Infrastructure with the given name already exists.") // Print Chaos Infra list if existing Chaos Infra name is entered twice utils.White_B.Println("\nConnected Chaos Infrastructure list:") @@ -40,7 +42,7 @@ func PrintExistingInfra(infra apis.InfraData) { utils.White_B.Println("\n❗ Please enter a different name.") } -func PrintExistingEnvironments(env apis.ListEnvironmentData) { +func PrintExistingEnvironments(env environment.ListEnvironmentData) { // Print Chaos EnvironmentID list if Given ID doesn't exist utils.White_B.Println("\nExisting Chaos Environments list:") @@ -114,7 +116,7 @@ INFRA_NAME: } // Check if Chaos Infra with the given name already exists - Infra, err := apis.GetInfraList(c, pid, model.ListInfraRequest{}) + Infra, err := infrastructure.GetInfraList(c, pid, model.ListInfraRequest{}) if err != nil { return types.Infra{}, err } @@ -146,7 +148,7 @@ ENVIRONMENT: } // Check if Chaos Environment with the given name exists - Env, err := apis.GetEnvironmentList(pid, c) + Env, err := environment.GetEnvironmentList(pid, c) if err != nil { return types.Infra{}, err } diff --git a/pkg/infra/platform.go b/pkg/ops/platform.go similarity index 99% rename from pkg/infra/platform.go rename to pkg/ops/platform.go index 8cf0571d..3952c71c 100644 --- a/pkg/infra/platform.go +++ b/pkg/ops/platform.go @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -package infra +package ops import ( "context"