summaryrefslogtreecommitdiff
path: root/openstack_dashboard/dashboards/project/backups/tables.py
blob: 8abcadf6cb232e898969df03b4354de13b8ee89b (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# 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.urls import reverse
from django.utils import html
from django.utils import http
from django.utils import safestring
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext_lazy
from django.utils.translation import pgettext_lazy

from horizon import tables

from openstack_dashboard import api
from openstack_dashboard.api import cinder


DELETABLE_STATES = ("available", "error",)


class BackupVolumeNameColumn(tables.Column):
    def get_raw_data(self, backup):
        volume = backup.volume
        if volume:
            volume_name = volume.name
            volume_name = html.escape(volume_name)
        else:
            volume_name = _("Unknown")
        return safestring.mark_safe(volume_name)

    def get_link_url(self, backup):
        volume = backup.volume
        if volume:
            volume_id = volume.id
            return reverse(self.link, args=(volume_id,))


class SnapshotColumn(tables.Column):
    url = "horizon:project:snapshots:detail"

    def get_raw_data(self, backup):
        snapshot = backup.snapshot
        if snapshot:
            snapshot_name = snapshot.name
            snapshot_name = html.escape(snapshot_name)
        elif backup.snapshot_id:
            snapshot_name = _("Unknown")
        else:
            return None
        return safestring.mark_safe(snapshot_name)

    def get_link_url(self, backup):
        if backup.snapshot:
            return reverse(self.url,
                           args=(backup.snapshot_id,))


class DeleteBackup(tables.DeleteAction):
    help_text = _("Deleted volume backups are not recoverable.")
    policy_rules = (("volume", "backup:delete"),)

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

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

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

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


class RestoreBackup(tables.LinkAction):
    name = "restore"
    verbose_name = _("Restore Backup")
    url = "horizon:project:backups:restore"
    classes = ("ajax-modal",)
    policy_rules = (("volume", "backup:restore"),)

    def allowed(self, request, backup=None):
        return backup.status == "available"

    def get_link_url(self, datum):
        backup_id = datum.id
        backup_name = datum.name
        volume_id = getattr(datum, 'volume_id', None)
        url = reverse(self.url, args=(backup_id,))
        url += '?%s' % http.urlencode({'backup_name': backup_name,
                                       'volume_id': volume_id})
        return url


class UpdateRow(tables.Row):
    ajax = True

    def get_data(self, request, backup_id):
        backup = cinder.volume_backup_get(request, backup_id)
        try:
            backup.volume = cinder.volume_get(request,
                                              backup.volume_id)
        except Exception:
            pass
        if backup.snapshot_id is not None:
            try:
                backup.snapshot = cinder.volume_snapshot_get(
                    request, backup.snapshot_id)
            except Exception:
                pass
        else:
            backup.snapshot = None
        return backup


def get_size(backup):
    return _("%sGB") % backup.size


class BackupsFilterAction(tables.FilterAction):

    def filter(self, table, volumes, filter_string):
        q = filter_string.lower()
        return [volume for volume in volumes
                if q in volume.name.lower()]


class BackupsTable(tables.DataTable):
    STATUS_CHOICES = (
        ("available", True),
        ("creating", None),
        ("restoring", None),
        ("error", False),
    )
    STATUS_DISPLAY_CHOICES = (
        ("available", pgettext_lazy("Current status of a Volume Backup",
                                    "Available")),
        ("error", pgettext_lazy("Current status of a Volume Backup",
                                "Error")),
        ("creating", pgettext_lazy("Current status of a Volume Backup",
                                   "Creating")),
        ("restoring", pgettext_lazy("Current status of a Volume Backup",
                                    "Restoring")),
        ("deleting", pgettext_lazy("Current status of a Volume Backup",
                                   "Deleting")),
        ("error_restoring", pgettext_lazy("Current status of a Volume Backup",
                                          "Error Restoring")),
    )
    name = tables.Column("name",
                         verbose_name=_("Name"),
                         link="horizon:project:backups:detail")
    description = tables.Column("description",
                                verbose_name=_("Description"),
                                truncate=40)
    size = tables.Column(get_size,
                         verbose_name=_("Size"),
                         attrs={'data-type': 'size'})
    status = tables.Column("status",
                           verbose_name=_("Status"),
                           status=True,
                           status_choices=STATUS_CHOICES,
                           display_choices=STATUS_DISPLAY_CHOICES)
    volume_name = BackupVolumeNameColumn("name",
                                         verbose_name=_("Volume Name"),
                                         link="horizon:project:volumes:detail")
    snapshot = SnapshotColumn("snapshot",
                              verbose_name=_("Snapshot"),
                              link="horizon:project:snapshots:detail")

    def current_page(self):
        return self._meta.current_page()

    def number_of_pages(self):
        return self._meta.number_of_pages()

    def get_pagination_string(self):
        return '?%s=' % self._meta.pagination_param

    class Meta(object):
        name = "volume_backups"
        verbose_name = _("Volume Backups")
        pagination_param = 'page'
        status_columns = ("status",)
        row_class = UpdateRow
        table_actions = (DeleteBackup, BackupsFilterAction)
        row_actions = (RestoreBackup, DeleteBackup)