blob: 4d3ddc0208d208faec8c087e2079887c1730fba4 [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
Michal Kobus211ee922019-04-15 17:44:06 +020025from sf_notifier.helpers import alert_fields_and_action, create_file
26from sf_notifier.salesforce.client import SESSION_FILE, SalesforceClient
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010027
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
Michal Kobus211ee922019-04-15 17:44:06 +020042
43create_file(SESSION_FILE)
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010044sf_cli = SalesforceClient(settings.SF_CONFIG)
45
46
Michal Kobus819cf022018-11-29 16:39:22 +010047@app.route('/info', methods=['GET'])
48def info():
Michal Kobus73d33522018-12-10 11:41:13 +010049 return jsonify({
50 'version': settings.VERSION,
51 'hashing': sf_cli.hash_func.__name__
52 })
Michal Kobus819cf022018-11-29 16:39:22 +010053
54
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010055@app.route('/hook', methods=['POST'])
56def webhook_receiver():
57
58 try:
59 data = json.loads(request.data)
60 except ValueError:
Michal Kobus17726ae2018-11-27 12:59:55 +010061 msg = 'Invalid request data: {}.'.format(request.data)
62 app.logger.error(msg)
63 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010064 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 Kobus27457d42019-02-13 14:06:11 +010072 alert['labels']['env_id'] = sf_cli.environment
Michal Kobus23ba2f72018-12-03 17:11:20 +010073 fields, action = alert_fields_and_action(alert)
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010074 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 Kobus4104c102019-02-22 17:05:11 +010084 except (SalesforceError, RequestsConnectionError) as err:
Michal Kobusafbf4d02018-11-28 14:18:05 +010085 msg = 'Salesforce request failure: {}.'.format(err)
86 sf_cli.metrics['sf_error_count'].inc()
Michal Kobus17726ae2018-11-27 12:59:55 +010087 app.logger.error(msg)
88 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010089 status=500,
90 mimetype='application/json')
91
92 if len(cases) == 1:
93 return jsonify(cases[0])
94 return jsonify(cases)
95
96
97if __name__ == '__main__':
98 app.run()