示例#1
0
def load_config(config_file, environment, region, template, parameters):
    """
    Build hierarchy of configurations by loading multi-document config file.
    There must be a matching config for the environment name and region.
    :param str config_file: Path to config file
    :param str environment: Environment being updated
    :param str region: Region for the Stack
    :param str template: Override value for Template from command line
    :param list parameters: Override values for Parameters from the command line
    :return: Top of Config hierarchy
    :raises validationException: If config file not found, matching environment not found in config or config is invalid
    """
    arg_config = create_arg_config(environment, region, template, parameters)

    try:
        with open(config_file) as c:
            raw_configs = yaml.safe_load_all(c)
            all_config = None
            env_config = None
            for rc in raw_configs:
                config = Config(rc)
                if Config.is_all(config.environment):
                    all_config = config
                elif config.environment == environment and config.region == region:
                    env_config = config

            if not env_config:
                raise ValidationError(
                    f'Environment {environment} for {region} not found in {config_file}'
                )

            env_config.set_parent(all_config)
            arg_config.set_parent(env_config)

            # Validate before returning
            arg_config.validate()
            return arg_config
    except FileNotFoundError:
        raise ValidationError(f'Config file {config_file} not found')
示例#2
0
def test_is_all():
    assert Config.is_all('all')
    assert not Config.is_all('dev')