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 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 23 | from sf_notifier.helpers import alert_fields_and_action |
| 24 | from sf_notifier.salesforce.client import SalesforceClient |
| 25 | |
| 26 | from simple_salesforce.exceptions import SalesforceMalformedRequest |
| 27 | |
| 28 | from simple_settings import settings |
| 29 | |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame^] | 30 | from werkzeug.wsgi import DispatcherMiddleware |
| 31 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 32 | |
| 33 | dictConfig(settings.LOGGING) |
| 34 | |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame^] | 35 | app = Flask(__name__) |
| 36 | app_dispatch = DispatcherMiddleware(app, { |
| 37 | '/metrics': make_wsgi_app() |
| 38 | }) |
| 39 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 40 | sf_cli = SalesforceClient(settings.SF_CONFIG) |
| 41 | |
| 42 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 43 | @app.route('/hook', methods=['POST']) |
| 44 | def webhook_receiver(): |
| 45 | |
| 46 | try: |
| 47 | data = json.loads(request.data) |
| 48 | except ValueError: |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 49 | msg = 'Invalid request data: {}.'.format(request.data) |
| 50 | app.logger.error(msg) |
| 51 | return Response(json.dumps({'error': msg}), |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 52 | status=400, |
| 53 | mimetype='application/json') |
| 54 | |
| 55 | app.logger.info('Received requests: {}'.format(data)) |
| 56 | |
| 57 | cases = [] |
| 58 | for alert in data['alerts']: |
| 59 | try: |
| 60 | fields, action = alert_fields_and_action(alert) |
| 61 | except KeyError as key: |
| 62 | msg = 'Alert misses {} key.'.format(key) |
| 63 | app.logger.error(msg) |
| 64 | return Response(json.dumps({'error': msg}), |
| 65 | status=400, |
| 66 | mimetype='application/json') |
| 67 | |
| 68 | if fields: |
| 69 | try: |
| 70 | cases.append(getattr(sf_cli, action)(*fields)) |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame^] | 71 | except SalesforceMalformedRequest as err: |
| 72 | msg = 'Salesforce request failure: {}.'.format(err) |
| 73 | sf_cli.metrics['sf_error_count'].inc() |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 74 | app.logger.error(msg) |
| 75 | return Response(json.dumps({'error': msg}), |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 76 | status=500, |
| 77 | mimetype='application/json') |
| 78 | |
| 79 | if len(cases) == 1: |
| 80 | return jsonify(cases[0]) |
| 81 | return jsonify(cases) |
| 82 | |
| 83 | |
| 84 | if __name__ == '__main__': |
| 85 | app.run() |