1# Copyright 2012 United States Government as represented by the
2# Administrator of the National Aeronautics and Space Administration.
3# All Rights Reserved.
4#
5# Copyright 2012 Nebula, Inc.
6#
7#    Licensed under the Apache License, Version 2.0 (the "License"); you may
8#    not use this file except in compliance with the License. You may obtain
9#    a copy of the License at
10#
11#         http://www.apache.org/licenses/LICENSE-2.0
12#
13#    Unless required by applicable law or agreed to in writing, software
14#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16#    License for the specific language governing permissions and limitations
17#    under the License.
18
19from django.urls import reverse_lazy
20from django.utils.translation import ugettext_lazy as _
21
22from horizon import exceptions
23from horizon import tables
24from horizon import workflows
25
26from openstack_dashboard import api
27
28from openstack_dashboard.dashboards.admin.flavors \
29    import tables as project_tables
30from openstack_dashboard.dashboards.admin.flavors \
31    import workflows as flavor_workflows
32
33
34INDEX_URL = "horizon:admin:flavors:index"
35
36
37class IndexView(tables.DataTableView):
38    table_class = project_tables.FlavorsTable
39    page_title = _("Flavors")
40
41    def has_prev_data(self, table):
42        return self._prev
43
44    def has_more_data(self, table):
45        return self._more
46
47    def get_data(self):
48        request = self.request
49        prev_marker = request.GET.get(
50            project_tables.FlavorsTable._meta.prev_pagination_param, None)
51
52        if prev_marker is not None:
53            marker = prev_marker
54        else:
55            marker = request.GET.get(
56                project_tables.FlavorsTable._meta.pagination_param, None)
57        reversed_order = prev_marker is not None
58        flavors = []
59        try:
60            # Removing the pagination params and adding "is_public=None"
61            # will return all flavors.
62            flavors, self._more, self._prev = api.nova.flavor_list_paged(
63                request, None,
64                marker=marker,
65                paginate=True,
66                sort_dir='asc',
67                sort_key='name',
68                reversed_order=reversed_order)
69        except Exception:
70            self._prev = self._more = False
71            exceptions.handle(request,
72                              _('Unable to retrieve flavor list.'))
73        return flavors
74
75
76class CreateView(workflows.WorkflowView):
77    workflow_class = flavor_workflows.CreateFlavor
78    template_name = 'admin/flavors/create.html'
79    page_title = _("Create Flavor")
80
81
82class UpdateView(workflows.WorkflowView):
83    workflow_class = flavor_workflows.UpdateFlavor
84    template_name = 'admin/flavors/update.html'
85    page_title = _("Edit Flavor")
86
87    def get_initial(self):
88        flavor_id = self.kwargs['id']
89        try:
90            # Get initial flavor information
91            flavor = api.nova.flavor_get(self.request, flavor_id)
92            if flavor.is_public:
93                flavor_access = []
94            else:
95                flavor_access = api.nova.flavor_access_list(self.request,
96                                                            flavor_id)
97        except Exception:
98            exceptions.handle(self.request,
99                              _('Unable to retrieve flavor details.'),
100                              redirect=reverse_lazy(INDEX_URL))
101        return {'flavor': flavor,
102                'current_flavor_access': flavor_access}
103