1# Copyright 2019 NEC Corporation
2#
3#    Licensed under the Apache License, Version 2.0 (the "License"); you may
4#    not use this file except in compliance with the License. You may obtain
5#    a copy of the License at
6#
7#         http://www.apache.org/licenses/LICENSE-2.0
8#
9#    Unless required by applicable law or agreed to in writing, software
10#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12#    License for the specific language governing permissions and limitations
13#    under the License.
14
15from django.urls import reverse
16from django.urls import reverse_lazy
17from django.utils.translation import ugettext_lazy as _
18
19from horizon import exceptions
20from horizon import tables
21
22from openstack_dashboard import api
23
24from openstack_dashboard.dashboards.admin.volume_groups \
25    import forms as admin_forms
26from openstack_dashboard.dashboards.admin.volume_groups \
27    import tables as admin_tables
28from openstack_dashboard.dashboards.admin.volume_groups \
29    import tabs as admin_tabs
30from openstack_dashboard.dashboards.admin.volume_groups \
31    import workflows as admin_workflows
32from openstack_dashboard.dashboards.project.volume_groups \
33    import views as project_views
34
35
36class IndexView(tables.DataTableView):
37    table_class = admin_tables.GroupsTable
38    page_title = _("Groups")
39
40    def get_data(self):
41        try:
42            groups = api.cinder.group_list_with_vol_type_names(
43                self.request, {'all_tenants': 1})
44        except Exception:
45            groups = []
46            exceptions.handle(self.request,
47                              _("Unable to retrieve volume groups."))
48        if not groups:
49            return groups
50        group_snapshots = api.cinder.group_snapshot_list(self.request)
51        snapshot_groups = {gs.group_id for gs in group_snapshots}
52
53        # Gather our tenants to correlate against Group IDs
54        try:
55            tenants, has_more = api.keystone.tenant_list(self.request)
56        except Exception:
57            tenants = []
58            msg = _('Unable to retrieve volume group project information.')
59            exceptions.handle(self.request, msg)
60
61        tenant_dict = dict((t.id, t) for t in tenants)
62        for g in groups:
63            g.has_snapshots = g.id in snapshot_groups
64            tenant_id = getattr(g, "project_id", None)
65            tenant = tenant_dict.get(tenant_id)
66
67            # NOTE: If horizon is using cinder API microversion below '3.58',
68            # it doesn't include any 'project id' information in group's
69            # object.
70            g.tenant_name = getattr(tenant, "name", None)
71        return groups
72
73
74class RemoveVolumesView(project_views.RemoveVolumesView):
75    template_name = 'admin/volume_groups/remove_vols.html'
76    form_class = admin_forms.RemoveVolsForm
77    success_url = reverse_lazy('horizon:admin:volume_groups:index')
78    submit_url = "horizon:admin:volume_groups:remove_volumes"
79
80
81class DeleteView(project_views.DeleteView):
82    template_name = 'admin/volume_groups/delete.html'
83    form_class = admin_forms.DeleteForm
84    success_url = reverse_lazy('horizon:admin:volume_groups:index')
85    submit_url = "horizon:admin:volume_groups:delete"
86
87
88class ManageView(project_views.ManageView):
89    workflow_class = admin_workflows.UpdateGroupWorkflow
90
91
92class DetailView(project_views.DetailView):
93    tab_group_class = admin_tabs.GroupsDetailTabs
94
95    def get_context_data(self, **kwargs):
96        context = super().get_context_data(**kwargs)
97        table = admin_tables.GroupsTable(self.request)
98        context["actions"] = table.render_row_actions(context["group"])
99        return context
100
101    @staticmethod
102    def get_redirect_url():
103        return reverse('horizon:admin:volume_groups:index')
104