예제 #1
0
def get_acls(env, provider, token):
    """ Method used to get all ACLs for given provider """

    logger = util.get_logger(env)
    logger.debug('TEA configuraton ACL')
    cmr_url = util.get_env(env)
    headers = util.standard_headers({
        'Authorization': token,
        'Content-Type': 'application/json'
    })
    url = (f'{cmr_url}/access-control/acls'
           f'?provider={provider}'
           f'&identity_type=catalog_item'
           f'&page_size=2000')
    try:
        response = requests.get(url, headers=headers)
        json_data = response.json()
        logging.debug('get_acls: response=%s', json_data)
        if response.status_code == 200:
            if 'items' in json_data:
                items = json_data['items']
                return items
    except requests.exceptions.RequestException as error:
        logging.error('Error occurred in get_acl: %s', error)
    return []
예제 #2
0
def get_collections_s3_prefixes_dict(env: dict, token, provider, page_num, page_size):
    """ Method returns a dictionary with concept_ids as keys and S3 prefixes array as values  """
    all_collections_s3_prefixes_dict = {}
    json_data = get_collections(env, token, provider, page_num, page_size)
    if 'hits' not in json_data or json_data['hits'] == '0':
        return {}
    hits = json_data['hits']
    logger = util.get_logger(env)
    logger.debug('Hits=%d',hits)
    for item in json_data['items']:
        if 's3-links' in item['meta']:
            all_collections_s3_prefixes_dict[item['meta']['concept-id']] = item['meta']['s3-links']
    if page_size < hits:
        remainder = hits % page_size
        pages = (hits - remainder) / page_size
        pages = int(pages)
        index = 1
        while index < pages + 1:
            index += 1
            json_data = get_collections(env, token, provider, index, page_size)
            collections_s3_prefixes_dict = {}
            items = json_data['items']
            for item in items:
                if 's3-links' in item['meta']:
                    collections_s3_prefixes_dict[item['meta']['concept-id']] = \
                        item['meta']['s3-links']
            all_collections_s3_prefixes_dict.update(collections_s3_prefixes_dict)

    return all_collections_s3_prefixes_dict
예제 #3
0
def get_group_names(env: dict, acl_json, token):
    """ Method used to get group names for given ACL json """
    logger = util.get_logger({})
    group_names = []
    if 'group_permissions' in acl_json:
        all_groups = acl_json['group_permissions']
        for group in all_groups:
            if ('group_id'
                    in group) and (not group['group_id'].endswith('-CMR')):
                group_json = get_group(env, group['group_id'], token)
                if 'name' in group_json:
                    group_name = group_json['name']
                    group_names.append(group_name)
                    logger.info('Found non-CMR group: %s', group_name)
    return group_names
예제 #4
0
def get_collection(env:dict, token, concept_id):
    """ Method returns collection for given concept_id """
    logger = util.get_logger(env)
    headers = util.standard_headers({'Authorization': token, 'Content-Type': 'application/json'})
    cmr_base = util.get_env(env)
    url = f'{cmr_base}/search/concepts/{concept_id}.umm_json'
    try:
        response = requests.get(env, url, headers=headers)
        json_data = response.json()
        logger.debug('get_collection: response=%s', json_data)
        if response.status_code == 200:
            return json_data
    except requests.exceptions.RequestException as error:
        logger.error('Error occurred in get_collection: %s', error)
    return {}
예제 #5
0
def get_acl(env, acl_url, token):
    """ Method retrieves ACL for given ACL URL """
    logger = util.get_logger(env)
    headers = util.standard_headers({
        'Authorization': token,
        'Content-Type': 'application/json'
    })
    try:
        response = requests.get(acl_url, headers=headers)
        json_data = response.json()
        logger.debug("get_acl: response=%s", json_data)
        if response.status_code == 200:
            return json_data
    except requests.exceptions.RequestException as error:
        logging.error('Error occurred in get_acl: %s', error)
    return {}
예제 #6
0
def get_group(env: dict, group_id, token):
    """ Method used to get group for given group ID """
    logger = util.get_logger({})
    cmr_base = util.get_env(env)
    url = f'{cmr_base}/access-control/groups/{group_id}'
    headers = util.standard_headers({
        'Authorization': token,
        'Content-Type': 'application/json'
    })
    try:
        response = requests.get(url, headers=headers)
        json_data = response.json()
        logger.debug('get_group: response=%s', json_data)
        if response.status_code == 200:
            return json_data
    except requests.exceptions.RequestException as error:
        logger.error('Error occurred in get_group: %s', error)
    return {}
예제 #7
0
def get_groups(env: dict, acl_url, token):
    """ Method gets groups for given ACL URL """
    logger = util.get_logger(env)
    headers = util.standard_headers({
        'Authorization': token,
        'Content-Type': 'application/json'
    })
    try:
        response = requests.get(acl_url, headers=headers)
        json_data = response.json()
        logger.debug('get_groups: response=%s', json_data)
        if response.status_code == 200:
            if 'group_permissions' in json_data:
                items = json_data['group_permissions']
                return items
    except requests.exceptions.RequestException as error:
        logger.error('Error occurred in get_groups: %s', error)
    return []
예제 #8
0
def get_collections(env:dict, token, provider, page_num, page_size):
    """ Method returns collections for given provider """
    logger = util.get_logger(env)
    headers = util.standard_headers({'Authorization': token})
    cmr_base = util.get_env(env)
    url = (f'{cmr_base}/search/collections.umm-json'
        f'?provider={provider}'
        f'&sort_key=entry_title'
        f'&page_num={page_num}'
        f'&page_size={page_size}')
    try:
        logger.debug('request url: %s', url)
        response = requests.get(url, headers=headers)
        json_data = response.json()
        logger.debug('get_collections: response=%s', json_data)
        if response.status_code == 200:
            return json_data
    except requests.exceptions.RequestException as error:
        logger.error('Error occurred in get_collections from calling %s:\n%s', url, error)
    return {}
예제 #9
0
 def __init__(self, env):
     self.logger = util.get_logger(env)
     self.logger.debug('Creating TEA configuraton')