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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright 2012 United States Government as represented by the
  2. # Administrator of the National Aeronautics and Space Administration.
  3. # All Rights Reserved.
  4. #
  5. # Copyright 2012 Nebula, Inc.
  6. # Copyright (c) 2012 X.commerce, a business unit of eBay Inc.
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. # not use this file except in compliance with the License. You may obtain
  10. # a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. # License for the specific language governing permissions and limitations
  18. # under the License.
  19. """
  20. Views for managing floating IPs.
  21. """
  22. from django.urls import reverse_lazy
  23. from django.utils.translation import ugettext_lazy as _
  24. from neutronclient.common import exceptions as neutron_exc
  25. from horizon import exceptions
  26. from horizon import forms
  27. from horizon import tables
  28. from horizon import workflows
  29. from openstack_dashboard import api
  30. from openstack_dashboard.usage import quotas
  31. from openstack_dashboard.dashboards.project.floating_ips \
  32. import forms as project_forms
  33. from openstack_dashboard.dashboards.project.floating_ips \
  34. import tables as project_tables
  35. from openstack_dashboard.dashboards.project.floating_ips \
  36. import workflows as project_workflows
  37. class AssociateView(workflows.WorkflowView):
  38. workflow_class = project_workflows.IPAssociationWorkflow
  39. class AllocateView(forms.ModalFormView):
  40. form_class = project_forms.FloatingIpAllocate
  41. form_id = "associate_floating_ip_form"
  42. page_title = _("Allocate Floating IP")
  43. template_name = 'project/floating_ips/allocate.html'
  44. submit_label = _("Allocate IP")
  45. submit_url = reverse_lazy("horizon:project:floating_ips:allocate")
  46. success_url = reverse_lazy('horizon:project:floating_ips:index')
  47. def get_object_display(self, obj):
  48. return obj.ip
  49. def get_context_data(self, **kwargs):
  50. context = super(AllocateView, self).get_context_data(**kwargs)
  51. try:
  52. context['usages'] = quotas.tenant_quota_usages(
  53. self.request, targets=('floatingip', ))
  54. except Exception:
  55. exceptions.handle(self.request)
  56. return context
  57. def get_initial(self):
  58. try:
  59. pools = api.neutron.floating_ip_pools_list(self.request)
  60. except neutron_exc.ConnectionFailed:
  61. pools = []
  62. exceptions.handle(self.request)
  63. except Exception:
  64. pools = []
  65. exceptions.handle(self.request,
  66. _("Unable to retrieve floating IP pools."))
  67. pool_list = [(pool.id, pool.name) for pool in pools]
  68. if not pool_list:
  69. pool_list = [(None, _("No floating IP pools available"))]
  70. return {'pool_list': pool_list}
  71. class IndexView(tables.DataTableView):
  72. table_class = project_tables.FloatingIPsTable
  73. page_title = _("Floating IPs")
  74. def get_data(self):
  75. try:
  76. search_opts = self.get_filters()
  77. floating_ips = api.neutron.tenant_floating_ip_list(self.request,
  78. **search_opts)
  79. except neutron_exc.ConnectionFailed:
  80. floating_ips = []
  81. exceptions.handle(self.request)
  82. except Exception:
  83. floating_ips = []
  84. exceptions.handle(self.request,
  85. _('Unable to retrieve floating IP addresses.'))
  86. try:
  87. floating_ip_pools = \
  88. api.neutron.floating_ip_pools_list(self.request)
  89. except neutron_exc.ConnectionFailed:
  90. floating_ip_pools = []
  91. exceptions.handle(self.request)
  92. except Exception:
  93. floating_ip_pools = []
  94. exceptions.handle(self.request,
  95. _('Unable to retrieve floating IP pools.'))
  96. pool_dict = dict([(obj.id, obj.name) for obj in floating_ip_pools])
  97. attached_instance_ids = [ip.instance_id for ip in floating_ips
  98. if ip.instance_id is not None]
  99. instances_dict = {}
  100. if attached_instance_ids:
  101. instances = []
  102. try:
  103. # TODO(tsufiev): we should pass attached_instance_ids to
  104. # nova.server_list as soon as Nova API allows for this
  105. instances, has_more = api.nova.server_list(self.request,
  106. detailed=False)
  107. except Exception:
  108. exceptions.handle(self.request,
  109. _('Unable to retrieve instance list.'))
  110. instances_dict = dict([(obj.id, obj.name) for obj in instances])
  111. for ip in floating_ips:
  112. ip.instance_name = instances_dict.get(ip.instance_id)
  113. ip.pool_name = pool_dict.get(ip.pool, ip.pool)
  114. return floating_ips