Skip to content

Commit f0d0c92

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "objects: Stop fetching from security_groups table"
2 parents b37d50f + 2b55e33 commit f0d0c92

File tree

2 files changed

+32
-127
lines changed

2 files changed

+32
-127
lines changed

nova/objects/instance.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def _from_db_object(context, instance, db_inst, expected_attrs=None):
429429
if 'security_groups' in expected_attrs:
430430
sec_groups = base.obj_make_list(
431431
context, objects.SecurityGroupList(context),
432-
objects.SecurityGroup, db_inst.get('security_groups', []))
432+
objects.SecurityGroup, [])
433433
instance['security_groups'] = sec_groups
434434

435435
if 'tags' in expected_attrs:
@@ -525,7 +525,7 @@ def _db_instance_get_by_uuid(context, uuid, columns_to_join,
525525
@base.remotable_classmethod
526526
def get_by_uuid(cls, context, uuid, expected_attrs=None, use_slave=False):
527527
if expected_attrs is None:
528-
expected_attrs = ['info_cache', 'security_groups']
528+
expected_attrs = ['info_cache']
529529
columns_to_join = _expected_cols(expected_attrs)
530530
db_inst = cls._db_instance_get_by_uuid(context, uuid, columns_to_join,
531531
use_slave=use_slave)
@@ -535,7 +535,7 @@ def get_by_uuid(cls, context, uuid, expected_attrs=None, use_slave=False):
535535
@base.remotable_classmethod
536536
def get_by_id(cls, context, inst_id, expected_attrs=None):
537537
if expected_attrs is None:
538-
expected_attrs = ['info_cache', 'security_groups']
538+
expected_attrs = ['info_cache']
539539
columns_to_join = _expected_cols(expected_attrs)
540540
db_inst = db.instance_get(context, inst_id,
541541
columns_to_join=columns_to_join)
@@ -576,10 +576,6 @@ def create(self):
576576
expected_attrs = [attr for attr in INSTANCE_DEFAULT_FIELDS
577577
if attr in updates]
578578

579-
# TODO(stephenfin): Remove this as it's related to nova-network
580-
if 'security_groups' in updates:
581-
updates['security_groups'] = [x.name for x in
582-
updates['security_groups']]
583579
if 'info_cache' in updates:
584580
updates['info_cache'] = {
585581
'network_info': updates['info_cache'].network_info.json()
@@ -699,11 +695,9 @@ def _save_info_cache(self, context):
699695

700696
# TODO(stephenfin): Remove this as it's related to nova-network
701697
def _save_security_groups(self, context):
702-
security_groups = self.security_groups or []
703-
for secgroup in security_groups:
704-
with secgroup.obj_alternate_context(context):
705-
secgroup.save()
706-
self.security_groups.obj_reset_changes()
698+
# NOTE(stephenfin): We no longer bother saving these since they
699+
# shouldn't be created in the first place
700+
pass
707701

708702
def _save_fault(self, context):
709703
# NOTE(danms): I don't think we need to worry about this, do we?
@@ -1007,11 +1001,6 @@ def _load_vcpu_model(self, db_vcpu_model=_NO_DATA_SENTINEL):
10071001
def _load_ec2_ids(self):
10081002
self.ec2_ids = objects.EC2Ids.get_by_instance(self._context, self)
10091003

1010-
# TODO(stephenfin): Remove this as it's related to nova-network
1011-
def _load_security_groups(self):
1012-
self.security_groups = objects.SecurityGroupList.get_by_instance(
1013-
self._context, self)
1014-
10151004
def _load_pci_devices(self):
10161005
self.pci_devices = objects.PciDeviceList.get_by_instance_uuid(
10171006
self._context, self.uuid)
@@ -1198,7 +1187,7 @@ def _obj_load_attr(self, attrname):
11981187
elif attrname == 'resources':
11991188
return self._load_resources()
12001189
elif attrname == 'security_groups':
1201-
self._load_security_groups()
1190+
self.security_groups = objects.SecurityGroupList()
12021191
elif attrname == 'pci_devices':
12031192
self._load_pci_devices()
12041193
elif 'flavor' in attrname:
@@ -1560,17 +1549,12 @@ def get_active_by_window_joined(cls, context, begin, end=None,
15601549
# TODO(stephenfin): Remove this as it's related to nova-network
15611550
@base.remotable_classmethod
15621551
def get_by_security_group_id(cls, context, security_group_id):
1563-
db_secgroup = db.security_group_get(
1564-
context, security_group_id,
1565-
columns_to_join=['instances.info_cache',
1566-
'instances.system_metadata'])
1567-
return _make_instance_list(context, cls(), db_secgroup['instances'],
1568-
['info_cache', 'system_metadata'])
1552+
raise NotImplementedError()
15691553

15701554
# TODO(stephenfin): Remove this as it's related to nova-network
15711555
@classmethod
15721556
def get_by_security_group(cls, context, security_group):
1573-
return cls.get_by_security_group_id(context, security_group.id)
1557+
raise NotImplementedError()
15741558

15751559
# TODO(stephenfin): Remove this as it's related to nova-network
15761560
@base.remotable_classmethod

nova/tests/unit/objects/test_instance.py

Lines changed: 23 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from nova.objects import instance
3737
from nova.objects import instance_info_cache
3838
from nova.objects import pci_device
39-
from nova.objects import security_group
4039
from nova import test
4140
from nova.tests.unit import fake_instance
4241
from nova.tests.unit.objects import test_instance_device_metadata
@@ -46,7 +45,6 @@
4645
from nova.tests.unit.objects import test_instance_pci_requests
4746
from nova.tests.unit.objects import test_migration_context as test_mig_ctxt
4847
from nova.tests.unit.objects import test_objects
49-
from nova.tests.unit.objects import test_security_group
5048
from nova.tests.unit.objects import test_vcpu_model
5149
from nova import utils
5250

@@ -66,7 +64,6 @@ def fake_instance(self):
6664
db_inst['launched_at'] = datetime.datetime(1955, 11, 12,
6765
22, 4, 0)
6866
db_inst['deleted'] = False
69-
db_inst['security_groups'] = []
7067
db_inst['pci_devices'] = []
7168
db_inst['user_id'] = self.context.user_id
7269
db_inst['project_id'] = self.context.project_id
@@ -380,7 +377,7 @@ def test_get_by_id(self, mock_get):
380377
inst = objects.Instance.get_by_id(self.context, 'instid')
381378
self.assertEqual(self.fake_instance['uuid'], inst.uuid)
382379
mock_get.assert_called_once_with(self.context, 'instid',
383-
columns_to_join=['info_cache', 'security_groups'])
380+
columns_to_join=['info_cache'])
384381

385382
@mock.patch.object(db, 'instance_get_by_uuid')
386383
def test_load(self, mock_get):
@@ -399,8 +396,7 @@ def test_load(self, mock_get):
399396
self.assertEqual({'foo': 'bar'}, meta2)
400397

401398
call_list = [mock.call(self.context, fake_uuid,
402-
columns_to_join=['info_cache',
403-
'security_groups']),
399+
columns_to_join=['info_cache']),
404400
mock.call(self.context, fake_uuid,
405401
columns_to_join=['metadata']),
406402
]
@@ -475,7 +471,7 @@ def test_get_remote(self, mock_get):
475471
str(inst.access_ip_v6))
476472

