diff options
author | 2017-08-23 11:21:44 -0400 | |
---|---|---|
committer | 2017-08-23 12:32:45 -0400 | |
commit | 81b9ed7a3af1032caa86f316a21e9b2912bb1271 (patch) | |
tree | fe3bb349f501619d3b4df4d4dbb07b9d72adc824 /python-cbs-docker-client/cbs_docker_client | |
parent | 280af47e5342de6bb4657ad7b85adcc9395ace20 (diff) |
Intiial utils push to ONAP
Issue-Id: DCAEGEN2-80
Change-Id: I1dc8f2d384b0af346fccb86e6767b7e3ca484078
Signed-off-by: Tommy Carpenter <tommy@research.att.com>
Diffstat (limited to 'python-cbs-docker-client/cbs_docker_client')
-rw-r--r-- | python-cbs-docker-client/cbs_docker_client/__init__.py | 19 | ||||
-rw-r--r-- | python-cbs-docker-client/cbs_docker_client/client.py | 85 |
2 files changed, 104 insertions, 0 deletions
diff --git a/python-cbs-docker-client/cbs_docker_client/__init__.py b/python-cbs-docker-client/cbs_docker_client/__init__.py new file mode 100644 index 0000000..9e81f65 --- /dev/null +++ b/python-cbs-docker-client/cbs_docker_client/__init__.py @@ -0,0 +1,19 @@ +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 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. + diff --git a/python-cbs-docker-client/cbs_docker_client/client.py b/python-cbs-docker-client/cbs_docker_client/client.py new file mode 100644 index 0000000..4423995 --- /dev/null +++ b/python-cbs-docker-client/cbs_docker_client/client.py @@ -0,0 +1,85 @@ +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 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. + +import json +import requests +import os +import logging + +root = logging.getLogger() +logger = root.getChild(__name__) + +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) + try: + res.raise_for_status() + services = res.json() + return "http://{0}:{1}".format(services[0]["ServiceAddress"], services[0]["ServicePort"]) + except Exception as e: + logger.error("Exception occured when querying Consul: either could not hit {0} or no service registered. Error code: {1}, Error Text: {2}".format(url, res.status_code, res.text)) + return None + +def _get_envs(): + """ + Returns HOSTNAME, CONSUL_HOST, CONFIG_BINDING_SERVICE or crashes for caller to deal with + """ + HOSTNAME = os.environ["HOSTNAME"] + CONSUL_HOST = os.environ["CONSUL_HOST"] + return HOSTNAME, CONSUL_HOST + +#Public +def get_config(): + """ + 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 = _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 the CBS URL. Would not need the following hoorahrah if we had DNS. + cbs_url = _get_uri_from_consul(consul_url, "config_binding_service") + if cbs_url is None: + logger.error("Cannot bind config at this time, cbs is unreachable") + else: + #get my config + my_config_endpoint = "{0}/service_component/{1}".format(cbs_url, HOSTNAME) + res = requests.get(my_config_endpoint) + try: + res.raise_for_status() + config = res.json() + logger.info("get_config returned the following configuration: {0}".format(json.dumps(config))) + except: + logger.error("in get_config, the config binding service endpoint {0} blew up on me. Error code: {1}, Error text: {2}".format(my_config_endpoint, res.status_code, res.text)) + return config + |