blob: 263d1b68b7ba3f4693c18c34524993bfa59120de [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
Michal Kobusaacc4a52019-01-18 12:21:10 +010026from simple_salesforce.exceptions import SalesforceError
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010027
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
Michal Kobus819cf022018-11-29 16:39:22 +010043@app.route('/info', methods=['GET'])
44def info():
Michal Kobus73d33522018-12-10 11:41:13 +010045 return jsonify({
46 'version': settings.VERSION,
47 'hashing': sf_cli.hash_func.__name__
48 })
Michal Kobus819cf022018-11-29 16:39:22 +010049
50
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010051@app.route('/hook', methods=['POST'])
52def webhook_receiver():
53
54 try:
55 data = json.loads(request.data)
56 except ValueError:
Michal Kobus17726ae2018-11-27 12:59:55 +010057 msg = 'Invalid request data: {}.'.format(request.data)
58 app.logger.error(msg)
59 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010060 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 Kobus27457d42019-02-13 14:06:11 +010068 alert['labels']['env_id'] = sf_cli.environment
Michal Kobus23ba2f72018-12-03 17:11:20 +010069 fields, action = alert_fields_and_action(alert)
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010070 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 Kobusaacc4a52019-01-18 12:21:10 +010080 except SalesforceError as err:
Michal Kobusafbf4d02018-11-28 14:18:05 +010081 msg = 'Salesforce request failure: {}.'.format(err)
82 sf_cli.metrics['sf_error_count'].inc()
Michal Kobus17726ae2018-11-27 12:59:55 +010083 app.logger.error(msg)
84 return Response(json.dumps({'error': msg}),
Mateusz Matuszkowiak2820c662018-11-21 12:07:25 +010085 status=500,
86 mimetype='application/json')
87
88 if len(cases) == 1:
89 return jsonify(cases[0])
90 return jsonify(cases)
91
92
93if __name__ == '__main__':
94 app.run()