477473
mock_get.assert_called_once_with(self.context, uuids.instance,
478-
columns_to_join=['info_cache', 'security_groups'])
474+
columns_to_join=['info_cache'])
479475

480476
@mock.patch.object(instance_info_cache.InstanceInfoCache, 'refresh')
481477
@mock.patch.object(db, 'instance_get_by_uuid')
@@ -492,11 +488,9 @@ def test_refresh(self, mock_get, mock_refresh):
492488
self.assertEqual(set([]), inst.obj_what_changed())
493489

494490
get_call_list = [mock.call(self.context, fake_uuid,
495-
columns_to_join=['info_cache',
496-
'security_groups']),
491+
columns_to_join=['info_cache']),
497492
mock.call(self.context, fake_uuid,
498-
columns_to_join=['info_cache',
499-
'security_groups']),
493+
columns_to_join=['info_cache']),
500494
]
501495
mock_get.assert_has_calls(get_call_list, any_order=False)
502496
mock_refresh.assert_called_once_with()
@@ -566,12 +560,10 @@ def _save_test_helper(self, save_kwargs,
566560
# NOTE(danms): Ignore flavor migrations for the moment
567561
self.assertEqual(set([]), inst.obj_what_changed() - set(['flavor']))
568562
mock_db_instance_get_by_uuid.assert_called_once_with(
569-
self.context, fake_uuid, columns_to_join=['info_cache',
570-
'security_groups'])
563+
self.context, fake_uuid, columns_to_join=['info_cache'])
571564
mock_db_instance_update_and_get_original.assert_called_once_with(
572565
self.context, fake_uuid, expected_updates,
573-
columns_to_join=['info_cache', 'security_groups',
574-
'system_metadata']
566+
columns_to_join=['info_cache', 'system_metadata']
575567
)
576568
mock_notifications_send_update.assert_called_with(self.context,
577569
mock.ANY,
@@ -609,10 +601,10 @@ def test_save_rename_sends_notification(self, mock_send, mock_get,
609601
self.assertEqual(set([]), inst.obj_what_changed() - set(['flavor']))
610602

611603
mock_get.assert_called_once_with(self.context, fake_uuid,
612-
columns_to_join=['info_cache', 'security_groups'])
604+
columns_to_join=['info_cache'])
613605
mock_update_and_get.assert_called_once_with(self.context, fake_uuid,
614-
expected_updates, columns_to_join=['info_cache', 'security_groups',
615-
'system_metadata'])
606+
expected_updates, columns_to_join=['info_cache', 'system_metadata']
607+
)
616608
mock_send.assert_called_once_with(self.context, mock.ANY, mock.ANY)
617609

