OpenStack Dashboard (Horizon)
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.

views.py 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Copyright 2016 Letv Cloud Computing
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. from collections import OrderedDict
  16. from django.urls import reverse
  17. from django.urls import reverse_lazy
  18. from django.utils.translation import ugettext_lazy as _
  19. import netaddr
  20. from horizon import exceptions
  21. from horizon import forms
  22. from horizon import tables
  23. from horizon.utils import memoized
  24. from horizon import views
  25. from openstack_dashboard import api
  26. from openstack_dashboard.dashboards.admin.floating_ips \
  27. import forms as fip_forms
  28. from openstack_dashboard.dashboards.admin.floating_ips \
  29. import tables as fip_tables
  30. from openstack_dashboard.dashboards.project.floating_ips \
  31. import tables as project_tables
  32. def get_floatingip_pools(request):
  33. pools = []
  34. try:
  35. search_opts = {'router:external': True}
  36. pools = api.neutron.network_list(request, **search_opts)
  37. except Exception:
  38. exceptions.handle(request,
  39. _("Unable to retrieve floating IP pools."))
  40. return pools
  41. def get_tenant_list(request):
  42. tenants = []
  43. try:
  44. tenants, has_more = api.keystone.tenant_list(request)
  45. except Exception:
  46. msg = _('Unable to retrieve project list.')
  47. exceptions.handle(request, msg)
  48. return tenants
  49. class IndexView(tables.DataTableView):
  50. table_class = fip_tables.FloatingIPsTable
  51. page_title = _("Floating IPs")
  52. @memoized.memoized_method
  53. def get_data(self):
  54. floating_ips = []
  55. search_opts = self.get_filters()
  56. try:
  57. floating_ips = api.neutron.tenant_floating_ip_list(
  58. self.request,
  59. all_tenants=True,
  60. **search_opts)
  61. except Exception:
  62. exceptions.handle(self.request,
  63. _('Unable to retrieve floating IP list.'))
  64. if floating_ips:
  65. instances = []
  66. try:
  67. instances, has_more = api.nova.server_list(
  68. self.request,
  69. search_opts={'all_tenants': True},
  70. detailed=False)
  71. except Exception:
  72. exceptions.handle(
  73. self.request,
  74. _('Unable to retrieve instance list.'))
  75. instances_dict = dict([(obj.id, obj.name) for obj in instances])
  76. tenants = get_tenant_list(self.request)
  77. tenant_dict = OrderedDict([(t.id, t) for t in tenants])
  78. pools = get_floatingip_pools(self.request)
  79. pool_dict = dict([(obj.id, obj.name) for obj in pools])
  80. for ip in floating_ips:
  81. ip.instance_name = instances_dict.get(ip.instance_id)
  82. ip.pool_name = pool_dict.get(ip.pool, ip.pool)
  83. tenant = tenant_dict.get(ip.tenant_id, None)
  84. ip.tenant_name = getattr(tenant, "name", None)
  85. return floating_ips
  86. class DetailView(views.HorizonTemplateView):
  87. template_name = 'admin/floating_ips/detail.html'
  88. page_title = _("Floating IP Details")
  89. def _get_corresponding_data(self, resource, resource_id):
  90. function_dict = {"floating IP": api.neutron.tenant_floating_ip_get,
  91. "instance": api.nova.server_get,
  92. "network": api.neutron.network_get,
  93. "router": api.neutron.router_get}
  94. url = reverse('horizon:admin:floating_ips:index')
  95. try:
  96. res = function_dict[resource](
  97. self.request, resource_id)
  98. if resource in ["network", "router"]:
  99. res.set_id_as_name_if_empty(length=0)
  100. return res
  101. except KeyError:
  102. msg = _('Unknown resource type for detail API.')
  103. exceptions.handle(self.request, msg, redirect=url)
  104. except Exception:
  105. msg = _('Unable to retrieve details for '
  106. '%(resource)s "%(resource_id)s".') % {
  107. "resource": resource,
  108. "resource_id": resource_id}
  109. exceptions.handle(self.request, msg, redirect=url)
  110. def get_context_data(self, **kwargs):
  111. context = super(DetailView, self).get_context_data(**kwargs)
  112. floating_ip_id = self.kwargs['floating_ip_id']
  113. floating_ip = self._get_corresponding_data("floating IP",
  114. floating_ip_id)
  115. network = self._get_corresponding_data("network", floating_ip.pool)
  116. floating_ip.pool_name = network.name
  117. if floating_ip.instance_id and floating_ip.instance_type == 'compute':
  118. instance = self._get_corresponding_data(
  119. "instance", floating_ip.instance_id)
  120. floating_ip.instance_name = instance.name
  121. floating_ip.mapped_fixed_ip = project_tables.get_instance_info(
  122. floating_ip)
  123. if floating_ip.router_id:
  124. router = self._get_corresponding_data("router",
  125. floating_ip.router_id)
  126. floating_ip.router_name = router.name
  127. table = fip_tables.FloatingIPsTable(self.request)
  128. context['floating_ip'] = floating_ip
  129. context["url"] = reverse('horizon:admin:floating_ips:index')
  130. context["actions"] = table.render_row_actions(floating_ip)
  131. return context
  132. class AllocateView(forms.ModalFormView):
  133. form_class = fip_forms.AdminFloatingIpAllocate
  134. form_id = "allocate_floating_ip_form"
  135. template_name = 'admin/floating_ips/allocate.html'
  136. submit_label = _("Allocate Floating IP")
  137. submit_url = reverse_lazy("horizon:admin:floating_ips:allocate")
  138. cancel_url = reverse_lazy('horizon:admin:floating_ips:index')
  139. success_url = reverse_lazy('horizon:admin:floating_ips:index')
  140. page_title = _("Allocate Floating IP")
  141. @memoized.memoized_method
  142. def get_initial(self):
  143. tenants = get_tenant_list(self.request)
  144. tenant_list = [(t.id, t.name) for t in tenants]
  145. if not tenant_list:
  146. tenant_list = [(None, _("No project available"))]
  147. pools = get_floatingip_pools(self.request)
  148. pool_list = []
  149. for pool in pools:
  150. for subnet in pool.subnets:
  151. if netaddr.IPNetwork(subnet.cidr).version != 4:
  152. continue
  153. pool_display_name = (_("%(pool_name)s %(cidr)s")
  154. % {'pool_name': pool.name,
  155. 'cidr': subnet.cidr})
  156. pool_list.append((subnet.id, pool_display_name))
  157. if not pool_list:
  158. pool_list = [
  159. (None, _("No floating IP pools with IPv4 subnet available"))]
  160. return {'pool_list': pool_list,
  161. 'tenant_list': tenant_list}