~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/api/openstack/v2/contrib/users.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-01-20 11:54:15 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: package-import@ubuntu.com-20120120115415-h2ujma9o536o1ut6
Tags: upstream-2012.1~e3~20120120.12170
ImportĀ upstreamĀ versionĀ 2012.1~e3~20120120.12170

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2011 OpenStack LLC.
2
 
# All Rights Reserved.
3
 
#
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
7
 
#
8
 
#         http://www.apache.org/licenses/LICENSE-2.0
9
 
#
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
14
 
#    under the License.
15
 
 
16
 
from webob import exc
17
 
 
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
26
 
 
27
 
 
28
 
FLAGS = flags.FLAGS
29
 
LOG = logging.getLogger('nova.api.openstack.users')
30
 
 
31
 
 
32
 
def make_user(elem):
33
 
    elem.set('id')
34
 
    elem.set('name')
35
 
    elem.set('access')
36
 
    elem.set('secret')
37
 
    elem.set('admin')
38
 
 
39
 
 
40
 
class UserTemplate(xmlutil.TemplateBuilder):
41
 
    def construct(self):
42
 
        root = xmlutil.TemplateElement('user', selector='user')
43
 
        make_user(root)
44
 
        return xmlutil.MasterTemplate(root, 1)
45
 
 
46
 
 
47
 
class UsersTemplate(xmlutil.TemplateBuilder):
48
 
    def construct(self):
49
 
        root = xmlutil.TemplateElement('users')
50
 
        elem = xmlutil.SubTemplateElement(root, 'user', selector='users')
51
 
        make_user(elem)
52
 
        return xmlutil.MasterTemplate(root, 1)
53
 
 
54
 
 
55
 
def _translate_keys(user):
56
 
    return dict(id=user.id,
57
 
                name=user.name,
58
 
                access=user.access,
59
 
                secret=user.secret,
60
 
                admin=user.admin)
61
 
 
62
 
 
63
 
class Controller(object):
64
 
 
65
 
    def __init__(self):
66
 
        self.manager = manager.AuthManager()
67
 
 
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()
73
 
 
74
 
    @wsgi.serializers(xml=UsersTemplate)
75
 
    def index(self, req):
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)
81
 
 
82
 
    @wsgi.serializers(xml=UsersTemplate)
83
 
    def detail(self, req):
84
 
        """Return all users in detail"""
85
 
        return self.index(req)
86
 
 
87
 
    @wsgi.serializers(xml=UserTemplate)
88
 
    def show(self, req, id):
89
 
        """Return data about the given user id"""
90
 
 
91
 
        #NOTE(justinsb): The drivers are a little inconsistent in how they
92
 
        #  deal with "NotFound" - some throw, some return None.
93
 
        try:
94
 
            user = self.manager.get_user(id)
95
 
        except exception.NotFound:
96
 
            user = None
97
 
 
98
 
        if user is None:
99
 
            raise exc.HTTPNotFound()
100
 
 
101
 
        return dict(user=_translate_keys(user))
102
 
 
103
 
    def delete(self, req, id):
104
 
        self._check_admin(req.environ['nova.context'])
105
 
        self.manager.delete_user(id)
106
 
        return {}
107
 
 
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))
117
 
 
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)))
128
 
 
129
 
 
130
 
class Users(extensions.ExtensionDescriptor):
131
 
    """Allow admins to acces user information"""
132
 
 
133
 
    name = "Users"
134
 
    alias = "os-users"
135
 
    namespace = "http://docs.openstack.org/compute/ext/users/api/v1.1"
136
 
    updated = "2011-08-08T00:00:00+00:00"
137
 
    admin_only = True
138
 
 
139
 
    def get_resources(self):
140
 
        coll_actions = {'detail': 'GET'}
141
 
        res = extensions.ResourceExtension('users',
142
 
                                           Controller(),
143
 
                                           collection_actions=coll_actions)
144
 
 
145
 
        return [res]