1
# Copyright 2011 OpenStack LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
18
from nova.api.openstack import common
19
from nova.api.openstack.v2 import extensions
20
from nova.api.openstack import wsgi
21
from nova.api.openstack import xmlutil
22
from nova.auth import manager
23
from nova import exception
24
from nova import flags
25
from nova import log as logging
29
LOG = logging.getLogger('nova.api.openstack.users')
40
class UserTemplate(xmlutil.TemplateBuilder):
42
root = xmlutil.TemplateElement('user', selector='user')
44
return xmlutil.MasterTemplate(root, 1)
47
class UsersTemplate(xmlutil.TemplateBuilder):
49
root = xmlutil.TemplateElement('users')
50
elem = xmlutil.SubTemplateElement(root, 'user', selector='users')
52
return xmlutil.MasterTemplate(root, 1)
55
def _translate_keys(user):
56
return dict(id=user.id,
63
class Controller(object):
66
self.manager = manager.AuthManager()
68
def _check_admin(self, context):
69
"""We cannot depend on the db layer to check for admin access
70
for the auth manager, so we do it here"""
71
if not context.is_admin:
72
raise exception.AdminRequired()
74
@wsgi.serializers(xml=UsersTemplate)
76
"""Return all users in brief"""
77
users = self.manager.get_users()
78
users = common.limited(users, req)
79
users = [_translate_keys(user) for user in users]
80
return dict(users=users)
82
@wsgi.serializers(xml=UsersTemplate)
83
def detail(self, req):
84
"""Return all users in detail"""
85
return self.index(req)
87
@wsgi.serializers(xml=UserTemplate)
88
def show(self, req, id):
89
"""Return data about the given user id"""
91
#NOTE(justinsb): The drivers are a little inconsistent in how they
92
# deal with "NotFound" - some throw, some return None.
94
user = self.manager.get_user(id)
95
except exception.NotFound:
99
raise exc.HTTPNotFound()
101
return dict(user=_translate_keys(user))
103
def delete(self, req, id):
104
self._check_admin(req.environ['nova.context'])
105
self.manager.delete_user(id)
108
@wsgi.serializers(xml=UserTemplate)
109
def create(self, req, body):
110
self._check_admin(req.environ['nova.context'])
111
is_admin = body['user'].get('admin') in ('T', 'True', True)
112
name = body['user'].get('name')
113
access = body['user'].get('access')
114
secret = body['user'].get('secret')
115
user = self.manager.create_user(name, access, secret, is_admin)
116
return dict(user=_translate_keys(user))
118
@wsgi.serializers(xml=UserTemplate)
119
def update(self, req, id, body):
120
self._check_admin(req.environ['nova.context'])
121
is_admin = body['user'].get('admin')
122
if is_admin is not None:
123
is_admin = is_admin in ('T', 'True', True)
124
access = body['user'].get('access')
125
secret = body['user'].get('secret')
126
self.manager.modify_user(id, access, secret, is_admin)
127
return dict(user=_translate_keys(self.manager.get_user(id)))
130
class Users(extensions.ExtensionDescriptor):
131
"""Allow admins to acces user information"""
135
namespace = "http://docs.openstack.org/compute/ext/users/api/v1.1"
136
updated = "2011-08-08T00:00:00+00:00"
139
def get_resources(self):
140
coll_actions = {'detail': 'GET'}
141
res = extensions.ResourceExtension('users',
143
collection_actions=coll_actions)