Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 5 additions & 1 deletion golang-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ linters:
- revive
- sqlclosecheck
- wastedassign
- wsl_v5
disable:
- noctx # false positive: finds errors with http.NewRequest that dont make sense
- unparam # false positives
Expand All @@ -39,7 +40,7 @@ linters:
- experimental
gosec:
excludes:
# Suppressions: (see https://github.com/securego/gosec#available-rules for details)
# Suppressions: (see https://github.com/securego/gosec#available-rules for details)
- G104 # "Audit errors not checked" -> which we don't need and is a badly implemented version of errcheck
- G102 # "Bind to all interfaces" -> since this is normal in k8s
- G304 # "File path provided as taint input" -> too many false positives
Expand All @@ -66,6 +67,9 @@ linters:
- name: atomic
- name: empty-lines
- name: early-return
wsl_v5:
allow-whole-block: true
branch-max-lines: 4
exclusions:
generated: lax
paths:
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/affinity-groups/affinity-groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
Run: utils.CmdHelp,
}
addSubcommands(cmd, params)

return cmd
}

Expand Down
7 changes: 7 additions & 0 deletions internal/cmd/affinity-groups/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()

model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
Expand All @@ -56,6 +57,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
}

prompt := fmt.Sprintf("Are you sure you want to create the affinity group %q?", model.Name)

err = params.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
Expand All @@ -68,13 +70,16 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
if err != nil {
return fmt.Errorf("create affinity group: %w", err)
}

if resp := result; resp != nil {
return outputResult(params.Printer, *model, *resp)
}

return fmt.Errorf("create affinity group: nil result")
},
}
configureFlags(cmd)

return cmd
}

Expand All @@ -95,6 +100,7 @@ func buildRequest(ctx context.Context, model inputModel, apiClient *iaas.APIClie
Policy: model.Policy,
},
)

return req
}

Expand All @@ -111,6 +117,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
}

p.DebugInputModel(model)

return &model, nil
}

Expand Down
10 changes: 10 additions & 0 deletions internal/cmd/affinity-groups/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
for _, mod := range mods {
mod(flagValues)
}

return flagValues
}

Expand All @@ -57,15 +58,18 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
for _, mod := range mods {
mod(model)
}

return model
}

func fixtureRequest(mods ...func(request *iaas.ApiCreateAffinityGroupRequest)) iaas.ApiCreateAffinityGroupRequest {
request := testClient.DefaultAPI.CreateAffinityGroup(testCtx, testProjectId, testRegion)

request = request.CreateAffinityGroupPayload(fixturePayload())
for _, mod := range mods {
mod(&request)
}

return request
}

Expand All @@ -77,6 +81,7 @@ func fixturePayload(mods ...func(payload *iaas.CreateAffinityGroupPayload)) iaas
for _, mod := range mods {
mod(&payload)
}

return payload
}

Expand Down Expand Up @@ -145,6 +150,7 @@ func TestBuildRequest(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
request := buildRequest(testCtx, tt.model, testClient)

diff := cmp.Diff(request, tt.expectedRequest,
cmp.AllowUnexported(tt.expectedRequest),
cmpopts.EquateComparable(testCtx, iaas.DefaultAPIService{}))
Expand Down Expand Up @@ -180,6 +186,7 @@ func TestOutputResult(t *testing.T) {
isValid: true,
},
}

params := testparams.NewTestParams()
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
Expand All @@ -188,9 +195,12 @@ func TestOutputResult(t *testing.T) {
if !tt.isValid {
return
}

t.Fatalf("error output result: %v", err)

return
}

if !tt.isValid {
t.Fatalf("did not fail on invalid input")
return
Expand Down
8 changes: 8 additions & 0 deletions internal/cmd/affinity-groups/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()

model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
Expand All @@ -57,32 +58,38 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)

projectLabel = model.ProjectId
}

affinityGroupLabel, err := iaasUtils.GetAffinityGroupName(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.AffinityGroupId)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get affinity group name: %v", err)

