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 import wsgi
20
from nova.api.openstack import xmlutil
21
from nova.auth import manager
22
from nova import exception
23
from nova import flags
24
from nova import log as logging
28
LOG = logging.getLogger('nova.api.openstack.users')
31
def _translate_keys(user):
32
return dict(id=user.id,
39
class Controller(object):
42
self.manager = manager.AuthManager()
44
def _check_admin(self, context):
45
"""We cannot depend on the db layer to check for admin access
46
for the auth manager, so we do it here"""
47
if not context.is_admin:
48
raise exception.AdminRequired()
51
"""Return all users in brief"""
52
users = self.manager.get_users()
53
users = common.limited(users, req)
54
users = [_translate_keys(user) for user in users]
55
return dict(users=users)
57
def detail(self, req):
58
"""Return all users in detail"""
59
return self.index(req)
61
def show(self, req, id):
62
"""Return data about the given user id"""
64
#NOTE(justinsb): The drivers are a little inconsistent in how they
65
# deal with "NotFound" - some throw, some return None.
67
user = self.manager.get_user(id)
68
except exception.NotFound:
72
raise exc.HTTPNotFound()
74
return dict(user=_translate_keys(user))
76
def delete(self, req, id):
77
self._check_admin(req.environ['nova.context'])
78
self.manager.delete_user(id)
81
def create(self, req, body):
82
self._check_admin(req.environ['nova.context'])
83
is_admin = body['user'].get('admin') in ('T', 'True', True)
84
name = body['user'].get('name')
85
access = body['user'].get('access')
86
secret = body['user'].get('secret')
87
user = self.manager.create_user(name, access, secret, is_admin)
88
return dict(user=_translate_keys(user))
90
def update(self, req, id, body):
91
self._check_admin(req.environ['nova.context'])
92
is_admin = body['user'].get('admin')
93
if is_admin is not None:
94
is_admin = is_admin in ('T', 'True', True)
95
access = body['user'].get('access')
96
secret = body['user'].get('secret')
97
self.manager.modify_user(id, access, secret, is_admin)
98
return dict(user=_translate_keys(self.manager.get_user(id)))
109
class UserTemplate(xmlutil.TemplateBuilder):
111
root = xmlutil.TemplateElement('user', selector='user')
113
return xmlutil.MasterTemplate(root, 1)
116
class UsersTemplate(xmlutil.TemplateBuilder):
118
root = xmlutil.TemplateElement('users')
119
elem = xmlutil.SubTemplateElement(root, 'user', selector='users')
121
return xmlutil.MasterTemplate(root, 1)
124
class UserXMLSerializer(xmlutil.XMLTemplateSerializer):
126
return UsersTemplate()
129
return UserTemplate()
132
def create_resource():
134
'application/xml': UserXMLSerializer(),
137
serializer = wsgi.ResponseSerializer(body_serializers)
139
return wsgi.Resource(Controller(), serializer=serializer)