Add 'flavor list --min-disk', '--min-ram' options

Allow us to filter on minimum disk and RAM, and close another gap with
novaclient.

Change-Id: Ib3f0bdf419675e1c35c3406fbac8a4c18ac56a33
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2020-10-14 10:48:44 +01:00
parent 0edd055f3f
commit da03bd80e3
3 changed files with 73 additions and 7 deletions

View File

@ -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='<min-disk>',
help=_('Filters the flavors by a minimum disk space, in GiB.'),
)
parser.add_argument(
'--min-ram',
type=int,
metavar='<min-ram>',
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="<num-flavors>",
help=_("Maximum number of flavors to display")
metavar='<num-flavors>',
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='<description>',
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

View File

@ -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):

View File

@ -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.