Skip to content

Commit d735b6c

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "volume: Support same_host, different_host hint as list"
2 parents 8b019fa + 60a0e37 commit d735b6c

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

openstackclient/tests/unit/volume/v2/test_volume.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,74 @@ def test_volume_create_with_multi_source(self):
682682
verifylist,
683683
)
684684

685+
def test_volume_create_hints(self):
686+
"""--hint needs to behave differently based on the given hint
687+
688+
different_host and same_host need to append to a list if given multiple
689+
times. All other parameter are strings.
690+
"""
691+
arglist = [
692+
'--size',
693+
str(self.new_volume.size),
694+
'--hint',
695+
'k=v',
696+
'--hint',
697+
'k=v2',
698+
'--hint',
699+
'same_host=v3',
700+
'--hint',
701+
'same_host=v4',
702+
'--hint',
703+
'different_host=v5',
704+
'--hint',
705+
'local_to_instance=v6',
706+
'--hint',
707+
'different_host=v7',
708+
self.new_volume.name,
709+
]
710+
verifylist = [
711+
('size', self.new_volume.size),
712+
(
713+
'hint',
714+
{
715+
'k': 'v2',
716+
'same_host': ['v3', 'v4'],
717+
'local_to_instance': 'v6',
718+
'different_host': ['v5', 'v7'],
719+
},
720+
),
721+
('name', self.new_volume.name),
722+
]
723+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
724+
725+
# In base command class ShowOne in cliff, abstract method take_action()
726+
# returns a two-part tuple with a tuple of column names and a tuple of
727+
# data to be shown.
728+
columns, data = self.cmd.take_action(parsed_args)
729+
730+
self.volumes_mock.create.assert_called_with(
731+
size=self.new_volume.size,
732+
snapshot_id=None,
733+
name=self.new_volume.name,
734+
description=None,
735+
volume_type=None,
736+
availability_zone=None,
737+
metadata=None,
738+
imageRef=None,
739+
source_volid=None,
740+
consistencygroup_id=None,
741+
scheduler_hints={
742+
'k': 'v2',
743+
'same_host': ['v3', 'v4'],
744+
'local_to_instance': 'v6',
745+
'different_host': ['v5', 'v7'],
746+
},
747+
backup_id=None,
748+
)
749+
750+
self.assertEqual(self.columns, columns)
751+
self.assertCountEqual(self.datalist, data)
752+
685753

686754
class TestVolumeDelete(TestVolume):
687755
def setUp(self):

openstackclient/volume/v2/volume.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@
3333
LOG = logging.getLogger(__name__)
3434

3535

36+
class KeyValueHintAction(argparse.Action):
37+
"""Uses KeyValueAction or KeyValueAppendAction based on the given key"""
38+
39+
APPEND_KEYS = ('same_host', 'different_host')
40+
41+
def __init__(self, *args, **kwargs):
42+
self._key_value_action = parseractions.KeyValueAction(*args, **kwargs)
43+
self._key_value_append_action = parseractions.KeyValueAppendAction(
44+
*args, **kwargs
45+
)
46+
super().__init__(*args, **kwargs)
47+
48+
def __call__(self, parser, namespace, values, option_string=None):
49+
if values.startswith(self.APPEND_KEYS):
50+
self._key_value_append_action(
51+
parser, namespace, values, option_string=option_string
52+
)
53+
else:
54+
self._key_value_action(
55+
parser, namespace, values, option_string=option_string
56+
)
57+
58+
3659
class AttachmentsColumn(cliff_columns.FormattableColumn):
3760
"""Formattable column for attachments column.
3861
@@ -162,10 +185,12 @@ def get_parser(self, prog_name):
162185
parser.add_argument(
163186
"--hint",
164187
metavar="<key=value>",
165-
action=parseractions.KeyValueAction,
188+
action=KeyValueHintAction,
166189
help=_(
167-
"Arbitrary scheduler hint key-value pairs to help boot "
168-
"an instance (repeat option to set multiple hints)"
190+
"Arbitrary scheduler hint key-value pairs to help creating "
191+
"a volume. Repeat the option to set multiple hints. "
192+
"'same_host' and 'different_host' get values appended when "
193+
"repeated, all other keys take the last given value"
169194
),
170195
)
171196
bootable_group = parser.add_mutually_exclusive_group()

0 commit comments

Comments
 (0)