Пример #1
0
def load_test_file(path):
    """Helper to json load the contents of a file with some error handling

    Test files can be either formatted as:

    {
        "records": [
            {"data": {}, "description": "", ...}
        ]
    }

    or

    [
        {"data": {}, "description": "", ...}
    ]

    Args:
        path (str): Relative path to file on disk

    Returns:
        list: Loaded JSON from test event file
    """
    message_template = 'Improperly formatted file ({}): {}'
    with open(path, 'r') as test_event_file:
        try:
            contents = json.load(test_event_file)
        except (ValueError, TypeError) as err:
            message = message_template.format(path, err.message)
            return [], message
        else:
            # Check for legacy format, return a list
            # TOOD: Remove legacy format support
            if 'records' in contents and isinstance(contents['records'], list):
                LOGGER_CLI.warning(
                    'Legacy testing format detected, '
                    'test events should be a JSON list: [%s]',
                    os.path.basename(path))
                return contents['records'], None
            # Expect that the test event is a JSON list
            elif isinstance(contents, list):
                return contents, None

        message = message_template.format(
            path, 'Test file must contain either a list of maps, or a list of '
            'maps preceeded with a `records` key')
        return [], message
Пример #2
0
def generate_global_lambda_settings(config, config_name, generate_func, tf_tmp_file, message):
    """Generate settings for global Lambda functions

    Args:
        config (dict): lambda function settings read from 'conf/' directory
        config_name (str): keyname of lambda function settings in config.
        generate_func (func): method to generate lambda function settings.
        tf_tmp_file (str): filename of terraform file, generated by CLI.
        message (str): Message will be logged by LOGGER.
    """
    if not config['lambda'].get(config_name):
        LOGGER_CLI.warning('Config for \'%s\' not in lambda.json', config_name)
        remove_temp_terraform_file(tf_tmp_file, message)
        return

    if config['lambda'][config_name].get('enabled', True):
        generated_config = generate_func(config=config)
        if generated_config:
            with open(tf_tmp_file, 'w') as tf_file:
                json.dump(generated_config, tf_file, indent=2, sort_keys=True)
    else:
        remove_temp_terraform_file(tf_tmp_file, message)