blob: a42c26a612dfaede90b5ab33bdc9c313f8014bcd [file] [log] [blame]
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +01001# 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
16import json
17from logging.config import dictConfig
18
19from flask import Flask, Response, jsonify, request
20
Michal Kobusafbf4d02018-11-28 14:18:05 +010021from prometheus_client import make_wsgi_app
22
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010023from sf_notifier.helpers import alert_fields_and_action
24from sf_notifier.salesforce.client import SalesforceClient
25
26from simple_salesforce.exceptions import SalesforceMalformedRequest
27
28from simple_settings import settings
29
Michal Kobusafbf4d02018-11-28 14:18:05 +010030from werkzeug.wsgi import DispatcherMiddleware
31
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010032
33dictConfig(settings.LOGGING)
34
Michal Kobusafbf4d02018-11-28 14:18:05 +010035app = Flask(__name__)
36app_dispatch = DispatcherMiddleware(app, {
37 '/metrics': make_wsgi_app()
38})
39
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010040sf_cli = SalesforceClient(settings.SF_CONFIG)
41
42
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010043@app.route('/hook', methods=['POST'])
44def webhook_receiver():
45
46 try:
47 data = json.loads(request.data)
48 except ValueError:
Michal Kobus17726ae2018-11-27 12:59:55 +010049 msg = 'Invalid request data: {}.'.format(request.data)
50 app.logger.error(msg)
51 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010052 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 Kobusafbf4d02018-11-28 14:18:05 +010071 except SalesforceMalformedRequest as err:
72 msg = 'Salesforce request failure: {}.'.format(err)
73 sf_cli.metrics['sf_error_count'].inc()
Michal Kobus17726ae2018-11-27 12:59:55 +010074 app.logger.error(msg)
75 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010076 status=500,
77 mimetype='application/json')
78
79 if len(cases) == 1:
80 return jsonify(cases[0])
81 return jsonify(cases)
82
83
84if __name__ == '__main__':
85 app.run()