Merge "objects: lazy-load instance.security_groups more efficiently"
This commit is contained in:
commit
a1308ae456
@ -804,6 +804,10 @@ class Instance(base.NovaPersistentObject, base.NovaObject,
|
||||
def _load_ec2_ids(self):
|
||||
self.ec2_ids = objects.EC2Ids.get_by_instance(self._context, self)
|
||||
|
||||
def _load_security_groups(self):
|
||||
self.security_groups = objects.SecurityGroupList.get_by_instance(
|
||||
self._context, self)
|
||||
|
||||
def _load_migration_context(self, db_context=_NO_DATA_SENTINEL):
|
||||
if db_context is _NO_DATA_SENTINEL:
|
||||
try:
|
||||
@ -883,6 +887,8 @@ class Instance(base.NovaPersistentObject, base.NovaObject,
|
||||
self._load_ec2_ids()
|
||||
elif attrname == 'migration_context':
|
||||
self._load_migration_context()
|
||||
elif attrname == 'security_groups':
|
||||
self._load_security_groups()
|
||||
elif 'flavor' in attrname:
|
||||
self._load_flavor()
|
||||
else:
|
||||
|
@ -1241,6 +1241,20 @@ class _TestInstanceObject(object):
|
||||
mock_get.assert_called_once_with(self.context, inst)
|
||||
self.assertEqual(fake_ec2_ids, ec2_ids)
|
||||
|
||||
@mock.patch('nova.objects.SecurityGroupList.get_by_instance')
|
||||
def test_load_security_groups(self, mock_get):
|
||||
secgroups = []
|
||||
for name in ('foo', 'bar'):
|
||||
secgroup = security_group.SecurityGroup()
|
||||
secgroup.name = name
|
||||
secgroups.append(secgroup)
|
||||
fake_secgroups = security_group.SecurityGroupList(objects=secgroups)
|
||||
mock_get.return_value = fake_secgroups
|
||||
inst = objects.Instance(context=self.context, uuid='fake')
|
||||
secgroups = inst.security_groups
|
||||
mock_get.assert_called_once_with(self.context, inst)
|
||||
self.assertEqual(fake_secgroups, secgroups)
|
||||
|
||||
def test_get_with_extras(self):
|
||||
pci_requests = objects.InstancePCIRequests(requests=[
|
||||
objects.InstancePCIRequest(count=123, spec=[])])
|
||||
|
Loading…
x
Reference in New Issue
Block a user