Skip to content
Open
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
25 changes: 12 additions & 13 deletions internal/cmd/beta/alb/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ type inputModel struct {
}

const (
labelSelectorFlag = "label-selector"
limitFlag = "limit"
limitFlag = "limit"
)

func NewCmd(params *params.CmdParams) *cobra.Command {
Expand Down Expand Up @@ -73,19 +72,14 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
if err != nil {
return fmt.Errorf("list load balancerse: %w", err)
}
items := response.GetLoadBalancers()

if items := response.LoadBalancers; items == nil || len(*items) == 0 {
params.Printer.Info("No load balancers found for project %q", projectLabel)
} else {
if model.Limit != nil && len(*items) > int(*model.Limit) {
*items = (*items)[:*model.Limit]
}
if err := outputResult(params.Printer, model.OutputFormat, *items); err != nil {
return fmt.Errorf("output loadbalancers: %w", err)
}
// Truncate output
if model.Limit != nil && len(items) > int(*model.Limit) {
items = items[:*model.Limit]
}

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

Expand Down Expand Up @@ -125,8 +119,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClie

return request
}
func outputResult(p *print.Printer, outputFormat string, items []alb.LoadBalancer) error {
func outputResult(p *print.Printer, outputFormat, projectLabel string, items []alb.LoadBalancer) error {
return p.OutputResult(outputFormat, items, func() error {
if len(items) == 0 {
p.Outputf("No load balancers found for project %q", projectLabel)
return nil
}

table := tables.NewTable()
table.SetHeader("NAME", "EXTERNAL ADDRESS", "REGION", "STATUS", "VERSION", "ERRORS")
for i := range items {
Expand Down
19 changes: 12 additions & 7 deletions internal/cmd/beta/alb/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

Check failure on line 5 in internal/cmd/beta/alb/list/list_test.go

View workflow job for this annotation

GitHub Actions / CI

File is not properly formatted (goimports)
"strconv"
"testing"

Expand All @@ -19,11 +20,14 @@
type testCtxKey struct{}

var (
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
testClient = &alb.APIClient{}
testProjectId = uuid.NewString()
testRegion = "eu01"
testLimit int64 = 10
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
testClient = &alb.APIClient{}
testProjectId = uuid.NewString()
)

const (
testRegion = "eu01"
testLimit int64 = 10
)

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
Expand All @@ -41,7 +45,7 @@
func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
model := &inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{ProjectId: testProjectId, Region: testRegion, Verbosity: globalflags.VerbosityDefault},
Limit: &testLimit,
Limit: utils.Ptr(testLimit),
}
for _, mod := range mods {
mod(model)
Expand Down Expand Up @@ -136,6 +140,7 @@
func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
items []alb.LoadBalancer
}
tests := []struct {
Expand Down Expand Up @@ -164,7 +169,7 @@
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
13 changes: 7 additions & 6 deletions internal/cmd/beta/alb/observability-credentials/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
if err != nil {
return fmt.Errorf("list credentials: %w", err)
}
items := resp.GetCredentials()

if resp.Credentials == nil || len(*resp.Credentials) == 0 {
params.Printer.Info("No credentials found\n")
return nil
}

items := *resp.Credentials
// Truncate output
if model.Limit != nil && len(items) > int(*model.Limit) {
items = items[:*model.Limit]
}
Expand Down Expand Up @@ -122,6 +118,11 @@ func outputResult(p *print.Printer, outputFormat string, items []alb.Credentials
}

return p.OutputResult(outputFormat, items, func() error {
if len(items) == 0 {
p.Outputf("No credentials found\n")
return nil
}

table := tables.NewTable()
table.SetHeader("CREDENTIAL REF", "DISPLAYNAME", "USERNAME", "REGION")

Expand Down
18 changes: 8 additions & 10 deletions internal/cmd/beta/alb/plans/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
if err != nil {
return fmt.Errorf("list plans: %w", err)
}
items := response.GetValidPlans()

if items := response.ValidPlans; items == nil || len(*items) == 0 {
params.Printer.Info("No plans found for project %q", projectLabel)
} else {
if err := outputResult(params.Printer, model.OutputFormat, *items); err != nil {
return fmt.Errorf("output plans: %w", err)
}
}

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

Expand All @@ -98,8 +91,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClie
return request
}

func outputResult(p *print.Printer, outputFormat string, items []alb.PlanDetails) error {
func outputResult(p *print.Printer, outputFormat, projectLabel string, items []alb.PlanDetails) error {
return p.OutputResult(outputFormat, items, func() error {
if len(items) == 0 {
p.Outputf("No plans found for project %q", projectLabel)
return nil
}

table := tables.NewTable()
table.SetHeader("PLAN ID", "NAME", "FLAVOR", "MAX CONNS", "DESCRIPTION")
for _, item := range items {
Expand Down
6 changes: 4 additions & 2 deletions internal/cmd/beta/alb/plans/plans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ var (
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
testClient = &alb.APIClient{}
testProjectId = uuid.NewString()
testRegion = "eu01"
)

const testRegion = "eu01"

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
globalflags.ProjectIdFlag: testProjectId,
Expand Down Expand Up @@ -132,6 +133,7 @@ func TestBuildRequest(t *testing.T) {
func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
items []alb.PlanDetails
}
tests := []struct {
Expand Down Expand Up @@ -160,7 +162,7 @@ func Test_outputResult(t *testing.T) {
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
Loading