blob: 37029926c25f2759e15adb6a84ee8868557c8c32 [file] [log] [blame]
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +01001import json
2from logging.config import dictConfig
3
4from flask import Flask, Response, jsonify, request
Michal Kobus4104c102019-02-22 17:05:11 +01005
Michal Kobusaa3accf2019-06-05 12:25:09 +02006from prometheus_client import make_wsgi_app
7
8from requests.exceptions import ConnectionError as RequestsConnectionError
Michal Kobusfc1e7732019-05-16 18:30:48 +02009
Michal Kobus211ee922019-04-15 17:44:06 +020010from sf_notifier.helpers import alert_fields_and_action, create_file
11from sf_notifier.salesforce.client import SESSION_FILE, SalesforceClient
Michal Kobusaa3accf2019-06-05 12:25:09 +020012
13from simple_salesforce.exceptions import SalesforceError
14
15from simple_settings import settings
16
Michal Kobus492d8bc2021-02-15 16:54:13 +010017from werkzeug.middleware.dispatcher import DispatcherMiddleware
Michal Kobusafbf4d02018-11-28 14:18:05 +010018
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010019
20dictConfig(settings.LOGGING)
21
Michal Kobusafbf4d02018-11-28 14:18:05 +010022app = Flask(__name__)
23app_dispatch = DispatcherMiddleware(app, {
24 '/metrics': make_wsgi_app()
25})
26
Michal Kobus211ee922019-04-15 17:44:06 +020027
28create_file(SESSION_FILE)
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010029sf_cli = SalesforceClient(settings.SF_CONFIG)
30
31
Michal Kobus819cf022018-11-29 16:39:22 +010032@app.route('/info', methods=['GET'])
33def info():
Michal Kobus73d33522018-12-10 11:41:13 +010034 return jsonify({
35 'version': settings.VERSION,
36 'hashing': sf_cli.hash_func.__name__
37 })
Michal Kobus819cf022018-11-29 16:39:22 +010038
39
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010040@app.route('/hook', methods=['POST'])
41def webhook_receiver():
42
43 try:
44 data = json.loads(request.data)
45 except ValueError:
Michal Kobus17726ae2018-11-27 12:59:55 +010046 msg = 'Invalid request data: {}.'.format(request.data)
47 app.logger.error(msg)
48 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010049 status=400,
50 mimetype='application/json')
51
52 app.logger.info('Received requests: {}'.format(data))
53
Michal Kobusaa3accf2019-06-05 12:25:09 +020054 cases = []
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010055 for alert in data['alerts']:
Michal Kobusaa3accf2019-06-05 12:25:09 +020056 try:
57 alert['labels']['env_id'] = sf_cli.environment
Michal Kobuse7589f72020-09-11 14:29:37 +020058 fields, action = alert_fields_and_action(
Michal Kobus28b37f92022-06-14 11:06:24 +020059 alert,
60 add_links=settings.ADD_LINKS,
61 cluster_id=settings.CLUSTER_ID)
Michal Kobusaa3accf2019-06-05 12:25:09 +020062 except KeyError as key:
63 msg = 'Alert misses {} key.'.format(key)
64 app.logger.error(msg)
65 return Response(json.dumps({'error': msg}),
66 status=400,
67 mimetype='application/json')
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010068
Michal Kobusaa3accf2019-06-05 12:25:09 +020069 if fields:
70 try:
71 cases.append(getattr(sf_cli, action)(*fields))
72 except (SalesforceError, RequestsConnectionError) as err:
73 msg = 'Salesforce request failure: {}.'.format(err)
74 sf_cli.metrics['sf_error_count'].inc()
75 app.logger.error(msg)
76 return Response(json.dumps({'error': msg}),
77 status=500,
78 mimetype='application/json')
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010079
Michal Kobusaa3accf2019-06-05 12:25:09 +020080 if len(cases) == 1:
81 return jsonify(cases[0])
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010082 return jsonify(cases)
83
84
85if __name__ == '__main__':
86 app.run()