Skip to content

Commit

Permalink
Merge pull request #332 from OP-TED/feature/TED-894
Browse files Browse the repository at this point in the history
Update config_resolver.py
  • Loading branch information
CaptainOfHacks authored Oct 31, 2022
2 parents 8e43e65 + 2c6dd98 commit 15a3261
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
58 changes: 25 additions & 33 deletions ted_sws/core/adapters/config_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,29 @@
import inspect
import logging
import os
from abc import ABC
from abc import ABC, abstractmethod

from ted_sws.core.adapters.vault_secrets_store import VaultSecretsStore

logger = logging.getLogger(__name__)


class abstractstatic(staticmethod):
"""
This class serves to create decorators
with the property of a static method and an abstract method.
"""

__slots__ = ()

def __init__(self, function):
super(abstractstatic, self).__init__(function)
function.__isabstractmethod__ = True

__isabstractmethod__ = True


class ConfigResolverABC(ABC):
"""
This class defines a configuration resolution abstraction.
"""

@classmethod
def config_resolve(cls, default_value: str = None) -> str:
def config_resolve(self, default_value: str = None) -> str:
"""
This method aims to search for a configuration and return its value.
:param default_value: the default return value, if the configuration is not found.
:return: the value of the search configuration if found, otherwise default_value returns
"""
config_name = inspect.stack()[1][3]
return cls._config_resolve(config_name, default_value)
return self.concrete_config_resolve(config_name, default_value)

@abstractstatic
def _config_resolve(config_name: str, default_value: str = None):
@abstractmethod
def concrete_config_resolve(self, config_name: str, default_value: str = None):
"""
This abstract method is used to be able to define the configuration search in different environments.
:param config_name: the name of the configuration you are looking for
Expand All @@ -64,9 +48,10 @@ class AirflowConfigResolver(ConfigResolverABC):
This class aims to search for configurations in Airflow environment variables.
"""

def _config_resolve(config_name: str, default_value: str = None):
def concrete_config_resolve(self, config_name: str, default_value: str = None):
"""
This method retrieve configuration values from Airflow environment.
:param config_name:
:param default_value:
:return:
"""
Expand All @@ -83,19 +68,32 @@ class EnvConfigResolver(ConfigResolverABC):
This class aims to search for configurations in environment variables.
"""

def _config_resolve(config_name: str, default_value: str = None):
def concrete_config_resolve(self, config_name: str, default_value: str = None):
value = os.environ.get(config_name, default=default_value)
logger.debug("[ENV] Value of '" + str(config_name) + "' is " + str(value) + "(supplied default is '" + str(
default_value) + "')")
return value


class AirflowAndEnvConfigResolver(ConfigResolverABC):
"""
This class aims to combine the search for configurations in Airflow secrets and environmental variables.
"""

def concrete_config_resolve(self, config_name: str, default_value: str = None):
value = AirflowConfigResolver().concrete_config_resolve(config_name=config_name, default_value=default_value)
if value is not None:
return value
else:
return EnvConfigResolver().concrete_config_resolve(config_name, default_value)


class VaultConfigResolver(ConfigResolverABC):
"""
This class aims to search for configurations in Vault secrets.
"""

def _config_resolve(config_name: str, default_value: str = None):
def concrete_config_resolve(self, config_name: str, default_value: str = None):
value = VaultSecretsStore().get_secret(config_name, default_value)
logger.debug("[VAULT] Value of '" + str(config_name) + "' is " + str(value) + "(supplied default is '" + str(
default_value) + "')")
Expand All @@ -107,17 +105,11 @@ class VaultAndEnvConfigResolver(ConfigResolverABC):
This class aims to combine the search for configurations in Vault secrets and environmental variables.
"""

def _config_resolve(config_name: str, default_value: str = None):
value = VaultSecretsStore().get_secret(config_name, default_value)
logger.debug(
"[VAULT&ENV] Value of '" + str(config_name) + "' is " + str(value) + "(supplied default is '" + str(
default_value) + "')")
def concrete_config_resolve(self, config_name: str, default_value: str = None):
value = VaultConfigResolver().concrete_config_resolve(config_name, default_value)
if value is not None:
os.environ[config_name] = str(value)
return value
else:
value = EnvConfigResolver._config_resolve(config_name, default_value)
logger.debug(
"[VAULT&ENV] Value of '" + str(config_name) + "' is " + str(value) + "(supplied default is '" + str(
default_value) + "')")
value = EnvConfigResolver().concrete_config_resolve(config_name, default_value)
return value
8 changes: 5 additions & 3 deletions tests/e2e/supra_notice_manager/test_supra_notice_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def test_supra_notice_validator(mongodb_client, daily_supra_notice_repository):
validate_and_update_daily_supra_notice(day, mongodb_client)
result = daily_supra_notice_repository.get(reference=day)
assert result
assert result.validation_report.missing_notice_ids
assert notice_ids[0] not in result.validation_report.missing_notice_ids
assert not result.validation_report.is_valid()
assert result.notice_ids is not None
if result.validation_report.missing_notice_ids is not None:
assert result.validation_report.missing_notice_ids
assert notice_ids[0] not in result.validation_report.missing_notice_ids
assert not result.validation_report.is_valid()

0 comments on commit 15a3261

Please sign in to comment.