def get_quota_usage_by_tenant(self, tenant_id, resource): """Get a specific quota usage by tenant.""" quotas = QuotaUsage.find_all(tenant_id=tenant_id, resource=resource).all() if len(quotas) == 0: return QuotaUsage.create(tenant_id=tenant_id, in_use=0, reserved=0, resource=resource) return quotas[0]
def get_all_quota_usages_by_tenant(self, tenant_id, resources): """ Retrieve the quota usagess for the given tenant. :param tenant_id: The ID of the tenant to return quotas for. :param resources: A list of the registered resources to get. """ all_usages = QuotaUsage.find_all(tenant_id=tenant_id).all() result_usages = {usage.resource: usage for usage in all_usages if usage.resource in resources} if len(result_usages) != len(resources): for resource in resources: # Not in the DB, return default value if resource not in result_usages: usage = QuotaUsage.create(tenant_id=tenant_id, in_use=0, reserved=0, resource=resource) result_usages[resource] = usage return result_usages
def rollback(self, reservations): """Roll back reservations. :param reservations: A list of the reservation UUIDs, as returned by the reserve() method. """ for reservation in reservations: usage = QuotaUsage.find_by(id=reservation.usage_id) usage.reserved -= reservation.delta reservation.status = Reservation.Statuses.ROLLEDBACK usage.save() reservation.save()
def test_commit(self): Reservation.save = Mock() QuotaUsage.save = Mock() FAKE_QUOTAS = [ QuotaUsage(id=1, tenant_id=FAKE_TENANT1, resource=Resource.INSTANCES, in_use=5, reserved=2), QuotaUsage(id=2, tenant_id=FAKE_TENANT1, resource=Resource.VOLUMES, in_use=1, reserved=2) ] FAKE_RESERVATIONS = [ Reservation(usage_id=1, delta=1, status=Reservation.Statuses.RESERVED), Reservation(usage_id=2, delta=2, status=Reservation.Statuses.RESERVED) ] QuotaUsage.find_by = Mock(side_effect=FAKE_QUOTAS) self.driver.commit(FAKE_RESERVATIONS) self.assertEqual(6, FAKE_QUOTAS[0].in_use) self.assertEqual(1, FAKE_QUOTAS[0].reserved) self.assertEqual(Reservation.Statuses.COMMITTED, FAKE_RESERVATIONS[0].status) self.assertEqual(3, FAKE_QUOTAS[1].in_use) self.assertEqual(0, FAKE_QUOTAS[1].reserved) self.assertEqual(Reservation.Statuses.COMMITTED, FAKE_RESERVATIONS[1].status)
def test_reserve_over_quota_with_reserved(self): FAKE_QUOTAS = [ QuotaUsage(id=1, tenant_id=FAKE_TENANT1, resource=Resource.INSTANCES, in_use=1, reserved=2), QuotaUsage(id=2, tenant_id=FAKE_TENANT1, resource=Resource.VOLUMES, in_use=0, reserved=0) ] self.mock_quota_result.all = Mock(return_value=[]) self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS) max_inst = CONF.max_instances_per_tenant delta = {'instances': max_inst - 1, 'volumes': 2} self.assertRaises(exception.QuotaExceeded, self.driver.reserve, FAKE_TENANT1, resources, delta)
def get_all_quota_usages_by_tenant(self, tenant_id, resources): """ Retrieve the quota usagess for the given tenant. :param tenant_id: The ID of the tenant to return quotas for. :param resources: A list of the registered resources to get. """ all_usages = QuotaUsage.find_all(tenant_id=tenant_id).all() result_usages = dict((usage.resource, usage) for usage in all_usages if usage.resource in resources) if len(result_usages) != len(resources): for resource in resources: # Not in the DB, return default value if resource not in result_usages: usage = QuotaUsage.create(tenant_id=tenant_id, in_use=0, reserved=0, resource=resource) result_usages[resource] = usage return result_usages
def commit(self, reservations): """Commit reservations. :param reservations: A list of the reservation UUIDs, as returned by the reserve() method. """ for reservation in reservations: usage = QuotaUsage.find_by(id=reservation.usage_id) usage.in_use += reservation.delta usage.reserved -= reservation.delta reservation.status = Reservation.Statuses.COMMITTED usage.save() reservation.save()
def test_get_quota_usage_by_tenant(self): FAKE_QUOTAS = [QuotaUsage(tenant_id=FAKE_TENANT1, resource=Resource.VOLUMES, in_use=3, reserved=1)] self.mock_usage_result.all = Mock(return_value=FAKE_QUOTAS) usage = self.driver.get_quota_usage_by_tenant(FAKE_TENANT1, Resource.VOLUMES) self.assertEqual(FAKE_TENANT1, usage.tenant_id) self.assertEqual(Resource.VOLUMES, usage.resource) self.assertEqual(3, usage.in_use) self.assertEqual(1, usage.reserved)
def test_get_quota_usage_by_tenant_default(self): FAKE_QUOTA = QuotaUsage(tenant_id=FAKE_TENANT1, resource=Resource.VOLUMES, in_use=0, reserved=0) self.mock_usage_result.all = Mock(return_value=[]) QuotaUsage.create = Mock(return_value=FAKE_QUOTA) usage = self.driver.get_quota_usage_by_tenant(FAKE_TENANT1, Resource.VOLUMES) self.assertEquals(FAKE_TENANT1, usage.tenant_id) self.assertEquals(Resource.VOLUMES, usage.resource) self.assertEquals(0, usage.in_use) self.assertEquals(0, usage.reserved)
def test_commit_cannot_be_less_than_zero(self): Reservation.save = Mock() QuotaUsage.save = Mock() FAKE_QUOTAS = [QuotaUsage(id=1, tenant_id=FAKE_TENANT1, resource=Resource.INSTANCES, in_use=0, reserved=-1)] FAKE_RESERVATIONS = [Reservation(usage_id=1, delta=-1, status=Reservation.Statuses.RESERVED)] QuotaUsage.find_by = Mock(side_effect=FAKE_QUOTAS) self.driver.commit(FAKE_RESERVATIONS) self.assertEqual(0, FAKE_QUOTAS[0].in_use) self.assertEqual(0, FAKE_QUOTAS[0].reserved) self.assertEqual(Reservation.Statuses.COMMITTED, FAKE_RESERVATIONS[0].status)