~ubuntu-branches/ubuntu/precise/horizon/precise-updates

« back to all changes in this revision

Viewing changes to horizon/horizon/dashboards/syspanel/projects/views.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-02-17 10:12:25 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20120217101225-5wulil2mv7f2nvnb
Tags: 2012.1~e4~20120217.1354-0ubuntu1
* debian/patches/openstack-config-settings.patch: Refreshed.
* debian/copyright: Updated copyright.
* debian/rules: Diable tests since it doesnt work without a
  virtualenv.
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2012 United States Government as represented by the
 
4
# Administrator of the National Aeronautics and Space Administration.
 
5
# All Rights Reserved.
 
6
#
 
7
# Copyright 2012 Nebula, Inc.
 
8
#
 
9
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
10
#    not use this file except in compliance with the License. You may obtain
 
11
#    a copy of the License at
 
12
#
 
13
#         http://www.apache.org/licenses/LICENSE-2.0
 
14
#
 
15
#    Unless required by applicable law or agreed to in writing, software
 
16
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
17
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
18
#    License for the specific language governing permissions and limitations
 
19
#    under the License.
 
20
 
 
21
import logging
 
22
import operator
 
23
 
 
24
from django import http
 
25
from django.contrib import messages
 
26
from django.core.urlresolvers import reverse
 
27
from django.utils.translation import ugettext as _
 
28
from keystoneclient import exceptions as api_exceptions
 
29
 
 
30
from horizon import api
 
31
from horizon import exceptions
 
32
from horizon import forms
 
33
from horizon import tables
 
34
from horizon import usage
 
35
from .forms import AddUser, CreateTenant, UpdateTenant, UpdateQuotas
 
36
from .tables import TenantsTable, TenantUsersTable, AddUsersTable
 
37
 
 
38
 
 
39
LOG = logging.getLogger(__name__)
 
40
 
 
41
 
 
42
class IndexView(tables.DataTableView):
 
43
    table_class = TenantsTable
 
44
    template_name = 'syspanel/projects/index.html'
 
45
 
 
46
    def get_data(self):
 
47
        tenants = []
 
48
        try:
 
49
            tenants = api.tenant_list(self.request)
 
50
        except api_exceptions.AuthorizationFailure, e:
 
51
            LOG.exception("Unauthorized attempt to list tenants.")
 
52
            messages.error(self.request, _('Unable to get tenant info: %s')
 
53
                                         % e.message)
 
54
        except Exception, e:
 
55
            LOG.exception('Exception while getting tenant list')
 
56
            if not hasattr(e, 'message'):
 
57
                e.message = str(e)
 
58
            messages.error(self.request, _('Unable to get tenant info: %s')
 
59
                                         % e.message)
 
60
        tenants.sort(key=lambda x: x.id, reverse=True)
 
61
        return tenants
 
62
 
 
63
 
 
64
class CreateView(forms.ModalFormView):
 
65
    form_class = CreateTenant
 
66
    template_name = 'syspanel/projects/create.html'
 
67
 
 
68
 
 
69
class UpdateView(forms.ModalFormView):
 
70
    form_class = UpdateTenant
 
71
    template_name = 'syspanel/projects/update.html'
 
72
    context_object_name = 'tenant'
 
73
 
 
74
    def get_object(self, *args, **kwargs):
 
75
        tenant_id = kwargs['tenant_id']
 
76
        try:
 
77
            return api.tenant_get(self.request, tenant_id)
 
78
        except Exception as e:
 
79
            LOG.exception('Error fetching tenant with id "%s"' % tenant_id)
 
80
            messages.error(self.request, _('Unable to update tenant: %s')
 
81
                                           % e.message)
 
82
            raise http.Http404("Project with ID %s not found." % tenant_id)
 
83
 
 
84
    def get_initial(self):
 
85
        return {'id': self.object.id,
 
86
                'name': self.object.name,
 
87
                'description': getattr(self.object, "description", ""),
 
88
                'enabled': self.object.enabled}
 
89
 
 
90
 
 
91
class UsersView(tables.MultiTableView):
 
92
    table_classes = (TenantUsersTable, AddUsersTable)
 
93
    template_name = 'syspanel/projects/users.html'
 
94
 
 
95
    def _get_shared_data(self, *args, **kwargs):
 
96
        tenant_id = self.kwargs["tenant_id"]
 
97
        if not hasattr(self, "_shared_data"):
 
98
            try:
 
99
                tenant = api.keystone.tenant_get(self.request, tenant_id)
 
