def update(self, req, body, tenant_id, id): LOG.info("req : '%s'\n\n" % req) LOG.info("Updating quota limits for tenant '%s'" % id) if not body: raise exception.BadRequest(_("Invalid request body.")) quotas = {} quota = None registered_resources = quota_engine.resources for resource, limit in body['quotas'].items(): if limit is None: continue if resource == "xmlns": continue if resource not in registered_resources: raise exception.QuotaResourceUnknown(unknown=resource) try: quota = Quota.find_by(tenant_id=id, resource=resource) quota.hard_limit = limit quota.save() except exception.ModelNotFoundError: quota = Quota.create(tenant_id=id, resource=resource, hard_limit=limit) quotas[resource] = quota return wsgi.Result(views.QuotaView(quotas).data(), 200)
def get_quota_by_tenant(self, tenant_id, resource): """Get a specific quota by tenant.""" quotas = Quota.find_all(tenant_id=tenant_id, resource=resource).all() if len(quotas) == 0: return Quota(tenant_id, resource, self.resources[resource].default) return quotas[0]
def get_all_quotas_by_tenant(self, tenant_id, resources): """ Retrieve the quotas for the given tenant. :param resources: A list of the registered resource to get. :param tenant_id: The ID of the tenant to return quotas for. """ all_quotas = Quota.find_all(tenant_id=tenant_id).all() result_quotas = dict((quota.resource, quota) for quota in all_quotas if quota.resource in resources) if len(result_quotas) != len(resources): for resource in resources: # Not in the DB, return default value if resource not in result_quotas: quota = Quota(tenant_id, resource, self.resources[resource].default) result_quotas[resource] = quota return result_quotas
def update(self, req, body, tenant_id, id): LOG.info("req : '%s'\n\n" % req) LOG.info("Updating quota limits for tenant '%s'" % id) if not body: raise exception.BadRequest(_("Invalid request body.")) quotas = {} quota = None for resource, limit in body['quotas'].items(): try: quota = Quota.find_by(tenant_id=id, resource=resource) quota.hard_limit = limit quota.save() except exception.ModelNotFoundError: quota = Quota.create(tenant_id=id, resource=resource, hard_limit=limit) quotas[resource] = quota return wsgi.Result(views.QuotaView(quotas).data(), 200)
def test_get_all_quotas_by_tenant(self): FAKE_QUOTAS = [Quota(tenant_id=FAKE_TENANT1, resource=Resource.INSTANCES, hard_limit=22), Quota(tenant_id=FAKE_TENANT1, resource=Resource.VOLUMES, hard_limit=15)] self.mock_quota_result.all = Mock(return_value=FAKE_QUOTAS) quotas = self.driver.get_all_quotas_by_tenant(FAKE_TENANT1, resources.keys()) self.assertEquals(FAKE_TENANT1, quotas[Resource.INSTANCES].tenant_id) self.assertEquals(Resource.INSTANCES, quotas[Resource.INSTANCES].resource) self.assertEquals(22, quotas[Resource.INSTANCES].hard_limit) self.assertEquals(FAKE_TENANT1, quotas[Resource.VOLUMES].tenant_id) self.assertEquals(Resource.VOLUMES, quotas[Resource.VOLUMES].resource) self.assertEquals(15, quotas[Resource.VOLUMES].hard_limit)
def test_get_quota_by_tenant(self): FAKE_QUOTAS = [Quota(tenant_id=FAKE_TENANT1, resource=Resource.INSTANCES, hard_limit=12)] self.mock_quota_result.all = Mock(return_value=FAKE_QUOTAS) quota = self.driver.get_quota_by_tenant(FAKE_TENANT1, Resource.VOLUMES) self.assertEquals(FAKE_TENANT1, quota.tenant_id) self.assertEquals(Resource.INSTANCES, quota.resource) self.assertEquals(12, quota.hard_limit)
def test_limit_index(self): tenant_id = "test_tenant_id" limit_controller = LimitsController() limits = [{ "URI": "*", "regex": ".*", "value": 10, "verb": "POST", "remaining": 2, "unit": "MINUTE", "resetTime": 1311272226 }, { "URI": "*", "regex": ".*", "value": 10, "verb": "PUT", "remaining": 2, "unit": "MINUTE", "resetTime": 1311272226 }, { "URI": "*", "regex": ".*", "value": 10, "verb": "DELETE", "remaining": 2, "unit": "MINUTE", "resetTime": 1311272226 }, { "URI": "*", "regex": ".*", "value": 10, "verb": "GET", "remaining": 2, "unit": "MINUTE", "resetTime": 1311272226 }] abs_limits = { "instances": Quota(tenant_id=tenant_id, resource="instances", hard_limit=100), "backups": Quota(tenant_id=tenant_id, resource="backups", hard_limit=40), "volumes": Quota(tenant_id=tenant_id, resource="volumes", hard_limit=55) } req = mock() req.environ = {"reddwarf.limits": limits} when(QUOTAS).get_all_quotas_by_tenant(tenant_id).thenReturn(abs_limits) view = limit_controller.index(req, tenant_id) expected = { 'limits': [{ 'max_instances': 100, 'max_backups': 40, 'verb': 'ABSOLUTE', 'max_volumes': 55 }, { 'regex': '.*', 'nextAvailable': '2011-07-21T18:17:06Z', 'uri': '*', 'value': 10, 'verb': 'POST', 'remaining': 2, 'unit': 'MINUTE' }, { 'regex': '.*', 'nextAvailable': '2011-07-21T18:17:06Z', 'uri': '*', 'value': 10, 'verb': 'PUT', 'remaining': 2, 'unit': 'MINUTE' }, { 'regex': '.*', 'nextAvailable': '2011-07-21T18:17:06Z', 'uri': '*', 'value': 10, 'verb': 'DELETE', 'remaining': 2, 'unit': 'MINUTE' }, { 'regex': '.*', 'nextAvailable': '2011-07-21T18:17:06Z', 'uri': '*', 'value': 10, 'verb': 'GET', 'remaining': 2, 'unit': 'MINUTE' }] } self.assertEqual(expected, view._data)