Merge "Fix bug in update_zone with recordsets"

This commit is contained in:
Zuul 2023-05-31 00:28:38 +00:00 committed by Gerrit Code Review
commit d8b019942f
2 changed files with 34 additions and 13 deletions
designate
storage/sqlalchemy
tests/test_storage

@ -574,28 +574,26 @@ class SQLAlchemyStorage(base.SQLAlchemy):
self.create_zone_master(context, zone.id, attr) self.create_zone_master(context, zone.id, attr)
if zone.obj_attr_is_set('recordsets'): if zone.obj_attr_is_set('recordsets'):
existing = self.find_recordsets(context, {'zone_id': zone.id}) existing = {}
for rrset in self.find_recordsets(context, {'zone_id': zone.id}):
data = {} existing[rrset.name, rrset.type] = rrset
for rrset in existing:
data[rrset.name, rrset.type] = rrset
keep = set() keep = set()
for rrset in zone.recordsets: for rrset in zone.recordsets:
current = data.get((rrset.name, rrset.type)) existing_recordset = existing.get((rrset.name, rrset.type))
if existing_recordset:
if current: existing_recordset.update(rrset)
current.update(rrset) existing_recordset.records = rrset.records
current.records = rrset.records existing_recordset.obj_reset_changes(['zone_name'])
self.update_recordset(context, current) self.update_recordset(context, existing_recordset)
keep.add(current.id) keep.add(existing_recordset.id)
else: else:
self.create_recordset(context, zone.id, rrset) self.create_recordset(context, zone.id, rrset)
keep.add(rrset.id) keep.add(rrset.id)
if zone.type == 'SECONDARY': if zone.type == 'SECONDARY':
# Purge anything that shouldn't be there :P # Purge anything that shouldn't be there :P
for i in set([i.id for i in data.values()]) - keep: for i in set([i.id for i in existing.values()]) - keep:
self.delete_recordset(context, i) self.delete_recordset(context, i)
if tenant_id_changed: if tenant_id_changed:

@ -917,6 +917,29 @@ class SqlalchemyStorageTest(TestCase):
# Ensure the version column was incremented # Ensure the version column was incremented
self.assertEqual(2, zone.version) self.assertEqual(2, zone.version)
def test_update_zone_new_recordset_with_existing(self):
zone = self.create_zone(name='example.org.')
recordset1 = self.create_recordset(zone)
recordset2 = objects.RecordSet(
name='www.example.org.', type='A',
records=objects.RecordList(objects=[
objects.Record(data='192.0.2.1'),
])
)
zone.name = 'example.net.'
zone.recordsets = objects.RecordSetList(
objects=[recordset1, recordset2]
)
# Perform the update
self.storage.update_zone(self.admin_context, zone)
recordsets = self.storage.find_recordsets(
self.admin_context, {'zone_id': zone['id']}
)
self.assertEqual(4, len(recordsets))
def test_update_zone_new_recordset(self): def test_update_zone_new_recordset(self):
zone = self.create_zone(name='example.org.') zone = self.create_zone(name='example.org.')