Last active
March 28, 2018 06:56
-
-
Save Satak/720ad40ed1206b16e1d54e9d630d6846 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This script parses environment variables and gets set of three variables at the time. | |
It then calls function with those arguments (database, username, password) based on pattern: | |
DB_<UNIQUENAME>_DATABASE | |
DB_<UNIQUENAME>_USERNAME | |
DB_<UNIQUENAME>_PASSWORD | |
''' | |
import re | |
# mimics environment variables | |
ENVS = { | |
'DB_X_USERNAME': 'x-username', | |
'DB_X_PASSWORD': 'x-password', | |
'DB_X_DATABASE': 'x-database', | |
'DB_Y_USERNAME': 'y-username', | |
'DB_Y_PASSWORD': 'y-password', | |
'DB_Y_DATABASE': 'y-database', | |
'TEMP': 'some random value', | |
'DBNAME': 'should not include' | |
} | |
def get_uniqe_names(envs): | |
'''Creates a set of unique names from environment variables based on regex.''' | |
name_set = set() | |
pattern = r'^DB_[A-Z]+_DATABASE' | |
for key in envs.keys(): | |
if re.search(pattern, key): | |
name_set.add(key.split('_')[1]) | |
return name_set | |
def main(): | |
'''Call a function if three variables are found from environment variables''' | |
for name in get_uniqe_names(ENVS): | |
database = ENVS.get(f'DB_{name}_DATABASE') | |
username = ENVS.get(f'DB_{name}_USERNAME') | |
password = ENVS.get(f'DB_{name}_PASSWORD') | |
if all((database, username, password)): | |
print('calling function with:', database, username, password) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment