-
Notifications
You must be signed in to change notification settings - Fork 54
feat(workflows): Add delete workflow and describe workflow commands
#77
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
Merged
imrajdas
merged 5 commits into
litmuschaos:master
from
PrayagS:feat-delete-workflow-cmd
Jun 14, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb49766
feat(workflows): Add `delete workflow` command
PrayagS cb26b01
feat(workflows): Add `describe workflow` command
PrayagS 9b0672d
fix(workflows): gofmt fixes
PrayagS 7bca0fd
Added error message
Saranya-jena 76878b5
Merge branch 'master' of https://github.com/litmuschaos/litmusctl int…
Saranya-jena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| `, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| `, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.