def get_settable_quotas(self, context, resources, project_id, user_id=None): """ Given a list of resources, retrieve the range of settable quotas for the given user or project. :param context: The request context, for access checks. :param resources: A dictionary of the registered resources. :param project_id: The ID of the project to return quotas for. :param user_id: The ID of the user to return quotas for. """ settable_quotas = {} project_quotas = self.get_project_quotas(context, resources, project_id, remains=True) if user_id: user_quotas = self.get_user_quotas(context, resources, project_id, user_id) setted_quotas = db.quota_get_all_by_project_and_user(context, project_id, user_id) for key, value in user_quotas.items(): maximum = project_quotas[key]['remains'] +\ setted_quotas.get(key, 0) settable_quotas[key] = dict( minimum=value['in_use'] + value['reserved'], maximum=maximum ) else: for key, value in project_quotas.items(): minimum = max(int(value['limit'] - value['remains']), int(value['in_use'] + value['reserved'])) settable_quotas[key] = dict(minimum=minimum, maximum=-1) return settable_quotas
def get_user_quotas(self, context, resources, project_id, user_id, quota_class=None, defaults=True, usages=True): """ Given a list of resources, retrieve the quotas for the given user and project. :param context: The request context, for access checks. :param resources: A dictionary of the registered resources. :param project_id: The ID of the project to return quotas for. :param user_id: The ID of the user to return quotas for. :param quota_class: If project_id != context.project_id, the quota class cannot be determined. This parameter allows it to be specified. It will be ignored if project_id == context.project_id. :param defaults: If True, the quota class value (or the default value, if there is no value from the quota class) will be reported if there is no specific value for the resource. :param usages: If True, the current in_use and reserved counts will also be returned. """ user_quotas = db.quota_get_all_by_project_and_user(context, project_id, user_id) # Use the project quota for default user quota. proj_quotas = db.quota_get_all_by_project(context, project_id) for key, value in proj_quotas.iteritems(): if key not in user_quotas.keys(): user_quotas[key] = value user_usages = None if usages: user_usages = db.quota_usage_get_all_by_project_and_user(context, project_id, user_id) return self._process_quotas( context, resources, project_id, user_quotas, quota_class, defaults=defaults, usages=user_usages )
def get_settable_quotas(self, context, resources, project_id, user_id=None): """Retrieve range of settable quotas. Given a list of resources, retrieve the range of settable quotas for the given user or project. :param context: The request context, for access checks. :param resources: A dictionary of the registered resources. :param project_id: The ID of the project to return quotas for. :param user_id: The ID of the user to return quotas for. """ settable_quotas = {} project_quotas = self.get_project_quotas(context, resources, project_id, remains=True) if user_id: user_quotas = self.get_user_quotas(context, resources, project_id, user_id) setted_quotas = db.quota_get_all_by_project_and_user( context, project_id, user_id) for key, value in user_quotas.items(): maximum = (project_quotas[key]['remains'] + setted_quotas.get(key, 0)) settable_quotas[key] = dict( minimum=value['in_use'] + value['reserved'], maximum=maximum) else: for key, value in project_quotas.items(): minimum = max(int(value['limit'] - value['remains']), int(value['in_use'] + value['reserved'])) settable_quotas[key] = dict(minimum=minimum, maximum=-1) return settable_quotas
def get_user_quotas(self, context, resources, project_id, user_id, quota_class=None, defaults=True, usages=True): """Retrieve quotas for user and project. Given a list of resources, retrieve the quotas for the given user and project. :param context: The request context, for access checks. :param resources: A dictionary of the registered resources. :param project_id: The ID of the project to return quotas for. :param user_id: The ID of the user to return quotas for. :param quota_class: If project_id != context.project_id, the quota class cannot be determined. This parameter allows it to be specified. It will be ignored if project_id == context.project_id. :param defaults: If True, the quota class value (or the default value, if there is no value from the quota class) will be reported if there is no specific value for the resource. :param usages: If True, the current in_use and reserved counts will also be returned. """ user_quotas = db.quota_get_all_by_project_and_user( context, project_id, user_id) # Use the project quota for default user quota. proj_quotas = db.quota_get_all_by_project(context, project_id) for key, value in proj_quotas.items(): if key not in user_quotas.keys(): user_quotas[key] = value user_usages = None if usages: user_usages = db.quota_usage_get_all_by_project_and_user( context, project_id, user_id) return self._process_quotas(context, resources, project_id, user_quotas, quota_class, defaults=defaults, usages=user_usages)