diff --git a/README.md b/README.md index d8f33972..bfb2c796 100644 --- a/README.md +++ b/README.md @@ -76,11 +76,12 @@ variant of this application config: - `conf/aggregator.dev.py` - `conf/aggregator.prod.py` -Which config to pick is determined through env variables: +Use the env var `OPENEO_AGGREGATOR_CONFIG` to point to the desired config path. +Currently, the dev config is used as fallback. -- if set, env variable `OPENEO_AGGREGATOR_CONFIG` is the path to the desired config file -- otherwise, if set, env variable `ENV` must be `dev` or `prod` -- otherwise, `dev` is used as default +Also note that these concrete config files will be refactored out of the `openeo-aggregator` repo +at some point in the future ([#117](https://github.com/Open-EO/openeo-aggregator/issues/117)) +and probably only a dummy default config will be preserved. ### Logging diff --git a/src/openeo_aggregator/config.py b/src/openeo_aggregator/config.py index 944d0049..e0dec69c 100644 --- a/src/openeo_aggregator/config.py +++ b/src/openeo_aggregator/config.py @@ -11,9 +11,6 @@ OPENEO_AGGREGATOR_CONFIG = "OPENEO_AGGREGATOR_CONFIG" -# TODO: #117 eliminate this (too simple) config handle -ENVIRONMENT_INDICATOR = "ENV" - CACHE_TTL_DEFAULT = 6 * 60 * 60 # Timeouts for requests to back-ends @@ -86,27 +83,24 @@ def get_config_dir() -> Path: raise RuntimeError("No config dir found") -def get_config(x: Any = None) -> AggregatorConfig: +def get_config(x: Union[str, Path, AggregatorConfig, None] = None) -> AggregatorConfig: """ Get aggregator config from given object: - if None: check env variable "OPENEO_AGGREGATOR_CONFIG" or return default config - if it is already an `AggregatorConfig` object: return as is - - if it is a string: try to parse it as JSON (file) + - if it is a string and looks like a path of a Python file: load config from that """ - # TODO #117 simplify this logic: just support direct file references, iso "aggregator.$ENV.py" if x is None: - for env_var in [OPENEO_AGGREGATOR_CONFIG, ENVIRONMENT_INDICATOR]: - if env_var in os.environ: - x = os.environ[env_var] - _log.info(f"Config from env var {env_var}: {x!r}") - break + if OPENEO_AGGREGATOR_CONFIG in os.environ: + x = os.environ[OPENEO_AGGREGATOR_CONFIG] + _log.info(f"Config from env var {OPENEO_AGGREGATOR_CONFIG}: {x!r}") else: - x = "dev" + # TODO #117 provide just a very simple default config, instead of a concrete dev one + x = get_config_dir() / "aggregator.dev.py" + _log.info(f"Config from fallback {x!r}") if isinstance(x, str): - if re.match("^[a-zA-Z]+$", x): - x = get_config_dir() / f"aggregator.{x.lower()}.py" x = Path(x) if isinstance(x, AggregatorConfig): diff --git a/tests/test_config.py b/tests/test_config.py index d37dacc2..b5c1b76e 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -5,7 +5,6 @@ import pytest from openeo_aggregator.config import ( - ENVIRONMENT_INDICATOR, OPENEO_AGGREGATOR_CONFIG, STREAM_CHUNK_SIZE_DEFAULT, AggregatorConfig, @@ -48,7 +47,6 @@ def test_config_from_py_file(tmp_path): def test_get_config_default_no_env(): assert OPENEO_AGGREGATOR_CONFIG not in os.environ - assert ENVIRONMENT_INDICATOR not in os.environ config = get_config() assert config.config_source.endswith("/conf/aggregator.dev.py") @@ -63,18 +61,6 @@ def test_get_config_py_file_path(tmp_path, convertor): assert config.streaming_chunk_size == 123 -@pytest.mark.parametrize(["value", "expected"], [ - ("dev", "/conf/aggregator.dev.py"), - ("DEV", "/conf/aggregator.dev.py"), - ("prod", "/conf/aggregator.prod.py"), - ("PROD", "/conf/aggregator.prod.py"), -]) -def test_get_config_arg(value, expected): - with mock.patch.dict(os.environ, {OPENEO_AGGREGATOR_CONFIG: "meh", ENVIRONMENT_INDICATOR: "pft"}): - config = get_config(value) - assert config.config_source.endswith(expected) - - def test_get_config_env_py_file(tmp_path): path = tmp_path / "aggregator-conf.py" path.write_text(CONFIG_PY_EXAMPLE) @@ -84,25 +70,3 @@ def test_get_config_env_py_file(tmp_path): assert config.config_source == str(path) assert config.aggregator_backends == {"b1": "https://b1.test"} assert config.streaming_chunk_size == 123 - - -@pytest.mark.parametrize("env_var", [ - OPENEO_AGGREGATOR_CONFIG, - ENVIRONMENT_INDICATOR, -]) -@pytest.mark.parametrize("value", ["dev", "DEV"]) -def test_get_config_none_env_dev(env_var, value): - with mock.patch.dict(os.environ, {env_var: value}): - config = get_config() - assert config.config_source.endswith("/conf/aggregator.dev.py") - - -@pytest.mark.parametrize("env_var", [ - OPENEO_AGGREGATOR_CONFIG, - ENVIRONMENT_INDICATOR, -]) -@pytest.mark.parametrize("value", ["prod", "PROD"]) -def test_get_config_none_env_prod(env_var, value): - with mock.patch.dict(os.environ, {env_var: value}): - config = get_config() - assert config.config_source.endswith("/conf/aggregator.prod.py")