From 5a9c55c23f4dd60fcc7d330a95c788aa88c285b4 Mon Sep 17 00:00:00 2001 From: Shivam Purohit Date: Fri, 12 Apr 2024 09:57:11 +0530 Subject: [PATCH 1/5] describe probe command Signed-off-by: Shivam Purohit --- pkg/apis/probe/probe.go | 44 +++++++++++++++ pkg/apis/probe/query.go | 5 ++ pkg/apis/probe/types.go | 20 +++++++ pkg/cmd/describe/describe.go | 3 + pkg/cmd/describe/probe.go | 104 +++++++++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 pkg/cmd/describe/probe.go diff --git a/pkg/apis/probe/probe.go b/pkg/apis/probe/probe.go index c87a3a3e..b6adcee1 100644 --- a/pkg/apis/probe/probe.go +++ b/pkg/apis/probe/probe.go @@ -151,3 +151,47 @@ func DeleteProbeRequest(pid string, probeid string, cred types.Credentials) (Del } } + +func GetProbeYAMLRequest(pid string, request models.GetProbeYAMLRequest, cred types.Credentials) (GetProbeYAMLResponse, error) { + var gqlReq GetProbeYAMLGQLRequest + gqlReq.Query = GetProbeYAMLQuery + gqlReq.Variables.ProjectID = pid + gqlReq.Variables.Request = request + + query, err := json.Marshal(gqlReq) + if err != nil { + return GetProbeYAMLResponse{}, errors.New("Error in getting probe details" + err.Error()) + } + resp, err := apis.SendRequest( + apis.SendRequestParams{ + Endpoint: cred.ServerEndpoint + utils.GQLAPIPath, + Token: cred.Token, + }, + query, + string(types.Post), + ) + if err != nil { + return GetProbeYAMLResponse{}, errors.New("Error in getting probe details" + err.Error()) + } + + bodyBytes, err := io.ReadAll(resp.Body) + defer resp.Body.Close() + if err != nil { + return GetProbeYAMLResponse{}, errors.New("Error in getting probe details" + err.Error()) + } + + if resp.StatusCode == http.StatusOK { + var getProbeYAMLResponse GetProbeYAMLResponse + err = json.Unmarshal(bodyBytes, &getProbeYAMLResponse) + if err != nil { + return GetProbeYAMLResponse{}, errors.New("Error in getting probes details" + err.Error()) + } + if len(getProbeYAMLResponse.Errors) > 0 { + return GetProbeYAMLResponse{}, errors.New(getProbeYAMLResponse.Errors[0].Message) + } + return getProbeYAMLResponse, nil + + } else { + return GetProbeYAMLResponse{}, errors.New("Unmatched status code:" + string(bodyBytes)) + } +} diff --git a/pkg/apis/probe/query.go b/pkg/apis/probe/query.go index b7e5cce1..b2c5df80 100644 --- a/pkg/apis/probe/query.go +++ b/pkg/apis/probe/query.go @@ -70,6 +70,11 @@ const ( } } ` + GetProbeYAMLQuery = `query getProbeYAML($projectID: ID!, $request: GetProbeYAMLRequest!) { + getProbeYAML(projectID: $projectID, request: $request) + } + ` + DeleteProbeQuery = `mutation deleteProbe($probeName: ID!, $projectID: ID!) { deleteProbe(probeName: $probeName, projectID: $projectID) } diff --git a/pkg/apis/probe/types.go b/pkg/apis/probe/types.go index 99219122..c3564778 100644 --- a/pkg/apis/probe/types.go +++ b/pkg/apis/probe/types.go @@ -60,3 +60,23 @@ type DeleteProbeResponse struct { type DeleteProbeResponseData struct { DeleteProbe bool `json:"deleteProbe"` } + +type GetProbeYAMLGQLRequest struct { + Query string `json:"query"` + Variables struct { + ProjectID string `json:"projectID"` + Request model.GetProbeYAMLRequest `json:"request"` + } `json:"variables"` +} + +type GetProbeYAMLResponse struct { + Errors []struct { + Message string `json:"message"` + Path []string `json:"path"` + } `json:"errors"` + Data GetProbeYAMLResponseData `json:"data"` +} + +type GetProbeYAMLResponseData struct { + GetProbeYAML string `json:"getProbeYAML"` +} diff --git a/pkg/cmd/describe/describe.go b/pkg/cmd/describe/describe.go index 24713d48..33fda543 100644 --- a/pkg/cmd/describe/describe.go +++ b/pkg/cmd/describe/describe.go @@ -27,6 +27,9 @@ var DescribeCmd = &cobra.Command{ #describe a Chaos Experiment litmusctl describe chaos-experiment d861b650-1549-4574-b2ba-ab754058dd04 --project-id="d861b650-1549-4574-b2ba-ab754058dd04" + #describe a Probe + litmusctl describe probe --project-id="d861b650-1549-4574-b2ba-ab754058dd04" --probe-id="exampleProbe" + Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag `, } diff --git a/pkg/cmd/describe/probe.go b/pkg/cmd/describe/probe.go new file mode 100644 index 00000000..22dfcdbd --- /dev/null +++ b/pkg/cmd/describe/probe.go @@ -0,0 +1,104 @@ +// /* +// 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 describe + +import ( + "fmt" + "os" + + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + apis "github.com/litmuschaos/litmusctl/pkg/apis/probe" + "github.com/litmuschaos/litmusctl/pkg/utils" + "github.com/manifoldco/promptui" + "github.com/spf13/cobra" +) + +var probeCmd = &cobra.Command{ + Use: "probe", + Short: "Describe a Probe within the project", + Long: `Describe a Probe within the project`, + Run: func(cmd *cobra.Command, args []string) { + credentials, err := utils.GetCredentials(cmd) + utils.PrintError(err) + + var getProbeYAMLRequest model.GetProbeYAMLRequest + + pid, err := cmd.Flags().GetString("project-id") + utils.PrintError(err) + + if pid == "" { + prompt := promptui.Prompt{ + Label: "Enter the Project ID", + } + result, err := prompt.Run() + if err != nil { + utils.PrintError(err) + os.Exit(1) + } + pid = result + } + + var probeID string + probeID, err = cmd.Flags().GetString("probe-id") + utils.PrintError(err) + // Handle blank input for Probe ID + + if probeID == "" { + utils.White_B.Print("\nEnter the Probe ID: ") + fmt.Scanln(&probeID) + + if probeID == "" { + utils.Red.Println("⛔ Probe ID can't be empty!!") + os.Exit(1) + } + } + getProbeYAMLRequest.ProbeName = probeID + + probeMode, err := cmd.Flags().GetString("mode") + utils.PrintError(err) + + if probeMode == "" { + prompt := promptui.Select{ + Label: "Please select the probe mode ?", + Items: []string{"SOT", "EOT", "Edge", "Continuous", "OnChaos"}, + } + _, option, err := prompt.Run() + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + probeMode = option + fmt.Printf("You chose %q\n", option) + } + getProbeYAMLRequest.Mode = model.Mode(probeMode) + getProbeYAML, err := apis.GetProbeYAMLRequest(pid, getProbeYAMLRequest, credentials) + if err != nil { + utils.Red.Println(err) + os.Exit(1) + } + getProbeYAMLData := getProbeYAML.Data.GetProbeYAML + utils.White_B.Println(getProbeYAMLData) + + }, +} + +func init() { + DescribeCmd.AddCommand(probeCmd) + probeCmd.Flags().String("project-id", "", "Set the project-id to get Probe details from the particular project. To see the projects, apply litmusctl get projects") + probeCmd.Flags().String("probe-id", "", "Set the probe-id to get the Probe details in Yaml format") + probeCmd.Flags().String("mode", "", "Set the mode for the probes from SOT/EOT/Edge/Continuous/OnChaos ") +} From 70c39a8f7dfcbf1b716089c9c65796295e30b1a4 Mon Sep 17 00:00:00 2001 From: Shivam Purohit Date: Sat, 13 Apr 2024 13:25:16 +0530 Subject: [PATCH 2/5] add output flag for exp describe Signed-off-by: Shivam Purohit --- pkg/cmd/describe/experiment.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/pkg/cmd/describe/experiment.go b/pkg/cmd/describe/experiment.go index 61973b90..d510fb5f 100644 --- a/pkg/cmd/describe/experiment.go +++ b/pkg/cmd/describe/experiment.go @@ -101,23 +101,28 @@ var experimentCmd = &cobra.Command{ utils.Red.Println("❌ Error parsing Chaos Experiment manifest: " + err.Error()) os.Exit(1) } - + + expOutput,err := cmd.Flags().GetString("output") + utils.PrintError(err) // Add an output format prompt - prompt := promptui.Select{ - Label: "Select an output format", - Items: []string{"YAML", "JSON"}, - } - i, _, err := prompt.Run() - if err != nil { - utils.PrintError(err) - os.Exit(1) + if expOutput=="" { + prompt := promptui.Select{ + Label: "Select an output format", + Items: []string{"yaml", "json"}, + } + _,value, err := prompt.Run() + expOutput = value + if err != nil { + utils.PrintError(err) + os.Exit(1) + } } - switch i { - case 0: + switch expOutput { + case "yaml": // Output as YAML (default) utils.PrintInYamlFormat(string(yamlManifest)) - case 1: + case "json": // Output as JSON jsonData, err := yaml.YAMLToJSON(yamlManifest) if err != nil { @@ -144,4 +149,5 @@ func init() { DescribeCmd.AddCommand(experimentCmd) experimentCmd.Flags().String("project-id", "", "Set the project-id to list Chaos Experiments from the particular project. To see the projects, apply litmusctl get projects") + experimentCmd.Flags().String("output", "", "Set the output format for the experiment") } From 9c4725ce1900119f6e88697057de7485f216a604 Mon Sep 17 00:00:00 2001 From: Shivam Purohit Date: Sat, 13 Apr 2024 13:29:06 +0530 Subject: [PATCH 3/5] describe probe: add option to change output type Signed-off-by: Shivam Purohit --- pkg/cmd/describe/probe.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/describe/probe.go b/pkg/cmd/describe/probe.go index 22dfcdbd..4c43501e 100644 --- a/pkg/cmd/describe/probe.go +++ b/pkg/cmd/describe/probe.go @@ -17,6 +17,8 @@ package describe import ( + "bytes" + "encoding/json" "fmt" "os" @@ -25,6 +27,7 @@ import ( "github.com/litmuschaos/litmusctl/pkg/utils" "github.com/manifoldco/promptui" "github.com/spf13/cobra" + "sigs.k8s.io/yaml" ) var probeCmd = &cobra.Command{ @@ -91,7 +94,28 @@ var probeCmd = &cobra.Command{ os.Exit(1) } getProbeYAMLData := getProbeYAML.Data.GetProbeYAML - utils.White_B.Println(getProbeYAMLData) + + probeOutput, err := cmd.Flags().GetString("output") + utils.PrintError(err) + + switch probeOutput{ + case "json": + jsonData, _ := yaml.YAMLToJSON([]byte(getProbeYAMLData)) + // utils.PrintInJsonFormat(string(jsonData)) + var prettyJSON bytes.Buffer + err = json.Indent(&prettyJSON, jsonData, "", " ") // Adjust the indentation as needed + if err != nil { + utils.Red.Println("❌ Error formatting JSON: " + err.Error()) + os.Exit(1) + } + + fmt.Println(prettyJSON.String()) + + default: + utils.PrintInYamlFormat(getProbeYAMLData) + } + + }, } @@ -101,4 +125,5 @@ func init() { probeCmd.Flags().String("project-id", "", "Set the project-id to get Probe details from the particular project. To see the projects, apply litmusctl get projects") probeCmd.Flags().String("probe-id", "", "Set the probe-id to get the Probe details in Yaml format") probeCmd.Flags().String("mode", "", "Set the mode for the probes from SOT/EOT/Edge/Continuous/OnChaos ") + probeCmd.Flags().String("output", "", "Set the output format for the probe") } From 9dd3f424ddddf0e33dc792ef6bda8ba8289f2ef7 Mon Sep 17 00:00:00 2001 From: Shivam Purohit Date: Sat, 13 Apr 2024 13:29:48 +0530 Subject: [PATCH 4/5] fix:fmt Signed-off-by: Shivam Purohit --- pkg/cmd/describe/experiment.go | 8 ++++---- pkg/cmd/describe/probe.go | 12 +++++------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/describe/experiment.go b/pkg/cmd/describe/experiment.go index d510fb5f..cf8a3d7d 100644 --- a/pkg/cmd/describe/experiment.go +++ b/pkg/cmd/describe/experiment.go @@ -101,16 +101,16 @@ var experimentCmd = &cobra.Command{ utils.Red.Println("❌ Error parsing Chaos Experiment manifest: " + err.Error()) os.Exit(1) } - - expOutput,err := cmd.Flags().GetString("output") + + expOutput, err := cmd.Flags().GetString("output") utils.PrintError(err) // Add an output format prompt - if expOutput=="" { + if expOutput == "" { prompt := promptui.Select{ Label: "Select an output format", Items: []string{"yaml", "json"}, } - _,value, err := prompt.Run() + _, value, err := prompt.Run() expOutput = value if err != nil { utils.PrintError(err) diff --git a/pkg/cmd/describe/probe.go b/pkg/cmd/describe/probe.go index 4c43501e..facaa72b 100644 --- a/pkg/cmd/describe/probe.go +++ b/pkg/cmd/describe/probe.go @@ -98,7 +98,7 @@ var probeCmd = &cobra.Command{ probeOutput, err := cmd.Flags().GetString("output") utils.PrintError(err) - switch probeOutput{ + switch probeOutput { case "json": jsonData, _ := yaml.YAMLToJSON([]byte(getProbeYAMLData)) // utils.PrintInJsonFormat(string(jsonData)) @@ -108,14 +108,12 @@ var probeCmd = &cobra.Command{ utils.Red.Println("❌ Error formatting JSON: " + err.Error()) os.Exit(1) } - - fmt.Println(prettyJSON.String()) - - default: - utils.PrintInYamlFormat(getProbeYAMLData) - } + fmt.Println(prettyJSON.String()) + default: + utils.PrintInYamlFormat(getProbeYAMLData) + } }, } From 0a08245412caf24b663dfd11779f89099617b9a9 Mon Sep 17 00:00:00 2001 From: Shivam Purohit Date: Mon, 29 Apr 2024 17:17:52 +0530 Subject: [PATCH 5/5] remove comments Signed-off-by: Shivam Purohit --- pkg/cmd/describe/probe.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/cmd/describe/probe.go b/pkg/cmd/describe/probe.go index facaa72b..63b780f8 100644 --- a/pkg/cmd/describe/probe.go +++ b/pkg/cmd/describe/probe.go @@ -101,9 +101,8 @@ var probeCmd = &cobra.Command{ switch probeOutput { case "json": jsonData, _ := yaml.YAMLToJSON([]byte(getProbeYAMLData)) - // utils.PrintInJsonFormat(string(jsonData)) var prettyJSON bytes.Buffer - err = json.Indent(&prettyJSON, jsonData, "", " ") // Adjust the indentation as needed + err = json.Indent(&prettyJSON, jsonData, "", " ") if err != nil { utils.Red.Println("❌ Error formatting JSON: " + err.Error()) os.Exit(1)