Skip to content

Commit

Permalink
delete probe command
Browse files Browse the repository at this point in the history
Signed-off-by: Shivam Purohit <shivampurohit900@gmail.com>
  • Loading branch information
shivam-Purohit committed Apr 12, 2024
1 parent 6b276da commit a59bb6f
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/apis/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,49 @@ func ListProbeRequest(pid string, probetypes []*models.ProbeType, cred types.Cre
return ListProbeResponse{}, errors.New("Unmatched status code:" + string(bodyBytes))
}
}

func DeleteProbeRequest(pid string, probeid string, cred types.Credentials) (DeleteProbeResponse, error) {
var gqlReq DeleteProbeGQLRequest
gqlReq.Query = DeleteProbeQuery
gqlReq.Variables.ProjectID = pid
gqlReq.Variables.ProbeName = probeid

query, err := json.Marshal(gqlReq)
if err != nil {
return DeleteProbeResponse{}, errors.New("Error in deleting 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 DeleteProbeResponse{}, errors.New("Error in deleting probe" + err.Error())
}

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

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

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

}
19 changes: 19 additions & 0 deletions pkg/apis/probe/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ type ListProbeResponse struct {
type ListProbeResponseData struct {
Probes []model.Probe `json:"listProbes"`
}
type DeleteProbeGQLRequest struct {
Query string `json:"query"`
Variables struct {
ProbeName string `json:"probeName"`
ProjectID string `json:"projectID"`
} `json:"variables"`
}

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

type DeleteProbeResponseData struct {
DeleteProbe bool `json:"deleteProbe"`
}
3 changes: 3 additions & 0 deletions pkg/cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var DeleteCmd = &cobra.Command{
#delete a Chaos Environment
litmusctl delete chaos-environment --project-id=8adf62d5-64f8-4c66-ab53-63729db9dd9a --environment-id=environmentexample
#delete a Probe
litmusctl delete probe --project-id=8adf62d5-64f8-4c66-ab53-63729db9dd9a --probe-id=exampleprobe
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
}
129 changes: 129 additions & 0 deletions pkg/cmd/delete/probe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
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 delete

import (
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/apis/probe"
"github.com/litmuschaos/litmusctl/pkg/utils"
"os"

"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

var probeCmd = &cobra.Command{
Use: "probe",
Short: `Delete a Probe
Example:
#delete a Probe
litmusctl delete probe --project-id=c520650e-7cb6-474c-b0f0-4df07b2b025b --probe-id="example"
Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,

Run: func(cmd *cobra.Command, args []string) {

// Fetch user credentials
credentials, err := utils.GetCredentials(cmd)
utils.PrintError(err)

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

// Handle blank input for project ID

if projectID == "" {
prompt := promptui.Prompt{
Label: "Enter the Project ID",
}
result, err := prompt.Run()
if err != nil {
utils.Red.Println("⛔ Error:", err)
os.Exit(1)
}
projectID = result
}
probeID, err := cmd.Flags().GetString("probe-id")
// Handle blank input for Probe ID
if probeID == "" {
prompt := promptui.Prompt{
Label: "Enter the Probe ID",
}
IDinput, err := prompt.Run()
if err != nil {
utils.Red.Println("⛔ Error:", err)
os.Exit(1)
}
probeID = IDinput
}

// Perform authorization
userDetails, err := apis.GetProjectDetails(credentials)
utils.PrintError(err)
var editAccess = false
var project apis.Project
for _, p := range userDetails.Data.Projects {
if p.ID == projectID {
project = p
}
}
for _, member := range project.Members {
if (member.UserID == userDetails.Data.ID) && (member.Role == "Owner" || member.Role == "Editor") {
editAccess = true
}
}
if !editAccess {
utils.Red.Println("⛔ User doesn't have edit access to the project!!")
os.Exit(1)
}

// confirm before deletion

prompt := promptui.Prompt{
Label: "Are you sure you want to delete this Probe? (y/n)",
AllowEdit: true,
}
result, err := prompt.Run()
if err != nil {
utils.Red.Println("⛔ Error:", err)
os.Exit(1)
}

if result != "y" {
utils.White_B.Println("\n❌ Probe was not deleted.")
os.Exit(0)
}

// Make API call
deleteProbe, err := probe.DeleteProbeRequest(projectID, probeID, credentials)
if err != nil {
utils.Red.Println("\n❌ Error in deleting Probe: ", err.Error())
os.Exit(1)
}

if deleteProbe.Data.DeleteProbe {
utils.White_B.Println("\n🚀 Probe was successfully deleted.")
} else {
utils.White_B.Println("\n❌ Failed to delete Probe. Please check if the ID is correct or not.")
}
},
}

func init() {
DeleteCmd.AddCommand(probeCmd)

probeCmd.Flags().String("project-id", "", "Set the project-id to delete Probe for the particular project. To see the projects, apply litmusctl get projects")
probeCmd.Flags().String("probe-id", "", "Set the probe-id to delete that particular probe. To see the probes, apply litmusctl get probes")
}

0 comments on commit a59bb6f

Please sign in to comment.