1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
7
# Copyright 2012 Nebula, Inc.
9
# Licensed under the Apache License, Version 2.0 (the "License"); you may
10
# not use this file except in compliance with the License. You may obtain
11
# a copy of the License at
13
# http://www.apache.org/licenses/LICENSE-2.0
15
# Unless required by applicable law or agreed to in writing, software
16
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18
# License for the specific language governing permissions and limitations
22
Views for managing Nova instances.
26
from django import http
27
from django import shortcuts
28
from django.core.urlresolvers import reverse
29
from django.utils.datastructures import SortedDict
30
from django.utils.translation import ugettext as _
32
from horizon import api
33
from horizon import exceptions
34
from horizon import forms
35
from horizon import views
36
from .forms import UpdateInstance
39
LOG = logging.getLogger(__name__)
42
def console(request, instance_id):
44
# TODO(jakedahn): clean this up once the api supports tailing.
45
length = request.GET.get('length', None)
46
console = api.server_console_output(request,
49
response = http.HttpResponse(mimetype='text/plain')
50
response.write(console)
54
msg = _('Unable to get log for instance "%s".') % instance_id
55
redirect = reverse('horizon:nova:instances_and_volumes:index')
56
exceptions.handle(request, msg, redirect=redirect)
59
def vnc(request, instance_id):
61
console = api.server_vnc_console(request, instance_id)
62
instance = api.server_get(request, instance_id)
63
return shortcuts.redirect(console.url +
64
("&title=%s(%s)" % (instance.name, instance_id)))
66
redirect = reverse("horizon:nova:instances_and_volumes:index")
67
msg = _('Unable to get VNC console for instance "%s".') % instance_id
68
exceptions.handle(request, msg, redirect=redirect)
71
class UpdateView(forms.ModalFormView):
72
form_class = UpdateInstance
73
template_name = 'nova/instances_and_volumes/instances/update.html'
74
context_object_name = 'instance'
76
def get_object(self, *args, **kwargs):
77
if not hasattr(self, "object"):
78
instance_id = self.kwargs['instance_id']
80
self.object = api.server_get(self.request, instance_id)
82
redirect = reverse("horizon:nova:instances_and_volumes:index")
83
msg = _('Unable to retrieve instance details.')
84
exceptions.handle(self.request, msg, redirect=redirect)
87
def get_initial(self):
88
return {'instance': self.kwargs['instance_id'],
89
'tenant_id': self.request.user.tenant_id,
90
'name': getattr(self.object, 'name', '')}
93
class DetailView(views.APIView):
94
template_name = 'nova/instances_and_volumes/instances/detail.html'
96
def get_data(self, request, context, *args, **kwargs):
97
instance_id = kwargs['instance_id']
99
if "show" in request.GET:
100
show_tab = request.GET["show"]
102
show_tab = "overview"
105
instance = api.server_get(request, instance_id)
106
volumes = api.volume_instance_list(request, instance_id)
108
# Gather our flavors and images and correlate our instances to
109
# them. Exception handling happens in the parent class.
110
flavors = api.flavor_list(request)
111
full_flavors = SortedDict([(str(flavor.id), flavor) for \
113
instance.full_flavor = full_flavors[instance.flavor["id"]]
115
context.update({'instance': instance, 'volumes': volumes})
117
redirect = reverse('horizon:nova:instances_and_volumes:index')
118
exceptions.handle(request,
119
_('Unable to retrieve details for '
120
'instance "%s".') % instance_id,
122
if show_tab == "vnc":
124
console = api.server_vnc_console(request, instance_id)
125
vnc_url = "%s&title=%s(%s)" % (console.url,
126
getattr(instance, "name", ""),
128
context.update({'vnc_url': vnc_url})
130
exceptions.handle(request,
131
_('Unable to get vnc console for '
132
'instance "%s".') % instance_id)
134
context.update({'show_tab': show_tab})