summaryrefslogtreecommitdiff
path: root/openstack_dashboard/dashboards/project/volume_groups/tables.py
blob: 7cfbf94fd47e701a2ecbd9756ff836f4cf84f84f (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
# 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.template import defaultfilters as filters
from django.utils.translation import pgettext_lazy
from django.utils.translation import ugettext_lazy as _

from horizon import exceptions
from horizon import tables

from openstack_dashboard.api import cinder
from openstack_dashboard import policy


class CreateGroup(policy.PolicyTargetMixin, tables.LinkAction):
    name = "create"
    verbose_name = _("Create Group")
    url = "horizon:project:volume_groups:create"
    classes = ("ajax-modal",)
    icon = "plus"
    policy_rules = (("volume", "group:create"),)


class DeleteGroup(policy.PolicyTargetMixin, tables.LinkAction):
    name = "deletecg"
    verbose_name = _("Delete Group")
    url = "horizon:project:volume_groups:delete"
    classes = ("ajax-modal", "btn-danger")
    policy_rules = (("volume", "group:delete"), )

    def allowed(self, request, datum=None):
        if datum and datum.has_snapshots:
            return False
        return True


class RemoveAllVolumes(policy.PolicyTargetMixin, tables.LinkAction):
    name = "remove_vols"
    verbose_name = _("Remove Volumes from Group")
    url = "horizon:project:volume_groups:remove_volumes"
    classes = ("ajax-modal",)
    policy_rules = (("volume", "group:update"), )


class EditGroup(policy.PolicyTargetMixin, tables.LinkAction):
    name = "edit"
    verbose_name = _("Edit Group")
    url = "horizon:project:volume_groups:update"
    classes = ("ajax-modal",)
    policy_rules = (("volume", "group:update"),)


class ManageVolumes(policy.PolicyTargetMixin, tables.LinkAction):
    name = "manage"
    verbose_name = _("Manage Volumes")
    url = "horizon:project:volume_groups:manage"
    classes = ("ajax-modal",)
    policy_rules = (("volume", "group:update"),)

    def allowed(self, request, group=None):
        if hasattr(group, 'status'):
            return group.status != 'error'
        return False


class CreateSnapshot(policy.PolicyTargetMixin, tables.LinkAction):
    name = "create_snapshot"
    verbose_name = _("Create Snapshot")
    url = "horizon:project:volume_groups:create_snapshot"
    classes = ("ajax-modal",)
    policy_rules = (("volume", "group:create_group_snapshot"),)

    def allowed(self, request, group=None):
        if hasattr(group, 'status'):
            return group.status != 'error'
        return False


class CloneGroup(policy.PolicyTargetMixin, tables.LinkAction):
    name = "clone_group"
    verbose_name = _("Clone Group")
    url = "horizon:project:volume_groups:clone_group"
    classes = ("ajax-modal",)
    policy_rules = (("volume", "group:create"),)

    def allowed(self, request, group=None):
        if hasattr(group, 'status'):
            return group.status != 'error'
        return False


class UpdateRow(tables.Row):
    ajax = True

    def get_data(self, request, group_id):
        group = cinder.group_get_with_vol_type_names(request, group_id)
        search_opts = {'group_id': group_id}
        try:
            group_snapshots = cinder.group_snapshot_list(
                request, search_opts=search_opts)
            group.has_snapshots = bool(group_snapshots)
        except Exception:
            exceptions.handle(request, _('Unable to retrieve group details.'))
            group.has_snapshots = False
        return group


class GroupsFilterAction(tables.FilterAction):

    def filter(self, table, groups, filter_string):
        """Naive case-insensitive search."""
        query = filter_string.lower()
        return [group for group in groups
                if query in group.name.lower()]


def get_volume_types(group):
    vtypes_str = ''
    if hasattr(group, 'volume_type_names'):
        vtypes_str = ",".join(group.volume_type_names)
    return vtypes_str


class GroupsTable(tables.DataTable):
    STATUS_CHOICES = (
        ("in-use", True),
        ("available", True),
        ("creating", None),
        ("error", False),
    )
    STATUS_DISPLAY_CHOICES = (
        ("available",
         pgettext_lazy("Current status of Volume Group", "Available")),
        ("in-use",
         pgettext_lazy("Current status of Volume Group", "In-use")),
        ("error",
         pgettext_lazy("Current status of Volume Group", "Error")),
        ("updating",
         pgettext_lazy("Current status of Volume Group", "Updating")),
        ("deleting",
         pgettext_lazy("Current status of Volume Group", "Deleting")),
    )

    name = tables.WrappingColumn("name_or_id",
                                 verbose_name=_("Name"),
                                 link="horizon:project:volume_groups:detail")
    description = tables.Column("description",
                                verbose_name=_("Description"),
                                truncate=40)
    status = tables.Column("status",
                           verbose_name=_("Status"),
                           status=True,
                           status_choices=STATUS_CHOICES,
                           display_choices=STATUS_DISPLAY_CHOICES)
    availability_zone = tables.Column("availability_zone",
                                      verbose_name=_("Availability Zone"))
    volume_type = tables.Column(get_volume_types,
                                verbose_name=_("Volume Type(s)"))
    has_snapshots = tables.Column("has_snapshots",
                                  verbose_name=_("Has Snapshots"),
                                  filters=(filters.yesno,))

    def get_object_id(self, group):
        return group.id

    class Meta(object):
        name = "volume_groups"
        verbose_name = _("Volume Groups")
        table_actions = (
            CreateGroup,
            GroupsFilterAction,
        )
        row_actions = (
            CreateSnapshot,
            ManageVolumes,
            EditGroup,
            CloneGroup,
            RemoveAllVolumes,
            DeleteGroup,
        )
        row_class = UpdateRow
        status_columns = ("status",)