@@ -23,31 +23,31 @@ 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 (
4145 `Get list of all machine types` ,
4246 "$ stackit server machine-type list" ,
4347 ),
4448 examples .NewExample (
45- `Get list of all machine types in JSON format` ,
46- "$ stackit server machine-type list --output-format json" ,
47- ),
48- examples .NewExample (
49- `List the first 10 machine types` ,
50- `$ stackit server machine-type list --limit=10` ,
49+ `Filter for machines with at least 8 vCPUs and 16GB RAM` ,
50+ "$ stackit server machine-type list --min-vcpu 8 --min-ram 16" ,
5151 ),
5252 ),
5353 RunE : func (cmd * cobra.Command , args []string ) error {
@@ -73,18 +73,26 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7373 if resp .Items == nil || len (* resp .Items ) == 0 {
7474 projectLabel , err := projectname .GetProjectName (ctx , params .Printer , params .CliVersion , cmd )
7575 if err != nil {
76- params .Printer .Debug (print .ErrorLevel , "get project name: %v" , err )
7776 projectLabel = model .ProjectId
7877 }
7978 params .Printer .Info ("No machine-types found for project %q\n " , projectLabel )
8079 return nil
8180 }
8281
83- // limit output
84- if model .Limit != nil && len (* resp .Items ) > int (* model .Limit ) {
85- * resp .Items = (* resp .Items )[:* model .Limit ]
82+ // Filter the items client-side
83+ filteredItems := filterMachineTypes (resp .Items , model )
84+
85+ if len (filteredItems ) == 0 {
86+ params .Printer .Info ("No machine-types found matching the criteria\n " )
87+ return nil
8688 }
8789
90+ // Apply limit to results
91+ if model .Limit != nil && len (filteredItems ) > int (* model .Limit ) {
92+ filteredItems = filteredItems [:* model .Limit ]
93+ }
94+
95+ resp .Items = & filteredItems
8896 return outputResult (params .Printer , model .OutputFormat , * resp )
8997 },
9098 }
@@ -95,6 +103,8 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
95103
96104func configureFlags (cmd * cobra.Command ) {
97105 cmd .Flags ().Int64 (limitFlag , 0 , "Limit the output to the first n elements" )
106+ cmd .Flags ().Int64 (minVcpuFlag , 0 , "Filter by minimum number of vCPUs" )
107+ cmd .Flags ().Int64 (minRamFlag , 0 , "Filter by minimum RAM amount in GB" )
98108}
99109
100110func parseInput (p * print.Printer , cmd * cobra.Command , _ []string ) (* inputModel , error ) {
@@ -105,21 +115,48 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
105115
106116 limit := flags .FlagToInt64Pointer (p , cmd , limitFlag )
107117 if limit != nil && * limit < 1 {
108- return nil , & errors.FlagValidationError {
109- Flag : limitFlag ,
110- Details : "must be greater than 0" ,
111- }
118+ return nil , & errors.FlagValidationError {Flag : limitFlag , Details : "must be greater than 0" }
112119 }
113120
114121 model := inputModel {
115122 GlobalFlagModel : globalFlags ,
116- Limit : flags .FlagToInt64Pointer (p , cmd , limitFlag ),
123+ Limit : limit ,
124+ MinVCPUs : flags .FlagToInt64Pointer (p , cmd , minVcpuFlag ),
125+ MinRAM : flags .FlagToInt64Pointer (p , cmd , minRamFlag ),
117126 }
118127
119128 p .DebugInputModel (model )
120129 return & model , nil
121130}
122131
132+ // filterMachineTypes applies logic to filter by resource minimums.
133+ // Note: Deprecated items are NOT hidden.
134+ func filterMachineTypes (items * []iaas.MachineType , model * inputModel ) []iaas.MachineType {
135+ if items == nil {
136+ return []iaas.MachineType {}
137+ }
138+
139+ var filtered []iaas.MachineType
140+ for _ , item := range * items {
141+ // Minimum vCPU check
142+ if model .MinVCPUs != nil && * model .MinVCPUs > 0 {
143+ if item .Vcpus == nil || * item .Vcpus < * model .MinVCPUs {
144+ continue
145+ }
146+ }
147+
148+ // Minimum RAM check (converting API MB to GB)
149+ if model .MinRAM != nil && * model .MinRAM > 0 {
150+ if item .Ram == nil || (* item .Ram / 1024 ) < * model .MinRAM {
151+ continue
152+ }
153+ }
154+
155+ filtered = append (filtered , item )
156+ }
157+ return filtered
158+ }
159+
123160func buildRequest (ctx context.Context , model * inputModel , apiClient * iaas.APIClient ) iaas.ApiListMachineTypesRequest {
124161 return apiClient .ListMachineTypes (ctx , model .ProjectId , model .Region )
125162}
@@ -128,19 +165,32 @@ func outputResult(p *print.Printer, outputFormat string, machineTypes iaas.Machi
128165 return p .OutputResult (outputFormat , machineTypes , func () error {
129166 table := tables .NewTable ()
130167 table .SetTitle ("Machine-Types" )
168+ table .SetHeader ("NAME" , "VCPUS" , "RAM (GB)" , "DESCRIPTION" , "EXTRA SPECS" )
131169
132- table .SetHeader ("NAME" , "DESCRIPTION" )
133170 if items := machineTypes .GetItems (); len (items ) > 0 {
134- for _ , machineType := range items {
135- table .AddRow (* machineType .Name , utils .PtrString (machineType .Description ))
136- }
137- }
171+ for _ , mt := range items {
172+ extraSpecMap := make (map [string ]string )
173+ if mt .ExtraSpecs != nil && len (* mt .ExtraSpecs ) > 0 {
174+ for key , value := range * mt .ExtraSpecs {
175+ extraSpecMap [key ] = fmt .Sprintf ("%v" , value )
176+ }
177+ }
138178
139- err := table . Display ( p )
140- if err != nil {
141- return fmt . Errorf ( "render table: %w" , err )
142- }
179+ ramGB := int64 ( 0 )
180+ if mt . Ram != nil {
181+ ramGB = * mt . Ram / 1024
182+ }
143183
144- return nil
184+ table .AddRow (
185+ utils .PtrString (mt .Name ),
186+ utils .PtrValue (mt .Vcpus ),
187+ ramGB ,
188+ utils .PtrString (mt .Description ),
189+ utils .JoinStringMap (extraSpecMap , ": " , "\n " ),
190+ )
191+ table .AddSeparator ()
192+ }
193+ }
194+ return table .Display (p )
145195 })
146196}
0 commit comments