Merge "volume: Support same_host, different_host hint as list"

This commit is contained in:
Zuul 2023-10-31 14:53:35 +00:00 committed by Gerrit Code Review
commit d735b6cc07
2 changed files with 96 additions and 3 deletions

View File

@ -682,6 +682,74 @@ class TestVolumeCreate(TestVolume):
verifylist,
)
def test_volume_create_hints(self):
"""--hint needs to behave differently based on the given hint
different_host and same_host need to append to a list if given multiple
times. All other parameter are strings.
"""
arglist = [
'--size',
str(self.new_volume.size),
'--hint',
'k=v',
'--hint',
'k=v2',
'--hint',
'same_host=v3',
'--hint',
'same_host=v4',
'--hint',
'different_host=v5',
'--hint',
'local_to_instance=v6',
'--hint',
'different_host=v7',
self.new_volume.name,
]
verifylist = [
('size', self.new_volume.size),
(
'hint',
{
'k': 'v2',
'same_host': ['v3', 'v4'],
'local_to_instance': 'v6',
'different_host': ['v5', 'v7'],
},
),
('name', self.new_volume.name),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
# In base command class ShowOne in cliff, abstract method take_action()
# returns a two-part tuple with a tuple of column names and a tuple of
# data to be shown.
columns, data = self.cmd.take_action(parsed_args)
self.volumes_mock.create.assert_called_with(
size=self.new_volume.size,
snapshot_id=None,
name=self.new_volume.name,
description=None,
volume_type=None,
availability_zone=None,
metadata=None,
imageRef=None,
source_volid=None,
consistencygroup_id=None,
scheduler_hints={
'k': 'v2',
'same_host': ['v3', 'v4'],
'local_to_instance': 'v6',
'different_host': ['v5', 'v7'],
},
backup_id=None,
)
self.assertEqual(self.columns, columns)
self.assertCountEqual(self.datalist, data)
class TestVolumeDelete(TestVolume):
def setUp(self):

View File

@ -33,6 +33,29 @@ from openstackclient.identity import common as identity_common
LOG = logging.getLogger(__name__)
class KeyValueHintAction(argparse.Action):
"""Uses KeyValueAction or KeyValueAppendAction based on the given key"""
APPEND_KEYS = ('same_host', 'different_host')
def __init__(self, *args, **kwargs):
self._key_value_action = parseractions.KeyValueAction(*args, **kwargs)
self._key_value_append_action = parseractions.KeyValueAppendAction(
*args, **kwargs
)
super().__init__(*args, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
if values.startswith(self.APPEND_KEYS):
self._key_value_append_action(
parser, namespace, values, option_string=option_string
)
else:
self._key_value_action(
parser, namespace, values, option_string=option_string
)
class AttachmentsColumn(cliff_columns.FormattableColumn):
"""Formattable column for attachments column.
@ -162,10 +185,12 @@ class CreateVolume(command.ShowOne):
parser.add_argument(
"--hint",
metavar="<key=value>",
action=parseractions.KeyValueAction,
action=KeyValueHintAction,
help=_(
"Arbitrary scheduler hint key-value pairs to help boot "
"an instance (repeat option to set multiple hints)"
"Arbitrary scheduler hint key-value pairs to help creating "
"a volume. Repeat the option to set multiple hints. "
"'same_host' and 'different_host' get values appended when "
"repeated, all other keys take the last given value"
),
)
bootable_group = parser.add_mutually_exclusive_group()