Debian Codegraph @

adjutantclient/v1/users.py

raw
1 # Copyright (C) 2016 Catalyst IT Ltd
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15
16 from urllib import parse
17
18 from adjutantclient.common import base
19
20
21 class User(base.Resource):
22 def __repr__(self):
23 return "<User %s>" % self._info
24
25 def invite(self, **fields):
26 return self.manager.create(self.identifier, **fields)
27
28 def get(self):
29 # set_loaded() first ... so if we have to bail, we know we tried.
30 self._loaded = True
31 if not hasattr(self.manager, 'get'):
32 return
33
34 new = self.manager.get(self.identifier)
35 if new:
36 self._add_details(new._info)
37
38 @property
39 def identifier(self):
40 return '%s/%s' % (self.stack_name, self.id)
41
42
43 class UserManager(base.ManagerWithFind):
44 resource_class = User
45
46 def get(self, user):
47 return self._get("/openstack/users/%s" % base.getid(user))
48
49 def list(self, **kwargs):
50 """Get a list of users.
51
52 :param limit: maximum number of users to return
53 :param marker: begin returning users that appear later in the user
54 list than that represented by this user id
55 :rtype: list of :class:`User`
56 """
57 def paginate(params):
58 '''Paginate users, even if more than API limit.'''
59 current_limit = int(params.get('limit') or 0)
60 url = 'openstack/users?%s' % parse.urlencode(params, True)
61 users = self._list(url, 'users')
62 for user in users:
63 yield user
64
65 num_users = len(users)
66 remaining_limit = current_limit - num_users
67 if remaining_limit > 0 and num_users > 0:
68 params['limit'] = remaining_limit
69 params['marker'] = user.id
70 for stack in paginate(params):
71 yield stack
72
73 params = {}
74 # if 'filters' in kwargs:
75 # filters = kwargs.pop('filters')
76 # params.update(filters)
77
78 for key, value in (kwargs).items():
79 if value:
80 params[key] = value
81
82 return paginate(params)
83
84 def invite(self, username, email, role_list):
85 """Invite a user to the current project."""
86
87 fields = {
88 'username': username,
89 'email': email,
90 'roles': role_list
91 }
92 return self.client.post('openstack/users', data=fields)
93
94 def cancel(self, user_id):
95 """Cancel a user invite task."""
96 url = 'openstack/users/%s' % user_id
97 return self._delete(url)
98
99 def revoke(self, user_id):
100 """Revoke all user roles on project."""
101 # NOTE(adriant): This doesn't work yet.
102 # Uses the same endpoint as cancel, but doesn't
103 # yet actually revoke all roles if an actual user
104 # rather than an invite.
105 return self.cancel(user_id)
106
107 def password_forgot(self, email, username=None):
108 """Forgot password email submission."""
109 params = {"email": email}
110 if username:
111 params['username'] = username
112
113 return self.client.post("openstack/users/password-reset",
114 data=params)
115
116 def password_force_reset(self, email):
117 """Force a password reset for a user.
118
119 This is an admin only function.
120 """
121 params = {"email": email}
122 return self.client.post(
123 "openstack/users/password-set", data=params)