Merge "Improve sink recordset creation"

This commit is contained in:
Zuul 2020-01-16 10:42:08 +00:00 committed by Gerrit Code Review
commit f355aae939
3 changed files with 30 additions and 31 deletions

View File

@ -78,8 +78,6 @@ class SampleHandler(NotificationHandler):
'data': fixed_ip['address'],
}
recordset = self._find_or_create_recordset(context,
**recordset_values)
self.central_api.create_record(context, zone_id, recordset['id'],
Record(**record_values))
self._create_or_update_recordset(
context, [Record(**record_values)], **recordset_values
)

View File

@ -25,6 +25,7 @@ from designate import exceptions
from designate.central import rpcapi as central_rpcapi
from designate.context import DesignateContext
from designate.objects import Record
from designate.objects import RecordList
from designate.objects import RecordSet
from designate.plugin import ExtensionPlugin
@ -64,28 +65,35 @@ class NotificationHandler(ExtensionPlugin):
context = DesignateContext.get_admin_context(all_tenants=True)
return self.central_api.get_zone(context, zone_id)
def _find_or_create_recordset(self, context, zone_id, name, type,
ttl=None):
def _create_or_update_recordset(self, context, records, zone_id, name,
type, ttl=None):
name = name.encode('idna').decode('utf-8')
try:
# Attempt to create an empty recordset
# Attempt to create a new recordset.
values = {
'name': name,
'type': type,
'ttl': ttl,
}
recordset = RecordSet(**values)
recordset.records = RecordList(objects=records)
recordset = self.central_api.create_recordset(
context, zone_id, RecordSet(**values))
context, zone_id, recordset
)
except exceptions.DuplicateRecordSet:
# Fetch the existing recordset
# Fetch and update the existing recordset.
recordset = self.central_api.find_recordset(context, {
'zone_id': zone_id,
'name': name,
'type': type,
})
for record in records:
recordset.records.append(record)
recordset = self.central_api.update_recordset(
context, recordset
)
LOG.debug('Creating record in %s / %s', zone_id, recordset['id'])
return recordset
@ -164,9 +172,6 @@ class BaseAddressHandler(NotificationHandler):
'name': fmt % event_data,
'type': 'A' if addr['version'] == 4 else 'AAAA'}
recordset = self._find_or_create_recordset(
context, **recordset_values)
record_values = {
'data': addr['address'],
'managed': True,
@ -175,13 +180,9 @@ class BaseAddressHandler(NotificationHandler):
'managed_resource_type': resource_type,
'managed_resource_id': resource_id
}
LOG.debug('Creating record in %s / %s with values %r',
zone['id'], recordset['id'], record_values)
self.central_api.create_record(context,
zone['id'],
recordset['id'],
Record(**record_values))
self._create_or_update_recordset(
context, [Record(**record_values)], **recordset_values
)
def _delete(self, zone_id, resource_id=None, resource_type='instance',
criterion=None):

View File

@ -146,8 +146,8 @@ class NovaFixedHandlerTest(TestCase, NotificationHandlerMixin):
formatv6=['%(label)s.example.com.'],
group='handler:nova_fixed')
fixture = self.get_notification_fixture('nova', event_type)
with mock.patch.object(self.plugin, '_find_or_create_recordset')\
as finder:
with mock.patch.object(
self.plugin, '_create_or_update_recordset') as finder:
with mock.patch.object(self.plugin.central_api,
'create_record'):
finder.return_value = {'id': 'fakeid'}
@ -155,7 +155,7 @@ class NovaFixedHandlerTest(TestCase, NotificationHandlerMixin):
self.admin_context.to_dict(),
event_type, fixture['payload'])
finder.assert_called_once_with(
mock.ANY, type='A', zone_id=self.zone_id,
mock.ANY, mock.ANY, type='A', zone_id=self.zone_id,
name='private.example.com.')
def test_formatv4(self):
@ -163,8 +163,8 @@ class NovaFixedHandlerTest(TestCase, NotificationHandlerMixin):
self.config(formatv4=['%(label)s-v4.example.com.'],
group='handler:nova_fixed')
fixture = self.get_notification_fixture('nova', event_type)
with mock.patch.object(self.plugin, '_find_or_create_recordset')\
as finder:
with mock.patch.object(
self.plugin, '_create_or_update_recordset') as finder:
with mock.patch.object(self.plugin.central_api,
'create_record'):
finder.return_value = {'id': 'fakeid'}
@ -172,7 +172,7 @@ class NovaFixedHandlerTest(TestCase, NotificationHandlerMixin):
self.admin_context.to_dict(),
event_type, fixture['payload'])
finder.assert_called_once_with(
mock.ANY, type='A', zone_id=self.zone_id,
mock.ANY, mock.ANY, type='A', zone_id=self.zone_id,
name='private-v4.example.com.')
def test_formatv6(self):
@ -180,8 +180,8 @@ class NovaFixedHandlerTest(TestCase, NotificationHandlerMixin):
self.config(formatv6=['%(label)s-v6.example.com.'],
group='handler:nova_fixed')
fixture = self.get_notification_fixture('nova', event_type)
with mock.patch.object(self.plugin, '_find_or_create_recordset')\
as finder:
with mock.patch.object(
self.plugin, '_create_or_update_recordset') as finder:
with mock.patch.object(self.plugin.central_api,
'create_record'):
finder.return_value = {'id': 'fakeid'}
@ -189,5 +189,5 @@ class NovaFixedHandlerTest(TestCase, NotificationHandlerMixin):
self.admin_context.to_dict(),
event_type, fixture['payload_v6'])
finder.assert_called_once_with(
mock.ANY, type='AAAA', zone_id=self.zone_id,
mock.ANY, mock.ANY, type='AAAA', zone_id=self.zone_id,
name='private-v6.example.com.')