11package list
22
33import (
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
3939func 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
172181func 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
0 commit comments