def get_project_quotas(self, context, resources, project_id, quota_class=None, defaults=True, usages=True): """ Given a list of resources, retrieve the quotas for the given 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 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. """ quotas = {} project_quotas = db.quota_get_all_by_project(context, project_id) if usages: project_usages = db.quota_usage_get_all_by_project(context, project_id) # Get the quotas for the appropriate class. If the project ID # matches the one in the context, we use the quota_class from # the context, otherwise, we use the provided quota_class (if # any) if project_id == context.project_id: quota_class = context.quota_class if quota_class: class_quotas = db.quota_class_get_all_by_name(context, quota_class) else: class_quotas = {} for resource in resources.values(): # Omit default/quota class values if not defaults and resource.name not in project_quotas: continue quotas[resource.name] = dict( limit=project_quotas.get(resource.name, class_quotas.get(resource.name, resource.default)), ) # Include usages if desired. This is optional because one # internal consumer of this interface wants to access the # usages directly from inside a transaction. if usages: usage = project_usages.get(resource.name, {}) quotas[resource.name].update( in_use=usage.get('in_use', 0), reserved=usage.get('reserved', 0), ) return quotas
def test_quota_get_all_by_project(self): for i in range(3): for j in range(3): db.quota_create(self.ctxt, "proj%d" % i, "res%d" % j, j) for i in range(3): quotas_db = db.quota_get_all_by_project(self.ctxt, "proj%d" % i) self.assertEqual(quotas_db, {"project_id": "proj%d" % i, "res0": 0, "res1": 1, "res2": 2})
def test_quota_destroy_all_by_project(self): reservations = _quota_reserve(self.ctxt, "project1") db.quota_destroy_all_by_project(self.ctxt, "project1") self.assertEqual(db.quota_get_all_by_project(self.ctxt, "project1"), {"project_id": "project1"}) self.assertEqual(db.quota_usage_get_all_by_project(self.ctxt, "project1"), {"project_id": "project1"}) for r in reservations: self.assertRaises(exception.ReservationNotFound, db.reservation_get, self.ctxt, r)
def get_project_quotas(context, project_id): defaults = _get_default_quotas() if context.quota_class: get_class_quotas(context, context.quota_class, defaults) quota = db.quota_get_all_by_project(context, project_id) for key in defaults.keys(): if key in quota: defaults[key] = quota[key] return defaults
def test_quota_get_all_by_project(self): for i in range(3): for j in range(3): db.quota_create(self.ctxt, 'proj%d' % i, 'res%d' % j, j) for i in range(3): quotas_db = db.quota_get_all_by_project(self.ctxt, 'proj%d' % i) self.assertEqual(quotas_db, {'project_id': 'proj%d' % i, 'res0': 0, 'res1': 1, 'res2': 2})
def test_quota_destroy_all_by_project(self): reservations = _quota_reserve(self.ctxt, 'project1') db.quota_destroy_all_by_project(self.ctxt, 'project1') self.assertEqual(db.quota_get_all_by_project(self.ctxt, 'project1'), {'project_id': 'project1'}) self.assertEqual( db.quota_usage_get_all_by_project(self.ctxt, 'project1'), {'project_id': 'project1'}) for r in reservations: self.assertRaises(exception.ReservationNotFound, db.reservation_get, self.ctxt, r)
def get_project_quotas(self, context, resources, project_id, quota_class=None, defaults=True, usages=True, parent_project_id=None): """Given a list of resources, retrieve the quotas for the given 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 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. :param parent_project_id: The id of the current project's parent, if any. """ quotas = {} project_quotas = db.quota_get_all_by_project(context, project_id) if usages: project_usages = db.quota_usage_get_all_by_project(context, project_id) # Get the quotas for the appropriate class. If the project ID # matches the one in the context, we use the quota_class from # the context, otherwise, we use the provided quota_class (if # any) if project_id == context.project_id: quota_class = context.quota_class if quota_class: class_quotas = db.quota_class_get_all_by_name(context, quota_class) else: class_quotas = {} default_quotas = self.get_defaults(context, resources, parent_project_id=parent_project_id) for resource in resources.values(): # Omit default/quota class values if not defaults and resource.name not in project_quotas: continue quotas[resource.name] = dict( limit=project_quotas.get( resource.name, class_quotas.get(resource.name, default_quotas[resource.name])), ) # Include usages if desired. This is optional because one # internal consumer of this interface wants to access the # usages directly from inside a transaction. if usages: usage = project_usages.get(resource.name, {}) quotas[resource.name].update( in_use=usage.get('in_use', 0), reserved=usage.get('reserved', 0), ) return quotas
def test_quota_destroy_all_by_project(self): _quota_reserve(self.ctxt, "project1") db.quota_destroy_all_by_project(self.ctxt, "project1") self.assertEqual(db.quota_get_all_by_project(self.ctxt, "project1"), {"project_id": "project1"}) self.assertEqual(db.quota_usage_get_all_by_project(self.ctxt, "project1"), {"project_id": "project1"})