Beispiel #1
0
    def _get_quotas(self,
                    context,
                    resources,
                    keys,
                    has_sync,
                    project_id=None,
                    user_id=None):
        """Retrieve quotas for a resource.

        A helper method which retrieves the quotas for the specific
        resources identified by keys, and which apply to the current
        context.

        :param context: The request context, for access checks.
        :param resources: A dictionary of the registered resources.
        :param keys: A list of the desired quotas to retrieve.
        :param has_sync: If True, indicates that the resource must
                         have a sync attribute; if False, indicates
                         that the resource must NOT have a sync
                         attribute.
        :param project_id: Specify the project_id if current context
                           is admin and admin wants to impact on
                           common user's tenant.
        :param user_id: Specify the user_id if current context
                        is admin and admin wants to impact on
                        common user.
        """

        # Filter resources
        if has_sync:
            sync_filt = lambda x: hasattr(x, 'sync')
        else:
            sync_filt = lambda x: not hasattr(x, 'sync')
        desired = set(keys)
        sub_resources = dict((k, v) for k, v in resources.items()
                             if k in desired and sync_filt(v))

        # Make sure we accounted for all of them...
        if len(keys) != len(sub_resources):
            unknown = desired - set(sub_resources.keys())
            raise exception.QuotaResourceUnknown(unknown=sorted(unknown))

        if user_id:
            # Grab and return the quotas (without usages)
            quotas = self.get_user_quotas(context,
                                          sub_resources,
                                          project_id,
                                          user_id,
                                          context.quota_class,
                                          usages=False)
        else:
            # Grab and return the quotas (without usages)
            quotas = self.get_project_quotas(context,
                                             sub_resources,
                                             project_id,
                                             context.quota_class,
                                             usages=False)

        return dict((k, v['limit']) for k, v in quotas.items())
Beispiel #2
0
    def count(self, context, resource, *args, **kwargs):
        """Count a resource.

        For countable resources, invokes the count() function and
        returns its result.  Arguments following the context and
        resource are passed directly to the count function declared by
        the resource.

        :param context: The request context, for access checks.
        :param resource: The name of the resource, as a string.
        """

        # Get the resource
        res = self._resources.get(resource)
        if not res or not hasattr(res, 'count'):
            raise exception.QuotaResourceUnknown(unknown=[resource])

        return res.count(context, *args, **kwargs)
Beispiel #3
0
 def test_quota_resource_unknown(self):
     # Verify response code for exception.QuotaResourceUnknown
     unknown = "fake_quota_resource"
     e = exception.QuotaResourceUnknown(unknown=unknown)
     self.assertEqual(404, e.code)
Beispiel #4
0
 def test_quota_resource_unknown(self):
     # Verify response code for exception.QuotaResourceUnknown
     e = exception.QuotaResourceUnknown()
     self.assertEqual(e.code, 404)