From 0568b66d238896cd12e52d3a800b2cb2cb9083de Mon Sep 17 00:00:00 2001 From: Kumar Acharya Date: Tue, 5 Jul 2016 09:35:24 +0530 Subject: [PATCH] different-format-for-ipv4-and-ipv6 This change will allow users to define seperate formats for ipv4 and ipv6 addresses. this requires formatv4 and formatv6 to de defined in designate.conf instead of format. format has been marked depricated Change-Id: Ief685ba6a00da5100013f4ab71768c661f37ee13 Implements: blueprint different-format-for-ipv4-and-ipv6 --- designate/notification_handler/base.py | 21 +++++++++++- designate/notification_handler/neutron.py | 8 +++-- designate/notification_handler/nova.py | 8 +++-- .../test_notification_handler/test_nova.py | 34 +++++++++++++++++++ etc/designate/designate.conf.sample | 22 ++++++++---- 5 files changed, 81 insertions(+), 12 deletions(-) diff --git a/designate/notification_handler/base.py b/designate/notification_handler/base.py index 91e6cb8dd..40031a974 100644 --- a/designate/notification_handler/base.py +++ b/designate/notification_handler/base.py @@ -90,6 +90,9 @@ class NotificationHandler(ExtensionPlugin): class BaseAddressHandler(NotificationHandler): + default_formatv4 = ('%(hostname)s.%(domain)s') + default_formatv6 = ('%(hostname)s.%(domain)s') + def _get_ip_data(self, addr_dict): ip = addr_dict['address'] version = addr_dict['version'] @@ -106,6 +109,16 @@ class BaseAddressHandler(NotificationHandler): data["octet%s" % i] = ip_data[i] return data + def _get_formatv4(self): + return cfg.CONF[self.name].get('formatv4') or \ + cfg.CONF[self.name].get('format') or \ + self.default_formatv4 + + def _get_formatv6(self): + return cfg.CONF[self.name].get('formatv6') or \ + cfg.CONF[self.name].get('format') or \ + self.default_formatv6 + def _create(self, addresses, extra, zone_id, managed=True, resource_type=None, resource_id=None): """ @@ -139,7 +152,13 @@ class BaseAddressHandler(NotificationHandler): event_data = data.copy() event_data.update(self._get_ip_data(addr)) - for fmt in cfg.CONF[self.name].get('format'): + formatv4 = self._get_formatv4() + formatv6 = self._get_formatv6() + if addr['version'] == 4: + format = formatv4 + else: + format = formatv6 + for fmt in format: recordset_values = { 'zone_id': zone['id'], 'name': fmt % event_data, diff --git a/designate/notification_handler/neutron.py b/designate/notification_handler/neutron.py index d5c17e0e8..7f3b64c0c 100644 --- a/designate/notification_handler/neutron.py +++ b/designate/notification_handler/neutron.py @@ -30,8 +30,12 @@ cfg.CONF.register_opts([ cfg.ListOpt('notification-topics', default=['notifications']), cfg.StrOpt('control-exchange', default='neutron'), cfg.StrOpt('zone-id'), - cfg.MultiStrOpt('format', default=[ - '%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(zone)s']) + cfg.MultiStrOpt('formatv4', default=None), + cfg.MultiStrOpt('format', default=None, + deprecated_for_removal=True, + deprecated_reason="Replaced by 'formatv4/formatv6'", + ), + cfg.MultiStrOpt('formatv6', default=None) ], group='handler:neutron_floatingip') diff --git a/designate/notification_handler/nova.py b/designate/notification_handler/nova.py index 7a937640a..0ff3cb5e1 100644 --- a/designate/notification_handler/nova.py +++ b/designate/notification_handler/nova.py @@ -30,8 +30,12 @@ cfg.CONF.register_opts([ cfg.ListOpt('notification-topics', default=['notifications']), cfg.StrOpt('control-exchange', default='nova'), cfg.StrOpt('zone-id'), - cfg.MultiStrOpt('format', default=[ - '%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(zone)s']) + cfg.MultiStrOpt('formatv4', default=None), + cfg.MultiStrOpt('format', default=None, + deprecated_for_removal=True, + deprecated_reason="Replaced by 'formatv4/formatv6'", + ), + cfg.MultiStrOpt('formatv6', default=None) ], group='handler:nova_fixed') diff --git a/designate/tests/test_notification_handler/test_nova.py b/designate/tests/test_notification_handler/test_nova.py index eb2f263dc..d157c835f 100644 --- a/designate/tests/test_notification_handler/test_nova.py +++ b/designate/tests/test_notification_handler/test_nova.py @@ -152,3 +152,37 @@ class NovaFixedHandlerTest(TestCase, NotificationHandlerMixin): finder.assert_called_once_with( mock.ANY, type='A', zone_id=self.zone_id, name='private.example.com') + + def test_formatv4_or_format(self): + event_type = 'compute.instance.create.end' + 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.central_api, + 'create_record'): + finder.return_value = {'id': 'fakeid'} + self.plugin.process_notification( + self.admin_context, event_type, fixture['payload']) + finder.assert_called_once_with( + mock.ANY, type='A', zone_id=self.zone_id, + name='private-v4.example.com') + + def test_formatv4_and_format(self): + event_type = 'compute.instance.create.end' + self.config(format=['%(label)s.example.com'], + group='handler:nova_fixed') + 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.central_api, + 'create_record'): + finder.return_value = {'id': 'fakeid'} + self.plugin.process_notification( + self.admin_context, event_type, fixture['payload']) + finder.assert_called_once_with( + mock.ANY, type='A', zone_id=self.zone_id, + name='private-v4.example.com') diff --git a/etc/designate/designate.conf.sample b/etc/designate/designate.conf.sample index 3062f86f2..769464d0d 100644 --- a/etc/designate/designate.conf.sample +++ b/etc/designate/designate.conf.sample @@ -431,10 +431,16 @@ debug = False #domain_id = #notification_topics = notifications #control_exchange = 'nova' -#format = '%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(domain)s' -#format = '%(hostname)s.%(project)s.%(domain)s' -#format = '%(hostname)s.%(domain)s' - +#"format" has been depricated. Instead we now have two formaters +#each for IPv4 and IPv6. +#"formatv4" has been defined for IPv4 addresses. +#"formatv6" is for IPv6 addresses. +# +#formatv4 = '%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(domain)s' +#formatv4 = '%(hostname)s.%(project)s.%(domain)s' +#formatv4 = '%(hostname)s.%(domain)s' +#formatv6 = '%(hostname)s.%(domain)s' +#formatv6 = '%(hostname)s.%(project)s.%(domain)s' #------------------------ # Neutron Floating Handler #------------------------ @@ -443,9 +449,11 @@ debug = False #domain_id = #notification_topics = notifications #control_exchange = 'neutron' -#format = '%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(domain)s' -#format = '%(hostname)s.%(project)s.%(domain)s' -#format = '%(hostname)s.%(domain)s' +#formatv4 = '%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(domain)s' +#formatv4 = '%(hostname)s.%(project)s.%(domain)s' +#formatv4 = '%(hostname)s.%(domain)s' +#formatv6 = '%(hostname)s.%(project)s.%(domain)s' +#formatv6 = '%(hostname)s.%(domain)s' ############################## ## Agent Backend Configuration