Get any single pagerduty resource by key.

    We allow flexible lookup by any of a list of identifier_fields.
    So, for example, you can look up users by email address or name by calling:

            get_resource('users', key, ['name', 'email'], ...)

    This method is mainly used to translate state sls into pagerduty id's for dependent objects.
    For example, a pagerduty escalation policy contains one or more schedules, which must be passed
    by their pagerduty id.  We look up the schedules by name (using this method), and then translate
    the names into id's.

    This method is implemented by getting all objects of the resource type (cached into __context__),
    then brute force searching through the list and trying to match any of the identifier_fields.
    The __context__ cache is purged after any create, update or delete to the resource.

def get_resource(resource_name, key, identifier_fields, profile='pagerduty', subdomain=None, api_key=None):
    '''
    Get any single pagerduty resource by key.

    We allow flexible lookup by any of a list of identifier_fields.
    So, for example, you can look up users by email address or name by calling:

            get_resource('users', key, ['name', 'email'], ...)

    This method is mainly used to translate state sls into pagerduty id's for dependent objects.
    For example, a pagerduty escalation policy contains one or more schedules, which must be passed
    by their pagerduty id.  We look up the schedules by name (using this method), and then translate
    the names into id's.

    This method is implemented by getting all objects of the resource type (cached into __context__),
    then brute force searching through the list and trying to match any of the identifier_fields.
    The __context__ cache is purged after any create, update or delete to the resource.
    '''
    # cache the expensive 'get all resources' calls into __context__ so that we do them once per salt run
    if 'pagerduty_util.resource_cache' not in __context__:
        __context__['pagerduty_util.resource_cache'] = {}
    if resource_name not in __context__['pagerduty_util.resource_cache']:
        if resource_name == 'services':
            action = resource_name + '?include[]=escalation_policy'
        else:
            action = resource_name
        __context__['pagerduty_util.resource_cache'][resource_name] = _query(action=action,
                                                                             profile=profile,
                                                                             subdomain=subdomain,
                                                                             api_key=api_key)[resource_name]
    for resource in __context__['pagerduty_util.resource_cache'][resource_name]:
        for field in identifier_fields:
            if resource[field] == key:
                # PagerDuty's /schedules endpoint returns less data than /schedules/:id.
                # so, now that we found the schedule, we need to get all the data for it.
                if resource_name == 'schedules':
                    full_resource_info = _query(action='{0}/{1}'.format(resource_name, resource['id']),
                                                profile=profile,
                                                subdomain=subdomain,
                                                api_key=api_key)
                    return full_resource_info
                return resource
    return None