OpenStack Dashboard (Horizon)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2. # Copyright 2012 United States Government as represented by the
  3. # Administrator of the National Aeronautics and Space Administration.
  4. # All Rights Reserved.
  5. #
  6. # Copyright 2012 Nebula, 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. Views for managing Nova instances.
  21. """
  22. import logging
  23. from django import http
  24. from django import shortcuts
  25. from django.core.urlresolvers import reverse
  26. from django.utils.datastructures import SortedDict
  27. from django.utils.translation import ugettext as _
  28. from horizon import api
  29. from horizon import exceptions
  30. from horizon import forms
  31. from horizon import tabs
  32. from .forms import UpdateInstance
  33. from .tabs import InstanceDetailTabs
  34. LOG = logging.getLogger(__name__)
  35. def console(request, instance_id):
  36. try:
  37. # TODO(jakedahn): clean this up once the api supports tailing.
  38. data = api.server_console_output(request, instance_id)
  39. except:
  40. data = _('Unable to get log for instance "%s".') % instance_id
  41. exceptions.handle(request, ignore=True)
  42. response = http.HttpResponse(mimetype='text/plain')
  43. response.write(data)
  44. response.flush()
  45. return response
  46. def vnc(request, instance_id):
  47. try:
  48. console = api.server_vnc_console(request, instance_id)
  49. instance = api.server_get(request, instance_id)
  50. return shortcuts.redirect(console.url +
  51. ("&title=%s(%s)" % (instance.name, instance_id)))
  52. except:
  53. redirect = reverse("horizon:nova:instances_and_volumes:index")
  54. msg = _('Unable to get VNC console for instance "%s".') % instance_id
  55. exceptions.handle(request, msg, redirect=redirect)
  56. class UpdateView(forms.ModalFormView):
  57. form_class = UpdateInstance
  58. template_name = 'nova/instances_and_volumes/instances/update.html'
  59. context_object_name = 'instance'
  60. def get_object(self, *args, **kwargs):
  61. if not hasattr(self, "object"):
  62. instance_id = self.kwargs['instance_id']
  63. try:
  64. self.object = api.server_get(self.request, instance_id)
  65. except:
  66. redirect = reverse("horizon:nova:instances_and_volumes:index")
  67. msg = _('Unable to retrieve instance details.')
  68. exceptions.handle(self.request, msg, redirect=redirect)
  69. return self.object
  70. def get_initial(self):
  71. return {'instance': self.kwargs['instance_id'],
  72. 'tenant_id': self.request.user.tenant_id,
  73. 'name': getattr(self.object, 'name', '')}
  74. class DetailView(tabs.TabView):
  75. tab_group_class = InstanceDetailTabs
  76. template_name = 'nova/instances_and_volumes/instances/detail.html'
  77. def get_context_data(self, **kwargs):
  78. context = super(DetailView, self).get_context_data(**kwargs)
  79. context["instance"] = self.get_data()
  80. return context
  81. def get_data(self):
  82. if not hasattr(self, "_instance"):
  83. try:
  84. instance_id = self.kwargs['instance_id']
  85. instance = api.server_get(self.request, instance_id)
  86. instance.volumes = api.volume_instance_list(self.request,
  87. instance_id)
  88. # Gather our flavors and images and correlate our instances to
  89. # them. Exception handling happens in the parent class.
  90. flavors = api.flavor_list(self.request)
  91. full_flavors = SortedDict([(str(flavor.id), flavor) for \
  92. flavor in flavors])
  93. instance.full_flavor = full_flavors[instance.flavor["id"]]
  94. except:
  95. redirect = reverse('horizon:nova:instances_and_volumes:index')
  96. exceptions.handle(self.request,
  97. _('Unable to retrieve details for '
  98. 'instance "%s".') % instance_id,
  99. redirect=redirect)
  100. self._instance = instance
  101. return self._instance
  102. def get_tabs(self, request, *args, **kwargs):
  103. instance = self.get_data()
  104. return self.tab_group_class(request, instance=instance, **kwargs)