Skip to content

Commit fbaf227

Browse files
committed
adapted server network-interfaces list command to align to expectations
1 parent 0c3fb99 commit fbaf227

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

internal/cmd/server/network-interface/list/list.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,22 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7272
return fmt.Errorf("list attached network interfaces: %w", err)
7373
}
7474

75-
if resp.Items == nil || len(*resp.Items) == 0 {
76-
serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.Region, model.ServerId)
77-
if err != nil {
78-
params.Printer.Debug(print.ErrorLevel, "get server name: %v", err)
79-
serverLabel = model.ServerId
80-
} else if serverLabel == "" {
81-
serverLabel = model.ServerId
82-
}
83-
params.Printer.Info("No attached network interfaces found for server %q\n", serverLabel)
84-
return nil
75+
items := resp.GetItems()
76+
77+
serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.Region, model.ServerId)
78+
if err != nil {
79+
params.Printer.Debug(print.ErrorLevel, "get server name: %v", err)
80+
serverLabel = model.ServerId
81+
} else if serverLabel == "" {
82+
serverLabel = model.ServerId
8583
}
8684

8785
// Truncate output
88-
items := *resp.Items
8986
if model.Limit != nil && len(items) > int(*model.Limit) {
9087
items = items[:*model.Limit]
9188
}
9289

93-
return outputResult(params.Printer, model.OutputFormat, model.ServerId, items)
90+
return outputResult(params.Printer, model.OutputFormat, model.ServerId, serverLabel, items)
9491
},
9592
}
9693
configureFlags(cmd)
@@ -133,8 +130,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
133130
return apiClient.ListServerNICs(ctx, model.ProjectId, model.Region, model.ServerId)
134131
}
135132

136-
func outputResult(p *print.Printer, outputFormat, serverId string, serverNics []iaas.NIC) error {
133+
func outputResult(p *print.Printer, outputFormat, serverId, serverLabel string, serverNics []iaas.NIC) error {
137134
return p.OutputResult(outputFormat, serverNics, func() error {
135+
if len(serverNics) == 0 {
136+
p.Outputf("No attached network interfaces found for server %q\n", serverLabel)
137+
return nil
138+
}
138139
table := tables.NewTable()
139140
table.SetHeader("NIC ID", "SERVER ID")
140141

internal/cmd/server/network-interface/list/list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func TestOutputResult(t *testing.T) {
182182
type args struct {
183183
outputFormat string
184184
serverId string
185+
serverLabel string
185186
serverNics []iaas.NIC
186187
}
187188
tests := []struct {
@@ -208,7 +209,7 @@ func TestOutputResult(t *testing.T) {
208209
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
209210
for _, tt := range tests {
210211
t.Run(tt.name, func(t *testing.T) {
211-
if err := outputResult(p, tt.args.outputFormat, tt.args.serverId, tt.args.serverNics); (err != nil) != tt.wantErr {
212+
if err := outputResult(p, tt.args.outputFormat, tt.args.serverId, tt.args.serverLabel, tt.args.serverNics); (err != nil) != tt.wantErr {
212213
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
213214
}
214215
})

0 commit comments

Comments
 (0)