Commit 64507cba authored by Dominik Rimpf's avatar Dominik Rimpf

Merge branch 'feature/endpoint' into 'master'

Separate base_url and endpoint

Closes #1

See merge request !3
parents 27141766 98904594
Pipeline #117768 passed with stage
in 23 seconds
[DEFAULT]
endpoint = www-net.scc.kit.edu
endpoint = prod
[www-net-devel.scc.kit.edu]
[dev]
base_url = www-net-devel.scc.kit.edu
version = 3.0
token = <token>
[www-net-test.scc.kit.edu]
[test]
base_url = www-net-test.scc.kit.edu
version = 3.0
token = <token>
[www-net.scc.kit.edu]
[prod]
base_url = www-net.scc.kit.edu
version = 3.0
token = <token>
......@@ -25,10 +25,14 @@ class ArgumentParser(argparse.ArgumentParser):
self.add_argument('--auth-config',
default=os.path.expanduser('~/.config/netdb_client.ini'),
help='config file path (default: %(default)s)')
self.add_argument('--endpoint', '-e',
help='endpoint to use.\n'
'Environment: "NETDB_ENDPOINT"\n'
'Config: [DEFAULT]: endpoint')
self.add_argument('--base-url', '-b',
help='webapi server.\n'
'Environment: "NETDB_BASE_URL"\n'
'Config: [DEFAULT]: endpoint')
'Config: [$endpoint]: base_url')
self.add_argument('--version',
help='webapi version.\n'
'Environment: "NETDB_VERSION"\n'
......@@ -47,23 +51,42 @@ class ArgumentParser(argparse.ArgumentParser):
self.error(f"Config file is readable by others. Please set it at least to 660.")
config.read(configpath)
def load_configoption(option):
if getattr(args, option) is None:
setattr(args, option, os.getenv(f'NETDB_{option.upper()}', None))
if getattr(args, option) is None:
if option == 'base_url':
setattr(args, option, config.get('DEFAULT', 'endpoint', fallback=None))
else:
setattr(args, option, config.get(args.base_url, option, fallback=None))
if getattr(args, option) is None:
self.error(
f'No {option} specified (looked in args, environment variable '
f'"NETDB_{option.upper()}", config-file in "{args.auth_config}")'
)
# load base_url first!
for configoption in ['base_url', 'token', 'version']:
load_configoption(configoption)
def load_config(option):
"""load config for an option"""
# if value is already set, we can stop here
if getattr(args, option) is not None:
return
# try to load option from environment
value = os.getenv(f'NETDB_{option.upper()}', None)
if value is None:
# endpoint should be loaded from DEFAULT section
if option == 'endpoint':
section = 'DEFAULT'
# everything else loads from the $endpoint section
else:
section = getattr(args, 'endpoint')
# load value from config section
value = config.get(section, option, fallback=None)
# For backwards compatibility:
# if base_url is still unknown use endpoint instead
if option == 'base_url' and value is None:
value = section
if value is None:
self.error(
f'No {option} specified (looked in args, environment variable '
f'"NETDB_{option.upper()}", config-file in "{args.auth_config} section {section}")')
# we have a value now and can use it
setattr(args, option, value)
# start with endpoint to get the right config section
for option in ['endpoint', 'base_url', 'token', 'version']:
load_config(option)
return args
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment