~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Adam Gandleman
  • Date: 2012-01-13 09:51:10 UTC
  • mfrom: (1.1.40)
  • Revision ID: package-import@ubuntu.com-20120113095110-ffd6163drcg77wez
Tags: 2012.1~e3~20120113.12049-0ubuntu1
[Chuck Short]
* New upstream version.
* debian/nova_sudoers, debian/nova-common.install, 
  Switch out to nova-rootwrap. (LP: #681774)
* Add "get-origsource-git" which allows developers to 
  generate a tarball from github, by doing:
  fakeroot debian/rules get-orig-source-git
* debian/debian/nova-objectstore.logrotate: Dont determine
  if we are running Debian or Ubuntu. (LP: #91379)

[Adam Gandleman]
* Removed python-nova.postinst, let dh_python2 generate instead since
  python-support is not a dependency. (LP: #907543)

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 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
25
 
 
26
 
 
27
 
FLAGS = flags.FLAGS
28
 
LOG = logging.getLogger('nova.api.openstack.users')
29
 
 
30
 
 
31
 
def _translate_keys(user):
32
 
    return dict(id=user.id,
33
 
                name=user.name,
34
 
                access=user.access,
35
 
                secret=user.secret,
36
 
                admin=user.admin)
37
 
 
38
 
 
39
 
class Controller(object):
40
 
 
41
 
    def __init__(self):
42
 
        self.manager = manager.AuthManager()
43
 
 
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()
49
 
 
50
 
    def index(self, req):
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)
56
 
 
57
 
    def detail(self, req):
58
 
        """Return all users in detail"""
59
 
        return self.index(req)
60
 
 
61
 
    def show(self, req, id):
62
 
        """Return data about the given user id"""
63
 
 
64
 
        #NOTE(justinsb): The drivers are a little inconsistent in how they
65
 
        #  deal with "NotFound" - some throw, some return None.
66
 
        try:
67
 
            user = self.manager.get_user(id)
68
 
        except exception.NotFound:
69
 
            user = None
70
 
 
71
 
        if user is None:
72
 
            raise exc.HTTPNotFound()
73
 
 
74
 
        return dict(user=_translate_keys(user))
75
 
 
76
 
    def delete(self, req, id):
77
 
        self._check_admin(req.environ['nova.context'])
78
 
        self.manager.delete_user(id)
79
 
        return {}
80
 
 
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))
89
 
 
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)))
99
 
 
100
 
 
101
 
def make_user(elem):
102
 
    elem.set('id')
103
 
    elem.set('name')
104
 
    elem.set('access')
105
 
    elem.set('secret')
106
 
    elem.set('admin')
107
 
 
108
 
 
109
 
class UserTemplate(xmlutil.TemplateBuilder):
110
 
    def construct(self):
111
 
        root = xmlutil.TemplateElement('user', selector='user')
112
 
        make_user(root)
113
 
        return xmlutil.MasterTemplate(root, 1)
114
 
 
115
 
 
116
 
class UsersTemplate(xmlutil.TemplateBuilder):
117
 
    def construct(self):
118
 
        root = xmlutil.TemplateElement('users')
119
 
        elem = xmlutil.SubTemplateElement(root, 'user', selector='users')
120
 
        make_user(elem)
121
 
        return xmlutil.MasterTemplate(root, 1)
122
 
 
123
 
 
124
 
class UserXMLSerializer(xmlutil.XMLTemplateSerializer):
125
 
    def index(self):
126
 
        return UsersTemplate()
127
 
 
128
 
    def default(self):
129
 
        return UserTemplate()
130
 
 
131
 
 
132
 
def create_resource():
133
 
    body_serializers = {
134
 
        'application/xml': UserXMLSerializer(),
135
 
    }
136
 
 
137
 
    serializer = wsgi.ResponseSerializer(body_serializers)
138
 
 
139
 
    return wsgi.Resource(Controller(), serializer=serializer)