Skip to content

Commit 0a595ed

Browse files
authored
fix(loadbalancer): print valid JSON/YAML output for list cmds (#1382)
relates to STACKITCLI-241 and #893
1 parent be5858e commit 0a595ed

4 files changed

Lines changed: 44 additions & 37 deletions

File tree

internal/cmd/load-balancer/list/list.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,20 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6767
return fmt.Errorf("get load balancers: %w", err)
6868
}
6969

70-
if resp.LoadBalancers == nil || (resp.LoadBalancers != nil && len(*resp.LoadBalancers) == 0) {
71-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
72-
if err != nil {
73-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
74-
projectLabel = model.ProjectId
75-
}
76-
params.Printer.Info("No load balancers found for project %q\n", projectLabel)
77-
return nil
70+
loadBalancers := utils.GetSliceFromPointer(resp.LoadBalancers)
71+
72+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
73+
if err != nil {
74+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
75+
projectLabel = model.ProjectId
7876
}
7977

80-
loadBalancers := *resp.LoadBalancers
8178
// Truncate output
8279
if model.Limit != nil && len(loadBalancers) > int(*model.Limit) {
8380
loadBalancers = loadBalancers[:*model.Limit]
8481
}
8582

86-
return outputResult(params.Printer, model.OutputFormat, loadBalancers)
83+
return outputResult(params.Printer, model.OutputFormat, projectLabel, loadBalancers)
8784
},
8885
}
8986

@@ -123,8 +120,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *loadbalance
123120
return req
124121
}
125122

126-
func outputResult(p *print.Printer, outputFormat string, loadBalancers []loadbalancer.LoadBalancer) error {
123+
func outputResult(p *print.Printer, outputFormat, projectLabel string, loadBalancers []loadbalancer.LoadBalancer) error {
127124
return p.OutputResult(outputFormat, loadBalancers, func() error {
125+
if len(loadBalancers) == 0 {
126+
p.Outputf("No load balancers found for project %q\n", projectLabel)
127+
return nil
128+
}
128129
table := tables.NewTable()
129130
table.SetHeader("NAME", "STATE", "IP ADDRESS", "LISTENERS", "TARGET POOLS")
130131
for i := range loadBalancers {

internal/cmd/load-balancer/list/list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ func TestBuildRequest(t *testing.T) {
155155
func TestOutputResult(t *testing.T) {
156156
type args struct {
157157
outputFormat string
158+
projectLabel string
158159
loadBalancers []loadbalancer.LoadBalancer
159160
}
160161
tests := []struct {
@@ -185,7 +186,7 @@ func TestOutputResult(t *testing.T) {
185186
params := testparams.NewTestParams()
186187
for _, tt := range tests {
187188
t.Run(tt.name, func(t *testing.T) {
188-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.loadBalancers); (err != nil) != tt.wantErr {
189+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.loadBalancers); (err != nil) != tt.wantErr {
189190
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
190191
}
191192
})

internal/cmd/load-balancer/observability-credentials/list/list.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,37 +84,35 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8484
if err != nil {
8585
return fmt.Errorf("list Load Balancer observability credentials: %w", err)
8686
}
87-
credentialsPtr := resp.Credentials
88-
89-
var credentials []loadbalancer.CredentialsResponse
90-
if credentialsPtr != nil && len(*credentialsPtr) > 0 {
91-
credentials = *credentialsPtr
92-
filterOp, err := getFilterOp(model.Used, model.Unused)
93-
if err != nil {
94-
return err
95-
}
96-
credentials, err = lbUtils.FilterCredentials(ctx, apiClient, credentials, model.ProjectId, model.Region, filterOp)
97-
if err != nil {
98-
return fmt.Errorf("filter credentials: %w", err)
99-
}
87+
88+
credentials := utils.GetSliceFromPointer(resp.Credentials)
89+
90+
filterOp, err := getFilterOp(model.Used, model.Unused)
91+
if err != nil {
92+
return err
10093
}
10194

102-
if len(credentials) == 0 {
103-
opLabel := "No "
104-
if model.Used {
105-
opLabel += "used"
106-
} else if model.Unused {
107-
opLabel += "unused"
108-
}
109-
params.Printer.Info("%s observability credentials found for Load Balancer on project %q\n", opLabel, projectLabel)
110-
return nil
95+
filteredCredentials, err := lbUtils.FilterCredentials(ctx, apiClient, credentials, model.ProjectId, model.Region, filterOp)
96+
if err != nil {
97+
return fmt.Errorf("filter credentials: %w", err)
98+
}
99+
if filteredCredentials != nil { // lbUtils.FilterCredentials can return nil with no error, if credentials is an empty slice and filterOp is not 0 (if either the --used or the --unused is set)
100+
credentials = filteredCredentials
111101
}
112102

113103
// Truncate output
114104
if model.Limit != nil && len(credentials) > int(*model.Limit) {
115105
credentials = credentials[:*model.Limit]
116106
}
117-
return outputResult(params.Printer, model.OutputFormat, credentials)
107+
108+
opLabel := "No"
109+
if model.Used {
110+
opLabel += " used"
111+
} else if model.Unused {
112+
opLabel += " unused"
113+
}
114+
115+
return outputResult(params.Printer, model.OutputFormat, projectLabel, opLabel, credentials)
118116
},
119117
}
120118
configureFlags(cmd)
@@ -159,8 +157,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *loadbalance
159157
return req
160158
}
161159

162-
func outputResult(p *print.Printer, outputFormat string, credentials []loadbalancer.CredentialsResponse) error {
160+
func outputResult(p *print.Printer, outputFormat, projectLabel, opLabel string, credentials []loadbalancer.CredentialsResponse) error {
163161
return p.OutputResult(outputFormat, credentials, func() error {
162+
if len(credentials) == 0 {
163+
p.Outputf("%s observability credentials found for Load Balancer on project %q\n", opLabel, projectLabel)
164+
return nil
165+
}
166+
164167
table := tables.NewTable()
165168
table.SetHeader("REFERENCE", "DISPLAY NAME", "USERNAME")
166169
for i := range credentials {

internal/cmd/load-balancer/observability-credentials/list/list_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ func TestGetFilterOp(t *testing.T) {
232232
func TestOutputResult(t *testing.T) {
233233
type args struct {
234234
outputFormat string
235+
opLabel string
236+
projectLabel string
235237
credentials []loadbalancer.CredentialsResponse
236238
}
237239
tests := []struct {
@@ -255,7 +257,7 @@ func TestOutputResult(t *testing.T) {
255257
params := testparams.NewTestParams()
256258
for _, tt := range tests {
257259
t.Run(tt.name, func(t *testing.T) {
258-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
260+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.opLabel, tt.args.credentials); (err != nil) != tt.wantErr {
259261
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
260262
}
261263
})

0 commit comments

Comments
 (0)