From b86e2caff424183a9d3adbb4917ba1bb328bcd8e Mon Sep 17 00:00:00 2001 From: Israel Leon Date: Fri, 12 Feb 2021 18:51:21 -0500 Subject: [PATCH] refactor: TT-114 Refactor in the validation of environment variables classes in backend --- .../feature_toggles/feature_toggle_manager.py | 12 ++-------- requirements/time_tracker_api/dev.txt | 2 +- utils/azure_users.py | 24 ++++++++----------- utils/environment_variables.py | 9 +++++++ 4 files changed, 22 insertions(+), 25 deletions(-) create mode 100644 utils/environment_variables.py diff --git a/commons/feature_toggles/feature_toggle_manager.py b/commons/feature_toggles/feature_toggle_manager.py index 9d240b49..0783b4a6 100644 --- a/commons/feature_toggles/feature_toggle_manager.py +++ b/commons/feature_toggles/feature_toggle_manager.py @@ -2,19 +2,11 @@ import json from time_tracker_api.security import current_user_email from azure.appconfiguration import AzureAppConfigurationClient +from utils.environment_variables import check_variables_are_defined class FeatureToggleConfig: - def check_variables_are_defined(): - azure_app_variable = 'AZURE_APP_CONFIGURATION_CONNECTION_STRING' - if azure_app_variable not in os.environ: - raise EnvironmentError( - "{} is not defined in the environment".format( - azure_app_variable - ) - ) - - check_variables_are_defined() + check_variables_are_defined(['AZURE_APP_CONFIGURATION_CONNECTION_STRING']) AZURE_APP_CONFIGURATION_CONNECTION_STRING = os.environ.get( 'AZURE_APP_CONFIGURATION_CONNECTION_STRING' ) diff --git a/requirements/time_tracker_api/dev.txt b/requirements/time_tracker_api/dev.txt index 3a26760c..c85a2bbc 100644 --- a/requirements/time_tracker_api/dev.txt +++ b/requirements/time_tracker_api/dev.txt @@ -15,4 +15,4 @@ pytest-mock==2.0.0 coverage==4.5.1 # Git hooks -pre-commit==2.2.0 \ No newline at end of file +pre-commit==2.2.0 diff --git a/utils/azure_users.py b/utils/azure_users.py index 279584b1..1ae7f0c3 100644 --- a/utils/azure_users.py +++ b/utils/azure_users.py @@ -3,24 +3,20 @@ import requests import json from typing import List +from utils.environment_variables import check_variables_are_defined class MSConfig: - def check_variables_are_defined(): - auth_variables = [ - 'MS_CLIENT_ID', - 'MS_AUTHORITY', - 'MS_SECRET', - 'MS_SCOPE', - 'MS_ENDPOINT', - ] - for var in auth_variables: - if var not in os.environ: - raise EnvironmentError( - "{} is not defined in the environment".format(var) - ) + ms_variables = [ + 'MS_CLIENT_ID', + 'MS_AUTHORITY', + 'MS_SECRET', + 'MS_SCOPE', + 'MS_ENDPOINT', + ] + + check_variables_are_defined(ms_variables) - check_variables_are_defined() CLIENT_ID = os.environ.get('MS_CLIENT_ID') AUTHORITY = os.environ.get('MS_AUTHORITY') SECRET = os.environ.get('MS_SECRET') diff --git a/utils/environment_variables.py b/utils/environment_variables.py new file mode 100644 index 00000000..52bb7a96 --- /dev/null +++ b/utils/environment_variables.py @@ -0,0 +1,9 @@ +import os + + +def check_variables_are_defined(variables): + for var in variables: + if var not in os.environ: + raise EnvironmentError( + "{} is not defined in the environment".format(var) + )