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/experiment.go b/pkg/cmd/describe/experiment.go index 61973b90..cf8a3d7d 100644 --- a/pkg/cmd/describe/experiment.go +++ b/pkg/cmd/describe/experiment.go @@ -102,22 +102,27 @@ var experimentCmd = &cobra.Command{ 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") } diff --git a/pkg/cmd/describe/probe.go b/pkg/cmd/describe/probe.go new file mode 100644 index 00000000..63b780f8 --- /dev/null +++ b/pkg/cmd/describe/probe.go @@ -0,0 +1,126 @@ +// /* +// 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 ( + "bytes" + "encoding/json" + "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" + "sigs.k8s.io/yaml" +) + +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 + + probeOutput, err := cmd.Flags().GetString("output") + utils.PrintError(err) + + switch probeOutput { + case "json": + jsonData, _ := yaml.YAMLToJSON([]byte(getProbeYAMLData)) + var prettyJSON bytes.Buffer + err = json.Indent(&prettyJSON, jsonData, "", " ") + if err != nil { + utils.Red.Println("❌ Error formatting JSON: " + err.Error()) + os.Exit(1) + } + + fmt.Println(prettyJSON.String()) + + default: + utils.PrintInYamlFormat(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 ") + probeCmd.Flags().String("output", "", "Set the output format for the probe") +}