# 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 volumes.
"""
from django.core.urlresolvers import reverse_lazy # noqa
from django.utils.datastructures import SortedDict # noqa
from django.utils.translation import ugettext_lazy as _ # noqa
from horizon import exceptions
from horizon import forms
from horizon import tables
from horizon import tabs
from openstack_dashboard import api
from openstack_dashboard.api import cinder
from openstack_dashboard.usage import quotas
from openstack_dashboard.dashboards.project.volumes \
import forms as project_forms
from openstack_dashboard.dashboards.project.volumes \
import tables as project_tables
from openstack_dashboard.dashboards.project.volumes \
import tabs as project_tabs
class VolumeTableMixIn(object):
def _get_volumes(self, search_opts=None):
[docs] template_name = 'project/volumes/index.html'
def get_data(self):
volumes = self._get_volumes()
[docs] instances = self._get_instances()
self._set_id_if_nameless(volumes, instances)
self._set_attachments_string(volumes, instances)
return volumes
class DetailView(tabs.TabView):
tab_group_class = project_tabs.VolumeDetailTabs
[docs] template_name = 'project/volumes/detail.html'
class CreateView(forms.ModalFormView):
form_class = project_forms.CreateForm
[docs] template_name = 'project/volumes/create.html'
success_url = reverse_lazy("horizon:project:volumes:index")
def get_context_data(self, **kwargs):
context = super(CreateView, self).get_context_data(**kwargs)
[docs] try:
context['usages'] = quotas.tenant_limit_usages(self.request)
except Exception:
exceptions.handle(self.request)
return context
class CreateSnapshotView(forms.ModalFormView):
form_class = project_forms.CreateSnapshotForm
[docs] template_name = 'project/volumes/create_snapshot.html'
success_url = reverse_lazy("horizon:project:images_and_snapshots:index")
def get_context_data(self, **kwargs):
context = super(CreateSnapshotView, self).get_context_data(**kwargs)
[docs] context['volume_id'] = self.kwargs['volume_id']
try:
context['usages'] = quotas.tenant_limit_usages(self.request)
except Exception:
exceptions.handle(self.request)
return context
def get_initial(self):
return {'volume_id': self.kwargs["volume_id"]}
[docs]
class EditAttachmentsView(tables.DataTableView, forms.ModalFormView):
table_class = project_tables.AttachmentsTable
[docs] form_class = project_forms.AttachForm
template_name = 'project/volumes/attach.html'
success_url = reverse_lazy("horizon:project:volumes:index")
def get_object(self):
if not hasattr(self, "_object"):
[docs] volume_id = self.kwargs['volume_id']
try:
self._object = cinder.volume_get(self.request, volume_id)
except Exception:
self._object = None
exceptions.handle(self.request,
_('Unable to retrieve volume information.'))
return self._object
def get_data(self):
try:
[docs] volumes = self.get_object()
attachments = [att for att in volumes.attachments if att]
except Exception:
attachments = []
exceptions.handle(self.request,
_('Unable to retrieve volume information.'))
return attachments
def get_initial(self):
try:
[docs] instances, has_more = api.nova.server_list(self.request)
except Exception:
instances = []
exceptions.handle(self.request,
_("Unable to retrieve attachment information."))
return {'volume': self.get_object(),
'instances': instances}
def get_form(self):
if not hasattr(self, "_form"):
[docs] context['form'] = self.get_form()
volume = self.get_object()
if volume and volume.status == 'available':
context['show_attach'] = True
else:
context['show_attach'] = False
context['volume'] = volume
if self.request.is_ajax():
context['hide'] = True
return context
def get(self, request, *args, **kwargs):
# Table action handling
[docs] handled = self.construct_tables()
if handled:
return handled
return self.render_to_response(self.get_context_data(**kwargs))
def post(self, request, *args, **kwargs):
form = self.get_form()
[docs] if form.is_valid():
return self.form_valid(form)
else:
return self.get(request, *args, **kwargs)