Revision 8283d6c1
b/snf-cyclades-app/synnefo/api/management/commands/network-list.py | ||
---|---|---|
34 | 34 |
from optparse import make_option |
35 | 35 |
|
36 | 36 |
from django.core.management.base import BaseCommand, CommandError |
37 |
from synnefo.management.common import (format_bool, filter_results, UUIDCache,
|
|
37 |
from synnefo.management.common import (format_bool, filter_results, UserCache,
|
|
38 | 38 |
Omit) |
39 | 39 |
from synnefo.db.models import Network |
40 | 40 |
from synnefo.management.common import pprint_table |
... | ... | |
87 | 87 |
if args: |
88 | 88 |
raise CommandError("Command doesn't accept any arguments") |
89 | 89 |
|
90 |
ucache = UUIDCache()
|
|
90 |
ucache = UserCache()
|
|
91 | 91 |
|
92 | 92 |
if options['deleted']: |
93 | 93 |
networks = Network.objects.all() |
... | ... | |
100 | 100 |
user = options['user'] |
101 | 101 |
if user: |
102 | 102 |
if '@' in user: |
103 |
user = ucache.get_user(user)
|
|
103 |
user = ucache.get_uuid(user)
|
|
104 | 104 |
networks = networks.filter(userid=user) |
105 | 105 |
|
106 | 106 |
filter_by = options['filter_by'] |
... | ... | |
128 | 128 |
else: |
129 | 129 |
headers.extend(['IPv4 Subnet', 'IPv4 Gateway']) |
130 | 130 |
|
131 |
uuids = list(set([network.userid for network in networks])) |
|
132 |
ucache.fetch_names(uuids) |
|
133 |
|
|
131 | 134 |
table = [] |
132 | 135 |
for network in networks.order_by("id"): |
133 | 136 |
uuid = network.userid |
134 | 137 |
if displayname: |
135 |
dname = ucache.get_user(uuid)
|
|
138 |
dname = ucache.get_name(uuid)
|
|
136 | 139 |
|
137 | 140 |
fields = filter(lambda x: x is not Omit, |
138 | 141 |
[str(network.id), |
b/snf-cyclades-app/synnefo/api/management/commands/server-inspect.py | ||
---|---|---|
77 | 77 |
|
78 | 78 |
displayname = options['displayname'] |
79 | 79 |
|
80 |
ucache = common.UUIDCache()
|
|
80 |
ucache = common.UserCache()
|
|
81 | 81 |
|
82 | 82 |
try: |
83 | 83 |
image = common.get_image(vm.imageid, vm.userid)['name'] |
... | ... | |
94 | 94 |
|
95 | 95 |
uuid = vm.userid |
96 | 96 |
if displayname: |
97 |
dname = ucache.get_user(uuid)
|
|
97 |
dname = ucache.get_name(uuid)
|
|
98 | 98 |
|
99 | 99 |
fields = filter(lambda x: x is not Omit, |
100 | 100 |
[vm.name, uuid, dname if displayname else Omit, |
b/snf-cyclades-app/synnefo/api/management/commands/server-list.py | ||
---|---|---|
35 | 35 |
|
36 | 36 |
from django.core.management.base import BaseCommand, CommandError |
37 | 37 |
from synnefo.management.common import (format_vm_state, get_backend, Omit, |
38 |
filter_results, pprint_table, UUIDCache)
|
|
38 |
filter_results, pprint_table, UserCache)
|
|
39 | 39 |
from synnefo.api.util import get_image |
40 | 40 |
from synnefo.db.models import VirtualMachine |
41 | 41 |
|
... | ... | |
92 | 92 |
if args: |
93 | 93 |
raise CommandError("Command doesn't accept any arguments") |
94 | 94 |
|
95 |
ucache = UUIDCache()
|
|
95 |
ucache = UserCache()
|
|
96 | 96 |
|
97 | 97 |
if options['backend_id']: |
98 | 98 |
backend = get_backend(options['backend_id']) |
... | ... | |
114 | 114 |
user = options['user'] |
115 | 115 |
if user: |
116 | 116 |
if '@' in user: |
117 |
user = ucache.get_user(user)
|
|
117 |
user = ucache.get_uuid(user)
|
|
118 | 118 |
servers = servers.filter(userid=user) |
119 | 119 |
|
120 | 120 |
filter_by = options['filter_by'] |
... | ... | |
136 | 136 |
'backend', |
137 | 137 |
]) |
138 | 138 |
|
139 |
uuids = list(set([server.userid for server in servers])) |
|
140 |
ucache.fetch_names(uuids) |
|
141 |
|
|
139 | 142 |
table = [] |
140 | 143 |
for server in servers.order_by('id'): |
141 | 144 |
try: |
... | ... | |
151 | 154 |
|
152 | 155 |
uuid = server.userid |
153 | 156 |
if displayname: |
154 |
dname = ucache.get_user(server.userid)
|
|
157 |
dname = ucache.get_name(server.userid)
|
|
155 | 158 |
|
156 | 159 |
fields = filter(lambda x: x is not Omit, |
157 | 160 |
[str(server.id), |
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.management.common import (format_bool, format_date, |
36 | 36 |
format_vm_state, get_vm, |
37 |
get_image, UUIDCache)
|
|
37 |
get_image, UserCache)
|
|
38 | 38 |
|
39 | 39 |
|
40 | 40 |
class Command(BaseCommand): |
... | ... | |
61 | 61 |
'id': server.id, |
62 | 62 |
'name': server.name, |
63 | 63 |
'owner_uuid': userid, |
64 |
'owner_name': UUIDCache().get_user(userid),
|
|
64 |
'owner_name': UserCache().get_name(userid),
|
|
65 | 65 |
'created': format_date(server.created), |
66 | 66 |
'updated': format_date(server.updated), |
67 | 67 |
'image': image, |
... | ... | |
69 | 69 |
'flavor': flavor, |
70 | 70 |
'deleted': format_bool(server.deleted), |
71 | 71 |
'suspended': format_bool(server.suspended), |
72 |
'state': format_vm_state(server) |
|
72 |
'state': format_vm_state(server),
|
|
73 | 73 |
} |
74 | 74 |
|
75 | 75 |
for key, val in sorted(kv.items()): |
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, UUIDCache, Omit
|
|
39 |
from synnefo.management.common import get_network, UserCache, Omit
|
|
40 | 40 |
|
41 | 41 |
from synnefo.db.models import (Backend, BackendNetwork, |
42 | 42 |
pooled_rapi_client) |
... | ... | |
62 | 62 |
|
63 | 63 |
net = get_network(args[0]) |
64 | 64 |
|
65 |
ucache = UUIDCache()
|
|
65 |
ucache = UserCache()
|
|
66 | 66 |
|
67 | 67 |
displayname = options['displayname'] |
68 | 68 |
|
... | ... | |
75 | 75 |
|
76 | 76 |
uuid = net.userid |
77 | 77 |
if displayname: |
78 |
dname = ucache.get_user(uuid)
|
|
78 |
dname = ucache.get_name(uuid)
|
|
79 | 79 |
|
80 | 80 |
fields = filter(lambda x: x is not Omit, |
81 | 81 |
[net.name, net.backend_id, net.state, uuid or '-', |
b/snf-cyclades-app/synnefo/management/common.py | ||
---|---|---|
248 | 248 |
for val, width in zip(row, widths))) |
249 | 249 |
|
250 | 250 |
|
251 |
class UUIDCache(object):
|
|
252 |
"""UUID-to-email cache"""
|
|
251 |
class UserCache(object):
|
|
252 |
"""uuid<->displayname user 'cache'"""
|
|
253 | 253 |
|
254 | 254 |
user_catalogs_url = ASTAKOS_URL.replace("im/authenticate", |
255 | 255 |
"service/api/user_catalogs") |
256 | 256 |
|
257 |
def __init__(self): |
|
257 |
def __init__(self, split=100):
|
|
258 | 258 |
self.users = {} |
259 | 259 |
|
260 |
def get_user(self, uuid): |
|
260 |
self.split = split |
|
261 |
assert(self.split > 0), "split must be positive" |
|
262 |
|
|
263 |
def fetch_names(self, uuid_list): |
|
264 |
l = len(uuid_list) |
|
265 |
|
|
266 |
start = 0 |
|
267 |
while start < l: |
|
268 |
end = self.split if l > self.split else l |
|
269 |
try: |
|
270 |
names = \ |
|
271 |
astakos.get_displaynames(token=ASTAKOS_TOKEN, |
|
272 |
url=UserCache.user_catalogs_url, |
|
273 |
uuids=uuid_list[start:end]) |
|
274 |
self.users.update(names) |
|
275 |
except Exception as e: |
|
276 |
log.error("Failed to fetch names: %s", e) |
|
277 |
|
|
278 |
start = end |
|
279 |
|
|
280 |
def get_uuid(self, name): |
|
281 |
if not name in self.users: |
|
282 |
try: |
|
283 |
self.users[name] = \ |
|
284 |
astakos.get_user_uuid(token=ASTAKOS_TOKEN, |
|
285 |
url=UserCache.user_catalogs_url, |
|
286 |
displayname=name) |
|
287 |
except Exception as e: |
|
288 |
log.error("Can not get uuid for name %s: %s", name, e) |
|
289 |
self.users[name] = name |
|
290 |
|
|
291 |
return self.users[name] |
|
292 |
|
|
293 |
def get_name(self, uuid): |
|
261 | 294 |
"""Do the uuid-to-email resolving""" |
262 | 295 |
|
263 | 296 |
if not uuid in self.users: |
264 | 297 |
try: |
265 | 298 |
self.users[uuid] = \ |
266 | 299 |
astakos.get_displayname(token=ASTAKOS_TOKEN, |
267 |
url=UUIDCache.user_catalogs_url,
|
|
300 |
url=UserCache.user_catalogs_url,
|
|
268 | 301 |
uuid=uuid) |
269 | 302 |
except Exception as e: |
270 | 303 |
log.error("Can not get display name for uuid %s: %s", uuid, e) |
Also available in: Unified diff