Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 1 | import json |
| 2 | from logging.config import dictConfig |
| 3 | |
| 4 | from flask import Flask, Response, jsonify, request |
Michal Kobus | 4104c10 | 2019-02-22 17:05:11 +0100 | [diff] [blame] | 5 | |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 6 | from prometheus_client import make_wsgi_app |
| 7 | |
| 8 | from requests.exceptions import ConnectionError as RequestsConnectionError |
Michal Kobus | fc1e773 | 2019-05-16 18:30:48 +0200 | [diff] [blame] | 9 | |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame] | 10 | from sf_notifier.helpers import alert_fields_and_action, create_file |
| 11 | from sf_notifier.salesforce.client import SESSION_FILE, SalesforceClient |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 12 | |
| 13 | from simple_salesforce.exceptions import SalesforceError |
| 14 | |
| 15 | from simple_settings import settings |
| 16 | |
Michal Kobus | 492d8bc | 2021-02-15 16:54:13 +0100 | [diff] [blame] | 17 | from werkzeug.middleware.dispatcher import DispatcherMiddleware |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 18 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 19 | |
| 20 | dictConfig(settings.LOGGING) |
| 21 | |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 22 | app = Flask(__name__) |
| 23 | app_dispatch = DispatcherMiddleware(app, { |
| 24 | '/metrics': make_wsgi_app() |
| 25 | }) |
| 26 | |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame] | 27 | |
| 28 | create_file(SESSION_FILE) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 29 | sf_cli = SalesforceClient(settings.SF_CONFIG) |
| 30 | |
| 31 | |
Michal Kobus | 819cf02 | 2018-11-29 16:39:22 +0100 | [diff] [blame] | 32 | @app.route('/info', methods=['GET']) |
| 33 | def info(): |
Michal Kobus | 73d3352 | 2018-12-10 11:41:13 +0100 | [diff] [blame] | 34 | return jsonify({ |
| 35 | 'version': settings.VERSION, |
| 36 | 'hashing': sf_cli.hash_func.__name__ |
| 37 | }) |
Michal Kobus | 819cf02 | 2018-11-29 16:39:22 +0100 | [diff] [blame] | 38 | |
| 39 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 40 | @app.route('/hook', methods=['POST']) |
| 41 | def webhook_receiver(): |
| 42 | |
| 43 | try: |
| 44 | data = json.loads(request.data) |
| 45 | except ValueError: |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 46 | msg = 'Invalid request data: {}.'.format(request.data) |
| 47 | app.logger.error(msg) |
| 48 | return Response(json.dumps({'error': msg}), |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 49 | status=400, |
| 50 | mimetype='application/json') |
| 51 | |
| 52 | app.logger.info('Received requests: {}'.format(data)) |
| 53 | |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 54 | cases = [] |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 55 | for alert in data['alerts']: |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 56 | try: |
| 57 | alert['labels']['env_id'] = sf_cli.environment |
Michal Kobus | e7589f7 | 2020-09-11 14:29:37 +0200 | [diff] [blame] | 58 | fields, action = alert_fields_and_action( |
Michal Kobus | 28b37f9 | 2022-06-14 11:06:24 +0200 | [diff] [blame] | 59 | alert, |
| 60 | add_links=settings.ADD_LINKS, |
| 61 | cluster_id=settings.CLUSTER_ID) |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 62 | 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 Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 68 | |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 69 | 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 Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 79 | |
Michal Kobus | aa3accf | 2019-06-05 12:25:09 +0200 | [diff] [blame] | 80 | if len(cases) == 1: |
| 81 | return jsonify(cases[0]) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 82 | return jsonify(cases) |
| 83 | |
| 84 | |
| 85 | if __name__ == '__main__': |
| 86 | app.run() |