/
workflow.go
148 lines (134 loc) · 4.92 KB
/
workflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Package cmd Copyright 2021 VMware, Inc.
SPDX-License-Identifier: BSD-2-Clause
*/
package cmd
import (
"os"
"strings"
"github.com/sammcgeown/vra-cli/pkg/cmd/orchestrator"
"github.com/sammcgeown/vra-cli/pkg/util/helpers"
log "github.com/sirupsen/logrus"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
// getWorkflowCmd represents the workflows command
var getWorkflowCmd = &cobra.Command{
Use: "workflow",
Short: "Get Orchestrator Workflows",
Long: `Get Orchestrator Workflows by ID, Name, Project and Status
# Get only failed workflows:
vra-cli get workflow --status FAILED
# Get an workflow by ID:
vra-cli get workflow --id bb3f6aff-311a-45fe-8081-5845a529068d
# Get Failed workflows in Project "Field Demo" with the name "Learn Code Stream"
vra-cli get workflow --status FAILED --project "Field Demo" --name "Learn Code Stream"`,
Run: func(cmd *cobra.Command, args []string) {
response, err := orchestrator.GetWorkflow(APIClient, id, category, name)
if err != nil {
log.Errorln("Unable to get workflows: ", err)
}
var resultCount = len(response)
if resultCount == 0 {
// No results
log.Infoln("No results found")
} else {
if APIClient.Output == "table" {
// Print result table
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Id", "Name", "Version", "Description", "Category"})
for _, c := range response {
category, _ := orchestrator.GetCategoryByID(APIClient, c.CategoryID)
table.Append([]string{c.ID, c.Name, c.Version, c.Description, category.Path})
}
table.Render()
} else if APIClient.Output == "export" {
// Export the Worfklow
for _, workflow := range response {
err := orchestrator.ExportWorkflow(APIClient, workflow.ID, workflow.Name, exportPath)
if err != nil {
log.Warnln("Unable to export workflow: ", err)
} else {
log.Infoln("Workflow", workflow.Name, "exported")
}
}
} else {
helpers.PrettyPrint(response)
}
}
},
}
// delWorkflowCmd represents the delete workflows command
var delWorkflowCmd = &cobra.Command{
Use: "workflow",
Short: "Delete an Workflow",
Long: `Delete an Workflow with a specific Workflow ID`,
Run: func(cmd *cobra.Command, args []string) {
_, err := orchestrator.DeleteWorkflow(APIClient, id)
if err != nil {
log.Errorln("Unable to delete workflow: ", err)
} else {
log.Infoln("Workflow with ID " + id + " deleted")
}
},
}
// createWorkflowCmd represents the workflows command
var createWorkflowCmd = &cobra.Command{
Use: "workflow",
Short: "Create a Workflow",
Long: `Create a Workflow`,
Run: func(cmd *cobra.Command, args []string) {
// Get the category ID
var CategoryID string
categoryName := (strings.Split(category, "/"))[len(strings.Split(category, "/"))-1]
categories, _ := orchestrator.GetCategoryByName(APIClient, categoryName, "WorkflowCategory")
if len(categories) == 0 {
log.Fatalln("Unable to find category:", categoryName)
} else if len(categories) == 1 {
// Only one category found
log.Debugln("Category found:", categories[0].Name, categories[0].ID)
CategoryID = categories[0].ID
} else {
for _, matchedCategory := range categories {
if matchedCategory.Path == category {
log.Debugln("Category ID:", matchedCategory.ID)
CategoryID = matchedCategory.ID
break
}
}
if CategoryID == "" {
log.Fatalln("Multiple categories found, try using a more specific category - e.g.: path/to/category")
}
}
for _, path := range helpers.GetFilePaths(importPath, ".zip") {
log.Infoln("Importing workflow:", path)
err := orchestrator.ImportWorkflow(APIClient, path, CategoryID)
if err != nil {
log.Errorln("Unable to import workflow: ", err)
} else {
workflow, err := orchestrator.GetWorkflow(APIClient, "", categoryName, name)
if err != nil {
log.Errorln("Workflow imported OK, but I'm unable to get imported workflow details: ", err)
}
log.Infoln("Workflow imported:", workflow[0].Name, "with ID:", workflow[0].ID)
}
}
},
}
func init() {
// Get
getCmd.AddCommand(getWorkflowCmd)
getWorkflowCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the Workflow")
getWorkflowCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the Workflows to list")
getWorkflowCmd.Flags().StringVarP(&category, "category", "c", "", "Filter Workflows by Category")
getWorkflowCmd.Flags().StringVarP(&exportPath, "exportPath", "", "", "Path to export objects - relative or absolute location")
// Delete
deleteCmd.AddCommand(delWorkflowCmd)
delWorkflowCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the Workflow to delete")
delWorkflowCmd.MarkFlagRequired("id")
// Create
createCmd.AddCommand(createWorkflowCmd)
createWorkflowCmd.Flags().StringVarP(&category, "category", "c", "", "Category to import")
createWorkflowCmd.Flags().StringVar(&importPath, "importPath", "", "Path to the zip file, or folder containing zip files, to import")
createWorkflowCmd.MarkFlagRequired("importPath")
}