forked from python-oca/python-oca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
85 lines (67 loc) · 2.51 KB
/
user.py
File metadata and controls
85 lines (67 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: UTF-8 -*-
from pool import Pool, PoolElement, Template, extractString
class User(PoolElement):
METHODS = {
'info': 'user.info',
'allocate': 'user.allocate',
'delete': 'user.delete',
'passwd': 'user.passwd',
'chgrp': 'user.chgrp'
}
XML_TYPES = {
'id': int,
'gid': int,
'group_ids': ['GROUPS', lambda group_ids: map(lambda group_id: int(group_id.text), group_ids)],
'gname': extractString,
'name': extractString,
'password': extractString,
'auth_driver': extractString,
'enabled': bool,
'template': ['TEMPLATE', Template],
#'datastore_quota': handled separately # see http://dev.opennebula.org/issues/3849
#'network_quota': handled separately # see http://dev.opennebula.org/issues/3849
#'vm_quota': handled separately # see http://dev.opennebula.org/issues/3849
#'image_quota' # see http://dev.opennebula.org/issues/3849
#'default_user_quotas' # see http://dev.opennebula.org/issues/3849
}
ELEMENT_NAME = 'USER'
@staticmethod
def allocate(client, user, password):
'''
allocates a new user in OpenNebula
``user``
username for the new user
``password``
password for the new user
'''
user_id = client.call(User.METHODS['allocate'], user, password)
return user_id
def __init__(self, xml, client):
super(User, self).__init__(xml, client)
self.id = self['ID'] if self['ID'] else None
def change_passwd(self, new_password):
'''
Changes the password for the given user.
``new_password``
The new password
'''
self.client.call(User.METHODS['passwd'], self.id, new_password)
def chgrp(self, gid):
'''
Changes the main group
``gid``
New group id. Set to -1 to leave the current one
'''
self.client.call(User.METHODS['chgrp'], self.id, gid)
def __repr__(self):
return '<oca.User("%s")>' % self.name
class UserPool(Pool):
METHODS = {
'info': 'userpool.info',
}
def __init__(self, client):
super(UserPool, self).__init__('USER_POOL', 'USER', client)
def _factory(self, xml):
u = User(xml, self.client)
u._convert_types()
return u