Skip to content
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
79 changes: 79 additions & 0 deletions pkg/apis/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,82 @@ func GetWorkflowRunsList(in model.ListWorkflowRunsRequest, cred types.Credential
return WorkflowRunsListData{}, errors.New("Error while fetching the chaos workflow runs")
}
}

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

type DeleteChaosWorkflowDetails struct {
IsDeleted bool `json:"deleteChaosWorkflow"`
}

type DeleteChaosWorkflowGraphQLRequest struct {
Query string `json:"query"`
Variables struct {
ProjectID string `json:"projectID"`
WorkflowID *string `json:"workflowID"`
WorkflowRunID *string `json:"workflowRunID"`
} `json:"variables"`
}

// DeleteChaosWorkflow sends GraphQL API request for deleting a given Chaos Workflow.
func DeleteChaosWorkflow(projectID string, workflowID *string, cred types.Credentials) (DeleteChaosWorkflowData, error) {

var gqlReq DeleteChaosWorkflowGraphQLRequest
var err error

gqlReq.Query = `mutation deleteChaosWorkflow($projectID: String!, $workflowID: String, $workflowRunID: String) {
deleteChaosWorkflow(
projectID: $projectID
workflowID: $workflowID
workflowRunID: $workflowRunID
)
}`
gqlReq.Variables.ProjectID = projectID
gqlReq.Variables.WorkflowID = workflowID
var workflow_run_id string = ""
gqlReq.Variables.WorkflowRunID = &workflow_run_id

query, err := json.Marshal(gqlReq)
if err != nil {
return DeleteChaosWorkflowData{}, err
}

resp, err := SendRequest(
SendRequestParams{
Endpoint: cred.Endpoint + utils.GQLAPIPath,
Token: cred.Token,
},
query,
string(types.Post),
)
if err != nil {
return DeleteChaosWorkflowData{}, err
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return DeleteChaosWorkflowData{}, err
}

if resp.StatusCode == http.StatusOK {
var deletedWorkflow DeleteChaosWorkflowData
err = json.Unmarshal(bodyBytes, &deletedWorkflow)
if err != nil {
return DeleteChaosWorkflowData{}, err
}

if len(deletedWorkflow.Errors) > 0 {
return DeleteChaosWorkflowData{}, errors.New(deletedWorkflow.Errors[0].Message)
}

return deletedWorkflow, nil
} else {
return DeleteChaosWorkflowData{}, errors.New("Error while deleting the chaos workflow")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also we can return custom error message instead of directly returning error.

}
32 changes: 32 additions & 0 deletions pkg/cmd/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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/spf13/cobra"
)

// deleteCmd represents the delete command
var DeleteCmd = &cobra.Command{
Use: "delete",
Short: `Delete resources for LitmusChaos agent plane.
Examples:
#delete a chaos workflow
litmusctl delete workflow c520650e-7cb6-474c-b0f0-4df07b2b025b --project-id=c520650e-7cb6-474c-b0f0-4df07b2b025b

Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
}
111 changes: 111 additions & 0 deletions pkg/cmd/delete/workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
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 (
"fmt"
"os"

"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/utils"

"github.com/spf13/cobra"
)

// workflowCmd represents the workflow command
var workflowCmd = &cobra.Command{
Use: "workflow",
Short: `Delete a Chaos Workflow
Example:
#delete a chaos workflow
litmusctl delete workflow c520650e-7cb6-474c-b0f0-4df07b2b025b --project-id=c520650e-7cb6-474c-b0f0-4df07b2b025b

Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
Args: cobra.ExactArgs(1),
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 == "" {
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)
}
}

workflowID := args[0]

// Handle blank input for workflow ID
if workflowID == "" {
utils.White_B.Print("\nEnter the Workflow ID: ")
fmt.Scanln(&workflowID)

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

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

// Make API call
deletedWorkflow, err := apis.DeleteChaosWorkflow(projectID, &workflowID, credentials)
if err != nil {
utils.Red.Println("\n❌ Error in deleting ChaosWorkflow: ", err.Error())
os.Exit(1)
}

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

func init() {
DeleteCmd.AddCommand(workflowCmd)

workflowCmd.Flags().String("project-id", "", "Set the project-id to create workflow for the particular project. To see the projects, apply litmusctl get projects")
}
32 changes: 32 additions & 0 deletions pkg/cmd/describe/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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 (
"github.com/spf13/cobra"
)

// DescribeCmd represents the describe command
var DescribeCmd = &cobra.Command{
Use: "describe",
Short: `Describe resources for LitmusChaos agent plane.
Examples:
#describe a ChaosWorkflow
litmusctl describe workflow d861b650-1549-4574-b2ba-ab754058dd04 --project-id="d861b650-1549-4574-b2ba-ab754058dd04"

Note: The default location of the config file is $HOME/.litmusconfig, and can be overridden by a --config flag
`,
}
83 changes: 83 additions & 0 deletions pkg/cmd/describe/workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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 (
"fmt"
"os"

"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/utils"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)

// workflowCmd represents the workflow command
var workflowCmd = &cobra.Command{
Use: "workflow",
Short: "Describe a ChaosWorkflow within the project",
Long: `Describe a ChaosWorkflow within the project`,
Run: func(cmd *cobra.Command, args []string) {
credentials, err := utils.GetCredentials(cmd)
utils.PrintError(err)

var describeWorkflowRequest model.ListWorkflowsRequest

describeWorkflowRequest.ProjectID, err = cmd.Flags().GetString("project-id")
utils.PrintError(err)

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

for describeWorkflowRequest.ProjectID == "" {
utils.Red.Println("⛔ Project ID can't be empty!!")
os.Exit(1)
}
}

workflowID := args[0]
// Handle blank input for workflow ID
if workflowID == "" {
utils.White_B.Print("\nEnter the Workflow ID: ")
fmt.Scanln(&workflowID)

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

describeWorkflowRequest.WorkflowIDs = append(describeWorkflowRequest.WorkflowIDs, &workflowID)

workflow, err := apis.GetWorkflowList(describeWorkflowRequest, credentials)
utils.PrintError(err)

yamlManifest, err := yaml.JSONToYAML([]byte(workflow.Data.ListWorkflowDetails.Workflows[0].WorkflowManifest))
if err != nil {
utils.Red.Println("❌ Error parsing workflow manifest: " + err.Error())
os.Exit(1)
}
utils.PrintInYamlFormat(string(yamlManifest))
},
}

func init() {
DescribeCmd.AddCommand(workflowCmd)

workflowCmd.Flags().String("project-id", "", "Set the project-id to list workflows from the particular project. To see the projects, apply litmusctl get projects")
}
4 changes: 4 additions & 0 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"os"

"github.com/litmuschaos/litmusctl/pkg/cmd/connect"
"github.com/litmuschaos/litmusctl/pkg/cmd/delete"
"github.com/litmuschaos/litmusctl/pkg/cmd/describe"
"github.com/litmuschaos/litmusctl/pkg/cmd/disconnect"
"github.com/litmuschaos/litmusctl/pkg/cmd/upgrade"
"github.com/litmuschaos/litmusctl/pkg/cmd/version"
Expand Down Expand Up @@ -62,6 +64,8 @@ func init() {
rootCmd.AddCommand(get.GetCmd)
rootCmd.AddCommand(connect.ConnectCmd)
rootCmd.AddCommand(disconnect.DisconnectCmd)
rootCmd.AddCommand(delete.DeleteCmd)
rootCmd.AddCommand(describe.DescribeCmd)
rootCmd.AddCommand(version.VersionCmd)
rootCmd.AddCommand(upgrade.UpgradeCmd)

Expand Down