From a4caf4782b5a287b24ce046e9c03b30f4c4984c4 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Thu, 5 May 2022 18:14:32 +0200 Subject: [PATCH] fix(shared): allow list settings (#1837) --- shared/libretime_shared/config.py | 19 +++++++++++++------ shared/tests/config_test.py | 20 +++++++++++++++++--- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/shared/libretime_shared/config.py b/shared/libretime_shared/config.py index 16123bb2b..4a80b0b08 100644 --- a/shared/libretime_shared/config.py +++ b/shared/libretime_shared/config.py @@ -2,7 +2,7 @@ import sys from configparser import ConfigParser from os import environ from pathlib import Path -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, List, Optional, Union from loguru import logger @@ -63,11 +63,18 @@ class BaseConfig(BaseModel): env_name = (env_prefix + field.name).upper() if field.is_complex(): - children = self._get_fields_from_env( - env_name, - env_delimiter, - field.type_.__fields__, - ) + children: Union[List[Any], Dict[str, Any]] = [] + + if field.sub_fields: + if env_name in environ: + children = [v.strip() for v in environ[env_name].split(",")] + + else: + children = self._get_fields_from_env( + env_name, + env_delimiter, + field.type_.__fields__, + ) if len(children) != 0: result[field.name] = children diff --git a/shared/tests/config_test.py b/shared/tests/config_test.py index c3b8fe0ce..7897ad0bd 100644 --- a/shared/tests/config_test.py +++ b/shared/tests/config_test.py @@ -1,5 +1,6 @@ from os import environ from pathlib import Path +from typing import List from unittest import mock from pydantic import BaseModel @@ -11,17 +12,21 @@ from libretime_shared.config import BaseConfig, DatabaseConfig, RabbitMQConfig # pylint: disable=too-few-public-methods class FixtureConfig(BaseConfig): api_key: str + allowed_hosts: List[str] = [] database: DatabaseConfig rabbitmq: RabbitMQConfig = RabbitMQConfig() FIXTURE_CONFIG_RAW = """ api_key: "f3bf04fc" +allowed_hosts: + - example.com + - sub.example.com # Comment ! database: - host: "localhost" - port: 5432 + host: "localhost" + port: 5432 ignored: "ignored" """ @@ -45,6 +50,7 @@ def test_base_config(tmp_path: Path): config = FixtureConfig(filepath=config_filepath) assert config.api_key == "f3bf04fc" + assert config.allowed_hosts == ["example.com", "sub.example.com"] assert config.database.host == "localhost" assert config.database.port == 8888 assert config.rabbitmq.host == "changed" @@ -53,12 +59,20 @@ def test_base_config(tmp_path: Path): # Optional model: loading default values (rabbitmq) with mock.patch.dict(environ, {}): config = FixtureConfig(filepath=config_filepath) + assert config.allowed_hosts == ["example.com", "sub.example.com"] assert config.rabbitmq.host == "localhost" assert config.rabbitmq.port == 5672 # Optional model: overriding using environment (rabbitmq) - with mock.patch.dict(environ, {"LIBRETIME_RABBITMQ_HOST": "changed"}): + with mock.patch.dict( + environ, + { + "LIBRETIME_RABBITMQ_HOST": "changed", + "LIBRETIME_ALLOWED_HOSTS": "example.com, changed.example.com", + }, + ): config = FixtureConfig(filepath=config_filepath) + assert config.allowed_hosts == ["example.com", "changed.example.com"] assert config.rabbitmq.host == "changed" assert config.rabbitmq.port == 5672