Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 1 | # Copyright 2018: Mirantis Inc. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | import json |
| 17 | from logging.config import dictConfig |
| 18 | |
| 19 | from flask import Flask, Response, jsonify, request |
| 20 | |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 21 | from prometheus_client import make_wsgi_app |
| 22 | |
Michal Kobus | 4104c10 | 2019-02-22 17:05:11 +0100 | [diff] [blame] | 23 | from requests.exceptions import ConnectionError as RequestsConnectionError |
| 24 | |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame^] | 25 | from sf_notifier.helpers import alert_fields_and_action, create_file |
| 26 | from sf_notifier.salesforce.client import SESSION_FILE, SalesforceClient |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 27 | |
Michal Kobus | aacc4a5 | 2019-01-18 12:21:10 +0100 | [diff] [blame] | 28 | from simple_salesforce.exceptions import SalesforceError |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 29 | |
| 30 | from simple_settings import settings |
| 31 | |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 32 | from werkzeug.wsgi import DispatcherMiddleware |
| 33 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 34 | |
| 35 | dictConfig(settings.LOGGING) |
| 36 | |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 37 | app = Flask(__name__) |
| 38 | app_dispatch = DispatcherMiddleware(app, { |
| 39 | '/metrics': make_wsgi_app() |
| 40 | }) |
| 41 | |
Michal Kobus | 211ee92 | 2019-04-15 17:44:06 +0200 | [diff] [blame^] | 42 | |
| 43 | create_file(SESSION_FILE) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 44 | sf_cli = SalesforceClient(settings.SF_CONFIG) |
| 45 | |
| 46 | |
Michal Kobus | 819cf02 | 2018-11-29 16:39:22 +0100 | [diff] [blame] | 47 | @app.route('/info', methods=['GET']) |
| 48 | def info(): |
Michal Kobus | 73d3352 | 2018-12-10 11:41:13 +0100 | [diff] [blame] | 49 | return jsonify({ |
| 50 | 'version': settings.VERSION, |
| 51 | 'hashing': sf_cli.hash_func.__name__ |
| 52 | }) |
Michal Kobus | 819cf02 | 2018-11-29 16:39:22 +0100 | [diff] [blame] | 53 | |
| 54 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 55 | @app.route('/hook', methods=['POST']) |
| 56 | def webhook_receiver(): |
| 57 | |
| 58 | try: |
| 59 | data = json.loads(request.data) |
| 60 | except ValueError: |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 61 | msg = 'Invalid request data: {}.'.format(request.data) |
| 62 | app.logger.error(msg) |
| 63 | return Response(json.dumps({'error': msg}), |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 64 | status=400, |
| 65 | mimetype='application/json') |
| 66 | |
| 67 | app.logger.info('Received requests: {}'.format(data)) |
| 68 | |
| 69 | cases = [] |
| 70 | for alert in data['alerts']: |
| 71 | try: |
Michal Kobus | 27457d4 | 2019-02-13 14:06:11 +0100 | [diff] [blame] | 72 | alert['labels']['env_id'] = sf_cli.environment |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 73 | fields, action = alert_fields_and_action(alert) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 74 | except KeyError as key: |
| 75 | msg = 'Alert misses {} key.'.format(key) |
| 76 | app.logger.error(msg) |
| 77 | return Response(json.dumps({'error': msg}), |
| 78 | status=400, |
| 79 | mimetype='application/json') |
| 80 | |
| 81 | if fields: |
| 82 | try: |
| 83 | cases.append(getattr(sf_cli, action)(*fields)) |
Michal Kobus | 4104c10 | 2019-02-22 17:05:11 +0100 | [diff] [blame] | 84 | except (SalesforceError, RequestsConnectionError) as err: |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 85 | msg = 'Salesforce request failure: {}.'.format(err) |
| 86 | sf_cli.metrics['sf_error_count'].inc() |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 87 | app.logger.error(msg) |
| 88 | return Response(json.dumps({'error': msg}), |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 89 | status=500, |
| 90 | mimetype='application/json') |
| 91 | |
| 92 | if len(cases) == 1: |
| 93 | return jsonify(cases[0]) |
| 94 | return jsonify(cases) |
| 95 | |
| 96 | |
| 97 | if __name__ == '__main__': |
| 98 | app.run() |