Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Probes describe command #215

Merged
merged 5 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkg/apis/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
5 changes: 5 additions & 0 deletions pkg/apis/probe/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/apis/probe/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
3 changes: 3 additions & 0 deletions pkg/cmd/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`,
}
28 changes: 17 additions & 11 deletions pkg/cmd/describe/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
126 changes: 126 additions & 0 deletions pkg/cmd/describe/probe.go
Original file line number Diff line number Diff line change
@@ -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")
}
Loading