Merge "compute: Add 'server create --server-group' option"

This commit is contained in:
Zuul 2023-12-05 18:43:15 +00:00 committed by Gerrit Code Review
commit 2642b070db
3 changed files with 33 additions and 6 deletions

View File

@ -1338,10 +1338,19 @@ class CreateServer(command.ShowOne):
'(supported by --os-compute-api-version 2.74 or above)'
),
)
parser.add_argument(
'--server-group',
metavar='<server-group>',
help=_(
"Server group to create the server within "
"(this is an alias for '--hint group=<server-group-id>')"
),
)
parser.add_argument(
'--hint',
metavar='<key=value>',
action=parseractions.KeyValueAppendAction,
dest='hints',
default={},
help=_('Hints for the scheduler'),
)
@ -1860,13 +1869,20 @@ class CreateServer(command.ShowOne):
security_group_names.append(sg['name'])
hints = {}
for key, values in parsed_args.hint.items():
for key, values in parsed_args.hints.items():
# only items with multiple values will result in a list
if len(values) == 1:
hints[key] = values[0]
else:
hints[key] = values
if parsed_args.server_group:
server_group_obj = utils.find_resource(
compute_client.server_groups,
parsed_args.server_group,
)
hints['group'] = server_group_obj.id
if isinstance(parsed_args.config_drive, bool):
# NOTE(stephenfin): The API doesn't accept False as a value :'(
config_drive = parsed_args.config_drive or None

View File

@ -1446,6 +1446,8 @@ class TestServerCreate(TestServer):
'a=b',
'--hint',
'a=c',
'--server-group',
'servergroup',
self.new_server.name,
]
verifylist = [
@ -1454,22 +1456,26 @@ class TestServerCreate(TestServer):
('key_name', 'keyname'),
('properties', {'Beta': 'b'}),
('security_group', ['securitygroup']),
('hint', {'a': ['b', 'c']}),
('hints', {'a': ['b', 'c']}),
('server_group', 'servergroup'),
('config_drive', True),
('password', 'passw0rd'),
('server_name', self.new_server.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.
fake_server_group = compute_fakes.create_one_server_group()
self.compute_client.server_groups.get.return_value = fake_server_group
fake_sg = network_fakes.FakeSecurityGroup.create_security_groups()
mock_find_sg = network_fakes.FakeSecurityGroup.get_security_groups(
fake_sg
)
self.app.client_manager.network.find_security_group = mock_find_sg
# 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)
mock_find_sg.assert_called_once_with(
@ -1489,7 +1495,7 @@ class TestServerCreate(TestServer):
admin_pass='passw0rd',
block_device_mapping_v2=[],
nics=[],
scheduler_hints={'a': ['b', 'c']},
scheduler_hints={'a': ['b', 'c'], 'group': fake_server_group.id},
config_drive=True,
)
# ServerManager.create(name, image, flavor, **kwargs)

View File

@ -0,0 +1,5 @@
---
features:
- |
The ``server create`` command now accepts a new option, ``--server-group``,
which is a shortcut for configuring the ``group`` scheduler hint.