@@ -23,18 +23,22 @@ import (
2323
2424type inputModel struct {
2525 * globalflags.GlobalFlagModel
26- Limit * int64
26+ Limit * int64
27+ MinVCPUs * int64
28+ MinRAM * int64
2729}
2830
2931const (
30- limitFlag = "limit"
32+ limitFlag = "limit"
33+ minVcpuFlag = "min-vcpu"
34+ minRamFlag = "min-ram"
3135)
3236
3337func NewCmd (params * types.CmdParams ) * cobra.Command {
3438 cmd := & cobra.Command {
3539 Use : "list" ,
3640 Short : "Get list of all machine types available in a project" ,
37- Long : "Get list of all machine types available in a project." ,
41+ Long : "Get list of all machine types available in a project. Supports filtering by minimum vCPU and RAM (GB). " ,
3842 Args : args .NoArgs ,
3943 Example : examples .Build (
4044 examples .NewExample (
@@ -49,6 +53,10 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
4953 `List the first 10 machine types` ,
5054 `$ stackit server machine-type list --limit=10` ,
5155 ),
56+ examples .NewExample (
57+ `Filter for machines with at least 8 vCPUs and 16GB RAM` ,
58+ "$ stackit server machine-type list --min-vcpu 8 --min-ram 16" ,
59+ ),
5260 ),
5361 RunE : func (cmd * cobra.Command , args []string ) error {
5462 ctx := context .Background ()
@@ -73,18 +81,26 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7381 if resp .Items == nil || len (* resp .Items ) == 0 {
7482 projectLabel , err := projectname .GetProjectName (ctx , params .Printer , params .CliVersion , cmd )
7583 if err != nil {
76- params .Printer .Debug (print .ErrorLevel , "get project name: %v" , err )
7784 projectLabel = model .ProjectId
7885 }
7986 params .Printer .Info ("No machine-types found for project %q\n " , projectLabel )
8087 return nil
8188 }
8289
83- // limit output
84- if model .Limit != nil && len (* resp .Items ) > int (* model .Limit ) {
85- * resp .Items = (* resp .Items )[:* model .Limit ]
90+ // Filter the items client-side
91+ filteredItems := filterMachineTypes (resp .Items , model )
92+
93+ if len (filteredItems ) == 0 {
94+ params .Printer .Info ("No machine-types found matching the criteria\n " )
95+ return nil
8696 }
8797
98+ // Apply limit to results
99+ if model .Limit != nil && len (filteredItems ) > int (* model .Limit ) {
100+ filteredItems = filteredItems [:* model .Limit ]
101+ }
102+
103+ resp .Items = & filteredItems
88104 return outputResult (params .Printer , model .OutputFormat , * resp )
89105 },
90106 }
@@ -95,6 +111,8 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
95111
96112func configureFlags (cmd * cobra.Command ) {
97113 cmd .Flags ().Int64 (limitFlag , 0 , "Limit the output to the first n elements" )
114+ cmd .Flags ().Int64 (minVcpuFlag , 0 , "Filter by minimum number of vCPUs" )
115+ cmd .Flags ().Int64 (minRamFlag , 0 , "Filter by minimum RAM amount in GB" )
98116}
99117
100118func parseInput (p * print.Printer , cmd * cobra.Command , _ []string ) (* inputModel , error ) {
@@ -105,21 +123,48 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
105123
106124 limit := flags .FlagToInt64Pointer (p , cmd , limitFlag )
107125 if limit != nil && * limit < 1 {
108- return nil , & errors.FlagValidationError {
109- Flag : limitFlag ,
110- Details : "must be greater than 0" ,
111- }
126+ return nil , & errors.FlagValidationError {Flag : limitFlag , Details : "must be greater than 0" }
112127 }
113128
114129 model := inputModel {
115130 GlobalFlagModel : globalFlags ,
116- Limit : flags .FlagToInt64Pointer (p , cmd , limitFlag ),
131+ Limit : limit ,
132+ MinVCPUs : flags .FlagToInt64Pointer (p , cmd , minVcpuFlag ),
133+ MinRAM : flags .FlagToInt64Pointer (p , cmd , minRamFlag ),
117134 }
118135
119136 p .DebugInputModel (model )
120137 return & model , nil
121138}
122139
140+ // filterMachineTypes applies logic to filter by resource minimums.
141+ // Discuss: hide deprecated machine-types?
142+ func filterMachineTypes (items * []iaas.MachineType , model * inputModel ) []iaas.MachineType {
143+ if items == nil {
144+ return []iaas.MachineType {}
145+ }
146+
147+ var filtered []iaas.MachineType
148+ for _ , item := range * items {
149+ // Minimum vCPU check
150+ if model .MinVCPUs != nil && * model .MinVCPUs > 0 {
151+ if item .Vcpus == nil || * item .Vcpus < * model .MinVCPUs {
152+ continue
153+ }
154+ }
155+
156+ // Minimum RAM check (converting API MB to GB)
157+ if model .MinRAM != nil && * model .MinRAM > 0 {
158+ if item .Ram == nil || (* item .Ram / 1024 ) < * model .MinRAM {
159+ continue
160+ }
161+ }
162+
163+ filtered = append (filtered , item )
164+ }
165+ return filtered
166+ }
167+
123168func buildRequest (ctx context.Context , model * inputModel , apiClient * iaas.APIClient ) iaas.ApiListMachineTypesRequest {
124169 return apiClient .ListMachineTypes (ctx , model .ProjectId , model .Region )
125170}
@@ -128,19 +173,32 @@ func outputResult(p *print.Printer, outputFormat string, machineTypes iaas.Machi
128173 return p .OutputResult (outputFormat , machineTypes , func () error {
129174 table := tables .NewTable ()
130175 table .SetTitle ("Machine-Types" )
176+ table .SetHeader ("NAME" , "VCPUS" , "RAM (GB)" , "DESCRIPTION" , "EXTRA SPECS" )
131177
132- table .SetHeader ("NAME" , "DESCRIPTION" )
133178 if items := machineTypes .GetItems (); len (items ) > 0 {
134- for _ , machineType := range items {
135- table .AddRow (* machineType .Name , utils .PtrString (machineType .Description ))
136- }
137- }
179+ for _ , mt := range items {
180+ extraSpecMap := make (map [string ]string )
181+ if mt .ExtraSpecs != nil && len (* mt .ExtraSpecs ) > 0 {
182+ for key , value := range * mt .ExtraSpecs {
183+ extraSpecMap [key ] = fmt .Sprintf ("%v" , value )
184+ }
185+ }
138186
139- err := table . Display ( p )
140- if err != nil {
141- return fmt . Errorf ( "render table: %w" , err )
142- }
187+ ramGB := int64 ( 0 )
188+ if mt . Ram != nil {
189+ ramGB = * mt . Ram / 1024
190+ }
143191
144- return nil
192+ table .AddRow (
193+ utils .PtrString (mt .Name ),
194+ utils .PtrValue (mt .Vcpus ),
195+ ramGB ,
196+ utils .PtrString (mt .Description ),
197+ utils .JoinStringMap (extraSpecMap , ": " , "\n " ),
198+ )
199+ table .AddSeparator ()
200+ }
201+ }
202+ return table .Display (p )
145203 })
146204}
0 commit comments