def test_get_config(self, mock_open, mock_validate, mock_environ, mock_json): def reset_mocks(): for m in (mock_open, mock_validate, mock_environ, mock_json): m.reset_mock() config.CONF = None mock_json.load.return_value = {"foo": 42, "bar": "spam"} mock_environ.get.return_value = "foo_path" mock_open.return_value = "foo_stream" cfg = config.get_config() self.assertEqual({"foo": 42, "bar": "spam"}, cfg) mock_environ.get.assert_called_once_with("NOTIFY_CONF", "/etc/notify/config.json") mock_open.assert_called_once_with("foo_path") mock_json.load.assert_called_once_with("foo_stream") mock_validate.assert_called_once_with({ "foo": 42, "bar": "spam" }, config.CONF_SCHEMA) reset_mocks() config.CONF = None mock_open.side_effect = IOError cfg = config.get_config() self.assertEqual(config.DEFAULT_CONF, cfg)
def send_notification(backends): global CACHE backends = set(backends.split(",")) payload = flask.request.get_json(force=True, silent=True) if not payload: return flask.jsonify({"error": "Missed Payload"}), 400 try: driver.Driver.validate_payload(payload) except ValueError as e: return flask.jsonify({"error": "Bad Payload: {}".format(e)}), 400 notify_backends = config.get_config()["notify_backends"] unexpected = backends - set(notify_backends) if unexpected: mesg = "Unexpected backends: {}".format(", ".join(unexpected)) return flask.jsonify({"error": mesg}), 400 result = { "payload": payload, "result": {}, "total": 0, "passed": 0, "failed": 0, "errors": 0 } for backend in backends: for drv_name, drv_conf in notify_backends[backend].items(): key = "{}.{}".format(drv_name, make_hash(drv_conf)) if key not in CACHE: CACHE[key] = driver.get_driver(drv_name, drv_conf) driver_ins = CACHE[key] result["total"] += 1 if backend not in result["result"]: result["result"][backend] = {} # TODO(maretskiy): run in parallel try: status = driver_ins.notify(payload) result["passed"] += status result["failed"] += not status result["result"][backend][drv_name] = {"status": status} except driver.ExplainedError as e: result["result"][backend][drv_name] = {"error": str(e)} result["errors"] += 1 except Exception as e: LOG.error("Backend '{}' driver '{}': {}: {}".format( backend, drv_name, type(e), e)) error = "Something has went wrong!" result["result"][backend][drv_name] = {"error": error} result["errors"] += 1 return flask.jsonify(result), 200
def test_get_config_cached(self): config.CONF = 42 self.assertEqual(42, config.get_config())
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import flask from flask_helpers import routing from notify.api.v1 import api from notify import config app = flask.Flask(__name__, static_folder=None) app.config.update(config.get_config()["flask"]) @app.errorhandler(404) def not_found(error): return flask.jsonify({"error": "Not Found"}), 404 for url_prefix, blueprint in api.get_blueprints(): app.register_blueprint(blueprint, url_prefix="/api/v1%s" % url_prefix) app = routing.add_routing_map(app, html_uri=None, json_uri="/") def main(): app.run(host=app.config.get("HOST", "0.0.0.0"),
def test_get_config_cached(self): with mock.patch.object(config, "CONF", 42): self.assertEqual(42, config.get_config())
# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import logging import flask from notify import config LOG = logging.getLogger("api") LOG.setLevel(config.get_config().get("logging", {}).get("level", "INFO")) bp = flask.Blueprint("alert", __name__) @bp.route("/alert", methods=["POST"]) def send_alert(period): return flask.jsonify({}) def get_blueprints(): return [["", bp]]