1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5#      http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import logging
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 forms
21from horizon import tables
22from horizon import tabs
23from horizon.utils import memoized
24
25from openstack_dashboard import api
26from openstack_dashboard.dashboards.project.backups \
27    import forms as backup_forms
28from openstack_dashboard.dashboards.project.backups \
29    import tables as backup_tables
30from openstack_dashboard.dashboards.project.backups \
31    import tabs as backup_tabs
32from openstack_dashboard.dashboards.project.volumes \
33    import views as volume_views
34
35LOG = logging.getLogger(__name__)
36
37
38class BackupsView(tables.PagedTableWithPageMenu, tables.DataTableView,
39                  volume_views.VolumeTableMixIn):
40    table_class = backup_tables.BackupsTable
41    page_title = _("Volume Backups")
42
43    def allowed(self, request):
44        return api.cinder.volume_backup_supported(self.request)
45
46    def get_data(self):
47        try:
48            self._current_page = self._get_page_number()
49            (backups, self._page_size, self._total_of_entries,
50             self._number_of_pages) = \
51                api.cinder.volume_backup_list_paged_with_page_menu(
52                    self.request, page_number=self._current_page)
53            volumes = api.cinder.volume_list(self.request)
54            volumes = dict((v.id, v) for v in volumes)
55            snapshots = api.cinder.volume_snapshot_list(self.request)
56            snapshots = dict((s.id, s) for s in snapshots)
57            for backup in backups:
58                backup.volume = volumes.get(backup.volume_id)
59                backup.snapshot = snapshots.get(backup.snapshot_id)
60        except Exception as e:
61            LOG.exception(e)
62            backups = []
63            exceptions.handle(self.request, _("Unable to retrieve "
64                                              "volume backups."))
65        return backups
66
67
68class CreateBackupView(forms.ModalFormView):
69    form_class = backup_forms.CreateBackupForm
70    template_name = 'project/backups/create_backup.html'
71    submit_label = _("Create Volume Backup")
72    submit_url = "horizon:project:volumes:create_backup"
73    success_url = reverse_lazy("horizon:project:backups:index")
74    page_title = _("Create Volume Backup")
75
76    def get_context_data(self, **kwargs):
77        context = super().get_context_data(**kwargs)
78        context['volume_id'] = self.kwargs['volume_id']
79        args = (self.kwargs['volume_id'],)
80        context['submit_url'] = reverse(self.submit_url, args=args)
81        return context
82
83    def get_initial(self):
84        if self.kwargs.get('snapshot_id'):
85            return {"volume_id": self.kwargs["volume_id"],
86                    "snapshot_id": self.kwargs["snapshot_id"]}
87        return {"volume_id": self.kwargs["volume_id"]}
88
89
90class BackupDetailView(tabs.TabView):
91    tab_group_class = backup_tabs.BackupDetailTabs
92    template_name = 'horizon/common/_detail.html'
93    page_title = "{{ backup.name|default:backup.id }}"
94
95    def get_context_data(self, **kwargs):
96        context = super().get_context_data(**kwargs)
97        backup = self.get_data()
98        table = backup_tables.BackupsTable(self.request)
99        context["backup"] = backup
100        context["url"] = self.get_redirect_url()
101        context["actions"] = table.render_row_actions(backup)
102        return context
103
104    @memoized.memoized_method
105    def get_data(self):
106        try:
107            backup_id = self.kwargs['backup_id']
108            backup = api.cinder.volume_backup_get(self.request,
109                                                  backup_id)
110        except Exception:
111            exceptions.handle(self.request,
112                              _('Unable to retrieve backup details.'),
113                              redirect=self.get_redirect_url())
114        return backup
115
116    def get_tabs(self, request, *args, **kwargs):
117        backup = self.get_data()
118        return self.tab_group_class(request, backup=backup, **kwargs)
119
120    @staticmethod
121    def get_redirect_url():
122        return reverse('horizon:project:backups:index')
123
124
125class RestoreBackupView(forms.ModalFormView):
126    form_class = backup_forms.RestoreBackupForm
127    template_name = 'project/backups/restore_backup.html'
128    submit_label = _("Restore Backup to Volume")
129    submit_url = "horizon:project:backups:restore"
130    success_url = reverse_lazy('horizon:project:volumes:index')
131    page_title = _("Restore Volume Backup")
132
133    def get_context_data(self, **kwargs):
134        context = super().get_context_data(**kwargs)
135        context['backup_id'] = self.kwargs['backup_id']
136        args = (self.kwargs['backup_id'],)
137        context['submit_url'] = reverse(self.submit_url, args=args)
138        return context
139
140    def get_initial(self):
141        backup_id = self.kwargs['backup_id']
142        backup_name = self.request.GET.get('backup_name')
143        volume_id = self.request.GET.get('volume_id')
144        return {
145            'backup_id': backup_id,
146            'backup_name': backup_name,
147            'volume_id': volume_id,
148        }
149