Skip to content

Commit

Permalink
add get probe command
Browse files Browse the repository at this point in the history
Signed-off-by: Shivam Purohit <[email protected]>
  • Loading branch information
shivam-Purohit committed Mar 3, 2024
1 parent 95fad15 commit 5656c8e
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
59 changes: 59 additions & 0 deletions pkg/apis/probe/probe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package probe
import (
"encoding/json"
"errors"
"io"
"net/http"

// 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"
)

func GetProbeRequest(pid string, probeID string, cred types.Credentials)(GetProbeResponse, error){
var gqlReq GetProbeGQLRequest
gqlReq.Query = GetProbeQuery
gqlReq.Variables.ProjectID = pid
gqlReq.Variables.ProbeName = probeID

query, err := json.Marshal(gqlReq)
if err != nil {
return GetProbeResponse{}, errors.New("Error in getting requested probe" + err.Error())
}

resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
string(types.Post),
)

if err != nil {
return GetProbeResponse{}, errors.New("Error in getting requested probe" + err.Error())
}

bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return GetProbeResponse{}, errors.New("Error in getting requested probe" + err.Error())
}

if resp.StatusCode == http.StatusOK {
var getProbeResponse GetProbeResponse
err = json.Unmarshal(bodyBytes, &getProbeResponse)
if err != nil {
return GetProbeResponse{}, errors.New("Error in getting requested probe" + err.Error())
}
if len(getProbeResponse.Errors) > 0 {
return GetProbeResponse{}, errors.New(getProbeResponse.Errors[0].Message)
}
return getProbeResponse, nil

} else {
return GetProbeResponse{}, errors.New("Unmatched status code:" + string(bodyBytes))
}

}
22 changes: 22 additions & 0 deletions pkg/apis/probe/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package probe

const (
GetProbeQuery = `query getProbe($projectID: ID!, $probeName: ID!) {
getProbe(projectID: $projectID, probeName: $probeName) {
name
description
type
infrastructureType
createdAt
createdBy{
username
}
updatedAt
updatedBy{
username
}
tags
}
}
`
)
23 changes: 23 additions & 0 deletions pkg/apis/probe/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package probe

import model "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"

type GetProbeGQLRequest struct {
Query string `json:"query"`
Variables struct {
ProjectID string `json:"projectID"`
ProbeName string `json:"probeName"`
} `json:"variables"`
}

type GetProbeResponse struct {
Errors []struct {
Message string `json:"message"`
Path []string `json:"path"`
} `json:"errors"`
Data GetProbeResponseData `json:"data"`
}

type GetProbeResponseData struct {
GetProbe model.Probe `json:"getProbe"`
}
3 changes: 3 additions & 0 deletions pkg/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var GetCmd = &cobra.Command{
#get list of Chaos Experiment runs
litmusctl get chaos-experiment-runs --project-id=""
#get details of Probe
litmusctl get probe --project-id="" --probe-id=""
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
}
105 changes: 105 additions & 0 deletions pkg/cmd/get/probe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
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 get

import (
"os"
"fmt"
"time"
"text/tabwriter"
"strconv"
"strings"

apis "github.com/litmuschaos/litmusctl/pkg/apis/probe"
"github.com/litmuschaos/litmusctl/pkg/utils"
// "github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

// projectCmd represents the project command
var probesCmd = &cobra.Command{
Use: "probe",
Short: "Display list of probes",
Long: `Display list of probes`,
Run: func(cmd *cobra.Command, args []string) {
credentials, err := utils.GetCredentials(cmd)
utils.PrintError(err)

var projectID string
projectID, err = cmd.Flags().GetString("project-id")
utils.PrintError(err)

if projectID == "" {
utils.White_B.Print("\nEnter the Project ID: ")
fmt.Scanln(&projectID)

if projectID == "" {
utils.Red.Println("⛔ Project ID can't be empty!!")
os.Exit(1)
}
}
var probeID string
probeID, err = cmd.Flags().GetString("probe-id")
utils.PrintError(err)

if probeID == ""{
utils.White_B.Print("\nEnter the Probe ID: ")
fmt.Scanln(&probeID)

if probeID == "" {
utils.Red.Println("⛔ ProbeID ID can't be empty!!")
os.Exit(1)
}
}

probes, err := apis.GetProbeRequest(projectID, probeID, credentials)
probesData := probes.Data.GetProbe
writer := tabwriter.NewWriter(os.Stdout, 30, 8, 0, '\t', tabwriter.AlignRight)
writer.Flush()
intUpdateTime, err := strconv.ParseInt(probesData.UpdatedAt, 10, 64)
if err != nil {
utils.Red.Println("Error converting UpdatedAt to int64:", err)
}
updatedTime := time.Unix(intUpdateTime, 0).String()
intCreatedTime, err := strconv.ParseInt(probesData.CreatedAt, 10, 64)
if err != nil {
utils.Red.Println("Error converting CreatedAt to int64:", err)
}
createdTime := time.Unix(intCreatedTime, 0).String()
defer writer.Flush()
description := ""
if probesData.Description != nil {
description = *probesData.Description
}
utils.White_B.Fprintln(writer, "PROBE DETAILS")
utils.White.Fprintln(writer, "PROBE NAME\t", probesData.Name)
utils.White.Fprintln(writer, "PROBE DESCRIPTION \t", description)
utils.White.Fprintln(writer, "PROBE TYPE \t", probesData.Type)
utils.White.Fprintln(writer, "PROBE INFRASTRUCTURE TYPE \t", probesData.InfrastructureType)
utils.White.Fprintln(writer, "CREATED AT\t", createdTime)
utils.White.Fprintln(writer, "CREATED BY\t", probesData.CreatedBy.Username)
utils.White.Fprintln(writer, "UPDATED AT\t", updatedTime)
utils.White.Fprintln(writer, "UPDATED BY\t", probesData.UpdatedBy.Username)
utils.White.Fprintln(writer, "TAGS\t", strings.Join(probesData.Tags, ", "))
},
}

func init() {
GetCmd.AddCommand(probesCmd)

probesCmd.Flags().String("project-id", "", "Set the project-id to get Probe from a particular project.")
probesCmd.Flags().String("probe-id", "", "Set the probe-id to get details of a single Probe.")
}

0 comments on commit 5656c8e

Please sign in to comment.