Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # vim: tabstop=4 shiftwidth=4 softtabstop=4
- # Copyright 2011 Andrew Bogott for the Wikimedia Foundation
- #
- # Licensed under the Apache License, Version 2.0 (the "License"); you may
- # not use this file except in compliance with the License. You may obtain
- # a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- # 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 webob
- from nova.api.openstack import wsgi
- from nova.api.openstack import xmlutil
- from nova.api.openstack.v2 import extensions
- from nova import compute
- from nova import exception
- from nova import log as logging
- from nova import network
- from nova import rpc
- LOG = logging.getLogger('nova.api.openstack.v2.contrib.floating_ip_dns')
- def _translate_dns_entry_view(dns_entry):
- result = {}
- try:
- result['ip'] = dns_entry['ip']
- except (TypeError, KeyError):
- result['ip'] = None
- try:
- result['type'] = dns_entry['type']
- except (TypeError, KeyError):
- result['type'] = None
- try:
- result['zone'] = dns_entry['zone']
- except (TypeError, KeyError):
- result['zone'] = None
- try:
- result['name'] = dns_entry['name']
- except (TypeError, KeyError):
- result['name'] = None
- return {'dns_entry': result}
- def _translate_dns_entries_view(dns_entries):
- return {'dns_entries': [_translate_dns_entry_view(entry)
- for entry in dns_entries]}
- class FloatingIPDNSController(object):
- """DNS Entry controller for OpenStack API"""
- def __init__(self):
- self.network_api = network.API()
- super(FloatingIPDNSController, self).__init__()
- def get(self, req, id, zone):
- zone = "bananas"
- """Return a list of dns entries for the specified ip"""
- context = req.environ['nova.context']
- try:
- floating_ip = self.network_api.get_floating_ip(context, id)
- except exception.NotFound:
- raise webob.exc.HTTPNotFound()
- entries = self.network_api.get_floating_dns_entries(context, floating_ip, zone)
- entrylist = [{'zone': zone,
- 'id': id,
- 'name': entry,
- 'address':floating_ip} for entry in entries]
- return _translate_dns_entries_view(entrylist)
- def index(self, req):
- """Return a list of available DNS zones."""
- context = req.environ['nova.context']
- zones = self.network_api.get_dns_zones(context)
- zonelist = [{'zone': zone} for zone in zones]
- zl = _translate_dns_entries_view(zonelist)
- return _translate_dns_entries_view(zonelist)
- def delete(self, req, name, zone):
- """Delete the dns record with name and zone"""
- context = req.environ['nova.context']
- self.network_api.delete_floating_dns_entry(context, name, zone)
- return webob.Response(status_int=202)
- def add(self, req, address, name, dns_type, zone):
- """Add dns entry for name and address"""
- context = req.environ['nova.context']
- self.network_api.add_dns_entry(context, address, name, dns_type, zone)
- return _translate_dns_entry_view({'address': address,
- 'name': name,
- 'type': dns_type,
- 'zone': zone})
- def make_dns_entry(elem):
- elem.set('id')
- elem.set('ip')
- elem.set('type')
- elem.set('zone')
- elem.set('name')
- class FloatingIPDNSTemplate(xmlutil.TemplateBuilder):
- def construct(self):
- root = xmlutil.TemplateElement('dns_entry',
- selector='dns_entry')
- make_dns_entry(root)
- return xmlutil.MasterTemplate(root, 1)
- class FloatingIPDNSsTemplate(xmlutil.TemplateBuilder):
- def construct(self):
- root = xmlutil.TemplateElement('dns_entries')
- elem = xmlutil.SubTemplateElement(root, 'dns_entry',
- selector='dns_entries')
- make_dns_entry(elem)
- return xmlutil.MasterTemplate(root, 1)
- class FloatingIPDNSSerializer(xmlutil.XMLTemplateSerializer):
- def index(self):
- return FloatingIPDNSsTemplate()
- def default(self):
- return FloatingIPDNSTemplate()
- class Floating_ip_dns(extensions.ExtensionDescriptor):
- """Floating IPs support"""
- name = "Floating_ip_dns"
- alias = "os-floating-ip-dns"
- # namespace = "http://docs.openstack.org/ext/floating_ip_dns/api/v1.1"
- updated = "2011-12-12:00:00+00:00"
- def __init__(self, ext_mgr):
- self.compute_api = compute.API()
- self.network_api = network.API()
- super(Floating_ip_dns, self).__init__(ext_mgr)
- def get_resources(self):
- resources = []
- body_serializers = {
- 'application/xml': FloatingIPDNSSerializer(),
- }
- serializer = wsgi.ResponseSerializer(body_serializers)
- res = extensions.ResourceExtension('os-floating-ip-dns',
- FloatingIPDNSController(),
- serializer=serializer,
- member_actions={'get': 'GET'})
- resources.append(res)
- return resources
Advertisement
Add Comment
Please, Sign In to add comment