diff options
author | Edyta Krukowska <edyta.krukowska@nokia.com> | 2021-05-12 12:00:07 +0200 |
---|---|---|
committer | Edyta Krukowska <edyta.krukowska@nokia.com> | 2021-05-17 10:14:13 +0200 |
commit | 1002e423a67cc4c22846ffc333b8027230e55dcb (patch) | |
tree | c356d859c8a6981dda9b6f804b74f9ba8a06b4e5 /onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py | |
parent | ad98c5f8850ffa031882e90b4f081c7f5e7c1709 (diff) |
Add env service for configuration
Issue-ID: DCAEGEN2-2630
Signed-off-by: Edyta Krukowska <edyta.krukowska@nokia.com>
Change-Id: I86b4a84fd49bfe41afd39a9a2096d72d8263e63a
Diffstat (limited to 'onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py')
-rw-r--r-- | onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py index d02b1d1..7f36169 100644 --- a/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py +++ b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py @@ -1,5 +1,6 @@ # ================================================================================ # Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. +# Copyright (C) 2021 Nokia. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -22,12 +23,33 @@ import requests from onap_dcae_cbs_docker_client import get_module_logger from onap_dcae_cbs_docker_client.exceptions import ENVsMissing, CantGetConfig, CBSUnreachable - logger = get_module_logger(__name__) ######### # HELPERS + + +def _recurse(config): + """ + Recurse through a configuration, or recursively a sub element of it. + If it's a dict: recurse over all the values + If it's a list: recurse over all the values + If it's a string: return the replacement + If none of the above, just return the item. + """ + if isinstance(config, list): + return [_recurse(item) for item in config] + if isinstance(config, dict): + for key in config: + config[key] = _recurse(config[key]) + return config + if isinstance(config, str): + return change_envs(config) + # not a dict, not a list, not a string, nothing to do. + return config + + def _get_path(path): """ Try to get the config, and return appropriate exceptions otherwise @@ -70,13 +92,26 @@ def _get_path(path): raise CBSUnreachable(e) +def change_envs(value): + """ + Replace env reference by actual value and return it + """ + if value.startswith('$'): + try: + value = os.environ[value.replace('${', '').replace('}', '')] + except KeyError as e: + raise ENVsMissing("Required ENV Variable {0} missing".format(e)) + return value + + ######### # Public def get_all(): """ Hit the CBS service_component_all endpoint """ - return _get_path("service_component_all") + config = _get_path("service_component_all") + return _recurse(config) def get_config(): @@ -86,4 +121,5 @@ def get_config(): TODO: should we take in a "retry" boolean, and retry on behalf of the caller? Currently, we return an exception and let the application decide how it wants to proceed (Crash, try again, etc). """ - return _get_path("service_component") + config = _get_path("service_component") + return _recurse(config) |