diff --git a/openstackclient/compute/v2/flavor.py b/openstackclient/compute/v2/flavor.py index 8477e8efb1..fa98e131b1 100644 --- a/openstackclient/compute/v2/flavor.py +++ b/openstackclient/compute/v2/flavor.py @@ -263,6 +263,18 @@ class ListFlavor(command.Lister): default=False, help=_("List all flavors, whether public or private") ) + parser.add_argument( + '--min-disk', + type=int, + metavar='', + help=_('Filters the flavors by a minimum disk space, in GiB.'), + ) + parser.add_argument( + '--min-ram', + type=int, + metavar='', + help=_('Filters the flavors by a minimum RAM, in MiB.'), + ) parser.add_argument( '--long', action='store_true', @@ -277,8 +289,13 @@ class ListFlavor(command.Lister): parser.add_argument( '--limit', type=int, - metavar="", - help=_("Maximum number of flavors to display") + metavar='', + help=_( + 'Maximum number of flavors to display. This is also ' + 'configurable on the server. The actual limit used will be ' + 'the lower of the user-supplied value and the server ' + 'configuration-derived value' + ), ) return parser @@ -293,15 +310,24 @@ class ListFlavor(command.Lister): query_attrs = { 'is_public': is_public } + if parsed_args.marker: query_attrs['marker'] = parsed_args.marker + if parsed_args.limit: query_attrs['limit'] = parsed_args.limit + if parsed_args.limit or parsed_args.marker: # User passed explicit pagination request, switch off SDK # pagination query_attrs['paginated'] = False + if parsed_args.min_disk: + query_attrs['min_disk'] = parsed_args.min_disk + + if parsed_args.min_ram: + query_attrs['min_ram'] = parsed_args.min_ram + data = list(compute_client.flavors(**query_attrs)) # Even if server supports 2.61 some policy might stop it sending us # extra_specs. So try to fetch them if they are absent @@ -341,10 +367,13 @@ class ListFlavor(command.Lister): "Properties", ) - return (column_headers, - (utils.get_item_properties( - s, columns, formatters=_formatters, - ) for s in data)) + return ( + column_headers, + ( + utils.get_item_properties(s, columns, formatters=_formatters) + for s in data + ), + ) class SetFlavor(command.Command): @@ -378,13 +407,13 @@ class SetFlavor(command.Command): help=_('Set flavor access to project (name or ID) ' '(admin only)'), ) + identity_common.add_project_domain_option_to_parser(parser) parser.add_argument( '--description', metavar='', help=_("Set description for the flavor.(Supported by API " "versions '2.55' - '2.latest'") ) - identity_common.add_project_domain_option_to_parser(parser) return parser diff --git a/openstackclient/tests/unit/compute/v2/test_flavor.py b/openstackclient/tests/unit/compute/v2/test_flavor.py index 8625b71202..af9e4a2c33 100644 --- a/openstackclient/tests/unit/compute/v2/test_flavor.py +++ b/openstackclient/tests/unit/compute/v2/test_flavor.py @@ -635,6 +635,37 @@ class TestFlavorList(TestFlavor): self.assertEqual(self.columns_long, columns) self.assertListItemEqual(self.data_long, tuple(data)) + def test_flavor_list_min_disk_min_ram(self): + arglist = [ + '--min-disk', '10', + '--min-ram', '2048', + ] + verifylist = [ + ('min_disk', 10), + ('min_ram', 2048), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # In base command class Lister in cliff, abstract method take_action() + # returns a tuple containing the column names and an iterable + # containing the data to be listed. + columns, data = self.cmd.take_action(parsed_args) + + # Set expected values + kwargs = { + 'is_public': True, + 'min_disk': 10, + 'min_ram': 2048, + } + + self.sdk_client.flavors.assert_called_with( + **kwargs + ) + + self.assertEqual(self.columns, columns) + self.assertEqual(tuple(self.data), tuple(data)) + class TestFlavorSet(TestFlavor): diff --git a/releasenotes/notes/flavor-list-min-disk-min-ram-65ba35e7acc24134.yaml b/releasenotes/notes/flavor-list-min-disk-min-ram-65ba35e7acc24134.yaml new file mode 100644 index 0000000000..3418276c89 --- /dev/null +++ b/releasenotes/notes/flavor-list-min-disk-min-ram-65ba35e7acc24134.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + The ``openstack flavor list`` command now accepts two additional + options, ``--min-disk`` and ``--min-ram``, to filter flavor by + minimum disk and minimum RAM, respectively.