-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathupdate.go
More file actions
267 lines (228 loc) · 8.79 KB
/
update.go
File metadata and controls
267 lines (228 loc) · 8.79 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package update
import (
"context"
"fmt"
"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/types"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
)
const (
intakeIdArg = "INTAKE_ID"
// Top-level flags
displayNameFlag = "display-name"
runnerIdFlag = "runner-id"
descriptionFlag = "description"
labelsFlag = "labels"
// Catalog flags
catalogURIFlag = "catalog-uri"
catalogWarehouseFlag = "catalog-warehouse"
catalogNamespaceFlag = "catalog-namespace"
catalogTableNameFlag = "catalog-table-name"
// Auth flags
catalogAuthTypeFlag = "catalog-auth-type"
dremioTokenEndpointFlag = "dremio-token-endpoint" //nolint:gosec // false positive
dremioPatFlag = "dremio-pat"
)
type inputModel struct {
*globalflags.GlobalFlagModel
// Main
IntakeId string
DisplayName *string
RunnerId *string
Description *string
Labels *map[string]string
// Catalog
CatalogURI *string
CatalogWarehouse *string
CatalogNamespace *string
CatalogTableName *string
// Auth
CatalogAuthType *string
DremioTokenEndpoint *string
DremioToken *string
}
func NewCmd(p *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("update %s", intakeIdArg),
Short: "Updates an Intake",
Long: "Updates an Intake. Only the specified fields are updated.",
Args: args.SingleArg(intakeIdArg, utils.ValidateUUID),
Example: examples.Build(
examples.NewExample(
`Update the display name of an Intake with ID "xxx"`,
`$ stackit beta intake update xxx --runner-id yyy --display-name new-intake-name`),
examples.NewExample(
`Update the catalog details for an Intake with ID "xxx"`,
`$ stackit beta intake update xxx --runner-id yyy --catalog-uri "http://new.uri" --catalog-warehouse "new-warehouse"`),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(p.Printer, cmd, args)
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
}
// Configure API client
apiClient, err := client.ConfigureClient(p.Printer, p.CliVersion)
if err != nil {
return err
}
// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("update Intake: %w", err)
}
// Wait for async operation, if async mode not enabled
if !model.Async {
err := spinner.Run(p.Printer, "Updating STACKIT Intake Runner instance", func() error {
_, err = wait.CreateOrUpdateIntakeWaitHandler(ctx, apiClient, model.ProjectId, model.Region, model.IntakeId).WaitWithContext(ctx)
return err
})
if err != nil {
return fmt.Errorf("wait for STACKIT Instance creation: %w", err)
}
}
return outputResult(p.Printer, model, projectLabel, resp)
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
// Top-level flags
cmd.Flags().String(displayNameFlag, "", "Display name")
cmd.Flags().Var(flags.UUIDFlag(), runnerIdFlag, "The UUID of the Intake Runner to use")
cmd.Flags().String(descriptionFlag, "", "Description")
cmd.Flags().StringToString(labelsFlag, nil, `Labels in key=value format, separated by commas. Example: --labels "key1=value1,key2=value2".`)
// Catalog flags
cmd.Flags().String(catalogURIFlag, "", "The URI to the Iceberg catalog endpoint")
cmd.Flags().String(catalogWarehouseFlag, "", "The Iceberg warehouse to connect to")
cmd.Flags().String(catalogNamespaceFlag, "", "The namespace to which data shall be written")
cmd.Flags().String(catalogTableNameFlag, "", "The table name to identify the table in Iceberg")
// Auth flags
cmd.Flags().String(catalogAuthTypeFlag, "", "Authentication type for the catalog (e.g., 'none', 'dremio')")
cmd.Flags().String(dremioTokenEndpointFlag, "", "Dremio OAuth 2.0 token endpoint URL")
cmd.Flags().String(dremioPatFlag, "", "Dremio personal access token")
err := flags.MarkFlagsRequired(cmd, runnerIdFlag)
cobra.CheckErr(err)
}
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
intakeId := inputArgs[0]
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &cliErr.ProjectIdError{}
}
model := &inputModel{
GlobalFlagModel: globalFlags,
IntakeId: intakeId,
DisplayName: flags.FlagToStringPointer(p, cmd, displayNameFlag),
RunnerId: flags.FlagToStringPointer(p, cmd, runnerIdFlag),
Description: flags.FlagToStringPointer(p, cmd, descriptionFlag),
Labels: flags.FlagToStringToStringPointer(p, cmd, labelsFlag),
CatalogURI: flags.FlagToStringPointer(p, cmd, catalogURIFlag),
CatalogWarehouse: flags.FlagToStringPointer(p, cmd, catalogWarehouseFlag),
CatalogNamespace: flags.FlagToStringPointer(p, cmd, catalogNamespaceFlag),
CatalogTableName: flags.FlagToStringPointer(p, cmd, catalogTableNameFlag),
CatalogAuthType: flags.FlagToStringPointer(p, cmd, catalogAuthTypeFlag),
DremioTokenEndpoint: flags.FlagToStringPointer(p, cmd, dremioTokenEndpointFlag),
DremioToken: flags.FlagToStringPointer(p, cmd, dremioPatFlag),
}
// Check if any optional flag was provided
if model.DisplayName == nil && model.Description == nil && model.Labels == nil &&
model.CatalogURI == nil && model.CatalogWarehouse == nil && model.CatalogNamespace == nil &&
model.CatalogTableName == nil && model.CatalogAuthType == nil &&
model.DremioTokenEndpoint == nil && model.DremioToken == nil {
return nil, &cliErr.EmptyUpdateError{}
}
p.DebugInputModel(model)
return model, nil
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *intake.APIClient) intake.ApiUpdateIntakeRequest {
req := apiClient.UpdateIntake(ctx, model.ProjectId, model.Region, model.IntakeId)
payload := intake.UpdateIntakePayload{
IntakeRunnerId: model.RunnerId, // This is required by the API
DisplayName: model.DisplayName,
Description: model.Description,
Labels: model.Labels,
}
// Build catalog patch payload only if catalog-related flags are set
catalogPatch := &intake.IntakeCatalogPatch{}
catalogNeedsPatching := false
if model.CatalogURI != nil {
catalogPatch.Uri = model.CatalogURI
catalogNeedsPatching = true
}
if model.CatalogWarehouse != nil {
catalogPatch.Warehouse = model.CatalogWarehouse
catalogNeedsPatching = true
}
if model.CatalogNamespace != nil {
catalogPatch.Namespace = model.CatalogNamespace
catalogNeedsPatching = true
}
if model.CatalogTableName != nil {
catalogPatch.TableName = model.CatalogTableName
catalogNeedsPatching = true
}
// Build auth patch payload only if auth-related flags are set
authPatch := &intake.CatalogAuthPatch{}
authNeedsPatching := false
if model.CatalogAuthType != nil {
authType := intake.CatalogAuthType(*model.CatalogAuthType)
authPatch.Type = &authType
authNeedsPatching = true
}
dremioPatch := &intake.DremioAuthPatch{}
dremioNeedsPatching := false
if model.DremioTokenEndpoint != nil {
dremioPatch.TokenEndpoint = model.DremioTokenEndpoint
dremioNeedsPatching = true
}
if model.DremioToken != nil {
dremioPatch.PersonalAccessToken = model.DremioToken
dremioNeedsPatching = true
}
if dremioNeedsPatching {
authPatch.Dremio = dremioPatch
authNeedsPatching = true
}
if authNeedsPatching {
catalogPatch.Auth = authPatch
catalogNeedsPatching = true
}
if catalogNeedsPatching {
payload.Catalog = catalogPatch
}
req = req.UpdateIntakePayload(payload)
return req
}
func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *intake.IntakeResponse) error {
return p.OutputResult(model.OutputFormat, resp, func() error {
if resp == nil {
p.Outputf("Updated Intake for project %q, but no intake ID was returned.\n", projectLabel)
return nil
}
operationState := "Updated"
if model.Async {
operationState = "Triggered update of"
}
p.Outputf("%s Intake for project %q. Intake ID: %s\n", operationState, projectLabel, utils.PtrString(resp.Id))
return nil
})
}