diff options
Diffstat (limited to 'policyhandler/deploy_handler.py')
-rw-r--r-- | policyhandler/deploy_handler.py | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/policyhandler/deploy_handler.py b/policyhandler/deploy_handler.py index 5792631..0950dfd 100644 --- a/policyhandler/deploy_handler.py +++ b/policyhandler/deploy_handler.py @@ -18,13 +18,15 @@ """ send notification to deploy-handler""" -import logging import json +import logging + import requests from .config import Config from .discovery import DiscoveryClient from .onap.audit import REQUEST_X_ECOMP_REQUESTID, Audit, AuditHttpCode +from .customize import CustomizerUser POOL_SIZE = 1 @@ -38,14 +40,21 @@ class DeployHandler(object): _url = None _url_path = None _target_entity = None + _custom_kwargs = None @staticmethod - def _lazy_init(): + def _lazy_init(audit): """ set static properties """ if DeployHandler._lazy_inited: return DeployHandler._lazy_inited = True + DeployHandler._custom_kwargs = CustomizerUser.get_customizer() \ + .get_deploy_handler_kwargs(audit) + if not DeployHandler._custom_kwargs \ + or not isinstance(DeployHandler._custom_kwargs, dict): + DeployHandler._custom_kwargs = {} + DeployHandler._requests_session = requests.Session() DeployHandler._requests_session.mount( 'https://', @@ -56,8 +65,8 @@ class DeployHandler(object): requests.adapters.HTTPAdapter(pool_connections=POOL_SIZE, pool_maxsize=POOL_SIZE) ) - DeployHandler._target_entity = Config.config["deploy_handler"] - DeployHandler._url = DiscoveryClient.get_service_url(DeployHandler._target_entity) + DeployHandler._target_entity = Config.config.get("deploy_handler", "deploy_handler") + DeployHandler._url = DiscoveryClient.get_service_url(audit, DeployHandler._target_entity) DeployHandler._url_path = (DeployHandler._url or "") + '/policy' DeployHandler._logger.info("DeployHandler url(%s)", DeployHandler._url) @@ -67,7 +76,7 @@ class DeployHandler(object): if not message: return - DeployHandler._lazy_init() + DeployHandler._lazy_init(audit) sub_aud = Audit(aud_parent=audit, targetEntity=DeployHandler._target_entity, targetServiceName=DeployHandler._url_path) headers = {REQUEST_X_ECOMP_REQUESTID : sub_aud.request_id} @@ -75,10 +84,10 @@ class DeployHandler(object): msg_str = json.dumps(message) headers_str = json.dumps(headers) - DeployHandler._logger.info("message: %s", msg_str) - log_line = "post to deployment-handler {0} msg={1} headers={2}".format( - DeployHandler._url_path, msg_str, headers_str) - + log_action = "post to {0} at {1}".format( + DeployHandler._target_entity, DeployHandler._url_path) + log_data = " msg={0} headers={1}".format(msg_str, headers_str) + log_line = log_action + log_data DeployHandler._logger.info(log_line) sub_aud.metrics_start(log_line) @@ -93,11 +102,11 @@ class DeployHandler(object): res = None try: res = DeployHandler._requests_session.post( - DeployHandler._url_path, json=message, headers=headers + DeployHandler._url_path, json=message, headers=headers, + **DeployHandler._custom_kwargs ) except requests.exceptions.RequestException as ex: - error_msg = "failed to post to deployment-handler {0} {1} msg={2} headers={3}" \ - .format(DeployHandler._url_path, str(ex), msg_str, headers_str) + error_msg = "failed to {0}: {1}{2}".format(log_action, str(ex), log_data) DeployHandler._logger.exception(error_msg) sub_aud.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) audit.set_http_status_code(AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value) @@ -107,10 +116,8 @@ class DeployHandler(object): sub_aud.set_http_status_code(res.status_code) audit.set_http_status_code(res.status_code) - sub_aud.metrics( - "response from deployment-handler to post {0}: {1} msg={2} text={3} headers={4}" \ - .format(DeployHandler._url_path, res.status_code, msg_str, res.text, - res.request.headers)) + sub_aud.metrics("response {0} from {1}: text={2}{3}" \ + .format(res.status_code, log_action, res.text, log_data)) if res.status_code == requests.codes.ok: return res.json() |