Add group parameter to create_server

Server groups are a user-facing feature and can be requested via
scheduler hints. While that already exists it's not the world's cleanest
user interface, so add a specific parameter which will set the right
thing into the scheduler hints dict.

Change-Id: Idb28779ed1fde341acab2116b510fce349f74b50
This commit is contained in:
Monty Taylor 2017-10-11 12:09:56 -05:00 committed by David Shrewsbury
parent e7ade19fbb
commit fd9c2b5729
2 changed files with 21 additions and 3 deletions

View File

@ -0,0 +1,4 @@
---
features:
- Added ``group`` parameter to create_server to allow
booting a server into a specific server group.

View File

@ -6390,6 +6390,7 @@ class OpenStackCloud(
wait=False, timeout=180, reuse_ips=True, wait=False, timeout=180, reuse_ips=True,
network=None, boot_from_volume=False, volume_size='50', network=None, boot_from_volume=False, volume_size='50',
boot_volume=None, volumes=None, nat_destination=None, boot_volume=None, volumes=None, nat_destination=None,
group=None,
**kwargs): **kwargs):
"""Create a virtual server instance. """Create a virtual server instance.
@ -6468,6 +6469,10 @@ class OpenStackCloud(
be attached to, if it's not possible to be attached to, if it's not possible to
infer from the cloud's configuration. infer from the cloud's configuration.
(Optional, defaults to None) (Optional, defaults to None)
:param group: ServerGroup dict, name or id to boot the server in.
If a group is provided in both scheduler_hints and in
the group param, the group param will win.
(Optional, defaults to None)
:returns: A ``munch.Munch`` representing the created server. :returns: A ``munch.Munch`` representing the created server.
:raises: OpenStackCloudException on operation error. :raises: OpenStackCloudException on operation error.
""" """
@ -6477,15 +6482,14 @@ class OpenStackCloud(
security_groups = [security_groups] security_groups = [security_groups]
if security_groups: if security_groups:
kwargs['security_groups'] = [] kwargs['security_groups'] = []
for group in security_groups: for sec_group in security_groups:
kwargs['security_groups'].append(dict(name=group)) kwargs['security_groups'].append(dict(name=sec_group))
if 'userdata' in kwargs: if 'userdata' in kwargs:
user_data = kwargs.pop('userdata') user_data = kwargs.pop('userdata')
if user_data: if user_data:
kwargs['user_data'] = self._encode_server_userdata(user_data) kwargs['user_data'] = self._encode_server_userdata(user_data)
for (desired, given) in ( for (desired, given) in (
('OS-DCF:diskConfig', 'disk_config'), ('OS-DCF:diskConfig', 'disk_config'),
('os:scheduler_hints', 'scheduler_hints'),
('config_drive', 'config_drive'), ('config_drive', 'config_drive'),
('key_name', 'key_name'), ('key_name', 'key_name'),
('metadata', 'meta'), ('metadata', 'meta'),
@ -6494,6 +6498,16 @@ class OpenStackCloud(
if value: if value:
kwargs[desired] = value kwargs[desired] = value
hints = kwargs.pop('scheduler_hints', {})
if group:
group_obj = self.get_server_group(group)
if not group_obj:
raise OpenStackCloudException(
"Server Group {group} was requested but was not found"
" on the cloud".format(group=group))
hints['group'] = group_obj['id']
if hints:
kwargs['os:scheduler_hints'] = hints
kwargs.setdefault('max_count', kwargs.get('max_count', 1)) kwargs.setdefault('max_count', kwargs.get('max_count', 1))
kwargs.setdefault('min_count', kwargs.get('min_count', 1)) kwargs.setdefault('min_count', kwargs.get('min_count', 1))