Skip to content
Draft
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
29 changes: 16 additions & 13 deletions internal/cmd/affinity-groups/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
)

Expand Down Expand Up @@ -63,16 +64,19 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
if err != nil {
return fmt.Errorf("list affinity groups: %w", err)
}
items := result.GetItems()

if items := result.Items; items != nil {
if model.Limit != nil && len(*items) > int(*model.Limit) {
*items = (*items)[:*model.Limit]
}
return outputResult(params.Printer, *model, *items)
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.Outputln("No affinity groups found")
return nil
// Truncate Output
if model.Limit != nil && len(items) > int(*model.Limit) {
items = items[:*model.Limit]
}
return outputResult(params.Printer, model.OutputFormat, projectLabel, items)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -110,13 +114,12 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
return &model, nil
}

func outputResult(p *print.Printer, model inputModel, items []iaas.AffinityGroup) error {
var outputFormat string
if model.GlobalFlagModel != nil {
outputFormat = model.OutputFormat
}

func outputResult(p *print.Printer, outputFormat, projectLabel string, items []iaas.AffinityGroup) error {
return p.OutputResult(outputFormat, items, func() error {
if len(items) == 0 {
p.Outputf("No affinity groups found for project %q\n", projectLabel)
return nil
}
table := tables.NewTable()
table.SetHeader("ID", "NAME", "POLICY")
for _, item := range items {
Expand Down
13 changes: 8 additions & 5 deletions internal/cmd/affinity-groups/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,27 @@ func TestBuildRequest(t *testing.T) {
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
instances []iaas.AffinityGroup
}
tests := []struct {
description string
model inputModel
response []iaas.AffinityGroup
args args
isValid bool
}{
{
description: "empty",
model: inputModel{},
response: []iaas.AffinityGroup{},
args: args{},
isValid: true,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
err := outputResult(p, tt.model, tt.response)
err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.instances)
if err != nil {
if !tt.isValid {
return
Expand Down
23 changes: 12 additions & 11 deletions internal/cmd/image/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,18 @@ func NewCmd(params *types.CmdParams) *cobra.Command {

// Call API
request := buildRequest(ctx, model, apiClient)

response, err := request.Execute()
if err != nil {
return fmt.Errorf("list images: %w", err)
}
items := response.GetItems()

if items := response.GetItems(); len(items) == 0 {
params.Printer.Info("No images 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 images: %w", err)
}
// Truncate output
if model.Limit != nil && len(items) > int(*model.Limit) {
items = (items)[:*model.Limit]
}
if err := outputResult(params.Printer, model.OutputFormat, projectLabel, items); err != nil {
return fmt.Errorf("output images: %w", err)
}

return nil
Expand Down Expand Up @@ -149,8 +146,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return request
}

func outputResult(p *print.Printer, outputFormat string, items []iaas.Image) error {
func outputResult(p *print.Printer, outputFormat, projectLabel string, items []iaas.Image) error {
return p.OutputResult(outputFormat, items, func() error {
if len(items) == 0 {
p.Outputf("No images found for project %q\n", projectLabel)
return nil
}
table := tables.NewTable()
table.SetHeader("ID", "NAME", "OS", "ARCHITECTURE", "DISTRIBUTION", "VERSION", "SCOPE", "OWNER", "LABELS")
for i := range items {
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/image/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func TestBuildRequest(t *testing.T) {
func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
items []iaas.Image
}
tests := []struct {
Expand Down Expand Up @@ -217,7 +218,7 @@ func Test_outputResult(t *testing.T) {
p.Cmd = NewCmd(&types.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
21 changes: 15 additions & 6 deletions internal/cmd/key-pair/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"

"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
Expand Down Expand Up @@ -77,17 +78,20 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
return fmt.Errorf("list key pairs: %w", err)
}

if resp.Items == nil || len(*resp.Items) == 0 {
params.Printer.Info("No key pairs found\n")
return nil
items := resp.GetItems()

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
}

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

return outputResult(params.Printer, model.OutputFormat, items)
return outputResult(params.Printer, model.OutputFormat, projectLabel, items)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -128,8 +132,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return req
}

func outputResult(p *print.Printer, outputFormat string, keyPairs []iaas.Keypair) error {
func outputResult(p *print.Printer, outputFormat, projectLabel string, keyPairs []iaas.Keypair) error {
return p.OutputResult(outputFormat, keyPairs, func() error {
if len(keyPairs) == 0 {
p.Outputf("No key pairs found for project %q\n", projectLabel)
return nil
}

table := tables.NewTable()
table.SetHeader("KEY PAIR NAME", "LABELS", "FINGERPRINT", "CREATED AT", "UPDATED AT")

Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/key-pair/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 Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
keyPairs []iaas.Keypair
}
tests := []struct {
Expand All @@ -179,7 +180,7 @@ func Test_outputResult(t *testing.T) {
p := print.NewPrinter()
p.Cmd = NewCmd(&types.CmdParams{Printer: p})

if err := outputResult(p, tt.args.outputFormat, tt.args.keyPairs); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.keyPairs); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
40 changes: 22 additions & 18 deletions internal/cmd/network-area/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,30 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
return fmt.Errorf("list network areas: %w", err)
}

if resp.Items == nil || len(*resp.Items) == 0 {
var orgLabel string
rmApiClient, err := rmClient.ConfigureClient(params.Printer, params.CliVersion)
if err == nil {
orgLabel, err = rmUtils.GetOrganizationName(ctx, rmApiClient, *model.OrganizationId)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get organization name: %v", err)
orgLabel = *model.OrganizationId
} else if orgLabel == "" {
orgLabel = *model.OrganizationId
}
} else {
params.Printer.Debug(print.ErrorLevel, "configure resource manager client: %v", err)
items := resp.GetItems()

var orgLabel string
rmApiClient, err := rmClient.ConfigureClient(params.Printer, params.CliVersion)
if err == nil {
orgLabel, err = rmUtils.GetOrganizationName(ctx, rmApiClient, *model.OrganizationId)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get organization name: %v", err)
orgLabel = *model.OrganizationId
}
params.Printer.Info("No STACKIT Network Areas found for organization %q\n", orgLabel)
return nil
} else {
params.Printer.Debug(print.ErrorLevel, "configure resource manager client: %v", err)
}

if orgLabel == "" {
orgLabel = *model.OrganizationId
}

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

return outputResult(params.Printer, model.OutputFormat, items)
return outputResult(params.Printer, orgLabel, model.OutputFormat, items)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -149,8 +148,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return req
}

func outputResult(p *print.Printer, outputFormat string, networkAreas []iaas.NetworkArea) error {
func outputResult(p *print.Printer, orgLabel, outputFormat string, networkAreas []iaas.NetworkArea) error {
return p.OutputResult(outputFormat, networkAreas, func() error {
if len(networkAreas) == 0 {
p.Outputf("No STACKIT Network Areas found for organization %q\n", orgLabel)
return nil
}

table := tables.NewTable()
table.SetHeader("ID", "Name", "# Attached Projects")

Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/network-area/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func TestBuildRequest(t *testing.T) {
func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
orgLabel string
networkAreas []iaas.NetworkArea
}
tests := []struct {
Expand Down Expand Up @@ -200,7 +201,7 @@ func TestOutputResult(t *testing.T) {
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.networkAreas); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, tt.args.orgLabel, tt.args.networkAreas); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
26 changes: 13 additions & 13 deletions internal/cmd/network/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,20 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
return fmt.Errorf("list networks: %w", err)
}

if resp.Items == nil || len(*resp.Items) == 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
} else if projectLabel == "" {
projectLabel = model.ProjectId
}
params.Printer.Info("No networks found for project %q\n", projectLabel)
return nil
items := resp.GetItems()

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
}

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

return outputResult(params.Printer, model.OutputFormat, items)
return outputResult(params.Printer, model.OutputFormat, projectLabel, items)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -139,8 +134,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
return req
}

func outputResult(p *print.Printer, outputFormat string, networks []iaas.Network) error {
func outputResult(p *print.Printer, outputFormat, projectLabel string, networks []iaas.Network) error {
return p.OutputResult(outputFormat, networks, func() error {
if len(networks) == 0 {
p.Outputf("No networks found for project %q\n", projectLabel)
return nil
}

table := tables.NewTable()
table.SetHeader("ID", "NAME", "STATUS", "PUBLIC IP", "PREFIXES", "ROUTED", "ROUTING TABLE ID")

Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/network/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func TestBuildRequest(t *testing.T) {
func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
networks []iaas.Network
}
tests := []struct {
Expand All @@ -202,7 +203,7 @@ func TestOutputResult(t *testing.T) {
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.networks); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.networks); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
Loading