Skip to content

Commit

Permalink
add get/list probe command (litmuschaos#204)
Browse files Browse the repository at this point in the history
* fix

Signed-off-by: Shivam Purohit <[email protected]>

* add get/list probe command

Signed-off-by: Shivam Purohit <[email protected]>

* fix:fmt

Signed-off-by: Shivam Purohit <[email protected]>

* get-probes:add non-interactivte flag support

Signed-off-by: Shivam Purohit <[email protected]>

* fix:non-iteractive mode

Signed-off-by: Shivam Purohit <[email protected]>

* add probe-id flag for probe details

Signed-off-by: Shivam Purohit <[email protected]>

* improve logic for probe-id flag

Signed-off-by: Shivam Purohit <[email protected]>

* fix:filter query on listprobe command

Signed-off-by: Shivam Purohit <[email protected]>

* fix:fmt

Signed-off-by: Shivam Purohit <[email protected]>

* get probe:add more details

Signed-off-by: Shivam Purohit <[email protected]>

* refactor get probe command into smaller funcs

Signed-off-by: Shivam Purohit <[email protected]>

* fix:double printing of break statement get probe

Signed-off-by: Shivam Purohit <[email protected]>

* fix:fmt

Signed-off-by: Shivam Purohit <[email protected]>

* remove toolchain

Signed-off-by: Shivam Purohit <[email protected]>

* fix swap created by/at

Signed-off-by: Shivam Purohit <[email protected]>

* refactor the get probe cmd

Signed-off-by: Shivam Purohit <[email protected]>

* remove unnecessary fields from get probe query

Signed-off-by: Shivam Purohit <[email protected]>

* add referencedBy in output get probe

Signed-off-by: Shivam Purohit <[email protected]>

---------

Signed-off-by: Shivam Purohit <[email protected]>
  • Loading branch information
shivam-Purohit authored Mar 29, 2024
1 parent 1a69cc5 commit 6b276da
Show file tree
Hide file tree
Showing 7 changed files with 577 additions and 261 deletions.
14 changes: 9 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/litmuschaos/litmusctl

go 1.20
go 1.21


require (
github.com/argoproj/argo-workflows/v3 v3.5.5
Expand Down Expand Up @@ -34,7 +35,6 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic v0.7.0 // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
Expand All @@ -51,13 +51,17 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.18.0 // indirect
Expand Down
300 changes: 44 additions & 256 deletions go.sum

Large diffs are not rendered by default.

107 changes: 107 additions & 0 deletions pkg/apis/probe/probe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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))
}

}

func ListProbeRequest(pid string, probetypes []*models.ProbeType, cred types.Credentials) (ListProbeResponse, error) {
var gqlReq ListProbeGQLRequest
gqlReq.Query = ListProbeQuery
gqlReq.Variables.ProjectID = pid
gqlReq.Variables.Filter = models.ProbeFilterInput{
Type: probetypes,
}

query, err := json.Marshal(gqlReq)
if err != nil {
return ListProbeResponse{}, errors.New("Error in listing probes" + err.Error())
}
resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: cred.ServerEndpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
string(types.Post),
)

if err != nil {
return ListProbeResponse{}, errors.New("Error in listing probes" + err.Error())
}

bodyBytes, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return ListProbeResponse{}, errors.New("Error in listing probes" + err.Error())
}

if resp.StatusCode == http.StatusOK {
var listProbeResponse ListProbeResponse
err = json.Unmarshal(bodyBytes, &listProbeResponse)
if err != nil {
return ListProbeResponse{}, errors.New("Error in listing probes" + err.Error())
}
if len(listProbeResponse.Errors) > 0 {
return ListProbeResponse{}, errors.New(listProbeResponse.Errors[0].Message)
}
return listProbeResponse, nil

} else {
return ListProbeResponse{}, errors.New("Unmatched status code:" + string(bodyBytes))
}
}
77 changes: 77 additions & 0 deletions pkg/apis/probe/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package probe

const (
ListProbeQuery = `query ListProbes($projectID: ID!, $probeNames: [ID!], $filter: ProbeFilterInput) {
listProbes(projectID: $projectID, probeNames: $probeNames, filter: $filter) {
name
type
createdAt
createdBy{
username
}
}
}
`
GetProbeQuery = `query getProbe($projectID: ID!, $probeName: ID!) {
getProbe(projectID: $projectID, probeName: $probeName) {
name
description
type
infrastructureType
kubernetesHTTPProperties{
probeTimeout
interval
retry
attempt
probePollingInterval
initialDelay
evaluationTimeout
stopOnFailure
}
kubernetesCMDProperties{
probeTimeout
interval
retry
attempt
probePollingInterval
initialDelay
evaluationTimeout
stopOnFailure
}
k8sProperties {
probeTimeout
interval
retry
attempt
probePollingInterval
initialDelay
evaluationTimeout
stopOnFailure
}
promProperties {
probeTimeout
interval
retry
attempt
probePollingInterval
initialDelay
evaluationTimeout
stopOnFailure
}
createdAt
createdBy{
username
}
updatedAt
updatedBy{
username
}
tags
}
}
`
DeleteProbeQuery = `mutation deleteProbe($probeName: ID!, $projectID: ID!) {
deleteProbe(probeName: $probeName, projectID: $projectID)
}
`
)
43 changes: 43 additions & 0 deletions pkg/apis/probe/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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"`
}

type ListProbeGQLRequest struct {
Query string `json:"query"`
Variables struct {
ProjectID string `json:"projectID"`
Filter model.ProbeFilterInput `json:"filter"`
} `json:"variables"`
}

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

type ListProbeResponseData struct {
Probes []model.Probe `json:"listProbes"`
}
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 list of Probes in a Project
litmusctl get probes --project-id=""
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
}
Loading

0 comments on commit 6b276da

Please sign in to comment.