From 2229ad67e3513aeaf7bb544159543a8237c9af6b Mon Sep 17 00:00:00 2001 From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk> Date: Fri, 24 Aug 2018 15:50:04 +0100 Subject: [PATCH] Updates alerts API endpoint to provide error information from kapacitor --- src/service/clmcservice/alertsapi/tests.py | 13 +++++++++ src/service/clmcservice/alertsapi/views.py | 31 ++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/service/clmcservice/alertsapi/tests.py b/src/service/clmcservice/alertsapi/tests.py index b04601f..f92af20 100644 --- a/src/service/clmcservice/alertsapi/tests.py +++ b/src/service/clmcservice/alertsapi/tests.py @@ -164,6 +164,8 @@ class TestAlertsConfigurationAPI(object): assert (sfc, sfc_instance) == (clmc_service_response["service_function_chain_id"], clmc_service_response["service_function_chain_instance_id"]), \ "Incorrect extraction of metadata for file {0}". format(test_file_path) + assert "triggers_specification_errors" not in clmc_service_response, "Unexpected error was returned for triggers specification" + assert "triggers_action_errors" not in clmc_service_response, "Unexpected error was returned for handlers specification" # traverse through all alert IDs and check that they are created within Kapacitor for alert_id in alert_ids: @@ -192,6 +194,17 @@ class TestAlertsConfigurationAPI(object): assert kapacitor_response_json["kind"] == "post", "Incorrect kind of handler {0} in the Kapacitor response - test file {1}".format(handler_id, test_file_path) assert kapacitor_response_json["options"]["url"], "Incorrect url of handler {0} in the Kapacitor response - test file {1}".format(handler_id, test_file_path) + # send the same spec again to check that error messages are returned (because of ID duplication) + with open(alert_spec_abs_path) as alert_spec: + request.POST['alert-spec'] = FieldStorageMock(test_file_path, alert_spec) # a simple mock class is used to mimic the FieldStorage class + clmc_service_response = AlertsConfigurationAPI(request).post_alerts_specification() + assert (sfc, sfc_instance) == (clmc_service_response["service_function_chain_id"], clmc_service_response["service_function_chain_instance_id"]), \ + "Incorrect extraction of metadata for file {0}". format(test_file_path) + + assert len(clmc_service_response["triggers_specification_errors"]) == len(alert_ids), "Expected errors were not returned for triggers specification" + handlers_count = sum([len(topic_handlers[topic]) for topic in topic_handlers]) + assert len(clmc_service_response["triggers_action_errors"]) == handlers_count, "Expected errors were not returned for handlers specification" + clear_kapacitor_alerts(alert_ids, topic_handlers) diff --git a/src/service/clmcservice/alertsapi/views.py b/src/service/clmcservice/alertsapi/views.py index 00be03f..1ce908a 100644 --- a/src/service/clmcservice/alertsapi/views.py +++ b/src/service/clmcservice/alertsapi/views.py @@ -96,6 +96,9 @@ class AlertsConfigurationAPI(object): sfc, sfc_instance = tosca_tpl.tpl["metadata"]["sfc"], tosca_tpl.tpl["metadata"]["sfci"] db = sfc # ASSUMPTION: database per service function chain, named after the service function chain ID + alert_tasks_errors = [] + alert_handlers_errors = [] + for policy in tosca_tpl.policies: for trigger in policy.triggers: event_id = trigger.name @@ -148,6 +151,14 @@ class AlertsConfigurationAPI(object): # log the response log.info(response_content, response.status_code) + # track all reported errors + if response_content.get("error", "") != "": + alert_tasks_errors.append({ + "policy": policy.name, + "trigger": event_id, + "error": response_content.get("error") + }) + # exttranc http handlers http_handlers = trigger.trigger_tpl["action"]["implementation"] @@ -161,5 +172,21 @@ class AlertsConfigurationAPI(object): response_content = response.json() log.info(response_content, response.status_code) - return {"msg": "Alerts specification has been successfully validated and configured", "service_function_chain_id": sfc, - "service_function_chain_instance_id": sfc_instance} + if response_content.get("error", "") != "": + alert_handlers_errors.append({ + "policy": policy.name, + "trigger": event_id, + "handler": http_handler_url, + "error": response_content.get("error") + }) + + return_msg = {"msg": "Alerts specification has been successfully validated and forwarded to Kapacitor", "service_function_chain_id": sfc, + "service_function_chain_instance_id": sfc_instance} + + if len(alert_tasks_errors) > 0: + return_msg["triggers_specification_errors"] = alert_tasks_errors + + if len(alert_handlers_errors) > 0: + return_msg["triggers_action_errors"] = alert_handlers_errors + + return return_msg -- GitLab