1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 Nebula, Inc.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
18
Views for managing Nova volumes.
23
from django import shortcuts
24
from django.contrib import messages
25
from django.utils.translation import ugettext as _
26
from novaclient import exceptions as novaclient_exceptions
28
from horizon import api
29
from horizon import exceptions
30
from horizon import forms
31
from horizon import tables
32
from .forms import CreateForm, AttachForm, CreateSnapshotForm
33
from .tables import AttachmentsTable
36
LOG = logging.getLogger(__name__)
39
def detail(request, volume_id):
41
volume = api.volume_get(request, volume_id)
42
attachment = volume.attachments[0]
44
instance = api.server_get(
45
request, volume.attachments[0]['serverId'])
48
except novaclient_exceptions.ClientException, e:
49
LOG.exception("ClientException in volume get")
50
messages.error(request, _('Error fetching volume: %s') % e.message)
51
return shortcuts.redirect(
52
'horizon:nova:instances_and_volumes:volumes:index')
54
return shortcuts.render(request,
55
'nova/instances_and_volumes/volumes/detail.html', {
57
'attachment': attachment,
58
'instance': instance})
61
class CreateView(forms.ModalFormView):
62
form_class = CreateForm
63
template_name = 'nova/instances_and_volumes/volumes/create.html'
66
class CreateSnapshotView(forms.ModalFormView):
67
form_class = CreateSnapshotForm
68
template_name = 'nova/instances_and_volumes/volumes/create_snapshot.html'
70
def get_context_data(self, **kwargs):
71
return {'volume_id': kwargs['volume_id']}
73
def get_initial(self):
74
return {'volume_id': self.kwargs["volume_id"]}
77
class EditAttachmentsView(tables.DataTableView):
78
table_class = AttachmentsTable
79
template_name = 'nova/instances_and_volumes/volumes/attach.html'
82
if not hasattr(self, "_object"):
83
volume_id = self.kwargs['volume_id']
85
self._object = api.volume_get(self.request, volume_id)
88
exceptions.handle(self.request,
89
_('Unable to retrieve volume information.'))
94
volumes = self.get_object()
95
attachments = [att for att in volumes.attachments if att]
98
exceptions.handle(self.request,
99
_('Unable to retrieve volume information.'))
102
def get_context_data(self, **kwargs):
103
context = super(EditAttachmentsView, self).get_context_data(**kwargs)
104
context['form'] = self.form
105
context['volume'] = self.get_object()
108
def handle_form(self):
109
instances = api.nova.server_list(self.request)
110
initial = {'volume_id': self.kwargs["volume_id"],
111
'instances': instances}
112
return AttachForm.maybe_handle(self.request, initial=initial)
114
def get(self, request, *args, **kwargs):
115
self.form, handled = self.handle_form()
118
return super(EditAttachmentsView, self).get(request, *args, **kwargs)
120
def post(self, request, *args, **kwargs):
121
form, handled = self.handle_form()
124
return super(EditAttachmentsView, self).post(request, *args, **kwargs)