Commit 80cb6e53 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Move UserCache from cyclades.common to lib.astakos

Move UserCache from synnefo.management.common to synnefo.lib.astakos.
Also, update ListCommand with 'astakos_url' and 'astakos_token' class
attributes, which must be declared only when 'user_uuid_field' is used.
parent a17f117c
......@@ -239,3 +239,58 @@ def get_token_from_cookie(request, cookiename):
pass
return None
class UserCache(object):
"""uuid<->displayname user 'cache'"""
def __init__(self, astakos_url, astakos_token, split=100):
self.astakos_token = astakos_token
self.astakos_url = astakos_url
self.user_catalog_url = astakos_url.replace("im/authenticate",
"service/api/user_catalogs")
self.users = {}
self.split = split
assert(self.split > 0), "split must be positive"
def fetch_names(self, uuid_list):
total = len(uuid_list)
split = self.split
for start in range(0, total, split):
end = start + split
try:
names = get_displaynames(token=self.astakos_token,
url=self.user_catalog_url,
uuids=uuid_list[start:end])
self.users.update(names)
except Exception as e:
logger.error("Failed to fetch names: %s", e)
def get_uuid(self, name):
if not name in self.users:
try:
self.users[name] = get_user_uuid(token=self.astakos_token,
url=self.user_catalog_url,
displayname=name)
except Exception as e:
logger.error("Can not get uuid for name %s: %s", name, e)
self.users[name] = name
return self.users[name]
def get_name(self, uuid):
"""Do the uuid-to-email resolving"""
if not uuid in self.users:
try:
self.users[uuid] = get_displayname(token=self.astakos_token,
url=self.user_catalog_url,
uuid=uuid)
except Exception as e:
logging.error("Can not get display name for uuid %s: %s",
uuid, e)
self.users[uuid] = "-"
return self.users[uuid]
......@@ -35,6 +35,8 @@ from optparse import make_option
from synnefo.webproject.management.commands import ListCommand
from synnefo.db.models import Network
from synnefo.settings import (CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN,
ASTAKOS_URL)
from logging import getLogger
log = getLogger(__name__)
......@@ -59,6 +61,8 @@ class Command(ListCommand):
object_class = Network
deleted_field = "deleted"
user_uuid_field = "userid"
astakos_url = ASTAKOS_URL
astakos_token = ASTAKOS_TOKEN
def get_machines(network):
return network.machines.filter(deleted=False).count()
......
......@@ -36,9 +36,12 @@ from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.lib.utils import merge_time
from synnefo.lib.astakos import UserCache
from synnefo.logic.rapi import GanetiApiError
from synnefo.management.common import Omit
from synnefo.management import common
from synnefo.settings import (CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN,
ASTAKOS_URL)
# Fields to print from a gnt-instance info
......@@ -79,7 +82,7 @@ class Command(BaseCommand):
displayname = options['displayname']
ucache = common.UserCache()
ucache = UserCache(ASTAKOS_URL, ASTAKOS_TOKEN)
try:
image = common.get_image(vm.imageid, vm.userid)['name']
......
......@@ -37,7 +37,8 @@ from synnefo.webproject.management.commands import ListCommand
from synnefo.db.models import VirtualMachine
from synnefo.management.common import get_backend
from synnefo.api.util import get_image
from synnefo.settings import (CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN,
ASTAKOS_URL)
from logging import getLogger
log = getLogger(__name__)
......@@ -73,6 +74,8 @@ class Command(ListCommand):
object_class = VirtualMachine
deleted_field = "deleted"
user_uuid_field = "userid"
astakos_url = ASTAKOS_URL
astakos_token = ASTAKOS_TOKEN
def get_public_ip(vm):
try:
......
......@@ -34,7 +34,10 @@
from django.core.management.base import BaseCommand, CommandError
from synnefo.webproject.management.util import format_bool, format_date
from synnefo.management.common import (format_vm_state, get_vm,
get_image, UserCache)
get_image)
from synnefo.lib.astakos import UserCache
from synnefo.settings import (CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN,
ASTAKOS_URL)
class Command(BaseCommand):
......@@ -61,7 +64,7 @@ class Command(BaseCommand):
'id': server.id,
'name': server.name,
'owner_uuid': userid,
'owner_name': UserCache().get_name(userid),
'owner_name': UserCache(ASTAKOS_URL, ASTAKOS_TOKEN).get_name(userid),
'created': format_date(server.created),
'updated': format_date(server.updated),
'image': image,
......
......@@ -36,11 +36,14 @@ import json
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import get_network, UserCache, Omit
from synnefo.management.common import get_network, Omit
from synnefo.db.models import (Backend, BackendNetwork,
pooled_rapi_client)
from synnefo.logic.rapi import GanetiApiError
from synnefo.lib.astakos import UserCache
from synnefo.settings import (CYCLADES_ASTAKOS_SERVICE_TOKEN as ASTAKOS_TOKEN,
ASTAKOS_URL)
from util import pool_map_chunks
......@@ -63,7 +66,7 @@ class Command(BaseCommand):
net = get_network(args[0])
ucache = UserCache()
ucache = UserCache(ASTAKOS_URL, ASTAKOS_TOKEN)
displayname = options['displayname']
......
......@@ -168,61 +168,5 @@ def check_backend_credentials(clustername, port, username, password):
" Ganeti Cluster name: %s" % info_name)
class UserCache(object):
"""uuid<->displayname user 'cache'"""
user_catalogs_url = ASTAKOS_URL.replace("im/authenticate",
"service/api/user_catalogs")
def __init__(self, split=100):
self.users = {}
self.split = split
assert(self.split > 0), "split must be positive"
def fetch_names(self, uuid_list):
total = len(uuid_list)
split = self.split
for start in range(0, total, split):
end = start + split
try:
names = \
astakos.get_displaynames(token=ASTAKOS_TOKEN,
url=UserCache.user_catalogs_url,
uuids=uuid_list[start:end])
self.users.update(names)
except Exception as e:
log.error("Failed to fetch names: %s", e)
def get_uuid(self, name):
if not name in self.users:
try:
self.users[name] = \
astakos.get_user_uuid(token=ASTAKOS_TOKEN,
url=UserCache.user_catalogs_url,
displayname=name)
except Exception as e:
log.error("Can not get uuid for name %s: %s", name, e)
self.users[name] = name
return self.users[name]
def get_name(self, uuid):
"""Do the uuid-to-email resolving"""
if not uuid in self.users:
try:
self.users[uuid] = \
astakos.get_displayname(token=ASTAKOS_TOKEN,
url=UserCache.user_catalogs_url,
uuid=uuid)
except Exception as e:
log.error("Can not get display name for uuid %s: %s", uuid, e)
self.users[uuid] = "-"
return self.users[uuid]
class Omit(object):
pass
......@@ -37,7 +37,7 @@ from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import FieldError
from synnefo.webproject.management import util
from synnefo.management.common import UserCache
from synnefo.lib.astakos import UserCache
class ListCommand(BaseCommand):
......@@ -92,6 +92,10 @@ class ListCommand(BaseCommand):
filters = {}
excludes = {}
# Fields used only with user_user_field
astakos_url = None
astakos_token = None
help = "Generic List Command"
option_list = BaseCommand.option_list + (
make_option(
......@@ -135,6 +139,10 @@ class ListCommand(BaseCommand):
def __init__(self, *args, **kwargs):
if self.user_uuid_field:
assert(self.astakos_url), "astakos_url attribute is needed when"\
" user_uuid_field is declared"
assert(self.astakos_token), "astakos_token attribute is needed"\
" user_uuid_field is declared"
self.option_list += (
make_option(
"-u", "--user",
......@@ -195,7 +203,8 @@ class ListCommand(BaseCommand):
user = options.get("user")
if user:
if "@" in user:
user = UserCache().get_uuid(user)
ucache = UserCache(self.astakos_url, self.astakos_token)
user = ucache.get_uuid(user)
self.filters[self.user_uuid_field] = user
# --deleted option
......@@ -228,7 +237,7 @@ class ListCommand(BaseCommand):
self.FIELDS["user.email"] =\
("user_email", "The email of the owner.")
uuids = [getattr(obj, self.user_uuid_field) for obj in objects]
ucache = UserCache()
ucache = UserCache(self.astakos_url, self.astakos_token)
ucache.fetch_names(list(set(uuids)))
for obj in objects:
uuid = getattr(obj, self.user_uuid_field)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment