OpenStack DNS As A Service (Designate)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.7KB

  1. # Copyright 2013 Hewlett-Packard Development Company, L.P.
  2. #
  3. # Author: Kiall Mac Innes <kiall@hp.com>
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. from oslo_config import cfg
  17. from oslo_log import log as logging
  18. from designate.context import DesignateContext
  19. from designate.objects import Record
  20. from designate.notification_handler.base import NotificationHandler
  21. LOG = logging.getLogger(__name__)
  22. # Setup a config group
  23. cfg.CONF.register_group(cfg.OptGroup(
  24. name='handler:sample',
  25. title="Configuration for Sample Notification Handler"
  26. ))
  27. # Setup the config options
  28. cfg.CONF.register_opts([
  29. cfg.StrOpt('control-exchange', default='nova'),
  30. cfg.ListOpt('notification-topics', default=['designate']),
  31. cfg.StrOpt('zone-name', default='example.org.'),
  32. cfg.StrOpt('zone-id', default='12345'),
  33. ], group='handler:sample')
  34. class SampleHandler(NotificationHandler):
  35. """Sample Handler"""
  36. __plugin_name__ = 'sample'
  37. def get_exchange_topics(self):
  38. """
  39. Return a tuple of (exchange, [topics]) this handler wants to receive
  40. events from.
  41. """
  42. exchange = cfg.CONF[self.name].control_exchange
  43. topics = [topic for topic in cfg.CONF[self.name].notification_topics]
  44. return exchange, topics
  45. def get_event_types(self):
  46. return [
  47. 'compute.instance.create.end'
  48. ]
  49. def process_notification(self, context, event_type, payload):
  50. # Do something with the notification.. e.g:
  51. zone_id = cfg.CONF[self.name].zone_id
  52. zone_name = cfg.CONF[self.name].zone_name
  53. record_name = '%s.%s' % (payload['instance_id'], zone_name)
  54. context = DesignateContext().elevated()
  55. context.all_tenants = True
  56. # context.edit_managed_records = True
  57. for fixed_ip in payload['fixed_ips']:
  58. recordset_values = {
  59. 'zone_id': zone_id,
  60. 'name': record_name,
  61. 'type': 'A' if fixed_ip['version'] == 4 else 'AAAA'
  62. }
  63. record_values = {
  64. 'data': fixed_ip['address'],
  65. }
  66. self._create_or_update_recordset(
  67. context, [Record(**record_values)], **recordset_values
  68. )