Example #1
0
def get_policies(inst):
    policies = inst.projectresourcequota_set.all().prefetch_related('resource')
    policy_list = []

    for p in policies:
        r = p.resource
        if not is_resource_useful(r, p.project_capacity):
            continue
        policy_list.append(p)

    return policy_list
Example #2
0
def get_quotas(user):
    """Transform the resource usage dictionary of a user.

    Return a list of dictionaries that represent the quotas of the user. Each
    dictionary has the following form:

    {
        'project': <Project instance>,
        'resources': [('Resource Name1', <Resource dict>),
                      ('Resource Name2', <Resource dict>),...]
    }

    where 'Resource Name' is the name of the resource and <Resource dict> is
    the dictionary that is returned by list_user_quotas and has the following
    fields:

        pending, project_pending, project_limit, project_usage, usage.

    Note, the get_quota_usage function returns many dicts, but we only keep the
    ones that have limit > 0
    """
    usage = get_user_quotas(user)

    quotas = []
    for project_id, resource_dict in usage.iteritems():
        source = {}
        source['project'] = Project.objects.get(uuid=project_id)
        q_res = source['resources'] = []

        for resource_name, resource in resource_dict.iteritems():
            # Chech if the resource is useful to display
            project_limit = resource['project_limit']
            usage = resource['usage']
            r = get_resource(resource_name)
            if not is_resource_useful(r, project_limit, usage):
                continue

            usage = units.show(usage, r.unit)
            limit = units.show(resource['limit'], r.unit)
            taken_by_others = resource['project_usage'] - resource['usage']
            effective_limit = min(resource['limit'], project_limit - taken_by_others)
            if effective_limit < 0:
                effective_limit = 0
            effective_limit = units.show(effective_limit, r.unit)

            if limit != effective_limit:
                limit += " (Effective Limit: " + effective_limit + ")"

            q_res.append((r.report_desc, usage, limit,))

        quotas.append(source)

    return quotas
Example #3
0
def get_policies(inst, quota_dict = None):
    policies = inst.projectresourcequota_set.all().prefetch_related('resource')
    policy_list = []

    for p in policies:
        r = p.resource

        if not quota_dict:
            quota_dict = get_project_quota(inst)
        if not quota_dict:
            usage = 0
        else:
            usage = quota_dict[r.name]['project_usage']
        if not is_resource_useful(r, p.project_capacity, usage):
            continue
        policy_list.append(p)

    return policy_list
Example #4
0
    def test_is_resource_useful(self):
        """Test if is_resource_useful function works as expected."""

        # Check if `is_resource_useful` produces the expected results,
        # regardless of the resource's unit.
        for unit in (None, "bytes"):
            r = MockResource(unit=unit)

            # Test if resource is useful, when its usage is zero
            self.assertTrue(utils.is_resource_useful(r, 2048))
            self.assertFalse(utils.is_resource_useful(r, 0))
            self.assertFalse(utils.is_resource_useful(r, PRACTICALLY_INFINITE))

            # Test if resource is useful, when its usage is not zero
            self.assertTrue(utils.is_resource_useful(r, 2048, 1024))
            self.assertTrue(utils.is_resource_useful(r, 0, 1024))
            self.assertFalse(utils.is_resource_useful(r, PRACTICALLY_INFINITE, 1024))