Allow to use network label in nova_fixed handler format

Nova notifications provide network label per fixed_ip in create
payloads. This change updates code to allow nova_fixed handler format
to use the key "label" providing network label. The format:

 %(instance_name)s.%(label)s.%(domain)s

returns the fqdn:

 instanceXX.netYY.domainZZ

for instanceXX on netYY with domainZZ as nova_fixed handler domain.

Change-Id: I7ffef6464ab5e8e8f1e74dca8fcff972c2ad6d58
Closes-Bug: #1446242
This commit is contained in:
Cedric Brandily 2015-04-20 17:32:14 +02:00
parent 7c78ebf87e
commit 13f1584d09
3 changed files with 41 additions and 18 deletions

View File

@ -31,23 +31,6 @@ from designate.plugin import ExtensionPlugin
LOG = logging.getLogger(__name__)
def get_ip_data(addr_dict):
ip = addr_dict['address']
version = addr_dict['version']
data = {
'ip_version': version
}
# TODO(endre): Add v6 support
if version == 4:
data['ip_address'] = ip.replace('.', '-')
ip_data = ip.split(".")
for i in [0, 1, 2, 3]:
data["octet%s" % i] = ip_data[i]
return data
class NotificationHandler(ExtensionPlugin):
"""Base class for notification handlers"""
__plugin_ns__ = 'designate.notification.handler'
@ -104,6 +87,22 @@ class NotificationHandler(ExtensionPlugin):
class BaseAddressHandler(NotificationHandler):
default_format = '%(octet0)s-%(octet1)s-%(octet2)s-%(octet3)s.%(domain)s'
def _get_ip_data(self, addr_dict):
ip = addr_dict['address']
version = addr_dict['version']
data = {
'ip_version': version,
}
# TODO(endre): Add v6 support
if version == 4:
data['ip_address'] = ip.replace('.', '-')
ip_data = ip.split(".")
for i in [0, 1, 2, 3]:
data["octet%s" % i] = ip_data[i]
return data
def _get_format(self):
return cfg.CONF[self.name].get('format') or self.default_format
@ -138,7 +137,7 @@ class BaseAddressHandler(NotificationHandler):
for addr in addresses:
event_data = data.copy()
event_data.update(get_ip_data(addr))
event_data.update(self._get_ip_data(addr))
recordset_values = {
'domain_id': domain['id'],

View File

@ -51,6 +51,11 @@ class NovaFixedHandler(BaseAddressHandler):
'compute.instance.delete.start',
]
def _get_ip_data(self, addr_dict):
data = super(NovaFixedHandler, self)._get_ip_data(addr_dict)
data['label'] = addr_dict['label']
return data
def process_notification(self, context, event_type, payload):
LOG.debug('NovaFixedHandler received notification - %s' % event_type)

View File

@ -13,6 +13,9 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import mock
from oslo_log import log as logging
from designate.tests import TestCase
@ -93,3 +96,19 @@ class NovaFixedHandlerTest(TestCase, NotificationHandlerMixin):
criterion)
self.assertEqual(2, len(records))
def test_label_in_format(self):
event_type = 'compute.instance.create.end'
self.config(format='%(label)s.example.com', group='handler:nova_fixed')
fixture = self.get_notification_fixture('nova', event_type)
with contextlib.nested(
mock.patch.object(self.plugin, '_find_or_create_recordset'),
mock.patch.object(
self.plugin.central_api, 'create_record')) as (
finder, creator):
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', domain_id=self.domain_id,
name='private.example.com')