-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcreate.go
More file actions
169 lines (146 loc) · 5.96 KB
/
create.go
File metadata and controls
169 lines (146 loc) · 5.96 KB
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
package create
import (
"context"
"fmt"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/intake"
"github.com/stackitcloud/stackit-sdk-go/services/intake/wait"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/intake/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
)
const (
displayNameFlag = "display-name"
maxMessageSizeKiBFlag = "max-message-size-kib"
maxMessagesPerHourFlag = "max-messages-per-hour"
descriptionFlag = "description"
labelFlag = "labels"
)
// inputModel struct holds all the input parameters for the command
type inputModel struct {
*globalflags.GlobalFlagModel
DisplayName *string
MaxMessageSizeKiB *int64
MaxMessagesPerHour *int64
Description *string
Labels *map[string]string
}
func NewCmd(p *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a new Intake Runner",
Long: "Creates a new Intake Runner.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create a new Intake Runner with a display name and message capacity limits`,
`$ stackit beta intake runner create --display-name my-runner --max-message-size-kib 1000 --max-messages-per-hour 5000`),
examples.NewExample(
`Create a new Intake Runner with a description and labels`,
`$ stackit beta intake runner create --display-name my-runner --max-message-size-kib 1000 --max-messages-per-hour 5000 --description "Main runner for production" --labels="env=prod,team=billing"`),
),
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := context.Background()
model, err := parseInput(p.Printer, cmd)
if err != nil {
return err
}
// Configure API client
apiClient, err := client.ConfigureClient(p.Printer, p.CliVersion)
if err != nil {
return err
}
projectLabel, err := projectname.GetProjectName(ctx, p.Printer, p.CliVersion, cmd)
if err != nil {
p.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
}
prompt := fmt.Sprintf("Are you sure you want to create an Intake Runner for project %q?", projectLabel)
err = p.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}
// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("create Intake Runner: %w", err)
}
// Wait for async operation, if async mode not enabled
if !model.Async {
err := spinner.Run(p.Printer, "Creating STACKIT Intake Runner", func() error {
_, err = wait.CreateOrUpdateIntakeRunnerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, resp.GetId()).WaitWithContext(ctx)
return err
})
if err != nil {
return fmt.Errorf("wait for STACKIT Intake Runner creation: %w", err)
}
}
return outputResult(p.Printer, model, projectLabel, resp)
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().String(displayNameFlag, "", "Display name")
cmd.Flags().Int64(maxMessageSizeKiBFlag, 0, "Maximum message size in KiB")
cmd.Flags().Int64(maxMessagesPerHourFlag, 0, "Maximum number of messages per hour")
cmd.Flags().String(descriptionFlag, "", "Description")
cmd.Flags().StringToString(labelFlag, nil, "Labels in key=value format, separated by commas. Example: --labels \"key1=value1,key2=value2\"")
err := flags.MarkFlagsRequired(cmd, displayNameFlag, maxMessageSizeKiBFlag, maxMessagesPerHourFlag)
cobra.CheckErr(err)
}
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &cliErr.ProjectIdError{}
}
model := inputModel{
GlobalFlagModel: globalFlags,
DisplayName: flags.FlagToStringPointer(p, cmd, displayNameFlag),
MaxMessageSizeKiB: flags.FlagToInt64Pointer(p, cmd, maxMessageSizeKiBFlag),
MaxMessagesPerHour: flags.FlagToInt64Pointer(p, cmd, maxMessagesPerHourFlag),
Description: flags.FlagToStringPointer(p, cmd, descriptionFlag),
Labels: flags.FlagToStringToStringPointer(p, cmd, labelFlag),
}
p.DebugInputModel(model)
return &model, nil
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *intake.APIClient) intake.ApiCreateIntakeRunnerRequest {
// Start building the request by calling the base method with path parameters
req := apiClient.CreateIntakeRunner(ctx, model.ProjectId, model.Region)
// Create the payload struct with data from the input model
payload := intake.CreateIntakeRunnerPayload{
DisplayName: model.DisplayName,
MaxMessageSizeKiB: model.MaxMessageSizeKiB,
MaxMessagesPerHour: model.MaxMessagesPerHour,
Description: model.Description,
Labels: model.Labels,
}
// Attach the payload to the request builder
req = req.CreateIntakeRunnerPayload(payload)
return req
}
func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *intake.IntakeRunnerResponse) error {
return p.OutputResult(model.OutputFormat, resp, func() error {
if resp == nil {
p.Outputf("Triggered creation of Intake Runner for project %q, but no runner ID was returned.\n", projectLabel)
return nil
}
operationState := "Created"
if model.Async {
operationState = "Triggered creation of"
}
p.Outputf("%s Intake Runner for project %q. Runner ID: %s\n", operationState, projectLabel, utils.PtrString(resp.Id))
return nil
})
}