summaryrefslogtreecommitdiffstats
path: root/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client
diff options
context:
space:
mode:
authorTommy Carpenter <tommy@research.att.com>2019-06-19 14:54:53 +0000
committerTommy Carpenter <tommy@research.att.com>2019-06-21 14:07:29 -0400
commit6778c62c6f9bfb7f6dc0123f268eb3f4e960b847 (patch)
treeee5403fca9adac71741c1c05ea1817f34f91e567 /onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client
parent4952f1f70ec234b4a0e053e5bb0cf940d10687e5 (diff)
Move to k8s service names, exceptions
Issue-ID: DCAEGEN2-1537 Change-Id: Ibb7bd0233b5de9eb853f3516ba0907e148eae21d Signed-off-by: Tommy Carpenter <tommy@research.att.com>
Diffstat (limited to 'onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client')
-rw-r--r--onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py16
-rw-r--r--onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py88
-rw-r--r--onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/exceptions.py45
3 files changed, 91 insertions, 58 deletions
diff --git a/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py
index e9d0246..ce9debe 100644
--- a/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py
+++ b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py
@@ -1,5 +1,5 @@
# ================================================================================
-# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2017-2019 AT&T Intellectual Property. 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.
@@ -15,3 +15,17 @@
# ============LICENSE_END=========================================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
+import logging
+
+
+def get_module_logger(mod_name):
+ """
+ To use this, do logger = get_module_logger(__name__)
+ """
+ logger = logging.getLogger(mod_name)
+ handler = logging.StreamHandler()
+ formatter = logging.Formatter("%(asctime)s [%(name)-12s] %(levelname)-8s %(message)s")
+ handler.setFormatter(formatter)
+ logger.addHandler(handler)
+ logger.setLevel(logging.DEBUG)
+ return logger
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 80c9f75..ef0dfbc 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,5 @@
# ================================================================================
-# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2017-2019 AT&T Intellectual Property. 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.
@@ -18,82 +18,53 @@
import json
import os
-import logging
import requests
+from onap_dcae_cbs_docker_client import get_module_logger
+from onap_dcae_cbs_docker_client.exceptions import ENVsMissing, CantGetConfig, CBSUnreachable
-LOGGER = logging.getLogger().getChild(__name__)
-
-class ENVsMissing(Exception):
- """
- Exception to represent critical ENVs are missing
- """
- pass
+logger = get_module_logger(__name__)
#########
# HELPERS
-
-
-def _get_uri_from_consul(consul_url, name):
- """
- Call consul's catalog
- TODO: currently assumes there is only one service with this hostname
- """
- url = "{0}/v1/catalog/service/{1}".format(consul_url, name)
- LOGGER.debug("Trying to lookup service: {0}".format(url))
- res = requests.get(url)
- res.raise_for_status()
- services = res.json()
- return "http://{0}:{1}".format(services[0]["ServiceAddress"], services[0]["ServicePort"])
-
-
-def _get_envs():
+def _get_path(path):
"""
- Returns hostname, consul_host.
- If the necessary ENVs are not found, this is fatal, and raises an exception.
+ Try to get the config, and return appropriate exceptions otherwise
"""
try:
hostname = os.environ["HOSTNAME"] # this is the name of the component itself
- consul_host = os.environ["CONSUL_HOST"] # this is the host of consul itself
- cbs_name = os.environ["CONFIG_BINDING_SERVICE"] # this is the name under which the CBS is registered in Consul.
+ # in most cases, this is the K8s service name which is a resolvable DNS name
+ # if running outside k8s, this name needs to be resolvable by DNS via other means.
+ cbs_resolvable_hostname = os.environ["CONFIG_BINDING_SERVICE"]
except KeyError as e:
raise ENVsMissing("Required ENV Variable {0} missing".format(e))
- return hostname, consul_host, cbs_name
+ # TODO: https
+ cbs_url = "http://{0}:10000".format(cbs_resolvable_hostname)
-def _get_path(path):
- """
- This call does not raise an exception if Consul or the CBS cannot complete the request.
- It logs an error and returns {} if the config is not bindable.
- It could be a temporary network outage. Call me again later.
-
- It will raise an exception if the necessary env parameters were not set because that is irrecoverable.
- This function is called in my /heatlhcheck, so this will be caught early.
- """
-
- config = {}
-
- hostname, consul_host, cbs_name = _get_envs()
-
- # not sure how I as the component developer is supposed to know consul port
- consul_url = "http://{0}:8500".format(consul_host)
-
+ # get my config
try:
- # get my config
- cbs_url = _get_uri_from_consul(consul_url, cbs_name)
my_config_endpoint = "{0}/{1}/{2}".format(cbs_url, path, hostname)
res = requests.get(my_config_endpoint)
-
res.raise_for_status()
config = res.json()
- LOGGER.info("get_config returned the following configuration: {0}".format(
- json.dumps(config)))
- except requests.exceptions.HTTPError:
- LOGGER.error("in get_config, the config binding service endpoint %s was not reachable. Error code: %d, Error text: %s", my_config_endpoint, res.status_code, res.text)
- except Exception as exc:
- LOGGER.exception(exc)
- return config
+ logger.debug(
+ "get_config returned the following configuration: %s using the config url %s",
+ json.dumps(config),
+ my_config_endpoint,
+ )
+ return config
+ except requests.exceptions.HTTPError: # this is thrown by raise_for_status
+ logger.error(
+ "The config binding service endpoint %s returned a bad status. code: %d, text: %s",
+ my_config_endpoint,
+ res.status_code,
+ res.text,
+ )
+ raise CantGetConfig(res.status_code, res.text)
+ except requests.exceptions.ConnectionError: # this is thrown if requests.get cant even connect to the endpoint
+ raise CBSUnreachable()
#########
@@ -108,5 +79,8 @@ def get_all():
def get_config():
"""
Hit the CBS service_component endpoint
+
+ 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")
diff --git a/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/exceptions.py b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/exceptions.py
new file mode 100644
index 0000000..6b73b91
--- /dev/null
+++ b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/exceptions.py
@@ -0,0 +1,45 @@
+# ================================================================================
+# Copyright (c) 2019 AT&T Intellectual Property. 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+#
+# ECOMP is a trademark and service mark of AT&T Intellectual Property.
+
+
+class ENVsMissing(Exception):
+ """
+ Exception to represent critical ENVs are missing
+ """
+
+ pass
+
+
+class CantGetConfig(Exception):
+ """
+ Configuration could not be fetched likely due to the config not being in Consul
+ """
+
+ def __init__(self, code, text):
+ self.code = code
+ self.text = text
+
+ pass
+
+
+class CBSUnreachable(Exception):
+ """
+ CBS was not reachable at all
+ """
+
+ pass