aboutsummaryrefslogtreecommitdiffstats
path: root/config_binding_service/controller.py
diff options
context:
space:
mode:
authorTommy Carpenter <tommy@research.att.com>2018-03-09 10:41:05 -0500
committerTommy Carpenter <tommy@research.att.com>2018-03-11 11:14:57 -0400
commit226719d5b23c7b82940a0e4c5b6922eead419788 (patch)
treef1bb5016e6842a4c2cf78231e6ca53d3c41b8c24 /config_binding_service/controller.py
parent914d8ccb42befc04ec5b759dd84b242591bb5e91 (diff)
Implement logging that can get to ELK
This adds more logging into the front end, and also logs to a file so ELK can get them. All public calls to the CBS are logged with the return code. This is not to say logging is completed in the CBS, more may come later and under a more structured format. Change-Id: I63c966876972e06c51276eaf1204b5b9b9e6cea7 Issue-ID: DCAEGEN2-387 Signed-off-by: Tommy Carpenter <tommy@research.att.com>
Diffstat (limited to 'config_binding_service/controller.py')
-rw-r--r--config_binding_service/controller.py95
1 files changed, 46 insertions, 49 deletions
diff --git a/config_binding_service/controller.py b/config_binding_service/controller.py
index 7bb5f51..68ac1ac 100644
--- a/config_binding_service/controller.py
+++ b/config_binding_service/controller.py
@@ -16,71 +16,68 @@
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
+import json
import requests
from flask import Response
-import json
-from config_binding_service import client, get_consul_uri, get_logger
+from config_binding_service import client, get_consul_uri
+from config_binding_service.logging import LOGGER
+
-_logger = get_logger(__name__)
+def _get_helper(json_expecting_func, **kwargs):
+ """
+ Helper function used by several functions below
+ """
+ print(kwargs)
+ try:
+ payload = json_expecting_func(**kwargs)
+ response, status_code, mimetype = json.dumps(payload), 200, "application/json"
+ except client.BadRequest as exc:
+ response, status_code, mimetype = exc.response, exc.code, "text/plain"
+ except client.CantGetConfig as exc:
+ response, status_code, mimetype = exc.response, exc.code, "text/plain"
+ except Exception as exc:
+ LOGGER.error(exc)
+ response, status_code, mimetype = "Unknown error, please report", 500, "text/plain"
+ return response, status_code, mimetype
def bind_all(service_component_name):
- try:
- allk = client.resolve_all(service_component_name)
- return Response(response=json.dumps(allk),
- status=200,
- mimetype="application/json")
- except client.CantGetConfig as e:
- return Response(status=e.code,
- response=e.response)
- except Exception as e:
- _logger.error(e)
- return Response(response="Unknown error: please report",
- status=500)
+ """
+ Get all the keys in Consul for this SCN, and bind the config
+ """
+ response, status_code, mimetype = _get_helper(client.resolve_all, service_component_name=service_component_name)
+ LOGGER.info("bind_all called for %s, returned code %d", service_component_name, status_code)
+ return Response(response=response, status=status_code, mimetype=mimetype)
def bind_config_for_scn(service_component_name):
- try:
- bound = client.resolve(service_component_name)
- return Response(response=json.dumps(bound),
- status=200,
- mimetype="application/json")
- except client.CantGetConfig as e:
- return Response(status=e.code,
- response=e.response)
- except Exception as e: # should never happen...
- _logger.error(e)
- return Response(response="Please report this error",
- status=500)
+ """
+ Bind just the config for this SCN
+ """
+ response, status_code, mimetype = _get_helper(client.resolve, service_component_name=service_component_name)
+ LOGGER.info("bind_config_for_scn called for %s, returned code %d", service_component_name, status_code)
+ return Response(response=response, status=status_code, mimetype=mimetype)
def get_key(key, service_component_name):
- try:
- bound = client.get_key(key, service_component_name)
- return Response(response=json.dumps(bound),
- status=200,
- mimetype="application/json")
- except client.CantGetConfig as e:
- return Response(status=e.code,
- response=e.response)
- except client.BadRequest as exc:
- return Response(status=exc.code,
- response=exc.response,
- mimetype="text/plain")
- except Exception as e: # should never happen...
- _logger.error(e)
- return Response(response="Please report this error",
- status=500)
+ """
+ Get a single key k of the form service_component_name:k from Consul.
+ Should not be used and will return a BAD REQUEST for k=policies because it's a complex object
+ """
+ response, status_code, mimetype = _get_helper(client.get_key, key=key, service_component_name=service_component_name)
+ LOGGER.info("get_key called for %s, returned code %d", service_component_name, status_code)
+ return Response(response=response, status=status_code, mimetype=mimetype)
def healthcheck():
- # got this far, I must be alive... check my connection to Consul by checking myself
- CONSUL = get_consul_uri()
+ """
+ CBS Healthcheck
+ """
+ LOGGER.info("healthcheck called")
res = requests.get(
- "{0}/v1/catalog/service/config_binding_service".format(CONSUL))
+ "{0}/v1/catalog/service/config_binding_service".format(get_consul_uri()))
if res.status_code == 200:
return Response(response="CBS is alive and Consul connection OK",
status=200)
- else:
- return Response(response="CBS is alive but cannot reach Consul",
- status=503)
+ return Response(response="CBS is alive but cannot reach Consul",
+ status=503)