100
                all_users = api.keystone.user_list(self.request)
 
101
                tenant_users = api.keystone.user_list(self.request, tenant_id)
 
102
                self._shared_data = {'tenant': tenant,
 
103
                                     'all_users': all_users,
 
104
                                     'tenant_users': tenant_users}
 
105
            except:
 
106
                redirect = reverse("horizon:syspanel:projects:index")
 
107
                exceptions.handle(self.request,
 
108
                                  _("Unable to retrieve users."),
 
109
                                  redirect=redirect)
 
110
        return self._shared_data
 
111
 
 
112
    def get_tenant_users_data(self):
 
113
        return self._get_shared_data()["tenant_users"]
 
114
 
 
115
    def get_add_users_data(self):
 
116
        tenant_users = self._get_shared_data()["tenant_users"]
 
117
        all_users = self._get_shared_data()["all_users"]
 
118
        tenant_user_ids = [user.id for user in tenant_users]
 
119
        return filter(lambda u: u.id not in tenant_user_ids, all_users)
 
120
 
 
121
    def get_context_data(self, **kwargs):
 
122
        context = super(UsersView, self).get_context_data(**kwargs)
 
123
        context['tenant'] = self._get_shared_data()["tenant"]
 
124
        return context
 
125
 
 
126
 
 
127
class AddUserView(forms.ModalFormView):
 
128
    form_class = AddUser
 
129
    template_name = 'syspanel/projects/add_user.html'
 
130
    context_object_name = 'tenant'
 
131
 
 
132
    def get_object(self, *args, **kwargs):
 
133
        return api.keystone.tenant_get(self.request, kwargs["tenant_id"])
 
134
 
 
135
    def get_context_data(self, **kwargs):
 
136
        context = super(AddUserView, self).get_context_data(**kwargs)
 
137
        context['tenant_id'] = self.kwargs["tenant_id"]
 
138
        context['user_id'] = self.kwargs["user_id"]
 
139
        return context
 
140
 
 
141
    def get_form_kwargs(self):
 
142
        kwargs = super(AddUserView, self).get_form_kwargs()
 
143
        try:
 
144
            roles = api.keystone.role_list(self.request)
 
145
        except:
 
146
            redirect = reverse("horizon:syspanel:projects:users",
 
147
                               args=(self.kwargs["tenant_id"],))
 
148
            exceptions.handle(self.request,
 
149
                              _("Unable to retrieve roles."),
 
150
                              redirect=redirect)
 
151
        roles.sort(key=operator.attrgetter("id"))
 
152
        kwargs['roles'] = roles
 
153
        return kwargs
 
154
 
 
155
    def get_initial(self):
 
156
        default_role = api.keystone.get_default_role(self.request)
 
157
        return {'tenant_id': self.kwargs['tenant_id'],
 
158
                'user_id': self.kwargs['user_id'],
 
159
                'role_id': getattr(default_role, "id", None)}
 
160
 
 
161
 
 
162
class QuotasView(forms.ModalFormView):
 
163
    form_class = UpdateQuotas
 
164
    template_name = 'syspanel/projects/quotas.html'
 
165
    context_object_name = 'tenant'
 
166
 
 
167
    def get_object(self, *args, **kwargs):
 
168
        return api.keystone.tenant_get(self.request, kwargs["tenant_id"])
 
169
 
 
170
    def get_initial(self):
 
171
        quotas = api.nova.tenant_quota_get(self.request,
 
172
                                               self.kwargs['tenant_id'])
 
173
        return {
 
174
            'tenant_id': self.kwargs['tenant_id'],
 
175
            'metadata_items': quotas.metadata_items,
 
176
            'injected_file_content_bytes': quotas.injected_file_content_bytes,
 
177
            'volumes': quotas.volumes,
 
178
            'gigabytes': quotas.gigabytes,
 
179
            'ram': int(quotas.ram),
 
180
            'floating_ips': quotas.floating_ips,
 
181
            'instances': quotas.instances,
 
182
            'injected_files': quotas.injected_files,
 
183
            'cores': quotas.cores}
 
184
 
 
185
 
 
186
class TenantUsageView(usage.UsageView):
 
187
    table_class = usage.TenantUsageTable
 
188
    usage_class = usage.TenantUsage
 
189
    template_name = 'syspanel/projects/usage.html'
 
190
 
 
191
    def get_data(self):
 
192
        super(TenantUsageView, self).get_data()
 
193
        return self.usage.get_instances()