618610
@mock.patch('nova.db.main.api.instance_extra_update_by_uuid')
@@ -794,7 +786,7 @@ def test_get_deleted(self, mock_get):
794786
# NOTE(danms): Make sure it's actually a bool
795787
self.assertTrue(inst.deleted)
796788
mock_get.assert_called_once_with(self.context, fake_uuid,
797-
columns_to_join=['info_cache', 'security_groups'])
789+
columns_to_join=['info_cache'])
798790

799791
@mock.patch.object(db, 'instance_get_by_uuid')
800792
def test_get_not_cleaned(self, mock_get):
@@ -806,7 +798,7 @@ def test_get_not_cleaned(self, mock_get):
806798
# NOTE(mikal): Make sure it's actually a bool
807799
self.assertFalse(inst.cleaned)
808800
mock_get.assert_called_once_with(self.context, fake_uuid,
809-
columns_to_join=['info_cache', 'security_groups'])
801+
columns_to_join=['info_cache'])
810802

811803
@mock.patch.object(db, 'instance_get_by_uuid')
812804
def test_get_cleaned(self, mock_get):
@@ -818,7 +810,7 @@ def test_get_cleaned(self, mock_get):
818810
# NOTE(mikal): Make sure it's actually a bool
819811
self.assertTrue(inst.cleaned)
820812
mock_get.assert_called_once_with(self.context, fake_uuid,
821-
columns_to_join=['info_cache', 'security_groups'])
813+
columns_to_join=['info_cache'])
822814

823815
@mock.patch.object(db, 'instance_update_and_get_original')
824816
@mock.patch.object(db, 'instance_info_cache_update')
@@ -846,7 +838,7 @@ def test_with_info_cache(self, mock_get, mock_upd_cache, mock_upd_and_get):
846838
inst.save()
847839

