Skip to content

Commit d312060

Browse files
author
Juan Jesús Padrón Hernández
committed
fix(sca): pr revisor suggestions
1 parent 8000289 commit d312060

8 files changed

Lines changed: 95 additions & 63 deletions

File tree

‎internal/cmd/alpha/alpha.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
1818
Use: "alpha",
1919
Short: "Contains alpha STACKIT CLI commands",
2020
Long: fmt.Sprintf("%s\n%s",
21-
"Contains beta STACKIT CLI commands.",
21+
"Contains alpha STACKIT CLI commands.",
2222
"The commands under this group are still in an alpha state, and functionality may be incomplete or have breaking changes."),
2323
Args: args.NoArgs,
2424
Run: utils.CmdHelp,
2525
Example: examples.Build(
2626
examples.NewExample(
27-
"See the currently available beta commands",
27+
"See the currently available alpha commands",
2828
"$ stackit alpha --help"),
2929
examples.NewExample(
3030
"Execute a alpha command",

‎internal/cmd/alpha/sca/application/create-from-payload/create_from_payload.go‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *sca.APIClie
105105
}
106106

107107
func outputResult(p *print.Printer, model *inputModel, application *sca.Application) error {
108-
if application == nil {
109-
return fmt.Errorf("create application response is empty")
110-
}
111-
112108
return p.OutputResult(model.OutputFormat, application, func() error {
109+
if application == nil {
110+
return fmt.Errorf("create application response is empty")
111+
}
113112
operationState := "Created"
114113
if model.Async {
115114
operationState = "Triggered creation of"
@@ -122,9 +121,9 @@ func outputResult(p *print.Printer, model *inputModel, application *sca.Applicat
122121
func configureFlags(cmd *cobra.Command) {
123122
cmd.Flags().Var(flags.UUIDFlag(), environmentIDFlag, "Environment ID (uses default environment if not set)")
124123
cmd.Flags().String(nameFlag, "", "Application display name")
125-
cmd.Flags().Var(flags.ReadFromFileFlag(), payloadFlag, `Request payload (JSON). Can be a string or a file path, if prefixed with "@" (example: @./payload.json). If unset, will use a default payload (you can check it by running "stackit sca application generate-payload")`)
124+
cmd.Flags().Var(flags.ReadFromFileFlag(), payloadFlag, `Request payload (JSON). Can be a string or a file path, if prefixed with "@" (example: @./payload.json).`)
126125

127-
cobra.CheckErr(flags.MarkFlagsRequired(cmd, nameFlag))
126+
cobra.CheckErr(flags.MarkFlagsRequired(cmd, nameFlag, payloadFlag))
128127
}
129128

130129
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {

‎internal/cmd/alpha/sca/application/create/create.go‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *sca.APIClie
216216
}
217217

218218
func outputResult(p *print.Printer, model *inputModel, application *sca.Application) error {
219-
if application == nil {
220-
return fmt.Errorf("create application response is empty")
221-
}
222-
223219
return p.OutputResult(model.OutputFormat, application, func() error {
220+
if application == nil {
221+
return fmt.Errorf("create application response is empty")
222+
}
224223
operationState := "Created"
225224
if model.Async {
226225
operationState = "Triggered creation of"
@@ -276,12 +275,6 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
276275
if err := utils.ValidateInstances(instances, instancesFlag); err != nil {
277276
return nil, err
278277
}
279-
if instances < 0 || instances > 10 {
280-
return nil, &errors.FlagValidationError{
281-
Flag: instancesFlag,
282-
Details: "must be an integer between 0 and 10",
283-
}
284-
}
285278

286279
minInstances := flags.FlagWithDefaultToInt32Value(p, cmd, minInstancesFlag)
287280
if err := utils.ValidateInstances(minInstances, minInstancesFlag); err != nil {
@@ -302,8 +295,8 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
302295
}
303296
}
304297

305-
extertalPort := flags.FlagWithDefaultToInt32Value(p, cmd, externalPortFlag)
306-
if err := utils.ValidatePort(extertalPort, externalPortFlag); err != nil {
298+
externalPort := flags.FlagWithDefaultToInt32Value(p, cmd, externalPortFlag)
299+
if err := utils.ValidatePort(externalPort, externalPortFlag); err != nil {
307300
return nil, err
308301
}
309302

@@ -341,7 +334,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
341334
RPS: flags.FlagWithDefaultToInt32Value(p, cmd, rpsFlag),
342335
Concurrency: flags.FlagWithDefaultToInt32Value(p, cmd, concurrencyFlag),
343336
Public: flags.FlagToBoolValue(p, cmd, publicFlag),
344-
ContainerExternalPort: extertalPort,
337+
ContainerExternalPort: externalPort,
345338
}
346339

347340
p.DebugInputModel(model)

‎internal/cmd/alpha/sca/application/describe/describe.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package describe
33
import (
44
"context"
55
"fmt"
6+
"strconv"
67
"strings"
78

89
"github.com/spf13/cobra"
@@ -147,12 +148,12 @@ func outputResult(p *print.Printer, outputFormat string, application *sca.Applic
147148

148149
cpu := "-"
149150
if c.Cpu != nil {
150-
cpu = string(*c.Cpu)
151+
cpu = strconv.Itoa(int(*c.Cpu))
151152
}
152153

153154
memory := "-"
154155
if c.Memory != nil {
155-
memory = string(*c.Memory)
156+
memory = strconv.Itoa(int(*c.Memory))
156157
}
157158

158159
containersTable.AddRow(

‎internal/cmd/alpha/sca/application/generate-payload/generate_payload.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,14 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6868
return err
6969
}
7070

71-
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
72-
if err != nil {
73-
return err
74-
}
75-
7671
var payload *sca.CreateApplicationPayload
7772
if model.ApplicationID == nil {
7873
payload = scautils.GetDefaultPayload()
7974
} else {
75+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
76+
if err != nil {
77+
return err
78+
}
8079
req := apiClient.DefaultAPI.GetApplication(ctx, model.ProjectId, *model.EnvironmentID, *model.ApplicationID)
8180
resp, err := req.Execute()
8281
if err != nil {

‎internal/cmd/alpha/sca/application/update-from-payload/update-from-payload.go‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8888
}
8989
}
9090

91-
outputResult(params.Printer, model, resp)
92-
93-
return nil
91+
return outputResult(params.Printer, model, resp)
9492
},
9593
}
9694

@@ -103,18 +101,26 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *sca.APIClie
103101
UpdateApplicationPayload(*model.Payload)
104102
}
105103

106-
func outputResult(p *print.Printer, model *inputModel, application *sca.Application) {
107-
operationState := "Updated"
108-
if model.Async {
109-
operationState = "Triggered update of"
110-
}
104+
func outputResult(p *print.Printer, model *inputModel, application *sca.Application) error {
105+
return p.OutputResult(model.OutputFormat, application, func() error {
106+
if application == nil {
107+
return fmt.Errorf("update application response is empty")
108+
}
109+
operationState := "Updated"
110+
if model.Async {
111+
operationState = "Triggered update of"
112+
}
111113

112-
p.Outputf("%s application for environment %s. Application ID: %s\n", operationState, application.GetEnvironmentId(), application.GetId())
114+
p.Outputf("%s application for environment %s. Application ID: %s\n", operationState, application.GetEnvironmentId(), application.GetId())
115+
return nil
116+
})
113117
}
114118

115119
func configureFlags(cmd *cobra.Command) {
116120
cmd.Flags().Var(flags.UUIDFlag(), environmentIDFlag, "Environment ID (uses default environment if not set)")
117-
cmd.Flags().Var(flags.ReadFromFileFlag(), payloadFlag, `Request payload (JSON). Can be a string or a file path, if prefixed with "@" (example: @./payload.json). If unset, will use a default payload (you can check it by running "stackit sca application generate-payload")`)
121+
cmd.Flags().Var(flags.ReadFromFileFlag(), payloadFlag, `Request payload (JSON). Can be a string or a file path, if prefixed with "@" (example: @./payload.json).`)
122+
123+
flags.MarkFlagsRequired(cmd, payloadFlag)
118124
}
119125

120126
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
@@ -136,7 +142,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
136142
payload = &sca.UpdateApplicationPayload{}
137143
err := json.Unmarshal([]byte(*payloadValue), payload)
138144
if err != nil {
139-
return nil, fmt.Errorf("enconde payload: %w", err)
145+
return nil, fmt.Errorf("encode payload: %w", err)
140146
}
141147
}
142148

‎internal/cmd/alpha/sca/application/update/update.go‎

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ var scalingTypeFlag = flags.StringEnumFlag(
5050
"scaling-type",
5151
[]string{scalingTypeManual, scalingTypeAuto},
5252
"Scaling type",
53-
flags.StringEnumDefaultValue(scalingTypeManual),
5453
)
5554

5655
type inputModel struct {
@@ -291,7 +290,7 @@ func buildScalingConfig(model *inputModel, current *sca.Application) (*sca.Scali
291290
case scalingTypeAuto:
292291
if model.MinInstances == nil {
293292
return nil, &errors.FlagValidationError{
294-
Flag: instancesFlag,
293+
Flag: minInstancesFlag,
295294
Details: "required flag if replacing scaling type to auto",
296295
}
297296
}
@@ -315,7 +314,7 @@ func buildScalingConfig(model *inputModel, current *sca.Application) (*sca.Scali
315314
}
316315
}
317316

318-
// input's scaling type is ni or equal to current
317+
// input's scaling type is nil or equal to current
319318

320319
switch current.Scaling.Type {
321320
case sca.SCALINGTYPE_SCALING_TYPE_MANUAL:
@@ -351,13 +350,36 @@ func buildAutoscalingConfig(model *inputModel, current *sca.AutoScaling) (*sca.A
351350
}
352351
}
353352

354-
autoScaling := &sca.AutoScaling{}
355-
if current != nil {
356-
autoScaling = current
353+
if current == nil {
354+
if model.MinInstances == nil {
355+
return nil, &errors.FlagValidationError{
356+
Flag: instancesFlag,
357+
Details: "Required flag if auto scaling is being configured",
358+
}
359+
}
360+
361+
maxInstances := *model.MinInstances
362+
if model.MaxInstances != nil {
363+
maxInstances = *model.MaxInstances
364+
}
365+
366+
return &sca.AutoScaling{
367+
MinInstances: *model.MinInstances,
368+
MaxInstances: maxInstances,
369+
AllowScaleToZero: model.ScaleToZero,
370+
Rules: buildScalingRules(model, nil),
371+
}, nil
372+
}
373+
374+
autoScaling := &sca.AutoScaling{
375+
MinInstances: current.MinInstances,
376+
MaxInstances: current.MaxInstances,
357377
}
358378

359379
if model.ScaleToZero != nil {
360380
autoScaling.AllowScaleToZero = model.ScaleToZero
381+
} else if current.AllowScaleToZero != nil {
382+
autoScaling.AllowScaleToZero = new(*current.AllowScaleToZero)
361383
}
362384

363385
if model.MinInstances != nil {
@@ -372,9 +394,7 @@ func buildAutoscalingConfig(model *inputModel, current *sca.AutoScaling) (*sca.A
372394
autoScaling.MaxInstances = autoScaling.MinInstances
373395
}
374396

375-
if model.RPS != nil || model.Concurrency != nil {
376-
autoScaling.Rules = buildScalingRules(model, autoScaling.Rules)
377-
}
397+
autoScaling.Rules = buildScalingRules(model, current.Rules)
378398

379399
if len(autoScaling.Rules) == 0 {
380400
return nil, &errors.OneOfFlagsIsMissing{
@@ -390,31 +410,46 @@ func buildScalingRules(model *inputModel, current []sca.ScaleRule) []sca.ScaleRu
390410
if model.Concurrency == nil && model.RPS == nil {
391411
return current
392412
}
413+
393414
found := false
415+
updatedRules := make([]sca.ScaleRule, 0, len(current))
416+
417+
var httpRule *sca.HttpScaleRule
394418
for _, rule := range current {
395419
if rule.Type == sca.RULETYPE_RULE_TYPE_HTTP {
420+
httpRule = &sca.HttpScaleRule{}
396421
found = true
397422
if model.Concurrency != nil {
398-
rule.HttpRule.Concurrency = model.Concurrency
423+
httpRule.Concurrency = model.Concurrency
399424
}
400425
if model.RPS != nil {
401-
rule.HttpRule.Rps = model.RPS
426+
httpRule.Rps = model.RPS
402427
}
403428
}
429+
updatedRules = append(updatedRules, sca.ScaleRule{
430+
Type: rule.Type,
431+
Name: rule.Name,
432+
HttpRule: httpRule,
433+
CustomRule: rule.CustomRule,
434+
})
404435
}
405436

406437
if found {
407-
return current
438+
return updatedRules
408439
}
409440

410-
return append(current, sca.ScaleRule{
441+
return append(updatedRules, buildHttpScaleRule(model))
442+
}
443+
444+
func buildHttpScaleRule(model *inputModel) sca.ScaleRule {
445+
return sca.ScaleRule{
411446
Type: sca.RULETYPE_RULE_TYPE_HTTP,
412447
Name: "http-scaling-rule",
413448
HttpRule: &sca.HttpScaleRule{
414449
Concurrency: model.Concurrency,
415450
Rps: model.RPS,
416451
},
417-
})
452+
}
418453
}
419454

420455
func configureFlags(cmd *cobra.Command) {
@@ -483,9 +518,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
483518
}
484519
}
485520

486-
extertalPort := flags.FlagToInt32Pointer(p, cmd, externalPortFlag)
487-
if extertalPort != nil {
488-
if err := scautils.ValidatePort(*extertalPort, externalPortFlag); err != nil {
521+
externalPort := flags.FlagToInt32Pointer(p, cmd, externalPortFlag)
522+
if externalPort != nil {
523+
if err := scautils.ValidatePort(*externalPort, externalPortFlag); err != nil {
489524
return nil, err
490525
}
491526
}
@@ -516,19 +551,18 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
516551
Concurrency: flags.FlagToInt32Pointer(p, cmd, concurrencyFlag),
517552
RPS: flags.FlagToInt32Pointer(p, cmd, rpsFlag),
518553
Public: flags.FlagToBoolPointer(p, cmd, publicFlag),
519-
ContainerExternalPort: extertalPort,
554+
ContainerExternalPort: externalPort,
520555
}
521556

522557
p.DebugInputModel(model)
523558
return &model, nil
524559
}
525560

526561
func outputResult(p *print.Printer, model *inputModel, application *sca.Application) error {
527-
if application == nil {
528-
return fmt.Errorf("update application response is empty")
529-
}
530-
531562
return p.OutputResult(model.OutputFormat, application, func() error {
563+
if application == nil {
564+
return fmt.Errorf("update application response is empty")
565+
}
532566
operationState := "Updated"
533567
if model.Async {
534568
operationState = "Triggered update of"

‎internal/pkg/services/sca/utils/utils.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func EnvironmentVariablesFromMap(m map[string]string) []sca.EnvVar {
8585
return envVars
8686
}
8787

88-
func GetApplicationName(ctx context.Context, apiClient sca.DefaultAPI, projectId, environmentID, applicaitonID string) (string, error) {
89-
resp, err := apiClient.GetApplication(ctx, projectId, environmentID, applicaitonID).Execute()
88+
func GetApplicationName(ctx context.Context, apiClient sca.DefaultAPI, projectId, environmentID, applicationID string) (string, error) {
89+
resp, err := apiClient.GetApplication(ctx, projectId, environmentID, applicationID).Execute()
9090
if err != nil {
9191
return "", fmt.Errorf("get application: %w", err)
9292
}

0 commit comments

Comments
 (0)