Ejemplo n.º 1
0
    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]
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    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)