diff options
Diffstat (limited to 'policyhandler/discovery.py')
-rw-r--r-- | policyhandler/discovery.py | 68 |
1 files changed, 45 insertions, 23 deletions
diff --git a/policyhandler/discovery.py b/policyhandler/discovery.py index 7e16b90..ce24c3d 100644 --- a/policyhandler/discovery.py +++ b/policyhandler/discovery.py @@ -1,8 +1,5 @@ -"""client to talk to consul at the standard port 8500""" - -# org.onap.dcae # ================================================================================ -# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2018 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. @@ -19,11 +16,16 @@ # # ECOMP is a trademark and service mark of AT&T Intellectual Property. -import logging -import json +"""client to talk to consul at the standard port 8500""" + import base64 +import json +import logging + import requests +from .customize import CustomizerUser + class DiscoveryClient(object): """talking to consul at http://consul:8500 @@ -40,34 +42,54 @@ class DiscoveryClient(object): """ CONSUL_SERVICE_MASK = "http://consul:8500/v1/catalog/service/{0}" CONSUL_KV_MASK = "http://consul:8500/v1/kv/{0}" - SERVICE_MASK = "http://{0}:{1}" _logger = logging.getLogger("policy_handler.discovery") - @staticmethod - def get_service_url(service_name): + def get_service_url(audit, service_name): """find the service record in consul""" service_path = DiscoveryClient.CONSUL_SERVICE_MASK.format(service_name) - DiscoveryClient._logger.info("discover %s", service_path) + log_line = "discover {0}".format(service_path) + DiscoveryClient._logger.info(log_line) + audit.info(log_line) response = requests.get(service_path) + + log_line = "response {0} for {1}: {2}".format( + response.status_code, service_path, response.text) + DiscoveryClient._logger.info(log_line) + audit.info(log_line) + response.raise_for_status() - service = response.json()[0] - return DiscoveryClient.SERVICE_MASK.format( \ - service["ServiceAddress"], service["ServicePort"]) + + service = response.json() + if not service: + log_line = "failed discover {0}".format(service_path) + DiscoveryClient._logger.error(log_line) + audit.error(log_line) + return + service = service[0] + + service_url = CustomizerUser.get_customizer().get_service_url(audit, service_name, service) + if not service_url: + log_line = "failed to get service_url for {0}".format(service_name) + DiscoveryClient._logger.error(log_line) + audit.error(log_line) + return + + log_line = "got service_url: {0} for {1}".format(service_url, service_name) + DiscoveryClient._logger.info(log_line) + audit.info(log_line) + return service_url @staticmethod def get_value(key): """get the value for the key from consul-kv""" response = requests.get(DiscoveryClient.CONSUL_KV_MASK.format(key)) response.raise_for_status() - data = response.json()[0] - value = base64.b64decode(data["Value"]).decode("utf-8") - DiscoveryClient._logger.info("consul-kv key=%s data=%s value(%s)", \ - key, json.dumps(data), value) + data = response.json() + if not data: + DiscoveryClient._logger.error("failed get_value %s", key) + return + value = base64.b64decode(data[0]["Value"]).decode("utf-8") + DiscoveryClient._logger.info("consul-kv key=%s value(%s) data=%s", + key, value, json.dumps(data)) return json.loads(value) - - @staticmethod - def put_kv(key, value): - """put the value under the key in consul-kv""" - response = requests.put(DiscoveryClient.CONSUL_KV_MASK.format(key), data=value) - response.raise_for_status() |