848840
mock_get.assert_called_once_with(self.context, fake_uuid,
849-
columns_to_join=['info_cache', 'security_groups'])
841+
columns_to_join=['info_cache'])
850842
mock_upd_cache.assert_called_once_with(self.context, fake_uuid,
851843
{'network_info': nwinfo2_json})
852844
self.assertFalse(mock_upd_and_get.called)
@@ -900,22 +892,15 @@ def test_with_security_groups(self, mock_get, mock_upd_and_get,
900892
mock_upd_secgrp.return_value = fake_inst['security_groups'][0]
901893

902894
inst = objects.Instance.get_by_uuid(self.context, fake_uuid)
903-
self.assertEqual(2, len(inst.security_groups))
904-
for index, group in enumerate(fake_inst['security_groups']):
905-
for key in group:
906-
self.assertEqual(group[key],
907-
getattr(inst.security_groups[index], key))
908-
self.assertIsInstance(inst.security_groups[index],
909-
security_group.SecurityGroup)
895+
# we no longer actually save these, so this should return 0
896+
self.assertEqual(0, len(inst.security_groups))
910897
self.assertEqual(set(), inst.security_groups.obj_what_changed())
911-
inst.security_groups[0].description = 'changed'
912898
inst.save()
913899
self.assertEqual(set(), inst.security_groups.obj_what_changed())
914900

915901
mock_get.assert_called_once_with(self.context, fake_uuid,
916-
columns_to_join=['info_cache', 'security_groups'])
917-
mock_upd_secgrp.assert_called_once_with(self.context, 1,
918-
{'description': 'changed'})
902+
columns_to_join=['info_cache'])
903+
mock_upd_secgrp.assert_not_called()
919904
self.assertFalse(mock_upd_and_get.called)
920905

921906
@mock.patch.object(db, 'instance_get_by_uuid')
@@ -927,7 +912,7 @@ def test_with_empty_security_groups(self, mock_get):
927912
inst = objects.Instance.get_by_uuid(self.context, fake_uuid)
928913
self.assertEqual(0, len(inst.security_groups))
929914
mock_get.assert_called_once_with(self.context, fake_uuid,
930-
columns_to_join=['info_cache', 'security_groups'])
915+
columns_to_join=['info_cache'])
931916

932917
@mock.patch.object(db, 'instance_get_by_uuid')
933918
def test_with_empty_pci_devices(self, mock_get):
@@ -1201,23 +1186,16 @@ def test_create_with_special_things(self, mock_create):
12011186
fake_inst = fake_instance.fake_db_instance()
12021187
mock_create.return_value = fake_inst
12031188

1204-
secgroups = security_group.SecurityGroupList()
1205-
secgroups.objects = []
1206-
for name in ('foo', 'bar'):
1207-
secgroup = security_group.SecurityGroup()
1208-
secgroup.name = name
1209-
secgroups.objects.append(secgroup)
12101189
info_cache = instance_info_cache.InstanceInfoCache()
12111190
info_cache.network_info = network_model.NetworkInfo()
12121191
inst = objects.Instance(context=self.context,
1213-
host='foo-host', security_groups=secgroups,
1192+
host='foo-host',
12141193
info_cache=info_cache)
12151194
inst.create()
12161195

