Revision 76a13815
b/snf-common/synnefo/lib/astakos.py | ||
---|---|---|
239 | 239 |
pass |
240 | 240 |
|
241 | 241 |
return None |
242 |
|
|
243 |
|
|
244 |
class UserCache(object): |
|
245 |
"""uuid<->displayname user 'cache'""" |
|
246 |
|
|
247 |
def __init__(self, astakos_url, astakos_token, split=100): |
|
248 |
self.astakos_token = astakos_token |
|
249 |
self.astakos_url = astakos_url |
|
250 |
self.user_catalog_url = astakos_url.replace("im/authenticate", |
|
251 |
"service/api/user_catalogs") |
|
252 |
self.users = {} |
|
253 |
|
|
254 |
self.split = split |
|
255 |
assert(self.split > 0), "split must be positive" |
|
256 |
|
|
257 |
def fetch_names(self, uuid_list): |
|
258 |
total = len(uuid_list) |
|
259 |
split = self.split |
|
260 |
|
|
261 |
for start in range(0, total, split): |
|
262 |
end = start + split |
|
263 |
try: |
|
264 |
names = get_displaynames(token=self.astakos_token, |
|
265 |
url=self.user_catalog_url, |
|
266 |
uuids=uuid_list[start:end]) |
|
267 |
self.users.update(names) |
|
268 |
except Exception as e: |
|
269 |
logger.error("Failed to fetch names: %s", e) |
|
270 |
|
|
271 |
def get_uuid(self, name): |
|
272 |
if not name in self.users: |
|
273 |
try: |
|
274 |
self.users[name] = get_user_uuid(token=self.astakos_token, |
|
275 |
url=self.user_catalog_url, |
|
276 |
displayname=name) |
|
277 |
except Exception as e: |
|
278 |
logger.error("Can not get uuid for name %s: %s", name, e) |
|
279 |
self.users[name] = name |
|
280 |
|
|
281 |
return self.users[name] |
|
282 |
|
|
283 |
def get_name(self, uuid): |
|
284 |
"""Do the uuid-to-email resolving""" |
|
285 |
|
|
286 |
if not uuid in self.users: |
|
287 |
try: |
|
288 |
self.users[uuid] = get_displayname(token=self.astakos_token, |
|
289 |
url=self.user_catalog_url, |
|
290 |
uuid=uuid) |
|
291 |
except Exception as e: |
|
292 |
logging.error("Can not get display name for uuid %s: %s", |
|
293 |
uuid, e) |
|
294 |
self.users[uuid] = "-" |
|
295 |
|
|
296 |
return self.users[uuid] |
b/snf-cyclades-app/synnefo/api/management/commands/network-list.py | ||
---|---|---|
35 | 35 |
|
36 | 36 |
from synnefo.webproject.management.commands import ListCommand |
37 | 37 |
from synnefo.db.models import Network |
38 |
from synnefo.settings import (CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN, |
|
39 |
ASTAKOS_URL) |
|
38 | 40 |
|
39 | 41 |
from logging import getLogger |
40 | 42 |
log = getLogger(__name__) |
... | ... | |
59 | 61 |
object_class = Network |
60 | 62 |
deleted_field = "deleted" |
61 | 63 |
user_uuid_field = "userid" |
64 |
astakos_url = ASTAKOS_URL |
|
65 |
astakos_token = ASTAKOS_TOKEN |
|
62 | 66 |
|
63 | 67 |
def get_machines(network): |
64 | 68 |
return network.machines.filter(deleted=False).count() |
b/snf-cyclades-app/synnefo/api/management/commands/server-inspect.py | ||
---|---|---|
36 | 36 |
from django.core.management.base import BaseCommand, CommandError |
37 | 37 |
|
38 | 38 |
from synnefo.lib.utils import merge_time |
39 |
from synnefo.lib.astakos import UserCache |
|
39 | 40 |
from synnefo.logic.rapi import GanetiApiError |
40 | 41 |
from synnefo.management.common import Omit |
41 | 42 |
from synnefo.management import common |
43 |
from synnefo.settings import (CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN, |
|
44 |
ASTAKOS_URL) |
|
42 | 45 |
|
43 | 46 |
|
44 | 47 |
# Fields to print from a gnt-instance info |
... | ... | |
79 | 82 |
|
80 | 83 |
displayname = options['displayname'] |
81 | 84 |
|
82 |
ucache = common.UserCache()
|
|
85 |
ucache = UserCache(ASTAKOS_URL, ASTAKOS_TOKEN)
|
|
83 | 86 |
|
84 | 87 |
try: |
85 | 88 |
image = common.get_image(vm.imageid, vm.userid)['name'] |
b/snf-cyclades-app/synnefo/api/management/commands/server-list.py | ||
---|---|---|
37 | 37 |
from synnefo.db.models import VirtualMachine |
38 | 38 |
from synnefo.management.common import get_backend |
39 | 39 |
from synnefo.api.util import get_image |
40 |
|
|
40 |
from synnefo.settings import (CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN, |
|
41 |
ASTAKOS_URL) |
|
41 | 42 |
from logging import getLogger |
42 | 43 |
log = getLogger(__name__) |
43 | 44 |
|
... | ... | |
73 | 74 |
object_class = VirtualMachine |
74 | 75 |
deleted_field = "deleted" |
75 | 76 |
user_uuid_field = "userid" |
77 |
astakos_url = ASTAKOS_URL |
|
78 |
astakos_token = ASTAKOS_TOKEN |
|
76 | 79 |
|
77 | 80 |
def get_public_ip(vm): |
78 | 81 |
try: |
b/snf-cyclades-app/synnefo/api/management/commands/server-show.py | ||
---|---|---|
34 | 34 |
from django.core.management.base import BaseCommand, CommandError |
35 | 35 |
from synnefo.webproject.management.util import format_bool, format_date |
36 | 36 |
from synnefo.management.common import (format_vm_state, get_vm, |
37 |
get_image, UserCache) |
|
37 |
get_image) |
|
38 |
from synnefo.lib.astakos import UserCache |
|
39 |
from synnefo.settings import (CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN, |
|
40 |
ASTAKOS_URL) |
|
38 | 41 |
|
39 | 42 |
|
40 | 43 |
class Command(BaseCommand): |
... | ... | |
61 | 64 |
'id': server.id, |
62 | 65 |
'name': server.name, |
63 | 66 |
'owner_uuid': userid, |
64 |
'owner_name': UserCache().get_name(userid), |
|
67 |
'owner_name': UserCache(ASTAKOS_URL, ASTAKOS_TOKEN).get_name(userid),
|
|
65 | 68 |
'created': format_date(server.created), |
66 | 69 |
'updated': format_date(server.updated), |
67 | 70 |
'image': image, |
b/snf-cyclades-app/synnefo/logic/management/commands/network-inspect.py | ||
---|---|---|
36 | 36 |
from optparse import make_option |
37 | 37 |
|
38 | 38 |
from django.core.management.base import BaseCommand, CommandError |
39 |
from synnefo.management.common import get_network, UserCache, Omit
|
|
39 |
from synnefo.management.common import get_network, Omit |
|
40 | 40 |
|
41 | 41 |
from synnefo.db.models import (Backend, BackendNetwork, |
42 | 42 |
pooled_rapi_client) |
43 | 43 |
from synnefo.logic.rapi import GanetiApiError |
44 |
from synnefo.lib.astakos import UserCache |
|
45 |
from synnefo.settings import (CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN, |
|
46 |
ASTAKOS_URL) |
|
44 | 47 |
from util import pool_map_chunks |
45 | 48 |
|
46 | 49 |
|
... | ... | |
63 | 66 |
|
64 | 67 |
net = get_network(args[0]) |
65 | 68 |
|
66 |
ucache = UserCache() |
|
69 |
ucache = UserCache(ASTAKOS_URL, ASTAKOS_TOKEN)
|
|
67 | 70 |
|
68 | 71 |
displayname = options['displayname'] |
69 | 72 |
|
b/snf-cyclades-app/synnefo/management/common.py | ||
---|---|---|
168 | 168 |
" Ganeti Cluster name: %s" % info_name) |
169 | 169 |
|
170 | 170 |
|
171 |
class UserCache(object): |
|
172 |
"""uuid<->displayname user 'cache'""" |
|
173 |
|
|
174 |
user_catalogs_url = ASTAKOS_URL.replace("im/authenticate", |
|
175 |
"service/api/user_catalogs") |
|
176 |
|
|
177 |
def __init__(self, split=100): |
|
178 |
self.users = {} |
|
179 |
|
|
180 |
self.split = split |
|
181 |
assert(self.split > 0), "split must be positive" |
|
182 |
|
|
183 |
def fetch_names(self, uuid_list): |
|
184 |
total = len(uuid_list) |
|
185 |
split = self.split |
|
186 |
|
|
187 |
for start in range(0, total, split): |
|
188 |
end = start + split |
|
189 |
try: |
|
190 |
names = \ |
|
191 |
astakos.get_displaynames(token=ASTAKOS_TOKEN, |
|
192 |
url=UserCache.user_catalogs_url, |
|
193 |
uuids=uuid_list[start:end]) |
|
194 |
self.users.update(names) |
|
195 |
except Exception as e: |
|
196 |
log.error("Failed to fetch names: %s", e) |
|
197 |
|
|
198 |
def get_uuid(self, name): |
|
199 |
if not name in self.users: |
|
200 |
try: |
|
201 |
self.users[name] = \ |
|
202 |
astakos.get_user_uuid(token=ASTAKOS_TOKEN, |
|
203 |
url=UserCache.user_catalogs_url, |
|
204 |
displayname=name) |
|
205 |
except Exception as e: |
|
206 |
log.error("Can not get uuid for name %s: %s", name, e) |
|
207 |
self.users[name] = name |
|
208 |
|
|
209 |
return self.users[name] |
|
210 |
|
|
211 |
def get_name(self, uuid): |
|
212 |
"""Do the uuid-to-email resolving""" |
|
213 |
|
|
214 |
if not uuid in self.users: |
|
215 |
try: |
|
216 |
self.users[uuid] = \ |
|
217 |
astakos.get_displayname(token=ASTAKOS_TOKEN, |
|
218 |
url=UserCache.user_catalogs_url, |
|
219 |
uuid=uuid) |
|
220 |
except Exception as e: |
|
221 |
log.error("Can not get display name for uuid %s: %s", uuid, e) |
|
222 |
self.users[uuid] = "-" |
|
223 |
|
|
224 |
return self.users[uuid] |
|
225 |
|
|
226 |
|
|
227 | 171 |
class Omit(object): |
228 | 172 |
pass |
b/snf-webproject/synnefo/webproject/management/commands/__init__.py | ||
---|---|---|
37 | 37 |
from django.core.exceptions import FieldError |
38 | 38 |
|
39 | 39 |
from synnefo.webproject.management import util |
40 |
from synnefo.management.common import UserCache
|
|
40 |
from synnefo.lib.astakos import UserCache
|
|
41 | 41 |
|
42 | 42 |
|
43 | 43 |
class ListCommand(BaseCommand): |
... | ... | |
92 | 92 |
filters = {} |
93 | 93 |
excludes = {} |
94 | 94 |
|
95 |
# Fields used only with user_user_field |
|
96 |
astakos_url = None |
|
97 |
astakos_token = None |
|
98 |
|
|
95 | 99 |
help = "Generic List Command" |
96 | 100 |
option_list = BaseCommand.option_list + ( |
97 | 101 |
make_option( |
... | ... | |
135 | 139 |
|
136 | 140 |
def __init__(self, *args, **kwargs): |
137 | 141 |
if self.user_uuid_field: |
142 |
assert(self.astakos_url), "astakos_url attribute is needed when"\ |
|
143 |
" user_uuid_field is declared" |
|
144 |
assert(self.astakos_token), "astakos_token attribute is needed"\ |
|
145 |
" user_uuid_field is declared" |
|
138 | 146 |
self.option_list += ( |
139 | 147 |
make_option( |
140 | 148 |
"-u", "--user", |
... | ... | |
195 | 203 |
user = options.get("user") |
196 | 204 |
if user: |
197 | 205 |
if "@" in user: |
198 |
user = UserCache().get_uuid(user) |
|
206 |
ucache = UserCache(self.astakos_url, self.astakos_token) |
|
207 |
user = ucache.get_uuid(user) |
|
199 | 208 |
self.filters[self.user_uuid_field] = user |
200 | 209 |
|
201 | 210 |
# --deleted option |
... | ... | |
228 | 237 |
self.FIELDS["user.email"] =\ |
229 | 238 |
("user_email", "The email of the owner.") |
230 | 239 |
uuids = [getattr(obj, self.user_uuid_field) for obj in objects] |
231 |
ucache = UserCache() |
|
240 |
ucache = UserCache(self.astakos_url, self.astakos_token)
|
|
232 | 241 |
ucache.fetch_names(list(set(uuids))) |
233 | 242 |
for obj in objects: |
234 | 243 |
uuid = getattr(obj, self.user_uuid_field) |
Also available in: Unified diff