Repository URL to install this package:
|
Version:
4.2.64-4.2 ▾
|
python3-tvault-horizon-plugin-el8
/
usr
/
lib
/
python3.6
/
site-packages
/
dashboards
/
workloads
/
snapshots
/
tables.py
|
|---|
# Copyright (c) 2014 TrilioData, Inc.
# All Rights Reserved.
import logging
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls import reverse
try:
from django.core.urlresolvers import reverse_lazy
except ImportError:
from django.urls import reverse_lazy
from django.core.paginator import Paginator
from django.utils.http import urlencode
from django.utils.translation import ugettext_lazy as _
from horizon import exceptions
from horizon import messages
from horizon import tables
from dashboards import workloadmgr
from openstack_dashboard import api
from . import workflows
LOG = logging.getLogger(__name__)
mount_snapshots_list = []
mount_snapshots_dict = {}
class CreateOneClickRestore(tables.LinkAction):
name = "oneclickrestore"
verbose_name = _("OneClick Restore")
url = "horizon:project:workloads:snapshots:oneclickrestore"
classes = ("ajax-modal", "btn-oneclickrestore")
def allowed(self, request, snapshot):
if snapshot is not None:
return ((snapshot.status == 'available'))
else:
return False
class SelectiveRestore(tables.LinkAction):
name = "selectiverestore"
verbose_name = _("Selective Restore")
url = "horizon:project:workloads:snapshots:selective_restore"
classes = ("ajax-modal", "btn-restore")
def allowed(self, request, snapshot):
if snapshot is not None:
return ((snapshot.status == 'available'))
else:
return False
def get_link_url(self, project):
return self._get_link_url(project, 'selective_restore')
def _get_link_url(self, snapshot, step_slug):
base_url = reverse(self.url, args=[snapshot.id])
param = urlencode({"step": step_slug})
return "?".join([base_url, param, snapshot.id])
class InplaceRestore(tables.LinkAction):
name = "inplacerestore"
verbose_name = _("Inplace Restore")
url = "horizon:project:workloads:snapshots:inplace_restore"
classes = ("ajax-modal", "btn-inplacerestore")
def allowed(self, request, snapshot):
if snapshot is not None:
return ((snapshot.status == 'available'))
else:
return False
def get_link_url(self, project):
return self._get_link_url(project, 'inplace_restore')
def _get_link_url(self, snapshot, step_slug):
base_url = reverse(self.url, args=[snapshot.id])
param = urlencode({"step": step_slug})
return "?".join([base_url, param, snapshot.id])
class MountSnapshot(tables.LinkAction):
name = "mount"
verbose_name = _("Mount Snapshot")
url = "horizon:project:workloads:snapshots:mount"
classes = ("ajax-modal", "btn-mount")
def allowed(self, request, snapshot):
if snapshot is not None:
return ((snapshot.status == 'available'))
else:
return False
def get_link_url(self, project):
return self._get_link_url(project, 'mount_snapshot')
def _get_link_url(self, snapshot, step_slug):
base_url = reverse(self.url, args=[snapshot.id])
param = urlencode({"step": step_slug})
return "?".join([base_url, param])
class DismountSnapshot(tables.BatchAction):
name = "umount"
verbose_name = _("Unmount Snapshot")
classes = ("btn-danger", "btn-unmount")
data_type_singular = _(" ")
data_type_plural = _(" ")
@staticmethod
def action_present(count):
return _(
"Unmount Snapshot"
)
@staticmethod
def action_past(count):
return _(
"Unmounted"
)
def allowed(self, request, snapshot):
if snapshot is not None:
global mount_snapshots_dict
try:
del mount_snapshots_dict[snapshot.id]
except Exception as ex:
LOG.exception(ex)
return ((snapshot.status == 'mounted'))
else:
return False
def action(self, request, obj_id):
try:
workloadmgr.snapshot_dismount(request, obj_id)
except Exception as ex:
messages.error(request, str(ex))
class TestSnapshot(tables.BatchAction):
name = "Test"
verbose_name = _("Test")
classes = ("btn-simple", "btn-testrestore")
data_type_singular = _("Snapshot")
data_type_plural = _("Snapshots")
@staticmethod
def action_present(count):
return _(
"Test Snapshot"
)
@staticmethod
def action_past(count):
return _(
"Scheduled test of"
)
def action(self, request, obj_id):
try:
workloadmgr.snapshot_restore(request, obj_id, test=True)
except Exception as ex:
messages.error(request, str(ex))
def allowed(self, request, snapshot):
if snapshot is not None:
return ((snapshot.status == 'available'))
else:
return False
class DeleteSnapshot(tables.DeleteAction):
classes = ("btn-danger")
data_type_singular = _("Snapshot")
data_type_plural = _("Snapshots")
@staticmethod
def action_present(count):
return _(
"Delete Snapshot"
)
@staticmethod
def action_past(count):
return _(
"Submitted Delete Request for"
)
def delete(self, request, obj_id):
try:
workloadmgr.snapshot_delete(request, obj_id)
except Exception as ex:
messages.error(request, str(ex))
def allowed(self, request, snapshot):
if snapshot is not None:
return ((snapshot.status in ('available', 'error', 'cancelled')))
else:
return True
class CancelSnapshot(tables.DeleteAction):
name = "cancel"
verbose_name = _("Cancel Snapshot")
classes = ("btn-danger", "btn-cancel")
data_type_singular = _(" ")
data_type_plural = _(" ")
@staticmethod
def action_present(count):
return _(
"Cancel Snapshot"
)
@staticmethod
def action_past(count):
return _(
"Cancelled"
)
def action(self, request, obj_id):
try:
workloadmgr.snapshot_cancel(request, obj_id)
except Exception as ex:
messages.error(request, str(ex))
def allowed(self, request, snapshot):
if snapshot is not None:
return ((snapshot.status == 'executing' or
'uploading' in snapshot.status))
else:
return False
class UpdateRow(tables.Row):
ajax = True
def get_data(self, request, snapshot_id):
# tz = workloadmgr.get_time_zone(request)
global mount_snapshots_dict
global mount_snapshots_list
snapshot = workloadmgr.snapshot_get(request, snapshot_id)
"""snapshot.created_at = workloadmgr.get_local_time(
snapshot.created_at,
"%m/%d/%Y %I:%M:%S %p",
"%I:%M:%S %p - %m/%d/%Y", tz)"""
if snapshot.status == 'uploading':
snapshot.status = snapshot.status + ' ' + \
'(' + workloadmgr.sizeof_fmt(snapshot.uploaded_size) + ')'
if snapshot.id in list(mount_snapshots_dict.keys()):
if snapshot.status != 'mounted' and snapshot.status != 'mounting':
try:
del mount_snapshots_dict[snapshot.id]
except Exception as ex:
LOG.exception(ex)
messages.error(request, 'Unable to mount snapshot with id' +
snapshot.id + ' of workload with id' + snapshot.workload_id)
elif snapshot.status == 'mounted':
try:
del mount_snapshots_dict[snapshot.id]
except Exception as ex:
LOG.exception(ex)
if snapshot.status == 'mounting':
if len(mount_snapshots_list) >= 20:
key = mount_snapshots_list.pop(0)
try:
del mount_snapshots_dict[key]
except Exception as ex:
LOG.exception(ex)
if snapshot.id not in list(mount_snapshots_dict.keys()):
mount_snapshots_dict[snapshot.id] = 'mounting'
mount_snapshots_list.append(snapshot.id)
return snapshot
class CreateSnapshot(tables.LinkAction):
name = "create"
verbose_name = _("Create Snapshot")
url = "horizon:project:workloads:snapshots:create"
classes = ("ajax-modal", "btn-create")
class SnapshotFilterAction(tables.FilterAction):
# Search Box implemented on top of horizon.Datatable.table module.
name = "snapshot_filter"
verbose_name = _("Snapshot Filter")
needs_preloading = True
def filter(self, table, data, filter_string):
"""Provides the actual filtering logic.
This is a overridden method and returns
the filtered data.
"""
return data
class SnapshotsTable(tables.DataTable):
STATUS_CHOICES = (
("available", True),
("error", True),
("mounted", True),
("cancelled", True)
)
# time_stamp = tables.Column("created_at", verbose_name=_("Time Stamp"))
# id = tables.Column("id", verbose_name=_("ID"),
# link='horizon:project:workloads:snapshots:detail')
created_at = tables.Column("created_at",
verbose_name=_("Created at"),
empty_value="...",
classes=('t_col165', 'test'))
name = tables.Column("name", verbose_name=_("Name"),
link='horizon:project:workloads:snapshots:detail',
classes=('test', 'snapshotname'))
restores_info = tables.Column("restores_info",
verbose_name=_("Restores"),
empty_value="-",
classes=('t_col200', 'restores_info'))
description = tables.Column("description", verbose_name=_("Description"),
classes=('t_colFree', 'test'))
type = tables.Column("snapshot_type", verbose_name=_("Type"),
classes=('t_col75', 'test'))
size = tables.Column("size", verbose_name=_("Size"),
classes=('t_col75', 'test'))
status = tables.Column("status",
verbose_name=_("Status"),
classes=('t_col75', 'test'),
status=True,
status_choices=STATUS_CHOICES)
failure_url = reverse_lazy('horizon:project:workloads:index')
class Meta:
name = "snapshots"
verbose_name = _(" ")
status_columns = ["status", "status"]
row_class = UpdateRow
table_actions = (CreateSnapshot, DeleteSnapshot,)
row_actions = (CreateOneClickRestore, SelectiveRestore, InplaceRestore,
MountSnapshot, DismountSnapshot, DeleteSnapshot, CancelSnapshot)