Skip to content

fix(iaas): print valid JSON/YAML output for list cmds#1365

Open
j1n-o9r wants to merge 39 commits intostackitcloud:mainfrom
j1n-o9r:fix/STACKITCLI-241-iaas-json-list-output
Open

fix(iaas): print valid JSON/YAML output for list cmds#1365
j1n-o9r wants to merge 39 commits intostackitcloud:mainfrom
j1n-o9r:fix/STACKITCLI-241-iaas-json-list-output

Conversation

@j1n-o9r
Copy link
Copy Markdown

@j1n-o9r j1n-o9r commented Apr 13, 2026

Description

relates to STACKITCLI-241 / #893

Addition: I encountered a missing --limit-flag within the stackit security-group list command and directly fixed this after consulting @marceljk

Testing Instructions

Test for every list command that is changed (like f.e. stackit server list) if the command returns the expected outputs like:

  • stackit server list -> Expected: No servers found for project "xxx"
  • stackit server list --output-format json -> Should output valid JSON
  • stackit server list --output-format yaml -> Should output valid YAML

Mock responses if testing against API is not possible.

Checklist

  • Issue was linked above
  • Code format was applied: make fmt
  • Examples were added / adjusted (see e.g. here)
  • Docs are up-to-date: make generate-docs (will be checked by CI)
  • Unit tests got implemented or updated
  • Unit tests are passing: make test (will be checked by CI)
  • No linter issues: make lint (will be checked by CI)

@j1n-o9r j1n-o9r marked this pull request as ready for review April 13, 2026 15:07
@j1n-o9r j1n-o9r requested a review from a team as a code owner April 13, 2026 15:07
@rubenhoenle
Copy link
Copy Markdown
Member

Missing:

... and maybe more. Please check carefully 😅

@j1n-o9r
Copy link
Copy Markdown
Author

j1n-o9r commented Apr 15, 2026

Missing:

... and maybe more. Please check carefully 😅

Sorry, I mistakenly understood the examples in the ticket as acceptance criteria and did not check the other resources. I will go through all and add the missing parts.😅

@j1n-o9r j1n-o9r marked this pull request as draft April 15, 2026 12:52
@rubenhoenle
Copy link
Copy Markdown
Member

@j1n-o9r We currently have a lot of merge conflicts here, could you please resolve them before one of the collegues starts with a review? 😅

@j1n-o9r j1n-o9r force-pushed the fix/STACKITCLI-241-iaas-json-list-output branch 2 times, most recently from fadd859 to eaa4afb Compare April 22, 2026 10:26
j1n-o9r added 22 commits April 22, 2026 12:28
… any items via outputResult function instead of returning nil bluntly. adapted tests therefore.
@j1n-o9r j1n-o9r force-pushed the fix/STACKITCLI-241-iaas-json-list-output branch from eaa4afb to 9b8c40d Compare April 22, 2026 10:35
@j1n-o9r j1n-o9r marked this pull request as ready for review April 22, 2026 11:46
}

func outputResult(p *print.Printer, model inputModel, items []iaas.AffinityGroup) error {
var outputFormat string
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this and the method signature?

Copy link
Copy Markdown
Author

@j1n-o9r j1n-o9r Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looked weird at first glance that the signature differed from all other outputResult functions. I also could not see a benefit from passing the model into the function. Furthermore I also think the state that is checked in the function will not happen, since if the input model will be nil and the outputResult function cannot be called. But maybe I am overseeing something here. Should I change it to the old signature?

description: "empty",
model: inputModel{},
response: []iaas.AffinityGroup{},
args: args{},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You changed that testcase with that change. The response parameter was an empty slice before, now it's nil.

Valid testcase, but be aware what you're doing here. As commented above the question is why you changed the method signature at all? 😄

Copy link
Copy Markdown
Author

@j1n-o9r j1n-o9r Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reminding that the case differs, honestly I don't know if I was aware of that when editing but I am now. 😄

I would suggest maybe having both cases tested.

Comment thread internal/cmd/key-pair/list/list.go Outdated
Comment thread internal/cmd/key-pair/list/list.go Outdated
return nil
items := resp.GetItems()

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above, please remove this

Comment thread internal/cmd/key-pair/list/list_test.go Outdated
params := testparams.NewTestParams()

if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.keyPairs); (err != nil) != tt.wantErr {
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.keyPairs); (err != nil) != tt.wantErr {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert pls, see above

Comment thread internal/cmd/network-area/list/list.go Outdated
}

if orgLabel == "" {
orgLabel = *model.OrganizationId
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take care: potential nil pointer de-reference. May result in a panic.

Actually the organization id flag is required:

err := flags.MarkFlagsRequired(cmd, organizationIdFlag)

So I would suggest you mark it as a string type instead of a string pointer (*string) in the model struct

Seems like this was implemented poorly in the past. If you correct it you don't have to take care here of the nil pointer de-reference at all. 😅

return nil
items := resp.GetItems()

var networkAreaLabel string
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var networkAreaLabel string

items := resp.GetItems()

var networkAreaLabel string
networkAreaLabel, err = iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
networkAreaLabel, err = iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId)
networkAreaLabel, err := iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId)

networkAreaLabel, err = iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get organization name: %v", err)
networkAreaLabel = *model.NetworkAreaId
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Potential nil pointer de-reference. The networkAreaIdFlag is marked as required. So change it's type from *string to string in the model struct please.

func outputResult(p *print.Printer, outputFormat, networkAreaLabel string, networkRanges []iaas.NetworkRange) error {
return p.OutputResult(outputFormat, networkRanges, func() error {
if len(networkRanges) == 0 {
p.Outputf("No network ranges found for SNA %q\n", networkAreaLabel)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
p.Outputf("No network ranges found for SNA %q\n", networkAreaLabel)
p.Outputf("No network ranges found for STACKIT network area %q\n", networkAreaLabel)

}

return nil
// Truncate output
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants