Merge "Automatically create groups that do not exist"
This commit is contained in:
commit
6901c10a03
|
@ -28,6 +28,9 @@ from nova.objects import instance_group as instance_group_obj
|
|||
from nova.openstack.common.gettextutils import _
|
||||
from nova import utils
|
||||
|
||||
# NOTE(russellb) There is one other policy, 'legacy', but we don't allow that
|
||||
# being set via the API. It's only used when a group gets automatically
|
||||
# created to support the legacy behavior of the 'group' scheduler hint.
|
||||
SUPPORTED_POLICIES = ['anti-affinity', 'affinity']
|
||||
|
||||
authorize = extensions.extension_authorizer('compute', 'server_groups')
|
||||
|
|
|
@ -875,16 +875,44 @@ class API(base.Base):
|
|||
|
||||
return {}
|
||||
|
||||
@staticmethod
|
||||
def _update_instance_group_by_name(context, instance_uuids, group_name):
|
||||
try:
|
||||
ig = instance_group_obj.InstanceGroup.get_by_name(context,
|
||||
group_name)
|
||||
instance_group_obj.InstanceGroup.add_members(context, ig.uuid,
|
||||
instance_uuids)
|
||||
except exception.InstanceGroupNotFound:
|
||||
# NOTE(russellb) If the group does not already exist, we need to
|
||||
# automatically create it to be backwards compatible with old
|
||||
# handling of the 'group' scheduler hint. The policy type will be
|
||||
# 'legacy', indicating that this group was created to emulate
|
||||
# legacy group behavior.
|
||||
ig = instance_group_obj.InstanceGroup()
|
||||
ig.name = group_name
|
||||
ig.project_id = context.project_id
|
||||
ig.user_id = context.user_id
|
||||
ig.policies = ['legacy']
|
||||
ig.members = instance_uuids
|
||||
ig.create(context)
|
||||
|
||||
@staticmethod
|
||||
def _update_instance_group(context, instances, scheduler_hints):
|
||||
if not scheduler_hints:
|
||||
return
|
||||
group_uuid = scheduler_hints.get('group')
|
||||
if not group_uuid:
|
||||
|
||||
group_hint = scheduler_hints.get('group')
|
||||
if not group_hint:
|
||||
return
|
||||
|
||||
instance_uuids = [instance.uuid for instance in instances]
|
||||
instance_group_obj.InstanceGroup.add_members(context, group_uuid,
|
||||
instance_uuids)
|
||||
|
||||
if uuidutils.is_uuid_like(group_hint):
|
||||
instance_group_obj.InstanceGroup.add_members(context, group_hint,
|
||||
instance_uuids)
|
||||
else:
|
||||
API._update_instance_group_by_name(context, instance_uuids,
|
||||
group_hint)
|
||||
|
||||
def _create_instance(self, context, instance_type,
|
||||
image_href, kernel_id, ramdisk_id,
|
||||
|
|
|
@ -6941,6 +6941,36 @@ class ComputeAPITestCase(BaseTestCase):
|
|||
|
||||
db.instance_destroy(self.context, refs[0]['uuid'])
|
||||
|
||||
def test_instance_create_auto_creates_group(self):
|
||||
self.stubs.Set(fake_image._FakeImageService, 'show', self.fake_show)
|
||||
|
||||
inst_type = flavors.get_default_flavor()
|
||||
(refs, resv_id) = self.compute_api.create(
|
||||
self.context, inst_type, self.fake_image['id'],
|
||||
scheduler_hints={'group': 'groupname'})
|
||||
|
||||
group = instance_group_obj.InstanceGroup.get_by_name(self.context,
|
||||
'groupname')
|
||||
self.assertEqual('groupname', group.name)
|
||||
self.assertIn('legacy', group.policies)
|
||||
self.assertEqual(1, len(group.members))
|
||||
self.assertIn(refs[0]['uuid'], group.members)
|
||||
|
||||
# On a second instance, make sure it gets added to the group that was
|
||||
# auto-created above
|
||||
(refs2, resv_id) = self.compute_api.create(
|
||||
self.context, inst_type, self.fake_image['id'],
|
||||
scheduler_hints={'group': 'groupname'})
|
||||
group = instance_group_obj.InstanceGroup.get_by_name(self.context,
|
||||
'groupname')
|
||||
self.assertEqual('groupname', group.name)
|
||||
self.assertIn('legacy', group.policies)
|
||||
self.assertEqual(2, len(group.members))
|
||||
self.assertIn(refs[0]['uuid'], group.members)
|
||||
self.assertIn(refs2[0]['uuid'], group.members)
|
||||
|
||||
db.instance_destroy(self.context, refs[0]['uuid'])
|
||||
|
||||
def test_destroy_instance_disassociates_security_groups(self):
|
||||
# Make sure destroying disassociates security groups.
|
||||
group = self._create_group()
|
||||
|
|
Loading…
Reference in New Issue