Skip to content

Commit b49d7a1

Browse files
committed
feat(iaas): List NICs for Project
relates to STACKITCLI-307 and #1214
1 parent 4074118 commit b49d7a1

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

docs/stackit_network-interface_list.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ stackit network-interface list [flags]
1313
### Examples
1414

1515
```
16+
Lists all network interfaces in your current project
17+
$ stackit network-interface list
18+
1619
Lists all network interfaces with network ID "xxx"
1720
$ stackit network-interface list --network-id xxx
1821

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

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ type inputModel struct {
3333
NetworkId string
3434
}
3535

36+
type ExecutableRequest interface {
37+
Execute() (*iaas.NICListResponse, error)
38+
}
39+
3640
func NewCmd(params *types.CmdParams) *cobra.Command {
3741
cmd := &cobra.Command{
3842
Use: "list",
3943
Short: "Lists all network interfaces of a network",
4044
Long: "Lists all network interfaces of a network.",
4145
Args: args.NoArgs,
4246
Example: examples.Build(
47+
examples.NewExample(
48+
`Lists all network interfaces in your current project`,
49+
`$ stackit network-interface list`,
50+
),
4351
examples.NewExample(
4452
`Lists all network interfaces with network ID "xxx"`,
4553
`$ stackit network-interface list --network-id xxx`,
@@ -71,22 +79,26 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7179
}
7280

7381
// Call API
74-
req := buildRequest(ctx, model, apiClient)
75-
resp, err := req.Execute()
76-
if err != nil {
77-
return fmt.Errorf("list network interfaces: %w", err)
78-
}
79-
80-
if resp.Items == nil || len(*resp.Items) == 0 {
81-
networkLabel, err := iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, model.Region, model.NetworkId)
82+
var req ExecutableRequest
83+
var networkLabel = ""
84+
if model.NetworkId == "" {
85+
// Return all NICs in the Project
86+
req = buildProjectRequest(ctx, model, apiClient)
87+
} else {
88+
// Return the NICs for one Network
89+
req = buildRequest(ctx, model, apiClient)
90+
91+
networkLabel, err = iaasUtils.GetNetworkName(ctx, apiClient, model.ProjectId, model.Region, model.NetworkId)
8292
if err != nil {
8393
params.Printer.Debug(print.ErrorLevel, "get network name: %v", err)
8494
networkLabel = model.NetworkId
8595
} else if networkLabel == "" {
8696
networkLabel = model.NetworkId
8797
}
88-
params.Printer.Info("No network interfaces found for network %q\n", networkLabel)
89-
return nil
98+
}
99+
resp, err := req.Execute()
100+
if err != nil {
101+
return fmt.Errorf("list network interfaces: %w", err)
90102
}
91103

92104
// Truncate output
@@ -95,7 +107,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
95107
items = items[:*model.Limit]
96108
}
97109

98-
return outputResult(params.Printer, model.OutputFormat, items)
110+
return outputResult(params.Printer, model.OutputFormat, items, networkLabel)
99111
},
100112
}
101113
configureFlags(cmd)
@@ -106,9 +118,6 @@ func configureFlags(cmd *cobra.Command) {
106118
cmd.Flags().Var(flags.UUIDFlag(), networkIdFlag, "Network ID")
107119
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")
108120
cmd.Flags().String(labelSelectorFlag, "", "Filter by label")
109-
110-
err := flags.MarkFlagsRequired(cmd, networkIdFlag)
111-
cobra.CheckErr(err)
112121
}
113122

114123
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
@@ -136,7 +145,16 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
136145
return &model, nil
137146
}
138147

139-
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiListNicsRequest {
148+
func buildProjectRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) ExecutableRequest {
149+
req := apiClient.ListProjectNICs(ctx, model.ProjectId, model.Region)
150+
if model.LabelSelector != nil {
151+
req = req.LabelSelector(*model.LabelSelector)
152+
}
153+
154+
return req
155+
}
156+
157+
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) ExecutableRequest {
140158
req := apiClient.ListNics(ctx, model.ProjectId, model.Region, model.NetworkId)
141159
if model.LabelSelector != nil {
142160
req = req.LabelSelector(*model.LabelSelector)
@@ -145,8 +163,17 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
145163
return req
146164
}
147165

148-
func outputResult(p *print.Printer, outputFormat string, nics []iaas.NIC) error {
166+
func outputResult(p *print.Printer, outputFormat string, nics []iaas.NIC, networkLabel string) error {
149167
return p.OutputResult(outputFormat, nics, func() error {
168+
if nics == nil || len(nics) == 0 {
169+
if networkLabel == "" {
170+
p.Outputf("No network interfaces found for your current project\n")
171+
} else {
172+
p.Outputf("No network interfaces found for network %q\n", networkLabel)
173+
}
174+
return nil
175+
}
176+
150177
table := tables.NewTable()
151178
table.SetHeader("ID", "NAME", "NIC SECURITY", "DEVICE ID", "IPv4 ADDRESS", "STATUS", "TYPE")
152179

0 commit comments

Comments
 (0)