summaryrefslogtreecommitdiff
path: root/openstack_dashboard/dashboards/admin/backups/tables.py
blob: a61f4b0e99c8025687c15f2ffe87707cb7021ea2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext_lazy

from horizon import exceptions
from horizon import tables

from openstack_dashboard import api
from openstack_dashboard.dashboards.project.backups \
    import tables as project_tables
from openstack_dashboard import policy


FORCE_DELETABLE_STATES = ("error_deleting", "restoring", "creating")


class AdminSnapshotColumn(project_tables.SnapshotColumn):
    url = "horizon:admin:snapshots:detail"


class AdminDeleteBackup(project_tables.DeleteBackup):
    pass


class ForceDeleteBackup(policy.PolicyTargetMixin, tables.DeleteAction):
    help_text = _("Deleted volume backups are not recoverable.")
    name = "delete_force"
    policy_rules = (("volume",
                     "volume_extension:backup_admin_actions:force_delete"),)

    @staticmethod
    def action_present(count):
        return ngettext_lazy(
            "Force Delete Volume Backup",
            "Force Delete Volume Backups",
            count
        )

    @staticmethod
    def action_past(count):
        return ngettext_lazy(
            "Scheduled forced deletion of Volume Backup",
            "Scheduled forced deletion of Volume Backups",
            count
        )

    def delete(self, request, obj_id):
        api.cinder.volume_backup_delete(request, obj_id, force=True)

    def allowed(self, request, backup=None):
        if backup:
            return backup.status in FORCE_DELETABLE_STATES
        return True


class UpdateRow(project_tables.UpdateRow):
    ajax = True

    def get_data(self, request, backup_id):
        backup = super().get_data(request, backup_id)
        tenant_id = getattr(backup, 'project_id')
        try:
            tenant = api.keystone.tenant_get(request, tenant_id)
            backup.tenant_name = getattr(tenant, "name")
        except Exception:
            msg = _('Unable to retrieve volume backup project information.')
            exceptions.handle(request, msg)

        return backup


class AdminRestoreBackup(project_tables.RestoreBackup):
    url = "horizon:admin:backups:restore"


class UpdateVolumeBackupStatusAction(tables.LinkAction):
    name = "update_status"
    verbose_name = _("Update Volume backup Status")
    url = "horizon:admin:backups:update_status"
    classes = ("ajax-modal",)
    icon = "pencil"
    policy_rules = (("volume",
                     "volume_extension:backup_admin_actions:reset_status"),)


class AdminBackupsTable(project_tables.BackupsTable):
    project = tables.Column("tenant_name", verbose_name=_("Project"))
    name = tables.Column("name",
                         verbose_name=_("Name"),
                         link="horizon:admin:backups:detail")
    volume_name = project_tables.BackupVolumeNameColumn(
        "name", verbose_name=_("Volume Name"),
        link="horizon:admin:volumes:detail")
    snapshot = AdminSnapshotColumn("snapshot",
                                   verbose_name=_("Snapshot"),
                                   link="horizon:admin:snapshots:detail")

    class Meta(object):
        name = "volume_backups"
        verbose_name = _("Volume Backups")
        pagination_param = 'page'
        status_columns = ("status",)
        row_class = UpdateRow
        table_actions = (AdminDeleteBackup,)
        row_actions = (AdminRestoreBackup, ForceDeleteBackup,
                       AdminDeleteBackup, UpdateVolumeBackupStatusAction,)
        columns = ('project', 'name', 'description', 'size', 'status',
                   'volume_name', 'snapshot',)