From c0c9d86c3330fa3dbf09897e6b761e62e71da32a Mon Sep 17 00:00:00 2001 From: "Alan B. Christie" <29806285+alanbchristie@users.noreply.github.com> Date: Wed, 6 Mar 2024 16:31:08 +0100 Subject: [PATCH] feat: DM variables are now optional in the environments file (#4) Co-authored-by: Alan Christie --- docs/developer.rst | 2 +- src/squonk2/environment.py | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/developer.rst b/docs/developer.rst index 3b0d3ae..8167587 100644 --- a/docs/developer.rst +++ b/docs/developer.rst @@ -17,7 +17,7 @@ And uninstall with:: .. note:: If you've set ``PYTHONPATH`` (see testing below) you will need to unset it - (``unset PYTHONPATH``). Otherrwise you will get a + (``unset PYTHONPATH``). Otherwise you will get a **No files were found to uninstall.** error from ``pip``. ******* diff --git a/src/squonk2/environment.py b/src/squonk2/environment.py index 6d8e6b6..da8ba12 100644 --- a/src/squonk2/environment.py +++ b/src/squonk2/environment.py @@ -36,14 +36,14 @@ # Keys required in each environment. _KEYCLOAK_HOSTNAME_KEY: str = "keycloak-hostname" _KEYCLOAK_REALM_KEY: str = "keycloak-realm" -_KEYCLOAK_DM_CLIENT_ID_KEY: str = "keycloak-dm-client-id" -_DM_HOSTNAME_KEY: str = "dm-hostname" # Optional keys (extracted from ENV if not present) _ADMIN_USER_KEY: str = "admin-user" _ADMIN_PASSWORD_KEY: str = "admin-password" # Optional keys _KEYCLOAK_AS_CLIENT_ID_KEY: str = "keycloak-as-client-id" _AS_HOSTNAME_KEY: str = "as-hostname" +_KEYCLOAK_DM_CLIENT_ID_KEY: str = "keycloak-dm-client-id" +_DM_HOSTNAME_KEY: str = "dm-hostname" class Environment: @@ -177,10 +177,6 @@ def __init__(self, environment: Optional[str]): self.__get_config_value(_KEYCLOAK_HOSTNAME_KEY) ) self.__keycloak_realm: str = str(self.__get_config_value(_KEYCLOAK_REALM_KEY)) - self.__keycloak_dm_client_id: str = str( - self.__get_config_value(_KEYCLOAK_DM_CLIENT_ID_KEY) - ) - self.__dm_hostname: str = str(self.__get_config_value(_DM_HOSTNAME_KEY)) # Get required keys, but allowing environment variables # if not present in the file... @@ -200,6 +196,12 @@ def __init__(self, environment: Optional[str]): self.__as_hostname: Optional[str] = self.__get_config_value( _AS_HOSTNAME_KEY, optional=True ) + self.__keycloak_dm_client_id: Optional[str] = self.__get_config_value( + _KEYCLOAK_DM_CLIENT_ID_KEY, optional=True + ) + self.__dm_hostname: Optional[str] = self.__get_config_value( + _DM_HOSTNAME_KEY, optional=True + ) @property def environment(self) -> str: @@ -237,7 +239,7 @@ def keycloak_as_client_id(self) -> Optional[str]: return self.__keycloak_as_client_id @property - def keycloak_dm_client_id(self) -> str: + def keycloak_dm_client_id(self) -> Optional[str]: """Return the keycloak Data Manager client ID.""" return self.__keycloak_dm_client_id @@ -274,17 +276,19 @@ def as_api(self) -> Optional[str]: return ret_val @property - def dm_hostname(self) -> str: + def dm_hostname(self) -> Optional[str]: """Return the keycloak hostname. This is the unmodified value found in the environment. """ return self.__dm_hostname @property - def dm_api(self) -> str: + def dm_api(self) -> Optional[str]: """Return the DM API. This is the environment hostname with a 'http' prefix and '/data-manager-api' postfix. """ + if not self.__dm_hostname: + return None if not self.__dm_hostname.startswith("http"): ret_val: str = f"https://{self.__dm_hostname}" else: