Skip to content
Snippets Groups Projects
Commit 2229ad67 authored by Nikolay Stanchev's avatar Nikolay Stanchev
Browse files

Updates alerts API endpoint to provide error information from kapacitor

parent 8c229534
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment