"Fossies" - the Fresh Open Source Software Archive

Member "horizon-14.0.4/openstack_dashboard/dashboards/project/floating_ips/views.py" (22 Oct 2019, 4993 Bytes) of package /linux/misc/openstack/horizon-14.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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 """
   21 Views for managing floating IPs.
   22 """
   23 
   24 from django.urls import reverse_lazy
   25 from django.utils.translation import ugettext_lazy as _
   26 
   27 from neutronclient.common import exceptions as neutron_exc
   28 
   29 from horizon import exceptions
   30 from horizon import forms
   31 from horizon import tables
   32 from horizon import workflows
   33 
   34 from openstack_dashboard import api
   35 from openstack_dashboard.usage import quotas
   36 
   37 from openstack_dashboard.dashboards.project.floating_ips \
   38     import forms as project_forms
   39 from openstack_dashboard.dashboards.project.floating_ips \
   40     import tables as project_tables
   41 from openstack_dashboard.dashboards.project.floating_ips \
   42     import workflows as project_workflows
   43 
   44 
   45 class AssociateView(workflows.WorkflowView):
   46     workflow_class = project_workflows.IPAssociationWorkflow
   47 
   48 
   49 class AllocateView(forms.ModalFormView):
   50     form_class = project_forms.FloatingIpAllocate
   51     form_id = "associate_floating_ip_form"
   52     page_title = _("Allocate Floating IP")
   53     template_name = 'project/floating_ips/allocate.html'
   54     submit_label = _("Allocate IP")
   55     submit_url = reverse_lazy("horizon:project:floating_ips:allocate")
   56     success_url = reverse_lazy('horizon:project:floating_ips:index')
   57 
   58     def get_object_display(self, obj):
   59         return obj.ip
   60 
   61     def get_context_data(self, **kwargs):
   62         context = super(AllocateView, self).get_context_data(**kwargs)
   63         try:
   64             context['usages'] = quotas.tenant_quota_usages(
   65                 self.request, targets=('floatingip', ))
   66         except Exception:
   67             exceptions.handle(self.request)
   68         return context
   69 
   70     def get_initial(self):
   71         try:
   72             pools = api.neutron.floating_ip_pools_list(self.request)
   73         except neutron_exc.ConnectionFailed:
   74             pools = []
   75             exceptions.handle(self.request)
   76         except Exception:
   77             pools = []
   78             exceptions.handle(self.request,
   79                               _("Unable to retrieve floating IP pools."))
   80         pool_list = [(pool.id, pool.name) for pool in pools]
   81         if not pool_list:
   82             pool_list = [(None, _("No floating IP pools available"))]
   83         return {'pool_list': pool_list}
   84 
   85 
   86 class IndexView(tables.DataTableView):
   87     table_class = project_tables.FloatingIPsTable
   88     page_title = _("Floating IPs")
   89 
   90     def get_data(self):
   91         try:
   92             floating_ips = api.neutron.tenant_floating_ip_list(self.request)
   93         except neutron_exc.ConnectionFailed:
   94             floating_ips = []
   95             exceptions.handle(self.request)
   96         except Exception:
   97             floating_ips = []
   98             exceptions.handle(self.request,
   99                               _('Unable to retrieve floating IP addresses.'))
  100 
  101         try:
  102             floating_ip_pools = \
  103                 api.neutron.floating_ip_pools_list(self.request)
  104         except neutron_exc.ConnectionFailed:
  105             floating_ip_pools = []
  106             exceptions.handle(self.request)
  107         except Exception:
  108             floating_ip_pools = []
  109             exceptions.handle(self.request,
  110                               _('Unable to retrieve floating IP pools.'))
  111         pool_dict = dict([(obj.id, obj.name) for obj in floating_ip_pools])
  112 
  113         attached_instance_ids = [ip.instance_id for ip in floating_ips
  114                                  if ip.instance_id is not None]
  115         instances_dict = {}
  116         if attached_instance_ids:
  117             instances = []
  118             try:
  119                 # TODO(tsufiev): we should pass attached_instance_ids to
  120                 # nova.server_list as soon as Nova API allows for this
  121                 instances, has_more = api.nova.server_list(self.request,
  122                                                            detailed=False)
  123             except Exception:
  124                 exceptions.handle(self.request,
  125                                   _('Unable to retrieve instance list.'))
  126 
  127             instances_dict = dict([(obj.id, obj.name) for obj in instances])
  128 
  129         for ip in floating_ips:
  130             ip.instance_name = instances_dict.get(ip.instance_id)
  131             ip.pool_name = pool_dict.get(ip.pool, ip.pool)
  132 
  133         return floating_ips