'Get usages of specified resources.
This function is called to get resource usages for validating quota
limit creates or updates in the os-quota-sets API and for displaying
resource usages in the os-used-limits API. This function is not used
for checking resource usage against quota limits.
:param context: The request context for access checks
:param resources: The dict of Resources for which to get usages
:param project_id: The project_id for scoping the usage count
:param user_id: Optional user_id for scoping the usage count
:returns: A dict containing resources and their usage information,
for example:
{\'project_id\': \'project-uuid\',
\'user_id\': \'user-uuid\',
\'instances\': {\'in_use\': 5},
\'fixed_ips\': {\'in_use\': 5}}'

def _get_usages(self, context, resources, project_id, user_id=None):

    usages = {}
    for resource in resources.values():
        if (not isinstance(resource, CountableResource)):
            continue
        if (resource.name in usages):
            continue
        if (resource.name in ('key_pairs', 'server_group_members', 'security_group_rules')):
            usages[resource.name] = {'in_use': 0}
        else:
            if (resource.name in db.quota_get_per_project_resources()):
                count = resource.count_as_dict(context, project_id)
                key = 'project'
            else:
                count = resource.count_as_dict(context, project_id, user_id=user_id)
                key = ('user' if user_id else 'project')
            counted_resources = count[key].keys()
            for res in counted_resources:
                count_value = count[key][res]
                usages[res] = {'in_use': count_value}
    return usages
