Commit 03ad21de authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

astakos: Query service quota with lists

Extend service_get_quota and service_get_project_quota in a backwards
compatible manner in order to support querying for multiple users or
projects.
parent a661663e
......@@ -522,13 +522,18 @@ class AstakosClient(object):
"""
return self._call_astakos(self.api_quotas)
def _join_if_list(self, val):
return ','.join(map(str, val)) if isinstance(val, list) else val
# ----------------------------------
# do a GET to ``API_SERVICE_QUOTAS``
def service_get_quotas(self, user=None):
def service_get_quotas(self, user=None, project=None):
"""Get all quotas for resources associated with the service
Keyword arguments:
user -- optionally, the uuid of a specific user
user -- optionally, the uuid of a specific user, or a list thereof
project -- optionally, the uuid of a specific project, or a list
thereof
In case of success return a dict of dicts of dicts with current quotas
for all users, or of a specified user, if user argument is set.
......@@ -536,8 +541,13 @@ class AstakosClient(object):
"""
query = self.api_service_quotas
filters = {}
if user is not None:
query += "?user=" + user
filters['user'] = self._join_if_list(user)
if project is not None:
filters['project'] = self._join_if_list(project)
if filters:
query += "?" + urllib.urlencode(filters)
return self._call_astakos(query)
# ----------------------------------
......@@ -546,16 +556,20 @@ class AstakosClient(object):
"""Get all project quotas for resources associated with the service
Keyword arguments:
project -- optionally, the uuid of a specific project
project -- optionally, the uuid of a specific project, or a list
thereof
In case of success return a dict of dicts with current quotas
for all projects, or of a specified project, if project argument is set.
Otherwise raise an AstakosClientException
for all projects, or of a specified project, if project argument is
set. Otherwise raise an AstakosClientException
"""
query = self.api_service_project_quotas
filters = {}
if project is not None:
query += "?project=" + project
filters['project'] = self._join_if_list(project)
if filters:
query += "?" + urllib.urlencode(filters)
return self._call_astakos(query)
# ----------------------------------
......
......@@ -133,17 +133,18 @@ retry=0, use_pool=False, pool_size=8, logger=None\ **)**
It returns user's current quotas (as dict of dicts). In case of error
it raises an AstakosClientException exception.
**service_get_quotas(**\ user=None\ **)**
**service_get_quotas(**\ user=None, project=None\ **)**
It returns all users' current quotas for the resources associated with
the service (as dict of dicts of dicts). Optionally, one can query the
quotas of a specific user with argument user=UUID. In case of error it
raises an AstakosClientException exception.
quotas of a specific user with argument user=UUID (or a list of UUID).
Likewise one can specify a project (or a list of projects). In case of
error it raises an AstakosClientException exception.
**service_get_project_quotas(**\ project=None\ **)**
It returns all projects' current quotas for the resources
associated with the service (as dict of dicts).
Optionally, one can query the quotas of a specific project with
argument project=UUID. In case of error it raises an
argument project=UUID (or a list of UUID). In case of error it raises an
AstakosClientException exception.
**issue_commission_generic(**\ user_provisions, project_provisions, name="", force=False, auto_accept=False\ **)**
......
......@@ -58,12 +58,15 @@ def quotas(request):
@api.api_method(http_method='GET', token_required=True, user_required=False)
@component_from_token
def service_quotas(request):
user = request.GET.get('user')
users = [user] if user is not None else None
result = service_get_quotas(request.component_instance, users=users)
userstr = request.GET.get('user')
users = userstr.split(",") if userstr is not None else None
projectstr = request.GET.get('project')
projects = projectstr.split(",") if projectstr is not None else None
result = service_get_quotas(request.component_instance, users=users,
sources=projects)
if user is not None and result == {}:
raise ItemNotFound("No such user '%s'" % user)
if userstr is not None and result == {}:
raise ItemNotFound("No user with UUID '%s'" % userstr)
return json_response(result)
......@@ -71,13 +74,13 @@ def service_quotas(request):
@api.api_method(http_method='GET', token_required=True, user_required=False)
@component_from_token
def service_project_quotas(request):
project = request.GET.get('project')
projects = [project] if project is not None else None
projectstr = request.GET.get('project')
projects = projectstr.split(',') if projectstr is not None else None
result = service_get_project_quotas(request.component_instance,
projects=projects)
if project is not None and result == {}:
raise ItemNotFound("No such project '%s'" % project)
if projectstr is not None and result == {}:
raise ItemNotFound("No project with UUID '%s'" % projectstr)
return json_response(result)
......
......@@ -114,7 +114,7 @@ def get_user_quotas(user, resources=None, sources=None):
return quotas.get(user.uuid, {})
def service_get_quotas(component, users=None):
def service_get_quotas(component, users=None, sources=None):
name_values = Service.objects.filter(
component=component).values_list('name')
service_names = [t for (t,) in name_values]
......@@ -123,7 +123,10 @@ def service_get_quotas(component, users=None):
astakosusers = AstakosUser.objects.verified()
if users is not None:
astakosusers = astakosusers.filter(uuid__in=users)
return get_users_quotas(astakosusers, resources=resource_names)
if sources is not None:
sources = [project_ref(s) for s in sources]
return get_users_quotas(astakosusers, resources=resource_names,
sources=sources)
def mk_limits_dict(counters):
......
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