Skip to content

Commit 1a3c819

Browse files
committed
adapted load balancer observability-credentials' list command to return valid json/yaml for empty api responses
1 parent 1c0da0f commit 1a3c819

2 files changed

Lines changed: 30 additions & 25 deletions

File tree

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+
filteredCredentials = credentials
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)