This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
recipe.go
180 lines (151 loc) · 6.29 KB
/
recipe.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package cli
import (
"strconv"
"strings"
"time"
"net/http"
"encoding/base64"
log "github.com/Sirupsen/logrus"
"github.com/hortonworks/cb-cli/cli/utils"
"github.com/hortonworks/cb-cli/client_cloudbreak/v3_workspace_id_recipes"
"github.com/hortonworks/cb-cli/models_cloudbreak"
"github.com/urfave/cli"
)
var recipeHeader = []string{"Name", "Description", "Execution Type"}
type recipeOut struct {
Name string `json:"Name" yaml:"Name"`
Description string `json:"Description" yaml:"Description"`
ExecutionType string `json:"ExecutionType" yaml:"ExecutionType"`
}
type recipeOutJsonDescribe struct {
*recipeOut
Content string `json:"RecipeTextAsBase64" yaml:"RecipeTextAsBase64"`
ID string `json:"ID" yaml:"ID"`
}
type recipeOutTableDescribe struct {
*recipeOut
ID string `json:"ID" yaml:"ID"`
}
func (r *recipeOut) DataAsStringArray() []string {
return []string{r.Name, r.Description, r.ExecutionType}
}
func (r *recipeOutJsonDescribe) DataAsStringArray() []string {
return append(r.recipeOut.DataAsStringArray(), r.ID)
}
func (r *recipeOutTableDescribe) DataAsStringArray() []string {
return append(r.recipeOut.DataAsStringArray(), r.ID)
}
func CreateRecipeFromUrl(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
log.Infof("[CreateRecipeFromUrl] creating recipe from a URL")
cbClient := NewCloudbreakHTTPClientFromContext(c)
urlLocation := c.String(FlURL.Name)
createRecipeImpl(
cbClient.Cloudbreak.V3WorkspaceIDRecipes,
c.Int64(FlWorkspaceOptional.Name),
c.String(FlName.Name),
c.String(FlDescriptionOptional.Name),
getExecutionType(c.String(FlExecutionType.Name)),
base64.StdEncoding.EncodeToString(utils.ReadContentFromURL(urlLocation, new(http.Client))))
}
func CreateRecipeFromFile(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
log.Infof("[CreateRecipeFromFile] creating recipe from a file")
cbClient := NewCloudbreakHTTPClientFromContext(c)
fileLocation := c.String(FlFile.Name)
createRecipeImpl(
cbClient.Cloudbreak.V3WorkspaceIDRecipes,
c.Int64(FlWorkspaceOptional.Name),
c.String(FlName.Name),
c.String(FlDescriptionOptional.Name),
getExecutionType(c.String(FlExecutionType.Name)),
base64.StdEncoding.EncodeToString(utils.ReadFile(fileLocation)))
}
func getExecutionType(executionType string) string {
switch strings.ToLower(executionType) {
case "pre-ambari-start":
return "PRE_AMBARI_START"
case "post-ambari-start":
return "POST_AMBARI_START"
case "post-cluster-install":
return "POST_CLUSTER_INSTALL"
case "pre-termination":
return "PRE_TERMINATION"
default:
utils.LogErrorMessageAndExit("Recipe type not supported")
panic(3)
}
}
type recipeClient interface {
CreateRecipeInWorkspace(params *v3_workspace_id_recipes.CreateRecipeInWorkspaceParams) (*v3_workspace_id_recipes.CreateRecipeInWorkspaceOK, error)
}
func createRecipeImpl(client recipeClient, workspaceID int64, name string, description string, executionType string, recipeContent string) *models_cloudbreak.RecipeResponse {
defer utils.TimeTrack(time.Now(), "create recipe")
recipeRequest := &models_cloudbreak.RecipeRequest{
Name: name,
Description: &description,
Content: recipeContent,
RecipeType: &executionType,
}
var recipe *models_cloudbreak.RecipeResponse
log.Infof("[createRecipeImpl] sending create public recipe request")
resp, err := client.CreateRecipeInWorkspace(v3_workspace_id_recipes.NewCreateRecipeInWorkspaceParams().WithWorkspaceID(workspaceID).WithBody(recipeRequest))
if err != nil {
utils.LogErrorAndExit(err)
}
recipe = resp.Payload
log.Infof("[createRecipeImpl] recipe created: %s (id: %d)", recipe.Name, recipe.ID)
return recipe
}
func DescribeRecipe(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "describe recipe")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
workspaceID := c.Int64(FlWorkspaceOptional.Name)
resp, err := cbClient.Cloudbreak.V3WorkspaceIDRecipes.GetRecipeInWorkspace(v3_workspace_id_recipes.NewGetRecipeInWorkspaceParams().WithWorkspaceID(workspaceID).WithName(c.String(FlName.Name)))
if err != nil {
utils.LogErrorAndExit(err)
}
recipe := resp.Payload
if output.Format != "table" {
output.Write(append(recipeHeader, "ContentAsBase64", "ID"), &recipeOutJsonDescribe{&recipeOut{recipe.Name, *recipe.Description, *recipe.RecipeType}, recipe.Content, strconv.FormatInt(recipe.ID, 10)})
} else {
output.Write(append(recipeHeader, "ID"), &recipeOutTableDescribe{&recipeOut{recipe.Name, *recipe.Description, *recipe.RecipeType}, strconv.FormatInt(recipe.ID, 10)})
}
}
func DeleteRecipe(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "delete recipe")
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
workspaceID := c.Int64(FlWorkspaceOptional.Name)
log.Infof("[DeleteRecipe] sending delete recipe request with name: %s", name)
if _, err := cbClient.Cloudbreak.V3WorkspaceIDRecipes.DeleteRecipeInWorkspace(v3_workspace_id_recipes.NewDeleteRecipeInWorkspaceParams().WithWorkspaceID(workspaceID).WithName(name)); err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[DeleteRecipe] recipe deleted, name: %s", name)
}
func ListRecipes(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list recipes")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
workspaceID := c.Int64(FlWorkspaceOptional.Name)
listRecipesImpl(cbClient.Cloudbreak.V3WorkspaceIDRecipes, output.WriteList, workspaceID)
}
type getPublicsRecipeClient interface {
ListRecipesByWorkspace(*v3_workspace_id_recipes.ListRecipesByWorkspaceParams) (*v3_workspace_id_recipes.ListRecipesByWorkspaceOK, error)
}
func listRecipesImpl(client getPublicsRecipeClient, writer func([]string, []utils.Row), workspaceID int64) {
log.Infof("[listRecipesImpl] sending recipe list request")
recipesResp, err := client.ListRecipesByWorkspace(v3_workspace_id_recipes.NewListRecipesByWorkspaceParams().WithWorkspaceID(workspaceID))
if err != nil {
utils.LogErrorAndExit(err)
}
tableRows := []utils.Row{}
for _, recipe := range recipesResp.Payload {
tableRows = append(tableRows, &recipeOut{*recipe.Name, utils.SafeStringConvert(recipe.Description), *recipe.RecipeType})
}
writer(recipeHeader, tableRows)
}