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

Implements a GET request for the alerts API

parent cb1b8d59
No related branches found
No related tags found
No related merge requests found
......@@ -76,6 +76,7 @@ def main(global_config, **settings):
# add routes of the Alerts Configuration API
config.add_route('alerts_configuration', '/alerts')
config.add_route('alerts_configuration_instance', '/alerts/{sfc_id}/{sfc_instance_id}')
config.scan() # This method scans the packages and finds any views related to the routes added in the app configuration
config.scan() # this method scans the packages and finds any views related to the routes added in the app configuration
return config.make_wsgi_app()
This diff is collapsed.
......@@ -88,6 +88,49 @@ class AlertsConfigurationAPI(object):
"topic_handlers_api_endpoint": "/kapacitor/v1/alerts/topics/{0}/handlers".format(topic_id)
}
@view_config(route_name='alerts_configuration_instance', request_method='GET')
def get_alerts(self):
"""
The view for retrieving all alerts and Kapacitor resources registered for a specific service function chain instance.
:return: a list of objects, each object representing an alert and containing the policy ID, trigger ID, task ID, topic ID and kapacitor endpoints for these
"""
kapacitor_host, kapacitor_port = self.request.registry.settings['kapacitor_host'], self.request.registry.settings['kapacitor_port']
# fetch the URL parameters
sfc_id, sfc_instance_id = self.request.matchdict["sfc_id"], self.request.matchdict["sfc_instance_id"]
# get all tasks from kapacitor
kapacitor_tasks_url = "http://{0}:{1}/kapacitor/v1/tasks".format(kapacitor_host, kapacitor_port)
all_tasks = get(kapacitor_tasks_url).json()["tasks"]
# fetch all alerts that are configured for this SFC instance
sfc_instance_tasks = []
# traverse every registered task and check if it is configured for this SFC instance
for task in all_tasks:
# get the configured variables of this alert
task_config = task["vars"]
# if configured for this SFC instance
if task_config["sfc"]["value"] == sfc_id and task_config["sfci"]["value"] == sfc_instance_id:
task_id = task["id"]
topic_id = task_id
policy_id = task_config["policy"]["value"]
trigger_id = task_config["eventID"]["value"]
# add it to the list of alerts for this SFC instance
sfc_instance_tasks.append({"policy": policy_id, "trigger": trigger_id,
"task_identifier": task_id, "topic_identifier": topic_id,
"task_api_endpoint": "/kapacitor/v1/tasks/{0}".format(task_id),
"topic_api_endpoint": "/kapacitor/v1/alerts/topics/{0}".format(topic_id),
"topic_handlers_api_endpoint": "/kapacitor/v1/alerts/topics/{0}/handlers".format(topic_id)})
return sfc_instance_tasks
@view_config(route_name='alerts_configuration', request_method='DELETE')
def delete_alerts_specification(self):
"""
......
......@@ -85,6 +85,7 @@ class TestAlerts(object):
"""
Test is implemented using the following steps:
* Send to clmc service a POST request with TOSCA alert spec. and resource spec. files
* Check that the registered alerts can be fetched with a GET request
* Wait 10 seconds for Kapacitor to configure and start executing the defined tasks
* Send some test requests to nginx to increase the load
* Wait 15 seconds for alerts to be triggered
......@@ -107,6 +108,7 @@ class TestAlerts(object):
if clmc_service_host is not None and nginx_host is not None:
break
# create the alerts with a POST request
print("Sending alerts specification to clmc service...")
alerts_spec = join(dirname(__file__), "alerts_test_config.yaml")
resources_spec = join(dirname(__file__), "resources_test_config.yaml")
......@@ -121,8 +123,40 @@ class TestAlerts(object):
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"
sfc, sfc_instance = "MS_Template_1", "MS_Template_1_1"
assert (sfc, sfc_instance) == (clmc_service_response["service_function_chain_id"], clmc_service_response["service_function_chain_instance_id"])
print("Alert spec sent successfully")
# check that the alerts can be fetched with a GET request
print("Validate that the alerts were registered and can be fetched with a GET request.")
response = get("http://{0}/clmc-service/alerts/{1}/{2}".format(clmc_service_host, sfc, sfc_instance))
assert response.status_code == 200
clmc_service_response = response.json()
clmc_service_response = sorted(clmc_service_response, key=lambda x: x["trigger"]) # sort by trigger so that the response can be compared to what's expected
assert clmc_service_response == [
{"policy": "scale_nginx_policy", "trigger": "high_requests", "task_identifier": "46fb8800c8a5eeeb04b090d838d475df574a2e6d854b5d678fc981c096eb6c1b",
"topic_identifier": "46fb8800c8a5eeeb04b090d838d475df574a2e6d854b5d678fc981c096eb6c1b",
"task_api_endpoint": "/kapacitor/v1/tasks/46fb8800c8a5eeeb04b090d838d475df574a2e6d854b5d678fc981c096eb6c1b",
"topic_api_endpoint": "/kapacitor/v1/alerts/topics/46fb8800c8a5eeeb04b090d838d475df574a2e6d854b5d678fc981c096eb6c1b",
"topic_handlers_api_endpoint": "/kapacitor/v1/alerts/topics/46fb8800c8a5eeeb04b090d838d475df574a2e6d854b5d678fc981c096eb6c1b/handlers"},
{"policy": "scale_nginx_policy", "trigger": "increase_in_active_requests", "task_identifier": "7a9867f9270dba6635ac3760a3b70bc929f5bd0f3bf582e45d27fbd437f528ca",
"topic_identifier": "7a9867f9270dba6635ac3760a3b70bc929f5bd0f3bf582e45d27fbd437f528ca",
"task_api_endpoint": "/kapacitor/v1/tasks/7a9867f9270dba6635ac3760a3b70bc929f5bd0f3bf582e45d27fbd437f528ca",
"topic_api_endpoint": "/kapacitor/v1/alerts/topics/7a9867f9270dba6635ac3760a3b70bc929f5bd0f3bf582e45d27fbd437f528ca",
"topic_handlers_api_endpoint": "/kapacitor/v1/alerts/topics/7a9867f9270dba6635ac3760a3b70bc929f5bd0f3bf582e45d27fbd437f528ca/handlers"},
{"policy": "scale_nginx_policy", "trigger": "increase_in_running_processes", "task_identifier": "f5edaeb27fb847116be749c3815d240cbf0d7ba79aee1959daf0b3445a70f2c8",
"topic_identifier": "f5edaeb27fb847116be749c3815d240cbf0d7ba79aee1959daf0b3445a70f2c8",
"task_api_endpoint": "/kapacitor/v1/tasks/f5edaeb27fb847116be749c3815d240cbf0d7ba79aee1959daf0b3445a70f2c8",
"topic_api_endpoint": "/kapacitor/v1/alerts/topics/f5edaeb27fb847116be749c3815d240cbf0d7ba79aee1959daf0b3445a70f2c8",
"topic_handlers_api_endpoint": "/kapacitor/v1/alerts/topics/f5edaeb27fb847116be749c3815d240cbf0d7ba79aee1959daf0b3445a70f2c8/handlers"},
{"policy": "deadman_policy", "trigger": "no_measurements", "task_identifier": "f7dab6fd53001c812d44533d3bbb6ef45f0d1d39b9441bc3c60402ebda85d320",
"topic_identifier": "f7dab6fd53001c812d44533d3bbb6ef45f0d1d39b9441bc3c60402ebda85d320",
"task_api_endpoint": "/kapacitor/v1/tasks/f7dab6fd53001c812d44533d3bbb6ef45f0d1d39b9441bc3c60402ebda85d320",
"topic_api_endpoint": "/kapacitor/v1/alerts/topics/f7dab6fd53001c812d44533d3bbb6ef45f0d1d39b9441bc3c60402ebda85d320",
"topic_handlers_api_endpoint": "/kapacitor/v1/alerts/topics/f7dab6fd53001c812d44533d3bbb6ef45f0d1d39b9441bc3c60402ebda85d320/handlers"}
], "Incorrect response for GET alerts request"
print("Alert spec validated successfully")
print("Wait 10 seconds for Kapacitor stream/batch tasks to start working...")
sleep(10)
......@@ -138,6 +172,7 @@ class TestAlerts(object):
alert_logs = listdir(LOG_TEST_FOLDER_PATH)
assert len(alert_logs) == 4, "4 log files must have been created - one for each alert defined in the specification."
# check the content of eac log file
for alert_log in alert_logs:
alert_log_path = join(LOG_TEST_FOLDER_PATH, alert_log)
......@@ -152,6 +187,7 @@ class TestAlerts(object):
assert valid, "Alert log content is invalid - {0}".format(alert_log_path)
# delete the alerts with a DELETE request
with open(alerts_spec, 'rb') as alerts:
files = {'alert-spec': alerts}
response = delete("http://{0}/clmc-service/alerts".format(clmc_service_host), files=files)
......
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