def check_quotas(self, tenant_id, resources, deltas): """Check quotas for a tenant. This method checks quotas against current usage, reserved resources and the desired deltas. If any of the proposed values is over the defined quota, an QuotaExceeded exception will be raised with the sorted list of the resources which are too high. :param tenant_id: The ID of the tenant reserving the resources. :param resources: A dictionary of the registered resources. :param deltas: A dictionary of the proposed delta changes. """ unregistered_resources = [delta for delta in deltas if delta not in resources] if unregistered_resources: raise exception.QuotaResourceUnknown( unknown=unregistered_resources) quotas = self.get_all_quotas_by_tenant(tenant_id, deltas.keys()) quota_usages = self.get_all_quota_usages_by_tenant(tenant_id, deltas.keys()) overs = [resource for resource in deltas if (int(deltas[resource]) > 0 and (quota_usages[resource].in_use + quota_usages[resource].reserved + int(deltas[resource])) > quotas[resource].hard_limit)] if overs: raise exception.QuotaExceeded(overs=sorted(overs))
def reserve(self, tenant_id, resources, deltas): """Check quotas and reserve resources for a tenant. This method checks quotas against current usage, reserved resources and the desired deltas. If any of the proposed values is over the defined quota, an QuotaExceeded exception will be raised with the sorted list of the resources which are too high. Otherwise, the method returns a list of reservation objects which were created. :param tenant_id: The ID of the tenant reserving the resources. :param resources: A dictionary of the registered resources. :param deltas: A dictionary of the proposed delta changes. """ unregistered_resources = [ delta for delta in deltas if delta not in resources ] if unregistered_resources: raise exception.QuotaResourceUnknown( unknown=unregistered_resources) quotas = self.get_all_quotas_by_tenant(tenant_id, deltas.keys()) quota_usages = self.get_all_quota_usages_by_tenant( tenant_id, deltas.keys()) overs = [ resource for resource in deltas if (int(deltas[resource]) > 0 and (quota_usages[resource].in_use + quota_usages[resource].reserved + int(deltas[resource])) > quotas[resource].hard_limit) ] if overs: raise exception.QuotaExceeded(overs=sorted(overs)) reservations = [] for resource in deltas: reserved = deltas[resource] usage = quota_usages[resource] usage.reserved += reserved usage.save() resv = Reservation.create(usage_id=usage.id, delta=reserved, status=Reservation.Statuses.RESERVED) reservations.append(resv) return reservations