12171196
mock_create.assert_called_once_with(self.context,
12181197
{'host': 'foo-host',
12191198
'deleted': 0,
1220-
'security_groups': ['foo', 'bar'],
12211199
'info_cache': {'network_info': '[]'},
12221200
'extra': {
12231201
'vcpu_model': None,
@@ -1340,6 +1318,7 @@ def test_from_db_object_info_cache_not_set(self):
13401318
self.assertIsNone(inst.info_cache)
13411319

13421320
def test_from_db_object_security_groups_net_set(self):
1321+
# this is the default now since we no longer set these things
13431322
inst = instance.Instance(context=self.context,
13441323
info_cache=None)
13451324
db_inst = fake_instance.fake_db_instance()
@@ -1601,20 +1580,6 @@ def test_load_ec2_ids(self, mock_get):
16011580
mock_get.assert_called_once_with(self.context, inst)
16021581
self.assertEqual(fake_ec2_ids, ec2_ids)
16031582

1604-
@mock.patch('nova.objects.SecurityGroupList.get_by_instance')
1605-
def test_load_security_groups(self, mock_get):
1606-
secgroups = []
1607-
for name in ('foo', 'bar'):
1608-
secgroup = security_group.SecurityGroup()
1609-
secgroup.name = name
1610-
secgroups.append(secgroup)
1611-
fake_secgroups = security_group.SecurityGroupList(objects=secgroups)
1612-
mock_get.return_value = fake_secgroups
1613-
inst = objects.Instance(context=self.context, uuid=uuids.instance)
1614-
secgroups = inst.security_groups
1615-
mock_get.assert_called_once_with(self.context, inst)
1616-
self.assertEqual(fake_secgroups, secgroups)
1617-
16181583
@mock.patch('nova.objects.PciDeviceList.get_by_instance_uuid')
16191584
def test_load_pci_devices(self, mock_get):
16201585
fake_pci_devices = pci_device.PciDeviceList()
@@ -1744,7 +1709,6 @@ def fake_instance(self, id, updates=None):
17441709
db_inst['updated_at'] = None
17451710
db_inst['launched_at'] = datetime.datetime(1955, 11, 12,
17461711
22, 4, 0)
1747-
db_inst['security_groups'] = []
17481712
db_inst['deleted'] = 0
17491713

17501714
db_inst['info_cache'] = dict(test_instance_info_cache.fake_info_cache,
@@ -2010,49 +1974,6 @@ def test_fill_faults(self, mock_fault_get):
20101974
[x.uuid for x in insts],
20111975
latest=True)
20121976

2013-
@mock.patch('nova.objects.instance.Instance.obj_make_compatible')
2014-
def test_get_by_security_group(self, mock_compat):
2015-
fake_secgroup = dict(test_security_group.fake_secgroup)
2016-
fake_secgroup['instances'] = [
2017-
fake_instance.fake_db_instance(id=1,
2018-
system_metadata={'foo': 'bar'}),
2019-
fake_instance.fake_db_instance(id=2),
2020-
]
2021-
2022-
with mock.patch.object(db, 'security_group_get') as sgg:
2023-
sgg.return_value = fake_secgroup
2024-
secgroup = security_group.SecurityGroup()
2025-
secgroup.id = fake_secgroup['id']
2026-
instances = instance.InstanceList.get_by_security_group(
2027-
self.context, secgroup)
2028-
2029-
self.assertEqual(2, len(instances))
2030-
self.assertEqual([1, 2], [x.id for x in instances])
2031-
self.assertTrue(instances[0].obj_attr_is_set('system_metadata'))
2032-
self.assertEqual({'foo': 'bar'}, instances[0].system_metadata)
2033-
2034-
def test_get_by_security_group_after_destroy(self):
2035-
db_sg = db.security_group_create(
2036-
self.context,
2037-
{'name': 'foo',
2038-
'description': 'test group',
2039-
'user_id': self.context.user_id,
2040-
'project_id': self.context.project_id})
2041-
self.assertFalse(db.security_group_in_use(self.context, db_sg.id))
2042-
inst = objects.Instance(
2043-
context=self.context,
2044-
user_id=self.context.user_id,
2045-
project_id=self.context.project_id)
2046-
inst.create()
2047-
2048-
db.instance_add_security_group(self.context,
2049-
inst.uuid,
2050-
db_sg.id)
2051-
2052-
self.assertTrue(db.security_group_in_use(self.context, db_sg.id))
2053-
inst.destroy()
2054-
self.assertFalse(db.security_group_in_use(self.context, db_sg.id))
2055-
20561977
@mock.patch('nova.db.main.api.instance_get_all_uuids_by_hosts')
20571978
def test_get_uuids_by_host_no_match(self, mock_get_all):
20581979
mock_get_all.return_value = collections.defaultdict(list)
@@ -2159,14 +2080,14 @@ def test_expected_cols_extra(self):
21592080

21602081
def test_expected_cols_no_duplicates(self):
21612082
expected_attr = ['metadata', 'system_metadata', 'info_cache',
2162-
'security_groups', 'info_cache', 'metadata',
2083+
'info_cache', 'metadata',
21632084
'pci_devices', 'tags', 'extra', 'flavor']
21642085

21652086
result_list = instance._expected_cols(expected_attr)
21662087

21672088
self.assertEqual(len(result_list), len(set(expected_attr)))
21682089
self.assertEqual(['metadata', 'system_metadata', 'info_cache',
2169-
'security_groups', 'pci_devices', 'tags', 'extra',
2090+
'pci_devices', 'tags', 'extra',
21702091
'extra.flavor'], result_list)
21712092

21722093

0 commit comments

Comments
 (0)