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 | |
Michal Kobus | aacc4a5 | 2019-01-18 12:21:10 +0100 | [diff] [blame] | 26 | from simple_salesforce.exceptions import SalesforceError |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 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 | |
Michal Kobus | 819cf02 | 2018-11-29 16:39:22 +0100 | [diff] [blame] | 43 | @app.route('/info', methods=['GET']) |
| 44 | def info(): |
Michal Kobus | 73d3352 | 2018-12-10 11:41:13 +0100 | [diff] [blame] | 45 | return jsonify({ |
| 46 | 'version': settings.VERSION, |
| 47 | 'hashing': sf_cli.hash_func.__name__ |
| 48 | }) |
Michal Kobus | 819cf02 | 2018-11-29 16:39:22 +0100 | [diff] [blame] | 49 | |
| 50 | |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 51 | @app.route('/hook', methods=['POST']) |
| 52 | def webhook_receiver(): |
| 53 | |
| 54 | try: |
| 55 | data = json.loads(request.data) |
| 56 | except ValueError: |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 57 | msg = 'Invalid request data: {}.'.format(request.data) |
| 58 | app.logger.error(msg) |
| 59 | return Response(json.dumps({'error': msg}), |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 60 | status=400, |
| 61 | mimetype='application/json') |
| 62 | |
| 63 | app.logger.info('Received requests: {}'.format(data)) |
| 64 | |
| 65 | cases = [] |
| 66 | for alert in data['alerts']: |
| 67 | try: |
Michal Kobus | 27457d4 | 2019-02-13 14:06:11 +0100 | [diff] [blame^] | 68 | alert['labels']['env_id'] = sf_cli.environment |
Michal Kobus | 23ba2f7 | 2018-12-03 17:11:20 +0100 | [diff] [blame] | 69 | fields, action = alert_fields_and_action(alert) |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 70 | except KeyError as key: |
| 71 | msg = 'Alert misses {} key.'.format(key) |
| 72 | app.logger.error(msg) |
| 73 | return Response(json.dumps({'error': msg}), |
| 74 | status=400, |
| 75 | mimetype='application/json') |
| 76 | |
| 77 | if fields: |
| 78 | try: |
| 79 | cases.append(getattr(sf_cli, action)(*fields)) |
Michal Kobus | aacc4a5 | 2019-01-18 12:21:10 +0100 | [diff] [blame] | 80 | except SalesforceError as err: |
Michal Kobus | afbf4d0 | 2018-11-28 14:18:05 +0100 | [diff] [blame] | 81 | msg = 'Salesforce request failure: {}.'.format(err) |
| 82 | sf_cli.metrics['sf_error_count'].inc() |
Michal Kobus | 17726ae | 2018-11-27 12:59:55 +0100 | [diff] [blame] | 83 | app.logger.error(msg) |
| 84 | return Response(json.dumps({'error': msg}), |
Mateusz Matuszkowiak | 2820c66 | 2018-11-21 12:07:25 +0100 | [diff] [blame] | 85 | status=500, |
| 86 | mimetype='application/json') |
| 87 | |
| 88 | if len(cases) == 1: |
| 89 | return jsonify(cases[0]) |
| 90 | return jsonify(cases) |
| 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | app.run() |