Advertisement
Guest User

Untitled

a guest
Dec 8th, 2011
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2.  
  3. # Copyright 2012 Andrew Bogott for the Wikimedia Foundation
  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.  
  17. import webob
  18.  
  19. from nova.api.openstack import wsgi
  20. from nova.api.openstack import xmlutil
  21. from nova.api.openstack.v2 import extensions
  22. from nova import compute
  23. from nova import exception
  24. from nova import log as logging
  25. from nova import network
  26. from nova import rpc
  27.  
  28.  
  29. LOG = logging.getLogger('nova.api.openstack.v2.contrib.floating_ip_dns')
  30.  
  31.  
  32. def _translate_dns_entry_view(dns_entry):
  33. result = {'id': dns_entry['id'],
  34. 'ip': dns_entry['address'],
  35. 'dns_name': dns_entry['dns_name']}
  36. try:
  37. result['fixed_ip'] = floating_ip['fixed_ip']['address']
  38. except (TypeError, KeyError):
  39. result['fixed_ip'] = None
  40. try:
  41. result['instance_id'] = floating_ip['fixed_ip']['instance_id']
  42. except (TypeError, KeyError):
  43. result['instance_id'] = None
  44. return {'floating_ip': result}
  45.  
  46.  
  47. def _translate_floating_ips_view(floating_ips):
  48. return {'floating_ips': [_translate_floating_ip_view(ip)['floating_ip']
  49. for ip in floating_ips]}
  50.  
  51.  
  52. def _translate_dns_zones(zones):
  53. return {'zones': [{'zone': zone} for zone in zones]}
  54.  
  55.  
  56. class FloatingIPDNSController(object):
  57. """FIXME"""
  58.  
  59. def __init__(self):
  60. self.network_api = network.API()
  61. super(FloatingIPDNSController, self).__init__()
  62.  
  63. def index(self, req):
  64. """Return a list of available DNS zones."""
  65. context = req.environ['nova.context']
  66.  
  67. zones = self.network_api.get_dns_zones(context)
  68.  
  69. return _translate_dns_zones(zones)
  70.  
  71.  
  72. def make_dns_entry(elem):
  73. elem.set('id')
  74. elem.set('ip')
  75. elem.set('dns_name')
  76.  
  77.  
  78. class FloatingIPDNSTemplate(xmlutil.TemplateBuilder):
  79. def construct(self):
  80. root = xmlutil.TemplateElement('dns_entry',
  81. selector='dns_entry')
  82. make_dns_entry(root)
  83. return xmlutil.MasterTemplate(root, 1)
  84.  
  85.  
  86. class FloatingIPDNSsTemplate(xmlutil.TemplateBuilder):
  87. def construct(self):
  88. root = xmlutil.TemplateElement('dns_entries')
  89. elem = xmlutil.SubTemplateElement(root, 'dns_entry',
  90. selector='dns_entries')
  91. make_dns_entry(elem)
  92. return xmlutil.MasterTemplate(root, 1)
  93.  
  94.  
  95. class FloatingIPDNSSerializer(xmlutil.XMLTemplateSerializer):
  96. def index(self):
  97. return FloatingIPDNSsTemplate()
  98.  
  99. def default(self):
  100. return FloatingIPDNSTemplate()
  101.  
  102.  
  103. class Floating_ip_dns(extensions.ExtensionDescriptor):
  104. """Floating IPs support"""
  105.  
  106. name = "Floating_ip_dns"
  107. alias = "os-floating-ip-dns"
  108. # namespace = "http://docs.openstack.org/ext/floating_ip_dns/api/v1.1"
  109. updated = "2011-12-12:00:00+00:00"
  110.  
  111. def __init__(self, ext_mgr):
  112. self.compute_api = compute.API()
  113. self.network_api = network.API()
  114. super(Floating_ips, self).__init__(ext_mgr)
  115.  
  116. def get_resources(self):
  117. resources = []
  118.  
  119. body_serializers = {
  120. 'application/xml': FloatingIPDNSSerializer(),
  121. }
  122.  
  123. serializer = wsgi.ResponseSerializer(body_serializers)
  124.  
  125. res = extensions.ResourceExtension('os-floating-ip-dns',
  126. FloatingIPDNSController(),
  127. serializer=serializer)
  128. resources.append(res)
  129.  
  130. return resources
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement