Skip to content
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions internal/cmd/load-balancer/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,20 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
return fmt.Errorf("get load balancers: %w", err)
}

if resp.LoadBalancers == nil || (resp.LoadBalancers != nil && len(*resp.LoadBalancers) == 0) {
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
}
params.Printer.Info("No load balancers found for project %q\n", projectLabel)
return nil
loadBalancers := utils.GetSliceFromPointer(resp.LoadBalancers)

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
}

loadBalancers := *resp.LoadBalancers
// Truncate output
if model.Limit != nil && len(loadBalancers) > int(*model.Limit) {
loadBalancers = loadBalancers[:*model.Limit]
}

return outputResult(params.Printer, model.OutputFormat, loadBalancers)
return outputResult(params.Printer, model.OutputFormat, projectLabel, loadBalancers)
},
}

Expand Down Expand Up @@ -123,8 +120,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *loadbalance
return req
}

func outputResult(p *print.Printer, outputFormat string, loadBalancers []loadbalancer.LoadBalancer) error {
func outputResult(p *print.Printer, outputFormat, projectLabel string, loadBalancers []loadbalancer.LoadBalancer) error {
return p.OutputResult(outputFormat, loadBalancers, func() error {
if len(loadBalancers) == 0 {
p.Outputf("No load balancers found for project %q\n", projectLabel)
return nil
}
table := tables.NewTable()
table.SetHeader("NAME", "STATE", "IP ADDRESS", "LISTENERS", "TARGET POOLS")
for i := range loadBalancers {
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/load-balancer/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func TestBuildRequest(t *testing.T) {
func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
loadBalancers []loadbalancer.LoadBalancer
}
tests := []struct {
Expand Down Expand Up @@ -185,7 +186,7 @@ func TestOutputResult(t *testing.T) {
params := testparams.NewTestParams()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.loadBalancers); (err != nil) != tt.wantErr {
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.loadBalancers); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
51 changes: 27 additions & 24 deletions internal/cmd/load-balancer/observability-credentials/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,35 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
if err != nil {
return fmt.Errorf("list Load Balancer observability credentials: %w", err)
}
credentialsPtr := resp.Credentials

var credentials []loadbalancer.CredentialsResponse
if credentialsPtr != nil && len(*credentialsPtr) > 0 {
credentials = *credentialsPtr
filterOp, err := getFilterOp(model.Used, model.Unused)
if err != nil {
return err
}
credentials, err = lbUtils.FilterCredentials(ctx, apiClient, credentials, model.ProjectId, model.Region, filterOp)
if err != nil {
return fmt.Errorf("filter credentials: %w", err)
}

credentials := utils.GetSliceFromPointer(resp.Credentials)

filterOp, err := getFilterOp(model.Used, model.Unused)
if err != nil {
return err
}

if len(credentials) == 0 {
opLabel := "No "
if model.Used {
opLabel += "used"
} else if model.Unused {
opLabel += "unused"
}
params.Printer.Info("%s observability credentials found for Load Balancer on project %q\n", opLabel, projectLabel)
return nil
filteredCredentials, err := lbUtils.FilterCredentials(ctx, apiClient, credentials, model.ProjectId, model.Region, filterOp)
if err != nil {
return fmt.Errorf("filter credentials: %w", err)
}
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)
credentials = filteredCredentials
}

// Truncate output
if model.Limit != nil && len(credentials) > int(*model.Limit) {
credentials = credentials[:*model.Limit]
}
return outputResult(params.Printer, model.OutputFormat, credentials)

opLabel := "No"
if model.Used {
opLabel += " used"
} else if model.Unused {
opLabel += " unused"
}

return outputResult(params.Printer, model.OutputFormat, projectLabel, opLabel, credentials)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -159,8 +157,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *loadbalance
return req
}

func outputResult(p *print.Printer, outputFormat string, credentials []loadbalancer.CredentialsResponse) error {
func outputResult(p *print.Printer, outputFormat, projectLabel, opLabel string, credentials []loadbalancer.CredentialsResponse) error {
return p.OutputResult(outputFormat, credentials, func() error {
if len(credentials) == 0 {
p.Outputf("%s observability credentials found for Load Balancer on project %q\n", opLabel, projectLabel)
return nil
}

table := tables.NewTable()
table.SetHeader("REFERENCE", "DISPLAY NAME", "USERNAME")
for i := range credentials {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ func TestGetFilterOp(t *testing.T) {
func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
opLabel string
projectLabel string
credentials []loadbalancer.CredentialsResponse
}
tests := []struct {
Expand All @@ -255,7 +257,7 @@ func TestOutputResult(t *testing.T) {
params := testparams.NewTestParams()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.opLabel, tt.args.credentials); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
Loading