Merge "different-format-for-ipv4-and-ipv6"

This commit is contained in:
Jenkins 2016-07-26 16:22:13 +00:00 committed by Gerrit Code Review
commit ebaca6b80b
5 changed files with 81 additions and 12 deletions

View File

@ -90,6 +90,9 @@ class NotificationHandler(ExtensionPlugin):
class BaseAddressHandler(NotificationHandler): class BaseAddressHandler(NotificationHandler):
default_formatv4 = ('%(hostname)s.%(domain)s')
default_formatv6 = ('%(hostname)s.%(domain)s')
def _get_ip_data(self, addr_dict): def _get_ip_data(self, addr_dict):
ip = addr_dict['address'] ip = addr_dict['address']
version = addr_dict['version'] version = addr_dict['version']
@ -106,6 +109,16 @@ class BaseAddressHandler(NotificationHandler):
data["octet%s" % i] = ip_data[i] data["octet%s" % i] = ip_data[i]
return data 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, def _create(self, addresses, extra, zone_id, managed=True,
resource_type=None, resource_id=None): resource_type=None, resource_id=None):
""" """
@ -139,7 +152,13 @@ class BaseAddressHandler(NotificationHandler):
event_data = data.copy() event_data = data.copy()
event_data.update(self._get_ip_data(addr)) 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 = { recordset_values = {
'zone_id': zone['id'], 'zone_id': zone['id'],
'name': fmt % event_data, 'name': fmt % event_data,

View File

@ -30,8 +30,12 @@ cfg.CONF.register_opts([
cfg.ListOpt('notification-topics', default=['notifications']), cfg.ListOpt('notification-topics', default=['notifications']),
cfg.StrOpt('control-exchange', default='neutron'), cfg.StrOpt('control-exchange', default='neutron'),
cfg.StrOpt('zone-id'), cfg.StrOpt('zone-id'),
cfg.MultiStrOpt('format', default=[ cfg.MultiStrOpt('formatv4', default=None),
'%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(zone)s']) cfg.MultiStrOpt('format', default=None,
deprecated_for_removal=True,
deprecated_reason="Replaced by 'formatv4/formatv6'",
),
cfg.MultiStrOpt('formatv6', default=None)
], group='handler:neutron_floatingip') ], group='handler:neutron_floatingip')

View File

@ -30,8 +30,12 @@ cfg.CONF.register_opts([
cfg.ListOpt('notification-topics', default=['notifications']), cfg.ListOpt('notification-topics', default=['notifications']),
cfg.StrOpt('control-exchange', default='nova'), cfg.StrOpt('control-exchange', default='nova'),
cfg.StrOpt('zone-id'), cfg.StrOpt('zone-id'),
cfg.MultiStrOpt('format', default=[ cfg.MultiStrOpt('formatv4', default=None),
'%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(zone)s']) cfg.MultiStrOpt('format', default=None,
deprecated_for_removal=True,
deprecated_reason="Replaced by 'formatv4/formatv6'",
),
cfg.MultiStrOpt('formatv6', default=None)
], group='handler:nova_fixed') ], group='handler:nova_fixed')

View File

@ -152,3 +152,37 @@ class NovaFixedHandlerTest(TestCase, NotificationHandlerMixin):
finder.assert_called_once_with( finder.assert_called_once_with(
mock.ANY, type='A', zone_id=self.zone_id, mock.ANY, type='A', zone_id=self.zone_id,
name='private.example.com') 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')

View File

@ -431,10 +431,16 @@ debug = False
#domain_id = #domain_id =
#notification_topics = notifications #notification_topics = notifications
#control_exchange = 'nova' #control_exchange = 'nova'
#format = '%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(domain)s' #"format" has been depricated. Instead we now have two formaters
#format = '%(hostname)s.%(project)s.%(domain)s' #each for IPv4 and IPv6.
#format = '%(hostname)s.%(domain)s' #"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 # Neutron Floating Handler
#------------------------ #------------------------
@ -443,9 +449,11 @@ debug = False
#domain_id = #domain_id =
#notification_topics = notifications #notification_topics = notifications
#control_exchange = 'neutron' #control_exchange = 'neutron'
#format = '%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(domain)s' #formatv4 = '%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(domain)s'
#format = '%(hostname)s.%(project)s.%(domain)s' #formatv4 = '%(hostname)s.%(project)s.%(domain)s'
#format = '%(hostname)s.%(domain)s' #formatv4 = '%(hostname)s.%(domain)s'
#formatv6 = '%(hostname)s.%(project)s.%(domain)s'
#formatv6 = '%(hostname)s.%(domain)s'
############################## ##############################
## Agent Backend Configuration ## Agent Backend Configuration