11package list
22
33import (
4+ "bytes"
45 "context"
56 "fmt"
7+ "sort"
68
9+ "github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
710 "github.com/stackitcloud/stackit-cli/internal/pkg/types"
811
912 "github.com/spf13/cobra"
@@ -33,10 +36,6 @@ type inputModel struct {
3336 NetworkId string
3437}
3538
36- type ExecutableRequest interface {
37- Execute () (* iaas.NICListResponse , error )
38- }
39-
4039func NewCmd (params * types.CmdParams ) * cobra.Command {
4140 cmd := & cobra.Command {
4241 Use : "list" ,
@@ -79,23 +78,39 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7978 }
8079
8180 // Call API
82- var req ExecutableRequest
83- var networkLabel = ""
8481 if model .NetworkId == "" {
8582 // 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 )
83+ 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+ }
9088
91- networkLabel , err = iaasUtils . GetNetworkName ( ctx , apiClient , model . ProjectId , model . Region , model . NetworkId )
89+ resp , err := req . Execute ( )
9290 if err != nil {
93- params .Printer .Debug (print .ErrorLevel , "get network name: %v" , err )
94- networkLabel = model .NetworkId
95- } else if networkLabel == "" {
96- networkLabel = model .NetworkId
91+ return fmt .Errorf ("list network interfaces: %w" , err )
92+ }
93+
94+ // Truncate output
95+ items := * resp .Items
96+ if model .Limit != nil && len (items ) > int (* model .Limit ) {
97+ items = items [:* model .Limit ]
9798 }
99+
100+ return outputProjectResult (params .Printer , model .OutputFormat , items , projectLabel )
101+ }
102+
103+ // Return the NICs for one Network
104+ req := buildNetworkRequest (ctx , model , apiClient )
105+
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
98112 }
113+
99114 resp , err := req .Execute ()
100115 if err != nil {
101116 return fmt .Errorf ("list network interfaces: %w" , err )
@@ -107,7 +122,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
107122 items = items [:* model .Limit ]
108123 }
109124
110- return outputResult (params .Printer , model .OutputFormat , items , networkLabel )
125+ return outputNetworkResult (params .Printer , model .OutputFormat , items , networkLabel )
111126 },
112127 }
113128 configureFlags (cmd )
@@ -145,7 +160,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
145160 return & model , nil
146161}
147162
148- func buildProjectRequest (ctx context.Context , model * inputModel , apiClient * iaas.APIClient ) ExecutableRequest {
163+ func buildProjectRequest (ctx context.Context , model * inputModel , apiClient * iaas.APIClient ) iaas. ApiListProjectNICsRequest {
149164 req := apiClient .ListProjectNICs (ctx , model .ProjectId , model .Region )
150165 if model .LabelSelector != nil {
151166 req = req .LabelSelector (* model .LabelSelector )
@@ -154,7 +169,7 @@ func buildProjectRequest(ctx context.Context, model *inputModel, apiClient *iaas
154169 return req
155170}
156171
157- func buildRequest (ctx context.Context , model * inputModel , apiClient * iaas.APIClient ) ExecutableRequest {
172+ func buildNetworkRequest (ctx context.Context , model * inputModel , apiClient * iaas.APIClient ) iaas. ApiListNicsRequest {
158173 req := apiClient .ListNics (ctx , model .ProjectId , model .Region , model .NetworkId )
159174 if model .LabelSelector != nil {
160175 req = req .LabelSelector (* model .LabelSelector )
@@ -163,14 +178,44 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
163178 return req
164179}
165180
166- func outputResult (p * print.Printer , outputFormat string , nics []iaas.NIC , networkLabel string ) error {
181+ func outputProjectResult (p * print.Printer , outputFormat string , nics []iaas.NIC , projectLabel string ) error {
167182 return p .OutputResult (outputFormat , nics , func () error {
168183 if 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- }
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
191+ })
192+
193+ table := tables .NewTable ()
194+ table .SetHeader ("ID" , "NAME" , "NETWORK ID" , "NIC SECURITY" , "DEVICE ID" , "IPv4 ADDRESS" , "STATUS" , "TYPE" )
195+
196+ for _ , nic := range nics {
197+ table .AddRow (
198+ utils .PtrString (nic .Id ),
199+ utils .PtrString (nic .Name ),
200+ utils .PtrString (nic .NetworkId ),
201+ utils .PtrString (nic .NicSecurity ),
202+ utils .PtrString (nic .Device ),
203+ utils .PtrString (nic .Ipv4 ),
204+ utils .PtrString (nic .Status ),
205+ utils .PtrString (nic .Type ),
206+ )
207+ table .AddSeparator ()
208+ }
209+
210+ p .Outputln (table .Render ())
211+ return nil
212+ })
213+ }
214+
215+ func outputNetworkResult (p * print.Printer , outputFormat string , nics []iaas.NIC , networkLabel string ) error {
216+ return p .OutputResult (outputFormat , nics , func () error {
217+ if len (nics ) == 0 {
218+ p .Outputf ("No network interfaces found for network %q\n " , networkLabel )
174219 return nil
175220 }
176221
0 commit comments