
blueprint quantum-security-group Rule table view * Add direction and ethertype columns (which are specific to Neutron) It may be better to hide "Direction" and "Ether Type" columns unless Quantum security group is enabled. * Merge ip_protocol/from_port/to_port into one column for better view * Use "::/0" for IPv6 ANY instead of "0.0.0.0/0" * Rename "Source" column to "Remote". (The naming "source" does not fit egress rules) * Display security group name in the title of rule detail view Rule creation form * New arguments 'direction' and 'ethertype' in security_group_rule_create() * Set the default value of 'direction' to 'ingress' in forms.handle() * Rename 'ip_protocol' to 'rule_menu' and 'source' to 'remote' Note that rule_menu is retrieved from rule.ip_protocol in the unit tests since they are tests for custom TCP/UDP/ICMP rules. Network abstraction layer for security group management * Move security group methods to api.network * Add Neutron security group API implementation * Move base classes for network abstraction to a separate module (api/network_base.py) to avoid circulated import between api.network and api.nova/api.neutron Add a configuration parameter to control Neutron security group support * Neutron security group support is enabled when Neutron is enabled and enable_security_group in OPENSTACK_NEUTRON_NETWORK in settings is True. * Not all neutron plugins support security group, so we need a way to control neutron security group is enabled or not. * It can be determined by supported extension list from Neutron and it is a possible future work. Move get_int_or_uuid to openstack_dashboard/utils/filters. * get_int_or_uuid is now used in security_group implementation as well as floating IP logics. * In addition the depth of the directory tree becomes longer and it is hard to fit the import line in 80 chars. It is a good chance to move it to a common directory. Add __repr__ to API**Wrapper to make it easier to debug. Limitations: Neutron supports per-port security group. security groups can be associated with a port instead of an instace and each port can have a different set of security groups. It is not a scope of this BP and is a future work. Change-Id: I5410e88043a364596037b9ebcc566cd50b317614
263 lines
10 KiB
Python
263 lines
10 KiB
Python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
# Copyright 2012 United States Government as represented by the
|
|
# Administrator of the National Aeronautics and Space Administration.
|
|
# All Rights Reserved.
|
|
#
|
|
# Copyright 2012 Nebula, Inc.
|
|
#
|
|
# 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.
|
|
|
|
"""
|
|
Views for managing instances.
|
|
"""
|
|
import logging
|
|
|
|
from django.core.urlresolvers import reverse
|
|
from django.core.urlresolvers import reverse_lazy
|
|
from django import http
|
|
from django import shortcuts
|
|
from django.utils.datastructures import SortedDict
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from horizon import exceptions
|
|
from horizon import tables
|
|
from horizon import tabs
|
|
from horizon import workflows
|
|
|
|
from openstack_dashboard import api
|
|
from openstack_dashboard.dashboards.project.instances.tables import \
|
|
InstancesTable
|
|
from openstack_dashboard.dashboards.project.instances.tabs import \
|
|
InstanceDetailTabs
|
|
from openstack_dashboard.dashboards.project.instances.workflows import \
|
|
LaunchInstance
|
|
from openstack_dashboard.dashboards.project.instances.workflows import \
|
|
ResizeInstance
|
|
from openstack_dashboard.dashboards.project.instances.workflows import \
|
|
UpdateInstance
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class IndexView(tables.DataTableView):
|
|
table_class = InstancesTable
|
|
template_name = 'project/instances/index.html'
|
|
|
|
def has_more_data(self, table):
|
|
return self._more
|
|
|
|
def get_data(self):
|
|
marker = self.request.GET.get(
|
|
InstancesTable._meta.pagination_param, None)
|
|
# Gather our instances
|
|
try:
|
|
instances, self._more = api.nova.server_list(
|
|
self.request,
|
|
search_opts={'marker': marker,
|
|
'paginate': True})
|
|
except:
|
|
self._more = False
|
|
instances = []
|
|
exceptions.handle(self.request,
|
|
_('Unable to retrieve instances.'))
|
|
# Gather our flavors and correlate our instances to them
|
|
if instances:
|
|
try:
|
|
flavors = api.nova.flavor_list(self.request)
|
|
except:
|
|
flavors = []
|
|
exceptions.handle(self.request, ignore=True)
|
|
|
|
full_flavors = SortedDict([(str(flavor.id), flavor)
|
|
for flavor in flavors])
|
|
# Loop through instances to get flavor info.
|
|
for instance in instances:
|
|
try:
|
|
flavor_id = instance.flavor["id"]
|
|
if flavor_id in full_flavors:
|
|
instance.full_flavor = full_flavors[flavor_id]
|
|
else:
|
|
# If the flavor_id is not in full_flavors list,
|
|
# get it via nova api.
|
|
instance.full_flavor = api.nova.flavor_get(
|
|
self.request, flavor_id)
|
|
except:
|
|
msg = _('Unable to retrieve instance size information.')
|
|
exceptions.handle(self.request, msg)
|
|
return instances
|
|
|
|
|
|
class LaunchInstanceView(workflows.WorkflowView):
|
|
workflow_class = LaunchInstance
|
|
|
|
def get_initial(self):
|
|
initial = super(LaunchInstanceView, self).get_initial()
|
|
initial['project_id'] = self.request.user.tenant_id
|
|
initial['user_id'] = self.request.user.id
|
|
return initial
|
|
|
|
|
|
def console(request, instance_id):
|
|
try:
|
|
# TODO(jakedahn): clean this up once the api supports tailing.
|
|
tail = request.GET.get('length', None)
|
|
data = api.nova.server_console_output(request,
|
|
instance_id,
|
|
tail_length=tail)
|
|
except:
|
|
data = _('Unable to get log for instance "%s".') % instance_id
|
|
exceptions.handle(request, ignore=True)
|
|
response = http.HttpResponse(mimetype='text/plain')
|
|
response.write(data)
|
|
response.flush()
|
|
return response
|
|
|
|
|
|
def vnc(request, instance_id):
|
|
try:
|
|
console = api.nova.server_vnc_console(request, instance_id)
|
|
instance = api.nova.server_get(request, instance_id)
|
|
return shortcuts.redirect(console.url +
|
|
("&title=%s(%s)" % (instance.name, instance_id)))
|
|
except:
|
|
redirect = reverse("horizon:project:instances:index")
|
|
msg = _('Unable to get VNC console for instance "%s".') % instance_id
|
|
exceptions.handle(request, msg, redirect=redirect)
|
|
|
|
|
|
def spice(request, instance_id):
|
|
try:
|
|
console = api.nova.server_spice_console(request, instance_id)
|
|
instance = api.nova.server_get(request, instance_id)
|
|
return shortcuts.redirect(console.url +
|
|
("&title=%s(%s)" % (instance.name, instance_id)))
|
|
except:
|
|
redirect = reverse("horizon:project:instances:index")
|
|
msg = _('Unable to get SPICE console for instance "%s".') % instance_id
|
|
exceptions.handle(request, msg, redirect=redirect)
|
|
|
|
|
|
class UpdateView(workflows.WorkflowView):
|
|
workflow_class = UpdateInstance
|
|
success_url = reverse_lazy("horizon:project:instances:index")
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(UpdateView, self).get_context_data(**kwargs)
|
|
context["instance_id"] = self.kwargs['instance_id']
|
|
return context
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
if not hasattr(self, "_object"):
|
|
instance_id = self.kwargs['instance_id']
|
|
try:
|
|
self._object = api.nova.server_get(self.request, instance_id)
|
|
except:
|
|
redirect = reverse("horizon:project:instances:index")
|
|
msg = _('Unable to retrieve instance details.')
|
|
exceptions.handle(self.request, msg, redirect=redirect)
|
|
return self._object
|
|
|
|
def get_initial(self):
|
|
initial = super(UpdateView, self).get_initial()
|
|
initial.update({'instance_id': self.kwargs['instance_id'],
|
|
'name': getattr(self.get_object(), 'name', '')})
|
|
return initial
|
|
|
|
|
|
class DetailView(tabs.TabView):
|
|
tab_group_class = InstanceDetailTabs
|
|
template_name = 'project/instances/detail.html'
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(DetailView, self).get_context_data(**kwargs)
|
|
context["instance"] = self.get_data()
|
|
return context
|
|
|
|
def get_data(self):
|
|
if not hasattr(self, "_instance"):
|
|
try:
|
|
instance_id = self.kwargs['instance_id']
|
|
instance = api.nova.server_get(self.request, instance_id)
|
|
instance.volumes = api.nova.instance_volumes_list(self.request,
|
|
instance_id)
|
|
# Sort by device name
|
|
instance.volumes.sort(key=lambda vol: vol.device)
|
|
instance.full_flavor = api.nova.flavor_get(
|
|
self.request, instance.flavor["id"])
|
|
instance.security_groups = api.network.server_security_groups(
|
|
self.request, instance_id)
|
|
except:
|
|
redirect = reverse('horizon:project:instances:index')
|
|
exceptions.handle(self.request,
|
|
_('Unable to retrieve details for '
|
|
'instance "%s".') % instance_id,
|
|
redirect=redirect)
|
|
self._instance = instance
|
|
return self._instance
|
|
|
|
def get_tabs(self, request, *args, **kwargs):
|
|
instance = self.get_data()
|
|
return self.tab_group_class(request, instance=instance, **kwargs)
|
|
|
|
|
|
class ResizeView(workflows.WorkflowView):
|
|
workflow_class = ResizeInstance
|
|
success_url = reverse_lazy("horizon:project:instances:index")
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(ResizeView, self).get_context_data(**kwargs)
|
|
context["instance_id"] = self.kwargs['instance_id']
|
|
return context
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
if not hasattr(self, "_object"):
|
|
instance_id = self.kwargs['instance_id']
|
|
try:
|
|
self._object = api.nova.server_get(self.request, instance_id)
|
|
flavor_id = self._object.flavor['id']
|
|
flavors = self.get_flavors()
|
|
if flavor_id in flavors:
|
|
self._object.flavor_name = flavors[flavor_id].name
|
|
else:
|
|
flavor = api.nova.flavor_get(self.request, flavor_id)
|
|
self._object.flavor_name = flavor.name
|
|
except:
|
|
redirect = reverse("horizon:project:instances:index")
|
|
msg = _('Unable to retrieve instance details.')
|
|
exceptions.handle(self.request, msg, redirect=redirect)
|
|
return self._object
|
|
|
|
def get_flavors(self, *args, **kwargs):
|
|
if not hasattr(self, "_flavors"):
|
|
try:
|
|
flavors = api.nova.flavor_list(self.request)
|
|
self._flavors = SortedDict([(str(flavor.id), flavor)
|
|
for flavor in flavors])
|
|
except:
|
|
redirect = reverse("horizon:project:instances:index")
|
|
exceptions.handle(self.request,
|
|
_('Unable to retrieve flavors.'), redirect=redirect)
|
|
return self._flavors
|
|
|
|
def get_initial(self):
|
|
initial = super(ResizeView, self).get_initial()
|
|
_object = self.get_object()
|
|
if _object:
|
|
initial.update({'instance_id': self.kwargs['instance_id'],
|
|
'old_flavor_id': _object.flavor['id'],
|
|
'old_flavor_name': getattr(_object, 'flavor_name', ''),
|
|
'flavors': self.get_flavors()})
|
|
return initial
|