blob: e7d6787d8c754bdd8ba35ce7d74079009bc92943 [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
Michal Kobus4104c102019-02-22 17:05:11 +010023from requests.exceptions import ConnectionError as RequestsConnectionError
24
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010025from sf_notifier.helpers import alert_fields_and_action
26from sf_notifier.salesforce.client import SalesforceClient
27
Michal Kobusaacc4a52019-01-18 12:21:10 +010028from simple_salesforce.exceptions import SalesforceError
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010029
30from simple_settings import settings
31
Michal Kobusafbf4d02018-11-28 14:18:05 +010032from werkzeug.wsgi import DispatcherMiddleware
33
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010034
35dictConfig(settings.LOGGING)
36
Michal Kobusafbf4d02018-11-28 14:18:05 +010037app = Flask(__name__)
38app_dispatch = DispatcherMiddleware(app, {
39 '/metrics': make_wsgi_app()
40})
41
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010042sf_cli = SalesforceClient(settings.SF_CONFIG)
43
44
Michal Kobus819cf022018-11-29 16:39:22 +010045@app.route('/info', methods=['GET'])
46def info():
Michal Kobus73d33522018-12-10 11:41:13 +010047 return jsonify({
48 'version': settings.VERSION,
49 'hashing': sf_cli.hash_func.__name__
50 })
Michal Kobus819cf022018-11-29 16:39:22 +010051
52
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010053@app.route('/hook', methods=['POST'])
54def webhook_receiver():
55
56 try:
57 data = json.loads(request.data)
58 except ValueError:
Michal Kobus17726ae2018-11-27 12:59:55 +010059 msg = 'Invalid request data: {}.'.format(request.data)
60 app.logger.error(msg)
61 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010062 status=400,
63 mimetype='application/json')
64
65 app.logger.info('Received requests: {}'.format(data))
66
67 cases = []
68 for alert in data['alerts']:
69 try:
Michal Kobus27457d42019-02-13 14:06:11 +010070 alert['labels']['env_id'] = sf_cli.environment
Michal Kobus23ba2f72018-12-03 17:11:20 +010071 fields, action = alert_fields_and_action(alert)
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010072 except KeyError as key:
73 msg = 'Alert misses {} key.'.format(key)
74 app.logger.error(msg)
75 return Response(json.dumps({'error': msg}),
76 status=400,
77 mimetype='application/json')
78
79 if fields:
80 try:
81 cases.append(getattr(sf_cli, action)(*fields))
Michal Kobus4104c102019-02-22 17:05:11 +010082 except (SalesforceError, RequestsConnectionError) as err:
Michal Kobusafbf4d02018-11-28 14:18:05 +010083 msg = 'Salesforce request failure: {}.'.format(err)
84 sf_cli.metrics['sf_error_count'].inc()
Michal Kobus17726ae2018-11-27 12:59:55 +010085 app.logger.error(msg)
86 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010087 status=500,
88 mimetype='application/json')
89
90 if len(cases) == 1:
91 return jsonify(cases[0])
92 return jsonify(cases)
93
94
95if __name__ == '__main__':
96 app.run()