Revision 8283d6c1 snf-cyclades-app/synnefo/management/common.py

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