affinityGroupLabel = model.AffinityGroupId
}

prompt := fmt.Sprintf("Are you sure you want to delete affinity group %q?", affinityGroupLabel)

err = params.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}

// Call API
request := buildRequest(ctx, *model, apiClient)

err = request.Execute()
if err != nil {
return fmt.Errorf("delete affinity group: %w", err)
}

params.Printer.Info("Deleted affinity group %q for %q\n", affinityGroupLabel, projectLabel)

return nil
},
}

return cmd
}

Expand All @@ -102,5 +109,6 @@ func parseInput(p *print.Printer, cmd *cobra.Command, cliArgs []string) (*inputM
}

p.DebugInputModel(model)

return &model, nil
}
10 changes: 10 additions & 0 deletions internal/cmd/affinity-groups/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func fixtureArgValues(mods ...func(argValues []string)) []string {
for _, mod := range mods {
mod(argValues)
}

return argValues
}

Expand All @@ -46,6 +47,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
for _, mod := range mods {
mod(flagValues)
}

return flagValues
}

Expand All @@ -61,6 +63,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
for _, mod := range mods {
mod(model)
}

return model
}

Expand All @@ -69,6 +72,7 @@ func fixtureRequest(mods ...func(request *iaas.ApiDeleteAffinityGroupRequest)) i
for _, mod := range mods {
mod(&request)
}

return request
}

Expand Down Expand Up @@ -104,6 +108,7 @@ func TestParseInput(t *testing.T) {
t.Run(tt.description, func(t *testing.T) {
params := testparams.NewTestParams()
cmd := NewCmd(params.CmdParams)

err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flags: %v", err)
Expand All @@ -115,6 +120,7 @@ func TestParseInput(t *testing.T) {
if !tt.isValid {
return
}

t.Fatalf("setting flag --%s=%s: %v", flag, value, err)
}
}
Expand All @@ -124,6 +130,7 @@ func TestParseInput(t *testing.T) {
if !tt.isValid {
return
}

t.Fatalf("error validating args: %v", err)
}

Expand All @@ -132,6 +139,7 @@ func TestParseInput(t *testing.T) {
if !tt.isValid {
return
}

t.Fatalf("error validating flags: %v", err)
}

Expand All @@ -140,12 +148,14 @@ func TestParseInput(t *testing.T) {
if !tt.isValid {
return
}

t.Fatalf("error parsing input: %v", err)
}

if !tt.isValid {
t.Fatalf("did not fail on invalid input")
}

diff := cmp.Diff(model, tt.expectedModel)
if diff != "" {
t.Fatalf("data does not match: %s", diff)
Expand Down
8 changes: 8 additions & 0 deletions internal/cmd/affinity-groups/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()

model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
Expand All @@ -56,6 +57,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {

// Call API
request := buildRequest(ctx, *model, apiClient)

result, err := request.Execute()
if err != nil {
return fmt.Errorf("get affinity group: %w", err)
Expand All @@ -64,9 +66,11 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
if err := outputResult(params.Printer, *model, *result); err != nil {
return err
}

return nil
},
}

return cmd
}

Expand All @@ -86,6 +90,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, cliArgs []string) (*inputM
}

p.DebugInputModel(model)

return &model, nil
}

Expand All @@ -102,10 +107,12 @@ func outputResult(p *print.Printer, model inputModel, resp iaas.AffinityGroup) e
table.AddRow("ID", utils.PtrString(resp.Id))
table.AddSeparator()
}

table.AddRow("NAME", resp.Name)
table.AddSeparator()
table.AddRow("POLICY", resp.Policy)
table.AddSeparator()

if resp.HasMembers() {
table.AddRow("Members", strings.Join(resp.Members, ", "))
table.AddSeparator()
Expand All @@ -114,6 +121,7 @@ func outputResult(p *print.Printer, model inputModel, resp iaas.AffinityGroup) e
if err := table.Display(p); err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
})
}
Loading
Loading