Revision 3170076a
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, |
38 |
filter_results, pprint_table) |
|
38 |
filter_results, pprint_table, UUIDCache)
|
|
39 | 39 |
from synnefo.api.util import get_image |
40 | 40 |
from synnefo.db.models import VirtualMachine |
41 | 41 |
|
... | ... | |
75 | 75 |
help="Filter results. Comma seperated list of key `cond` val pairs" |
76 | 76 |
" that displayed entries must satisfy. e.g." |
77 | 77 |
" --filter-by \"operstate=STARTED,id>=22\"." |
78 |
" Available keys are: %s" % ", ".join(FIELDS)) |
|
78 |
" Available keys are: %s" % ", ".join(FIELDS)), |
|
79 |
make_option('--uuids', |
|
80 |
action='store_true', |
|
81 |
dest='use_uuids', |
|
82 |
default=False, |
|
83 |
help="Display UUIDs instead of user emails"), |
|
79 | 84 |
) |
80 | 85 |
|
81 | 86 |
def handle(self, *args, **options): |
... | ... | |
104 | 109 |
servers = filter_results(servers, filter_by) |
105 | 110 |
|
106 | 111 |
cache = ImageCache() |
112 |
ucache = UUIDCache() |
|
107 | 113 |
|
108 | 114 |
headers = ('id', 'name', 'owner', 'flavor', 'image', 'state', |
109 | 115 |
'backend') |
... | ... | |
124 | 130 |
|
125 | 131 |
state = format_vm_state(server) |
126 | 132 |
|
127 |
fields = (str(server.id), name, server.userid, flavor, image, |
|
133 |
user = server.userid |
|
134 |
if not options['use_uuids']: |
|
135 |
try: |
|
136 |
user = ucache.get_user(server.userid) |
|
137 |
except: |
|
138 |
pass |
|
139 |
|
|
140 |
fields = (str(server.id), name, user, flavor, image, |
|
128 | 141 |
state, str(server.backend)) |
129 | 142 |
table.append(fields) |
130 | 143 |
|
b/snf-cyclades-app/synnefo/management/common.py | ||
---|---|---|
44 | 44 |
from django.core.exceptions import FieldError |
45 | 45 |
|
46 | 46 |
from synnefo.api.util import validate_network_size |
47 |
from synnefo.settings import MAX_CIDR_BLOCK |
|
47 |
from synnefo.settings import (MAX_CIDR_BLOCK, CYCLADES_ASTAKOS_SERVICE_TOKEN, |
|
48 |
ASTAKOS_URL) |
|
48 | 49 |
from synnefo.logic.rapi import GanetiApiError, GanetiRapiClient |
50 |
from synnefo.lib import astakos |
|
49 | 51 |
|
50 | 52 |
|
51 | 53 |
def format_bool(b): |
... | ... | |
61 | 63 |
raise Exception("Can not parse string %s to bool" % string) |
62 | 64 |
|
63 | 65 |
|
64 |
|
|
65 | 66 |
def format_date(d): |
66 | 67 |
if not d: |
67 | 68 |
return '' |
... | ... | |
259 | 260 |
for row in table: |
260 | 261 |
print >> out, sep.join((val.rjust(width).encode('utf8') \ |
261 | 262 |
for val, width in zip(row, widths))) |
263 |
|
|
264 |
|
|
265 |
class UUIDCache(object): |
|
266 |
def __init__(self): |
|
267 |
self.users = {} |
|
268 |
|
|
269 |
def get_user(self, uuid): |
|
270 |
if not uuid in self.users: |
|
271 |
astakos_url = ASTAKOS_URL.replace("im/authenticate", |
|
272 |
"service/api/user_catalogs") |
|
273 |
self.users[uuid] = \ |
|
274 |
astakos.get_displayname(token=CYCLADES_ASTAKOS_SERVICE_TOKEN, |
|
275 |
url=astakos_url, uuid=uuid) |
|
276 |
|
|
277 |
return self.users[uuid] |
Also available in: Unified diff