-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathupdate.go
More file actions
177 lines (153 loc) · 5.99 KB
/
update.go
File metadata and controls
177 lines (153 loc) · 5.99 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
170
171
172
173
174
175
176
177
package update
import (
"context"
"fmt"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
"github.com/spf13/cobra"
"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"
"github.com/stackitcloud/stackit-sdk-go/services/intake"
"github.com/stackitcloud/stackit-sdk-go/services/intake/wait"
)
const (
runnerIdArg = "RUNNER_ID"
)
const (
displayNameFlag = "display-name"
maxMessageSizeKiBFlag = "max-message-size-kib"
maxMessagesPerHourFlag = "max-messages-per-hour"
descriptionFlag = "description"
labelFlag = "labels"
)
type inputModel struct {
*globalflags.GlobalFlagModel
RunnerId string
DisplayName *string
MaxMessageSizeKiB *int64
MaxMessagesPerHour *int64
Description *string
Labels *map[string]string
}
func NewCmd(p *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("update %s", runnerIdArg),
Short: "Updates an Intake Runner",
Long: "Updates an Intake Runner. Only the specified fields are updated.",
Args: args.SingleArg(runnerIdArg, utils.ValidateUUID),
Example: examples.Build(
examples.NewExample(
`Update the display name of an Intake Runner with ID "xxx"`,
`$ stackit beta intake runner update xxx --display-name "new-runner-name"`),
examples.NewExample(
`Update the message capacity limits for an Intake Runner with ID "xxx"`,
`$ stackit beta intake runner update xxx --max-message-size-kib 1000 --max-messages-per-hour 10000`),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(p.Printer, cmd, args)
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
}
// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("update Intake Runner: %w", err)
}
// Wait for async operation, if async mode not enabled
if !model.Async {
err := spinner.Run(p.Printer, "Updating STACKIT Intake Runner", func() error {
_, err = wait.CreateOrUpdateIntakeRunnerWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.RunnerId).WaitWithContext(ctx)
return err
})
if err != nil {
return fmt.Errorf("wait for STACKIT Intake Runner update: %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. Note: Overall message capacity cannot be decreased.")
cmd.Flags().Int64(maxMessagesPerHourFlag, 0, "Maximum number of messages per hour. Note: Overall message capacity cannot be decreased.")
cmd.Flags().String(descriptionFlag, "", "Description")
cmd.Flags().StringToString(labelFlag, nil, `Labels in key=value format, separated by commas. Example: --labels "key1=value1,key2=value2".`)
}
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
runnerId := inputArgs[0]
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &cliErr.ProjectIdError{}
}
model := inputModel{
GlobalFlagModel: globalFlags,
RunnerId: runnerId,
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),
}
if model.DisplayName == nil && model.MaxMessageSizeKiB == nil && model.MaxMessagesPerHour == nil && model.Description == nil && model.Labels == nil {
return nil, &cliErr.EmptyUpdateError{}
}
p.DebugInputModel(model)
return &model, nil
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *intake.APIClient) intake.ApiUpdateIntakeRunnerRequest {
req := apiClient.UpdateIntakeRunner(ctx, model.ProjectId, model.Region, model.RunnerId)
payload := intake.UpdateIntakeRunnerPayload{}
if model.DisplayName != nil {
payload.DisplayName = model.DisplayName
}
if model.MaxMessageSizeKiB != nil {
payload.MaxMessageSizeKiB = model.MaxMessageSizeKiB
}
if model.MaxMessagesPerHour != nil {
payload.MaxMessagesPerHour = model.MaxMessagesPerHour
}
if model.Description != nil {
payload.Description = model.Description
}
if model.Labels != nil {
payload.Labels = model.Labels
}
req = req.UpdateIntakeRunnerPayload(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 update of Intake Runner for project %q, but no runner ID was returned.\n", projectLabel)
return nil
}
operationState := "Updated"
if model.Async {
operationState = "Triggered update of"
}
p.Outputf("%s Intake Runner for project %q. Runner ID: %s\n", operationState, projectLabel, utils.PtrString(resp.Id))
return nil
})
}