aboutsummaryrefslogtreecommitdiffstats
path: root/policyhandler/discovery.py
diff options
context:
space:
mode:
Diffstat (limited to 'policyhandler/discovery.py')
-rw-r--r--policyhandler/discovery.py43
1 files changed, 31 insertions, 12 deletions
diff --git a/policyhandler/discovery.py b/policyhandler/discovery.py
index a7e3c56..ce24c3d 100644
--- a/policyhandler/discovery.py
+++ b/policyhandler/discovery.py
@@ -18,11 +18,14 @@
"""client to talk to consul at the standard port 8500"""
-import logging
-import json
import base64
+import json
+import logging
+
import requests
+from .customize import CustomizerUser
+
class DiscoveryClient(object):
"""talking to consul at http://consul:8500
@@ -39,27 +42,43 @@ 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}"
- SERVICE_ADDRESS = "ServiceAddress"
- SERVICE_PORT = "ServicePort"
_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()
if not service:
- DiscoveryClient._logger.error("failed discover %s", service_path)
+ log_line = "failed discover {0}".format(service_path)
+ DiscoveryClient._logger.error(log_line)
+ audit.error(log_line)
return
service = service[0]
- return DiscoveryClient.SERVICE_MASK.format(
- service[DiscoveryClient.SERVICE_ADDRESS], service[DiscoveryClient.SERVICE_PORT]
- )
+
+ 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):