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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. from django.urls import reverse
  13. from django.urls import reverse_lazy
  14. from django.utils.translation import ugettext_lazy as _
  15. from horizon import exceptions
  16. from horizon import forms
  17. from horizon import tables
  18. from horizon.utils import memoized
  19. from openstack_dashboard.api import cinder
  20. from openstack_dashboard.api import keystone
  21. from openstack_dashboard.dashboards.admin.snapshots \
  22. import forms as vol_snapshot_forms
  23. from openstack_dashboard.dashboards.admin.snapshots \
  24. import tables as vol_snapshot_tables
  25. from openstack_dashboard.dashboards.admin.snapshots \
  26. import tabs as vol_snapshot_tabs
  27. from openstack_dashboard.dashboards.project.snapshots \
  28. import views
  29. class SnapshotsView(tables.PagedTableMixin, tables.DataTableView):
  30. table_class = vol_snapshot_tables.VolumeSnapshotsTable
  31. page_title = _("Volume Snapshots")
  32. def get_data(self):
  33. if cinder.is_volume_service_enabled(self.request):
  34. try:
  35. marker, sort_dir = self._get_marker()
  36. snapshots, self._has_more_data, self._has_prev_data = \
  37. cinder.volume_snapshot_list_paged(
  38. self.request, paginate=True, marker=marker,
  39. sort_dir=sort_dir, search_opts={'all_tenants': True})
  40. volumes = cinder.volume_list(
  41. self.request,
  42. search_opts={'all_tenants': True})
  43. volumes = dict((v.id, v) for v in volumes)
  44. except Exception:
  45. snapshots = []
  46. volumes = {}
  47. exceptions.handle(self.request, _("Unable to retrieve "
  48. "volume snapshots."))
  49. # Gather our tenants to correlate against volume IDs
  50. try:
  51. tenants, has_more = keystone.tenant_list(self.request)
  52. except Exception:
  53. tenants = []
  54. msg = _('Unable to retrieve volume project information.')
  55. exceptions.handle(self.request, msg)
  56. tenant_dict = dict((t.id, t) for t in tenants)
  57. for snapshot in snapshots:
  58. volume = volumes.get(snapshot.volume_id)
  59. tenant_id = getattr(volume,
  60. 'os-vol-tenant-attr:tenant_id', None)
  61. tenant = tenant_dict.get(tenant_id, None)
  62. snapshot._volume = volume
  63. snapshot.tenant_name = getattr(tenant, "name", None)
  64. snapshot.host_name = getattr(
  65. volume, 'os-vol-host-attr:host', None)
  66. else:
  67. snapshots = []
  68. return snapshots
  69. class UpdateStatusView(forms.ModalFormView):
  70. form_class = vol_snapshot_forms.UpdateStatus
  71. modal_id = "update_volume_snapshot_status"
  72. template_name = 'admin/snapshots/update_status.html'
  73. submit_label = _("Update Status")
  74. submit_url = "horizon:admin:snapshots:update_status"
  75. success_url = reverse_lazy("horizon:admin:snapshots:index")
  76. page_title = _("Update Volume Snapshot Status")
  77. @memoized.memoized_method
  78. def get_object(self):
  79. snap_id = self.kwargs['snapshot_id']
  80. try:
  81. self._object = cinder.volume_snapshot_get(self.request,
  82. snap_id)
  83. except Exception:
  84. exceptions.handle(self.request,
  85. _('Unable to retrieve volume snapshot.'),
  86. redirect=self.success_url)
  87. return self._object
  88. def get_context_data(self, **kwargs):
  89. context = super(UpdateStatusView, self).get_context_data(**kwargs)
  90. context['snapshot_id'] = self.kwargs["snapshot_id"]
  91. args = (self.kwargs['snapshot_id'],)
  92. context['submit_url'] = reverse(self.submit_url, args=args)
  93. return context
  94. def get_initial(self):
  95. snapshot = self.get_object()
  96. return {'snapshot_id': self.kwargs["snapshot_id"],
  97. 'status': snapshot.status}
  98. class DetailView(views.DetailView):
  99. tab_group_class = vol_snapshot_tabs.SnapshotDetailsTabs
  100. volume_url = 'horizon:admin:volumes:detail'
  101. def get_context_data(self, **kwargs):
  102. context = super(DetailView, self).get_context_data(**kwargs)
  103. snapshot = self.get_data()
  104. snapshot.volume_url = reverse(self.volume_url,
  105. args=(snapshot.volume_id,))
  106. table = vol_snapshot_tables.VolumeSnapshotsTable(self.request)
  107. context["snapshot"] = snapshot
  108. context["actions"] = table.render_row_actions(snapshot)
  109. return context
  110. @staticmethod
  111. def get_redirect_url():
  112. return reverse('horizon:admin:snapshots:index')