Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
scc-net
netvs
netdb-client-lib
Commits
64507cba
Commit
64507cba
authored
Nov 12, 2020
by
nk2202
Browse files
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
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
example_config.ini
View file @
64507cba
[DEFAULT]
[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
version
=
3.0
token
=
<token>
token
=
<token>
[www-net-test.scc.kit.edu]
[test]
base_url
=
www-net-test.scc.kit.edu
version
=
3.0
version
=
3.0
token
=
<token>
token
=
<token>
[www-net.scc.kit.edu]
[prod]
base_url
=
www-net.scc.kit.edu
version
=
3.0
version
=
3.0
token
=
<token>
token
=
<token>
netdb_client/util.py
View file @
64507cba
...
@@ -25,10 +25,14 @@ class ArgumentParser(argparse.ArgumentParser):
...
@@ -25,10 +25,14 @@ class ArgumentParser(argparse.ArgumentParser):
self
.
add_argument
(
'--auth-config'
,
self
.
add_argument
(
'--auth-config'
,
default
=
os
.
path
.
expanduser
(
'~/.config/netdb_client.ini'
),
default
=
os
.
path
.
expanduser
(
'~/.config/netdb_client.ini'
),
help
=
'config file path (default: %(default)s)'
)
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'
,
self
.
add_argument
(
'--base-url'
,
'-b'
,
help
=
'webapi server.
\n
'
help
=
'webapi server.
\n
'
'Environment: "NETDB_BASE_URL"
\n
'
'Environment: "NETDB_BASE_URL"
\n
'
'Config: [
DEFAULT]: endpoint
'
)
'Config: [
$endpoint]: base_url
'
)
self
.
add_argument
(
'--version'
,
self
.
add_argument
(
'--version'
,
help
=
'webapi version.
\n
'
help
=
'webapi version.
\n
'
'Environment: "NETDB_VERSION"
\n
'
'Environment: "NETDB_VERSION"
\n
'
...
@@ -47,23 +51,42 @@ class ArgumentParser(argparse.ArgumentParser):
...
@@ -47,23 +51,42 @@ class ArgumentParser(argparse.ArgumentParser):
self
.
error
(
f
"Config file is readable by others. Please set it at least to 660."
)
self
.
error
(
f
"Config file is readable by others. Please set it at least to 660."
)
config
.
read
(
configpath
)
config
.
read
(
configpath
)
def
load_configoption
(
option
):
def
load_config
(
option
):
if
getattr
(
args
,
option
)
is
None
:
"""load config for an option"""
setattr
(
args
,
option
,
os
.
getenv
(
f
'NETDB_
{
option
.
upper
()
}
'
,
None
))
if
getattr
(
args
,
option
)
is
None
:
# if value is already set, we can stop here
if
option
==
'base_url'
:
if
getattr
(
args
,
option
)
is
not
None
:
setattr
(
args
,
option
,
config
.
get
(
'DEFAULT'
,
'endpoint'
,
fallback
=
None
))
return
else
:
setattr
(
args
,
option
,
config
.
get
(
args
.
base_url
,
option
,
fallback
=
None
))
# try to load option from environment
if
getattr
(
args
,
option
)
is
None
:
value
=
os
.
getenv
(
f
'NETDB_
{
option
.
upper
()
}
'
,
None
)
self
.
error
(
f
'No
{
option
}
specified (looked in args, environment variable '
if
value
is
None
:
f
'"NETDB_
{
option
.
upper
()
}
", config-file in "
{
args
.
auth_config
}
")'
# endpoint should be loaded from DEFAULT section
)
if
option
==
'endpoint'
:
section
=
'DEFAULT'
# load base_url first!
# everything else loads from the $endpoint section
for
configoption
in
[
'base_url'
,
'token'
,
'version'
]:
else
:
load_configoption
(
configoption
)
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
return
args
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment