Skip to content

Commit

Permalink
Issue #117 drop ENVIRONMENT_INDICATOR support
Browse files Browse the repository at this point in the history
require full explicit config paths
  • Loading branch information
soxofaan committed Sep 20, 2023
1 parent e301fcc commit 8083dd2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 54 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 8 additions & 14 deletions src/openeo_aggregator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
36 changes: 0 additions & 36 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pytest

from openeo_aggregator.config import (
ENVIRONMENT_INDICATOR,
OPENEO_AGGREGATOR_CONFIG,
STREAM_CHUNK_SIZE_DEFAULT,
AggregatorConfig,
Expand Down Expand Up @@ -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")

Expand All @@ -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)
Expand All @@ -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")

0 comments on commit 8083dd2

Please sign in to comment.