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
150 lines (116 loc) · 3.5 KB
/
user.py
File metadata and controls
150 lines (116 loc) · 3.5 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: UTF-8 -*-
from .pool import Pool, PoolElement, Template, extractString, XMLElement
class Quota(XMLElement):
def __init__(self, xml):
super(Quota, self).__init__(xml)
self._convert_types()
class VMQuota(Quota):
XML_TYPES = {
'cpu': int,
'cpu_used': int,
'memory': int,
'memory_used': int,
'system_disk_size': int,
'system_disk_size_used': int,
'vms': int,
'vms_used': int,
}
class DatastoreQuota(Quota):
XML_TYPES = {
'images': int,
'images_used': int,
'size': int,
'size_used': int,
}
class NetworkQuota(Quota):
XML_TYPES = {
'leases': int,
'leases_used': int,
}
class VMQuotaList(Quota):
XML_TYPES = {
'vm': VMQuota,
}
class DatastoreQuotaList(Quota):
XML_TYPES = {
'datastore': DatastoreQuota,
}
class NetworkQuotaList(Quota):
XML_TYPES = {
'network': NetworkQuota,
}
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: [int(group_id.text) for group_id in group_ids]],
'gname': extractString,
'name': extractString,
'password': extractString,
'auth_driver': extractString,
'enabled': bool,
'template': ['TEMPLATE', Template],
# 'network_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)
@property
def vm_quota(self):
self.info()
return VMQuotaList(self.xml.find('VM_QUOTA')).vm
@property
def datastore_quota(self):
self.info()
return DatastoreQuotaList(self.xml.find('DATASTORE_QUOTA')).datastore
@property
def network_quota(self):
self.info()
return NetworkQuotaList(self.xml.find('NETWORK_QUOTA')).network
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