Skip to content

Commit a7928cb

Browse files
committed
resolved comments
1 parent 796aea1 commit a7928cb

File tree

3 files changed

+41
-43
lines changed

3 files changed

+41
-43
lines changed

docs/stackit_network-interface_list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ stackit network-interface list [flags]
1313
### Examples
1414

1515
```
16-
Lists all network interfaces in your current project
16+
Lists all network interfaces
1717
$ stackit network-interface list
1818
1919
Lists all network interfaces with network ID "xxx"

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

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package list
22

33
import (
4-
"bytes"
4+
"cmp"
55
"context"
66
"fmt"
7-
"sort"
7+
"slices"
88

99
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
1010
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
@@ -33,7 +33,7 @@ type inputModel struct {
3333
*globalflags.GlobalFlagModel
3434
Limit *int64
3535
LabelSelector *string
36-
NetworkId string
36+
NetworkId *string
3737
}
3838

3939
func NewCmd(params *types.CmdParams) *cobra.Command {
@@ -43,8 +43,9 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
4343
Long: "Lists all network interfaces of a network.",
4444
Args: args.NoArgs,
4545
Example: examples.Build(
46+
// Note: this subcommand uses two different API enpoints, which makes the implementation somewhat messy
4647
examples.NewExample(
47-
`Lists all network interfaces in your current project`,
48+
`Lists all network interfaces`,
4849
`$ stackit network-interface list`,
4950
),
5051
examples.NewExample(
@@ -77,52 +78,60 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7778
return err
7879
}
7980

80-
// Call API
81-
if model.NetworkId == "" {
82-
// Return all NICs in the Project
81+
if model.NetworkId == nil {
82+
// Call API to get all NICs in the Project
8383
req := buildProjectRequest(ctx, model, apiClient)
84-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
85-
if err != nil {
86-
projectLabel = model.ProjectId
87-
}
8884

8985
resp, err := req.Execute()
9086
if err != nil {
9187
return fmt.Errorf("list network interfaces: %w", err)
9288
}
9389

90+
if resp.Items == nil || len(*resp.Items) == 0 {
91+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
92+
if err != nil {
93+
projectLabel = model.ProjectId
94+
}
95+
params.Printer.Outputf("No network interfaces found for project %q\n", projectLabel)
96+
return nil
97+
}
98+
9499
// Truncate output
95100
items := *resp.Items
96101
if model.Limit != nil && len(items) > int(*model.Limit) {
97102
items = items[:*model.Limit]
98103
}
99104

100-
return outputProjectResult(params.Printer, model.OutputFormat, items, projectLabel)
105+
return outputProjectResult(params.Printer, model.OutputFormat, items)
101106
}
102107

103-
// Return the NICs for one Network
108+
// Call API to get NICs for one Network
104109
req := buildNetworkRequest(ctx, model, apiClient)
105110

106-
networkLabel, err := iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, model.Region, model.NetworkId)
107-
if err != nil {
108-
params.Printer.Debug(print.ErrorLevel, "get network name: %v", err)
109-
networkLabel = model.NetworkId
110-
} else if networkLabel == "" {
111-
networkLabel = model.NetworkId
112-
}
113-
114111
resp, err := req.Execute()
115112
if err != nil {
116113
return fmt.Errorf("list network interfaces: %w", err)
117114
}
118115

116+
if resp.Items == nil || len(*resp.Items) == 0 {
117+
networkLabel, err := iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, model.Region, *model.NetworkId)
118+
if err != nil {
119+
params.Printer.Debug(print.ErrorLevel, "get network name: %v", err)
120+
networkLabel = *model.NetworkId
121+
} else if networkLabel == "" {
122+
networkLabel = *model.NetworkId
123+
}
124+
params.Printer.Outputf("No network interfaces found for network %q\n", networkLabel)
125+
return nil
126+
}
127+
119128
// Truncate output
120129
items := *resp.Items
121130
if model.Limit != nil && len(items) > int(*model.Limit) {
122131
items = items[:*model.Limit]
123132
}
124133

125-
return outputNetworkResult(params.Printer, model.OutputFormat, items, networkLabel)
134+
return outputNetworkResult(params.Printer, model.OutputFormat, items)
126135
},
127136
}
128137
configureFlags(cmd)
@@ -153,7 +162,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
153162
GlobalFlagModel: globalFlags,
154163
Limit: limit,
155164
LabelSelector: flags.FlagToStringPointer(p, cmd, labelSelectorFlag),
156-
NetworkId: flags.FlagToStringValue(p, cmd, networkIdFlag),
165+
NetworkId: flags.FlagToStringPointer(p, cmd, networkIdFlag),
157166
}
158167

159168
p.DebugInputModel(model)
@@ -170,24 +179,18 @@ func buildProjectRequest(ctx context.Context, model *inputModel, apiClient *iaas
170179
}
171180

172181
func buildNetworkRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiListNicsRequest {
173-
req := apiClient.ListNics(ctx, model.ProjectId, model.Region, model.NetworkId)
182+
req := apiClient.ListNics(ctx, model.ProjectId, model.Region, *model.NetworkId)
174183
if model.LabelSelector != nil {
175184
req = req.LabelSelector(*model.LabelSelector)
176185
}
177186

178187
return req
179188
}
180189

181-
func outputProjectResult(p *print.Printer, outputFormat string, nics []iaas.NIC, projectLabel string) error {
190+
func outputProjectResult(p *print.Printer, outputFormat string, nics []iaas.NIC) error {
182191
return p.OutputResult(outputFormat, nics, func() error {
183-
if len(nics) == 0 {
184-
p.Outputf("No network interfaces found for project %q\n", projectLabel)
185-
return nil
186-
}
187-
188-
sort.SliceStable(nics, func(i, j int) bool {
189-
result := bytes.Compare([]byte(*nics[i].NetworkId), []byte(*nics[j].NetworkId))
190-
return result == -1
192+
slices.SortFunc(nics, func(a, b iaas.NIC) int {
193+
return cmp.Compare(*a.NetworkId, *b.NetworkId)
191194
})
192195

193196
table := tables.NewTable()
@@ -212,13 +215,8 @@ func outputProjectResult(p *print.Printer, outputFormat string, nics []iaas.NIC,
212215
})
213216
}
214217

215-
func outputNetworkResult(p *print.Printer, outputFormat string, nics []iaas.NIC, networkLabel string) error {
218+
func outputNetworkResult(p *print.Printer, outputFormat string, nics []iaas.NIC) error {
216219
return p.OutputResult(outputFormat, nics, func() error {
217-
if len(nics) == 0 {
218-
p.Outputf("No network interfaces found for network %q\n", networkLabel)
219-
return nil
220-
}
221-
222220
table := tables.NewTable()
223221
table.SetHeader("ID", "NAME", "NIC SECURITY", "DEVICE ID", "IPv4 ADDRESS", "STATUS", "TYPE")
224222

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
5353
},
5454
Limit: utils.Ptr(int64(10)),
5555
LabelSelector: utils.Ptr(testLabelSelector),
56-
NetworkId: testNetworkId,
56+
NetworkId: utils.Ptr(testNetworkId),
5757
}
5858
for _, mod := range mods {
5959
mod(model)
@@ -241,7 +241,7 @@ func TestOutputProjectResult(t *testing.T) {
241241
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
242242
for _, tt := range tests {
243243
t.Run(tt.name, func(t *testing.T) {
244-
if err := outputProjectResult(p, tt.args.outputFormat, tt.args.nics, "Label"); (err != nil) != tt.wantErr {
244+
if err := outputProjectResult(p, tt.args.outputFormat, tt.args.nics); (err != nil) != tt.wantErr {
245245
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
246246
}
247247
})
@@ -276,7 +276,7 @@ func TestOutputNetworkResult(t *testing.T) {
276276
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
277277
for _, tt := range tests {
278278
t.Run(tt.name, func(t *testing.T) {
279-
if err := outputNetworkResult(p, tt.args.outputFormat, tt.args.nics, "Label"); (err != nil) != tt.wantErr {
279+
if err := outputNetworkResult(p, tt.args.outputFormat, tt.args.nics); (err != nil) != tt.wantErr {
280280
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
281281
}
282282
})

0 commit comments

Comments
 (0)