From 92ad14e99aa5cd5dc9b5eef7a22a7ef83aa1a9b3 Mon Sep 17 00:00:00 2001 From: Cecilia Nievas Date: Mon, 2 May 2022 13:42:32 +0200 Subject: [PATCH] Made prefix of environment database variables a variable --- gdeimporter/configuration.py | 12 +++++++-- gdeimporter/tools/configuration_methods.py | 29 +++++++++++++--------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/gdeimporter/configuration.py b/gdeimporter/configuration.py index 2798fe1..9652656 100644 --- a/gdeimporter/configuration.py +++ b/gdeimporter/configuration.py @@ -177,10 +177,18 @@ class Configuration: config, "number_cores" ) self.database_built_up = ConfigurationMethods.retrieve_database_credentials( - config, "database_built_up", "test_db_built_up.env", force_config_over_hierarchies + config, + "database_built_up", + "test_db_built_up.env", + "GDEIMPORTER", + force_config_over_hierarchies, ) self.database_gde_tiles = ConfigurationMethods.retrieve_database_credentials( - config, "database_gde_tiles", "test_db_gde_tiles.env", force_config_over_hierarchies + config, + "database_gde_tiles", + "test_db_gde_tiles.env", + "GDEIMPORTER", + force_config_over_hierarchies, ) self.data_units_surface_threshold = ConfigurationMethods.assign_float_parameter( config, "data_units_surface_threshold", True, -1e-15, 100.0 diff --git a/gdeimporter/tools/configuration_methods.py b/gdeimporter/tools/configuration_methods.py index 3e87e00..32b9d8c 100644 --- a/gdeimporter/tools/configuration_methods.py +++ b/gdeimporter/tools/configuration_methods.py @@ -319,7 +319,7 @@ class ConfigurationMethods: @staticmethod def retrieve_database_credentials( - config, input_parameter, env_filename, force_config_over_hierarchies + config, input_parameter, env_filename, ci_variable_name, force_config_over_hierarchies ): """This function retrieves the credentials needed to (later) connect to a specific SQL database. If force_config_over_hierarchies is False, it does so hieararchically, by @@ -338,6 +338,9 @@ class ConfigurationMethods: Name of the desired parameter, to be searched for as a primary key of config. env_filename (str): Name of a local .env file that will be run to load environment variables. + ci_variable_name (str): + Prefix of the name of the variables associated with the test databases in the CI + pipeline. force_config_over_hierarchies (bool): If True, the contents of the .yml configuration file specified in filepath will take precedence over any other hierarchy (e.g. preference of environment @@ -366,24 +369,26 @@ class ConfigurationMethods: if "port" in config: db_config["port"] = int(db_config["port"]) - if "GDEIMPORTER_DB_HOST" in os.environ and not (force_config_over_hierarchies): + if ("%s_DB_HOST" % (ci_variable_name)) in os.environ and not ( + force_config_over_hierarchies + ): # Running the CI pipeline db_config = { - "host": os.environ.get("GDEIMPORTER_DB_HOST"), - "dbname": os.environ.get("GDEIMPORTER_DB"), - "username": os.environ.get("GDEIMPORTER_USER"), - "password": os.environ.get("GDEIMPORTER_PASSWORD"), - "sourceid": os.environ.get("GDEIMPORTER_SOURCEID"), + "host": os.environ.get("%s_DB_HOST" % (ci_variable_name)), + "dbname": os.environ.get("%s_DB" % (ci_variable_name)), + "username": os.environ.get("%s_USER" % (ci_variable_name)), + "password": os.environ.get("%s_PASSWORD" % (ci_variable_name)), + "sourceid": os.environ.get("%s_SOURCEID" % (ci_variable_name)), } elif os.path.isfile(env_filename) and not (force_config_over_hierarchies): # Testing locally with a test database load_dotenv(env_filename) db_config = { - "host": os.environ.get("GDEIMPORTER_LOCAL_DB_HOST"), - "dbname": os.environ.get("GDEIMPORTER_LOCAL_DB"), - "username": os.environ.get("GDEIMPORTER_LOCAL_USER"), - "password": os.environ.get("GDEIMPORTER_LOCAL_PASSWORD"), - "sourceid": os.environ.get("GDEIMPORTER_LOCAL_SOURCEID"), + "host": os.environ.get("%s_LOCAL_DB_HOST" % (ci_variable_name)), + "dbname": os.environ.get("%s_LOCAL_DB" % (ci_variable_name)), + "username": os.environ.get("%s_LOCAL_USER" % (ci_variable_name)), + "password": os.environ.get("%s_LOCAL_PASSWORD" % (ci_variable_name)), + "sourceid": os.environ.get("%s_LOCAL_SOURCEID" % (ci_variable_name)), } return db_config -- GitLab