fix(iaas): print valid JSON/YAML output for list cmds#1365
fix(iaas): print valid JSON/YAML output for list cmds#1365j1n-o9r wants to merge 39 commits intostackitcloud:mainfrom
Conversation
|
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 We currently have a lot of merge conflicts here, could you please resolve them before one of the collegues starts with a review? 😅 |
fadd859 to
eaa4afb
Compare
… any items via outputResult function instead of returning nil bluntly. adapted tests therefore.
eaa4afb to
9b8c40d
Compare
| } | ||
|
|
||
| func outputResult(p *print.Printer, model inputModel, items []iaas.AffinityGroup) error { | ||
| var outputFormat string |
There was a problem hiding this comment.
Why did you change this and the method signature?
There was a problem hiding this comment.
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{}, |
There was a problem hiding this comment.
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? 😄
There was a problem hiding this comment.
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.
| return nil | ||
| items := resp.GetItems() | ||
|
|
||
| projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) |
There was a problem hiding this comment.
see above, please remove this
| 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 { |
| } | ||
|
|
||
| if orgLabel == "" { | ||
| orgLabel = *model.OrganizationId |
There was a problem hiding this comment.
Take care: potential nil pointer de-reference. May result in a panic.
Actually the organization id flag is required:
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 |
There was a problem hiding this comment.
| var networkAreaLabel string | |
| items := resp.GetItems() | ||
|
|
||
| var networkAreaLabel string | ||
| networkAreaLabel, err = iaasUtils.GetNetworkAreaName(ctx, apiClient, *model.OrganizationId, *model.NetworkAreaId) |
There was a problem hiding this comment.
| 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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
| 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 |
Co-authored-by: Ruben Hönle <git@hoenle.xyz>
…o deal with nil values since it is required anyways
… string for not having to deal with nil values since it is required anyways
Description
relates to STACKITCLI-241 / #893
Addition: I encountered a missing
--limit-flag within thestackit security-group listcommand and directly fixed this after consulting @marceljkTesting 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 JSONstackit server list --output-format yaml-> Should output valid YAMLMock responses if testing against API is not possible.
Checklist
make fmtmake generate-docs(will be checked by CI)make test(will be checked by CI)make lint(will be checked by CI)