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 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. # Copyright 2012 United States Government as represented by the
  2. # Administrator of the National Aeronautics and Space Administration.
  3. # All Rights Reserved.
  4. #
  5. # Copyright 2012 Nebula, Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. # not use this file except in compliance with the License. You may obtain
  9. # a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. # License for the specific language governing permissions and limitations
  17. # under the License.
  18. """
  19. Views for managing instances.
  20. """
  21. from collections import OrderedDict
  22. import logging
  23. from django.conf import settings
  24. from django import http
  25. from django import shortcuts
  26. from django.urls import reverse
  27. from django.urls import reverse_lazy
  28. from django.utils.translation import ugettext_lazy as _
  29. from django.views import generic
  30. from horizon import exceptions
  31. from horizon import forms
  32. from horizon import messages
  33. from horizon import tables
  34. from horizon import tabs
  35. from horizon.utils import memoized
  36. from horizon import workflows
  37. from openstack_dashboard import api
  38. from openstack_dashboard.utils import filters
  39. from openstack_dashboard.dashboards.project.instances \
  40. import console as project_console
  41. from openstack_dashboard.dashboards.project.instances \
  42. import forms as project_forms
  43. from openstack_dashboard.dashboards.project.instances \
  44. import tables as project_tables
  45. from openstack_dashboard.dashboards.project.instances \
  46. import tabs as project_tabs
  47. from openstack_dashboard.dashboards.project.instances \
  48. import workflows as project_workflows
  49. from openstack_dashboard.dashboards.project.networks.ports \
  50. import views as port_views
  51. from openstack_dashboard.utils import futurist_utils
  52. from openstack_dashboard.views import get_url_with_pagination
  53. LOG = logging.getLogger(__name__)
  54. class IndexView(tables.PagedTableMixin, tables.DataTableView):
  55. table_class = project_tables.InstancesTable
  56. page_title = _("Instances")
  57. def has_prev_data(self, table):
  58. return getattr(self, "_prev", False)
  59. def has_more_data(self, table):
  60. return self._more
  61. def _get_flavors(self):
  62. # Gather our flavors to correlate our instances to them
  63. try:
  64. flavors = api.nova.flavor_list(self.request)
  65. return dict((str(flavor.id), flavor) for flavor in flavors)
  66. except Exception:
  67. exceptions.handle(self.request, ignore=True)
  68. return {}
  69. def _get_images(self):
  70. # Gather our images to correlate our instances to them
  71. try:
  72. # TODO(gabriel): Handle pagination.
  73. images = api.glance.image_list_detailed(self.request)[0]
  74. return dict((str(image.id), image) for image in images)
  75. except Exception:
  76. exceptions.handle(self.request, ignore=True)
  77. return {}
  78. def _get_instances(self, search_opts, sort_dir):
  79. try:
  80. instances, self._more, self._prev = api.nova.server_list_paged(
  81. self.request,
  82. search_opts=search_opts,
  83. sort_dir=sort_dir)
  84. except Exception:
  85. self._more = self._prev = False
  86. instances = []
  87. exceptions.handle(self.request,
  88. _('Unable to retrieve instances.'))
  89. # In case of exception when calling nova.server_list
  90. # don't call api.network
  91. if not instances:
  92. return []
  93. # TODO(future): Explore more efficient logic to sync IP address
  94. # and drop the setting OPENSTACK_INSTANCE_RETRIEVE_IP_ADDRESSES.
  95. # The situation servers_update_addresses() is needed is only
  96. # when IP address of a server is updated via neutron API and
  97. # nova network info cache is not synced. Precisely there is no
  98. # need to check IP addresses of all servers. It is sufficient to
  99. # fetch IP address information for servers recently updated.
  100. if not getattr(settings,
  101. 'OPENSTACK_INSTANCE_RETRIEVE_IP_ADDRESSES', True):
  102. return instances
  103. try:
  104. api.network.servers_update_addresses(self.request, instances)
  105. except Exception:
  106. exceptions.handle(
  107. self.request,
  108. message=_('Unable to retrieve IP addresses from Neutron.'),
  109. ignore=True)
  110. return instances
  111. def get_data(self):
  112. marker, sort_dir = self._get_marker()
  113. search_opts = self.get_filters({'marker': marker, 'paginate': True})
  114. image_dict, flavor_dict = futurist_utils.call_functions_parallel(
  115. self._get_images, self._get_flavors)
  116. non_api_filter_info = (
  117. ('image_name', 'image', image_dict.values()),
  118. ('flavor_name', 'flavor', flavor_dict.values()),
  119. )
  120. if not process_non_api_filters(search_opts, non_api_filter_info):
  121. self._more = False
  122. return []
  123. instances = self._get_instances(search_opts, sort_dir)
  124. # Loop through instances to get flavor info.
  125. for instance in instances:
  126. if hasattr(instance, 'image'):
  127. # Instance from image returns dict
  128. if isinstance(instance.image, dict):
  129. image_id = instance.image.get('id')
  130. if image_id in image_dict:
  131. instance.image = image_dict[image_id]
  132. # In case image not found in image_dict, set name to empty
  133. # to avoid fallback API call to Glance in api/nova.py
  134. # until the call is deprecated in api itself
  135. else:
  136. instance.image['name'] = _("-")
  137. flavor_id = instance.flavor["id"]
  138. if flavor_id in flavor_dict:
  139. instance.full_flavor = flavor_dict[flavor_id]
  140. else:
  141. # If the flavor_id is not in flavor_dict,
  142. # put info in the log file.
  143. LOG.info('Unable to retrieve flavor "%s" for instance "%s".',
  144. flavor_id, instance.id)
  145. return instances
  146. def process_non_api_filters(search_opts, non_api_filter_info):
  147. """Process filters by non-API fields
  148. There are cases where it is useful to provide a filter field
  149. which does not exist in a resource in a backend service.
  150. For example, nova server list provides 'image' field with image ID
  151. but 'image name' is more useful for GUI users.
  152. This function replaces fake fields into corresponding real fields.
  153. The format of non_api_filter_info is a tuple/list of
  154. (fake_field, real_field, resources).
  155. This returns True if further lookup is required.
  156. It returns False if there are no matching resources,
  157. for example, if no corresponding real field exists.
  158. """
  159. for fake_field, real_field, resources in non_api_filter_info:
  160. if not _swap_filter(resources, search_opts, fake_field, real_field):
  161. return False
  162. return True
  163. def _swap_filter(resources, search_opts, fake_field, real_field):
  164. if fake_field not in search_opts:
  165. return True
  166. filter_string = search_opts[fake_field]
  167. matched = [resource for resource in resources
  168. if resource.name.lower() == filter_string.lower()]
  169. if not matched:
  170. return False
  171. search_opts[real_field] = matched[0].id
  172. del search_opts[fake_field]
  173. return True
  174. class LaunchInstanceView(workflows.WorkflowView):
  175. workflow_class = project_workflows.LaunchInstance
  176. def get_initial(self):
  177. initial = super(LaunchInstanceView, self).get_initial()
  178. initial['project_id'] = self.request.user.tenant_id
  179. initial['user_id'] = self.request.user.id
  180. defaults = getattr(settings, 'LAUNCH_INSTANCE_DEFAULTS', {})
  181. initial['config_drive'] = defaults.get('config_drive', False)
  182. return initial
  183. def console(request, instance_id):
  184. data = _('Unable to get log for instance "%s".') % instance_id
  185. tail = request.GET.get('length')
  186. if tail and not tail.isdigit():
  187. msg = _('Log length must be a nonnegative integer.')
  188. messages.warning(request, msg)
  189. else:
  190. try:
  191. data = api.nova.server_console_output(request,
  192. instance_id,
  193. tail_length=tail)
  194. except Exception:
  195. exceptions.handle(request, ignore=True)
  196. return http.HttpResponse(data.encode('utf-8'), content_type='text/plain')
  197. def auto_console(request, instance_id):
  198. console_type = getattr(settings, 'CONSOLE_TYPE', 'AUTO')
  199. try:
  200. instance = api.nova.server_get(request, instance_id)
  201. console_url = project_console.get_console(request, console_type,
  202. instance)[1]
  203. return shortcuts.redirect(console_url)
  204. except Exception:
  205. redirect = reverse("horizon:project:instances:index")
  206. msg = _('Unable to get console for instance "%s".') % instance_id
  207. exceptions.handle(request, msg, redirect=redirect)
  208. def vnc(request, instance_id):
  209. try:
  210. instance = api.nova.server_get(request, instance_id)
  211. console_url = project_console.get_console(request, 'VNC', instance)[1]
  212. return shortcuts.redirect(console_url)
  213. except Exception:
  214. redirect = reverse("horizon:project:instances:index")
  215. msg = _('Unable to get VNC console for instance "%s".') % instance_id
  216. exceptions.handle(request, msg, redirect=redirect)
  217. def mks(request, instance_id):
  218. try:
  219. instance = api.nova.server_get(request, instance_id)
  220. console_url = project_console.get_console(request, 'MKS', instance)[1]
  221. return shortcuts.redirect(console_url)
  222. except Exception:
  223. redirect = reverse("horizon:project:instances:index")
  224. msg = _('Unable to get MKS console for instance "%s".') % instance_id
  225. exceptions.handle(request, msg, redirect=redirect)
  226. def spice(request, instance_id):
  227. try:
  228. instance = api.nova.server_get(request, instance_id)
  229. console_url = project_console.get_console(request, 'SPICE',
  230. instance)[1]
  231. return shortcuts.redirect(console_url)
  232. except Exception:
  233. redirect = reverse("horizon:project:instances:index")
  234. msg = _('Unable to get SPICE console for instance "%s".') % instance_id
  235. exceptions.handle(request, msg, redirect=redirect)
  236. def rdp(request, instance_id):
  237. try:
  238. instance = api.nova.server_get(request, instance_id)
  239. console_url = project_console.get_console(request, 'RDP', instance)[1]
  240. return shortcuts.redirect(console_url)
  241. except Exception:
  242. redirect = reverse("horizon:project:instances:index")
  243. msg = _('Unable to get RDP console for instance "%s".') % instance_id
  244. exceptions.handle(request, msg, redirect=redirect)
  245. class SerialConsoleView(generic.TemplateView):
  246. template_name = 'serial_console.html'
  247. def get_context_data(self, **kwargs):
  248. context = super(SerialConsoleView, self).get_context_data(**kwargs)
  249. instance = None
  250. try:
  251. instance = api.nova.server_get(self.request,
  252. self.kwargs['instance_id'])
  253. except Exception:
  254. context["error_message"] = _(
  255. "Cannot find instance %s.") % self.kwargs['instance_id']
  256. # name is unknown, so leave it blank for the window title
  257. # in full-screen mode, so only the instance id is shown.
  258. context['page_title'] = self.kwargs['instance_id']
  259. return context
  260. context['page_title'] = "%s (%s)" % (instance.name, instance.id)
  261. try:
  262. console_url = project_console.get_console(self.request,
  263. "SERIAL", instance)[1]
  264. context["console_url"] = console_url
  265. context["protocols"] = "['binary', 'base64']"
  266. except exceptions.NotAvailable:
  267. context["error_message"] = _(
  268. "Cannot get console for instance %s.") % self.kwargs[
  269. 'instance_id']
  270. return context
  271. class UpdateView(workflows.WorkflowView):
  272. workflow_class = project_workflows.UpdateInstance
  273. success_url = reverse_lazy("horizon:project:instances:index")
  274. def get_context_data(self, **kwargs):
  275. context = super(UpdateView, self).get_context_data(**kwargs)
  276. context["instance_id"] = self.kwargs['instance_id']
  277. return context
  278. @memoized.memoized_method
  279. def get_object(self, *args, **kwargs):
  280. instance_id = self.kwargs['instance_id']
  281. try:
  282. return api.nova.server_get(self.request, instance_id)
  283. except Exception:
  284. redirect = reverse("horizon:project:instances:index")
  285. msg = _('Unable to retrieve instance details.')
  286. exceptions.handle(self.request, msg, redirect=redirect)
  287. def get_initial(self):
  288. initial = super(UpdateView, self).get_initial()
  289. instance = self.get_object()
  290. initial.update({'instance_id': self.kwargs['instance_id'],
  291. 'name': getattr(instance, 'name', ''),
  292. 'description': getattr(instance, 'description', ''),
  293. 'target_tenant_id': self.request.user.tenant_id})
  294. return initial
  295. class RebuildView(forms.ModalFormView):
  296. form_class = project_forms.RebuildInstanceForm
  297. template_name = 'project/instances/rebuild.html'
  298. success_url = reverse_lazy('horizon:project:instances:index')
  299. page_title = _("Rebuild Instance")
  300. submit_label = page_title
  301. def get_context_data(self, **kwargs):
  302. context = super(RebuildView, self).get_context_data(**kwargs)
  303. context['instance_id'] = self.kwargs['instance_id']
  304. context['can_set_server_password'] = api.nova.can_set_server_password()
  305. return context
  306. @memoized.memoized_method
  307. def get_object(self, *args, **kwargs):
  308. instance_id = self.kwargs['instance_id']
  309. try:
  310. return api.nova.server_get(self.request, instance_id)
  311. except Exception:
  312. redirect = reverse("horizon:project:instances:index")
  313. msg = _('Unable to retrieve instance details.')
  314. exceptions.handle(self.request, msg, redirect=redirect)
  315. def get_initial(self):
  316. instance = self.get_object()
  317. initial = {'instance_id': self.kwargs['instance_id'],
  318. 'description': getattr(instance, 'description', '')}
  319. return initial
  320. class DecryptPasswordView(forms.ModalFormView):
  321. form_class = project_forms.DecryptPasswordInstanceForm
  322. template_name = 'project/instances/decryptpassword.html'
  323. success_url = reverse_lazy('horizon:project:instances:index')
  324. page_title = _("Retrieve Instance Password")
  325. def get_context_data(self, **kwargs):
  326. context = super(DecryptPasswordView, self).get_context_data(**kwargs)
  327. context['instance_id'] = self.kwargs['instance_id']
  328. context['keypair_name'] = self.kwargs['keypair_name']
  329. return context
  330. def get_initial(self):
  331. return {'instance_id': self.kwargs['instance_id'],
  332. 'keypair_name': self.kwargs['keypair_name']}
  333. class DisassociateView(forms.ModalFormView):
  334. form_class = project_forms.Disassociate
  335. template_name = 'project/instances/disassociate.html'
  336. success_url = reverse_lazy('horizon:project:instances:index')
  337. page_title = _("Disassociate floating IP")
  338. submit_label = _("Disassocaite")
  339. def get_context_data(self, **kwargs):
  340. context = super(DisassociateView, self).get_context_data(**kwargs)
  341. context['instance_id'] = self.kwargs['instance_id']
  342. return context
  343. def get_initial(self):
  344. return {'instance_id': self.kwargs['instance_id']}
  345. class DetailView(tabs.TabView):
  346. tab_group_class = project_tabs.InstanceDetailTabs
  347. template_name = 'horizon/common/_detail.html'
  348. redirect_url = 'horizon:project:instances:index'
  349. page_title = "{{ instance.name|default:instance.id }}"
  350. image_url = 'horizon:project:images:images:detail'
  351. volume_url = 'horizon:project:volumes:detail'
  352. def get_context_data(self, **kwargs):
  353. context = super(DetailView, self).get_context_data(**kwargs)
  354. instance = self.get_data()
  355. if instance.image:
  356. instance.image_url = reverse(self.image_url,
  357. args=[instance.image['id']])
  358. instance.volume_url = self.volume_url
  359. context["instance"] = instance
  360. context["url"] = get_url_with_pagination(
  361. self.request,
  362. project_tables.InstancesTable._meta.pagination_param,
  363. project_tables.InstancesTable._meta.prev_pagination_param,
  364. self.redirect_url)
  365. context["actions"] = self._get_actions(instance)
  366. return context
  367. def _get_actions(self, instance):
  368. table = project_tables.InstancesTable(self.request)
  369. return table.render_row_actions(instance)
  370. def _get_volumes(self, instance):
  371. instance_id = instance.id
  372. try:
  373. instance.volumes = api.nova.instance_volumes_list(self.request,
  374. instance_id)
  375. # Sort by device name
  376. instance.volumes.sort(key=lambda vol: vol.device)
  377. except Exception:
  378. msg = _('Unable to retrieve volume list for instance '
  379. '"%(name)s" (%(id)s).') % {'name': instance.name,
  380. 'id': instance_id}
  381. exceptions.handle(self.request, msg, ignore=True)
  382. def _get_flavor(self, instance):
  383. instance_id = instance.id
  384. try:
  385. instance.full_flavor = api.nova.flavor_get(
  386. self.request, instance.flavor["id"])
  387. except Exception:
  388. msg = _('Unable to retrieve flavor information for instance '
  389. '"%(name)s" (%(id)s).') % {'name': instance.name,
  390. 'id': instance_id}
  391. exceptions.handle(self.request, msg, ignore=True)
  392. def _get_security_groups(self, instance):
  393. instance_id = instance.id
  394. try:
  395. instance.security_groups = api.neutron.server_security_groups(
  396. self.request, instance_id)
  397. except Exception:
  398. msg = _('Unable to retrieve security groups for instance '
  399. '"%(name)s" (%(id)s).') % {'name': instance.name,
  400. 'id': instance_id}
  401. exceptions.handle(self.request, msg, ignore=True)
  402. def _update_addresses(self, instance):
  403. instance_id = instance.id
  404. try:
  405. api.network.servers_update_addresses(self.request, [instance])
  406. except Exception:
  407. msg = _('Unable to retrieve IP addresses from Neutron for '
  408. 'instance "%(name)s" (%(id)s).') \
  409. % {'name': instance.name, 'id': instance_id}
  410. exceptions.handle(self.request, msg, ignore=True)
  411. @memoized.memoized_method
  412. def get_data(self):
  413. instance_id = self.kwargs['instance_id']
  414. try:
  415. instance = api.nova.server_get(self.request, instance_id)
  416. except Exception:
  417. redirect = reverse(self.redirect_url)
  418. exceptions.handle(self.request,
  419. _('Unable to retrieve details for '
  420. 'instance "%s".') % instance_id,
  421. redirect=redirect)
  422. # Not all exception types handled above will result in a redirect.
  423. # Need to raise here just in case.
  424. raise exceptions.Http302(redirect)
  425. choices = project_tables.STATUS_DISPLAY_CHOICES
  426. instance.status_label = (
  427. filters.get_display_label(choices, instance.status))
  428. futurist_utils.call_functions_parallel(
  429. (self._get_volumes, [instance]),
  430. (self._get_flavor, [instance]),
  431. (self._get_security_groups, [instance]),
  432. (self._update_addresses, [instance]),
  433. )
  434. return instance
  435. def get_tabs(self, request, *args, **kwargs):
  436. instance = self.get_data()
  437. return self.tab_group_class(request, instance=instance, **kwargs)
  438. class ResizeView(workflows.WorkflowView):
  439. workflow_class = project_workflows.ResizeInstance
  440. success_url = reverse_lazy("horizon:project:instances:index")
  441. def get_context_data(self, **kwargs):
  442. context = super(ResizeView, self).get_context_data(**kwargs)
  443. context["instance_id"] = self.kwargs['instance_id']
  444. return context
  445. @memoized.memoized_method
  446. def get_object(self, *args, **kwargs):
  447. instance_id = self.kwargs['instance_id']
  448. try:
  449. instance = api.nova.server_get(self.request, instance_id)
  450. except Exception:
  451. redirect = reverse("horizon:project:instances:index")
  452. msg = _('Unable to retrieve instance details.')
  453. exceptions.handle(self.request, msg, redirect=redirect)
  454. flavor_id = instance.flavor['id']
  455. flavors = self.get_flavors()
  456. if flavor_id in flavors:
  457. instance.flavor_name = flavors[flavor_id].name
  458. else:
  459. try:
  460. flavor = api.nova.flavor_get(self.request, flavor_id)
  461. instance.flavor_name = flavor.name
  462. except Exception:
  463. msg = _('Unable to retrieve flavor information for instance '
  464. '"%s".') % instance_id
  465. exceptions.handle(self.request, msg, ignore=True)
  466. instance.flavor_name = _("Not available")
  467. return instance
  468. @memoized.memoized_method
  469. def get_flavors(self, *args, **kwargs):
  470. try:
  471. flavors = api.nova.flavor_list(self.request)
  472. return OrderedDict((str(flavor.id), flavor) for flavor in flavors)
  473. except Exception:
  474. redirect = reverse("horizon:project:instances:index")
  475. exceptions.handle(self.request,
  476. _('Unable to retrieve flavors.'),
  477. redirect=redirect)
  478. def get_initial(self):
  479. initial = super(ResizeView, self).get_initial()
  480. _object = self.get_object()
  481. if _object:
  482. initial.update(
  483. {'instance_id': self.kwargs['instance_id'],
  484. 'name': getattr(_object, 'name', None),
  485. 'old_flavor_id': _object.flavor['id'],
  486. 'old_flavor_name': getattr(_object, 'flavor_name', ''),
  487. 'flavors': self.get_flavors()})
  488. return initial
  489. class AttachInterfaceView(forms.ModalFormView):
  490. form_class = project_forms.AttachInterface
  491. template_name = 'project/instances/attach_interface.html'
  492. page_title = _("Attach Interface")
  493. form_id = "attach_interface_form"
  494. submit_label = _("Attach Interface")
  495. success_url = reverse_lazy('horizon:project:instances:index')
  496. def get_context_data(self, **kwargs):
  497. context = super(AttachInterfaceView, self).get_context_data(**kwargs)
  498. context['instance_id'] = self.kwargs['instance_id']
  499. return context
  500. def get_initial(self):
  501. args = {'instance_id': self.kwargs['instance_id']}
  502. submit_url = "horizon:project:instances:attach_interface"
  503. self.submit_url = reverse(submit_url, kwargs=args)
  504. return args
  505. class AttachVolumeView(forms.ModalFormView):
  506. form_class = project_forms.AttachVolume
  507. template_name = 'project/instances/attach_volume.html'
  508. page_title = _("Attach Volume")
  509. modal_id = "attach_volume_modal"
  510. submit_label = _("Attach Volume")
  511. success_url = reverse_lazy('horizon:project:instances:index')
  512. def get_initial(self):
  513. args = {'instance_id': self.kwargs['instance_id']}
  514. submit_url = "horizon:project:instances:attach_volume"
  515. self.submit_url = reverse(submit_url, kwargs=args)
  516. try:
  517. volume_list = api.cinder.volume_list(self.request)
  518. except Exception:
  519. volume_list = []
  520. exceptions.handle(self.request,
  521. _("Unable to retrieve volume information."))
  522. return {"instance_id": self.kwargs["instance_id"],
  523. "volume_list": volume_list}
  524. def get_context_data(self, **kwargs):
  525. context = super(AttachVolumeView, self).get_context_data(**kwargs)
  526. context['instance_id'] = self.kwargs['instance_id']
  527. return context
  528. class DetachVolumeView(forms.ModalFormView):
  529. form_class = project_forms.DetachVolume
  530. template_name = 'project/instances/detach_volume.html'
  531. page_title = _("Detach Volume")
  532. modal_id = "detach_volume_modal"
  533. submit_label = _("Detach Volume")
  534. success_url = reverse_lazy('horizon:project:instances:index')
  535. def get_initial(self):
  536. args = {'instance_id': self.kwargs['instance_id']}
  537. submit_url = "horizon:project:instances:detach_volume"
  538. self.submit_url = reverse(submit_url, kwargs=args)
  539. return {"instance_id": self.kwargs["instance_id"]}
  540. def get_context_data(self, **kwargs):
  541. context = super(DetachVolumeView, self).get_context_data(**kwargs)
  542. context['instance_id'] = self.kwargs['instance_id']
  543. return context
  544. class DetachInterfaceView(forms.ModalFormView):
  545. form_class = project_forms.DetachInterface
  546. template_name = 'project/instances/detach_interface.html'
  547. page_title = _("Detach Interface")
  548. form_id = "detach_interface_form"
  549. submit_label = _("Detach Interface")
  550. success_url = reverse_lazy('horizon:project:instances:index')
  551. def get_context_data(self, **kwargs):
  552. context = super(DetachInterfaceView, self).get_context_data(**kwargs)
  553. context['instance_id'] = self.kwargs['instance_id']
  554. return context
  555. def get_initial(self):
  556. args = {"instance_id": self.kwargs["instance_id"]}
  557. submit_url = "horizon:project:instances:detach_interface"
  558. self.submit_url = reverse(submit_url, kwargs=args)
  559. return args
  560. class UpdatePortView(port_views.UpdateView):
  561. workflow_class = project_workflows.UpdatePort
  562. failure_url = 'horizon:project:instances:detail'
  563. @memoized.memoized_method
  564. def _get_object(self, *args, **kwargs):
  565. port_id = self.kwargs['port_id']
  566. try:
  567. return api.neutron.port_get(self.request, port_id)
  568. except Exception:
  569. redirect = reverse(self.failure_url,
  570. args=(self.kwargs['instance_id'],))
  571. msg = _('Unable to retrieve port details')
  572. exceptions.handle(self.request, msg, redirect=redirect)
  573. def get_initial(self):
  574. initial = super(UpdatePortView, self).get_initial()
  575. initial['instance_id'] = self.kwargs['instance_id']
  576. return initial
  577. class RescueView(forms.ModalFormView):
  578. form_class = project_forms.RescueInstanceForm
  579. template_name = 'project/instances/rescue.html'
  580. submit_label = _("Confirm")
  581. submit_url = "horizon:project:instances:rescue"
  582. success_url = reverse_lazy('horizon:project:instances:index')
  583. page_title = _("Rescue Instance")
  584. def get_context_data(self, **kwargs):
  585. context = super(RescueView, self).get_context_data(**kwargs)
  586. context["instance_id"] = self.kwargs['instance_id']
  587. args = (self.kwargs['instance_id'],)
  588. context['submit_url'] = reverse(self.submit_url, args=args)
  589. return context
  590. def get_initial(self):
  591. return {'instance_id': self.kwargs["instance_id"]}