diff --git a/ceagle/api/client.py b/ceagle/api/client.py index 85895ed..89f1c50 100644 --- a/ceagle/api/client.py +++ b/ceagle/api/client.py @@ -18,6 +18,10 @@ from ceagle import config +class UnknownService(Exception): + pass + + class Client(object): """REST client.""" @@ -59,4 +63,4 @@ def get_client(service_name): endpoint = config.get_config().get("services", {}).get(service_name) if endpoint: return Client(name=service_name, endpoint=endpoint) - return None + raise UnknownService("Unknown service '%s'" % service_name) diff --git a/ceagle/api/v1/status.py b/ceagle/api/v1/status.py index 0268d1d..db77ed1 100644 --- a/ceagle/api/v1/status.py +++ b/ceagle/api/v1/status.py @@ -65,10 +65,6 @@ def get_status(period): @fake_status.get_status_health def get_status_health(period): health_client = client.get_client("health") - if not health_client: - return (flask.jsonify({"error": "No health endpoint configured"}), - 404) - api_endpoint = "/api/v1/health/{}".format(period) result, code = health_client.get(api_endpoint) @@ -85,10 +81,6 @@ def get_status_performance(period): @fake_status.get_status_availability def get_status_availability(period): ct = client.get_client("availability") - if not ct: - err = {"error": "No availability endpoint configured"} - return flask.jsonify(err), 404 - api_endpoint = "/api/v1/availability/{period}".format(period=period) resp, code = ct.get(api_endpoint) return flask.jsonify(resp), code @@ -104,10 +96,6 @@ def get_region_status(region, period): @fake_status.get_region_status_health def get_region_status_health(region, period): health_client = client.get_client("health") - if not health_client: - return (flask.jsonify({"error": "No health endpoint configured"}), - 404) - api_endpoint = "/api/v1/region/{region}/health/{period}".format( region=region, period=period) @@ -126,9 +114,6 @@ def get_region_status_performance(region, period): @fake_status.get_region_status_availability def get_region_status_availability(region, period): ct = client.get_client("availability") - if not ct: - err = {"error": "No availability endpoint configured"} - return flask.jsonify(err), 404 api_endpoint = "/api/v1/region/{region}/availability/{period}".format( region=region, period=period) resp, code = ct.get(api_endpoint) diff --git a/ceagle/main.py b/ceagle/main.py index 724b301..baf66a8 100644 --- a/ceagle/main.py +++ b/ceagle/main.py @@ -16,6 +16,7 @@ import flask from flask_helpers import routing +from ceagle.api import client from ceagle.api.v1 import capacity from ceagle.api.v1 import infrastructure from ceagle.api.v1 import intelligence @@ -44,6 +45,11 @@ def not_found(error): return flask.jsonify({"error": "Not Found"}), 404 +@app.errorhandler(client.UnknownService) +def handle_unknown_service(ex): + return flask.jsonify({"error": str(ex)}), 400 + + for bp in [status, infrastructure, intelligence, optimization, security, regions, capacity]: for url_prefix, blueprint in bp.get_blueprints(): diff --git a/tests/unit/api/test_client.py b/tests/unit/api/test_client.py index 3fd8b87..caf3789 100644 --- a/tests/unit/api/test_client.py +++ b/tests/unit/api/test_client.py @@ -76,9 +76,9 @@ class ModuleTestCase(test.TestCase): def test_get_config(self, mock_config): self.assertRaises(TypeError, client.get_client) mock_config.get_config.return_value = {"services": {"bar": {}}} - self.assertIsNone(client.get_client("foo")) + self.assertRaises(client.UnknownService, client.get_client, "foo") mock_config.get_config.return_value = {"services": {"foo": ""}} - self.assertIsNone(client.get_client("foo")) + self.assertRaises(client.UnknownService, client.get_client, "foo") cfg = {"services": {"foo": "http://foo_ep"}} mock_config.get_config.return_value = cfg diff --git a/tests/unit/api/v1/test_status.py b/tests/unit/api/v1/test_status.py index 23cc9b5..7cd0694 100644 --- a/tests/unit/api/v1/test_status.py +++ b/tests/unit/api/v1/test_status.py @@ -83,12 +83,12 @@ def test_region_status_health_api(self, mock_get_client): def test_health_api_no_endpoint(self, mock_get_config): mock_get_config.return_value = {} code, resp = self.get("/api/v1/status/health/day") - self.assertEqual(404, code) - self.assertEqual({"error": "No health endpoint configured"}, resp) + self.assertEqual(400, code) + self.assertEqual({"error": "Unknown service 'health'"}, resp) code, resp = self.get("/api/v1/region/test_region/status/health/day") - self.assertEqual(404, code) - self.assertEqual({"error": "No health endpoint configured"}, resp) + self.assertEqual(400, code) + self.assertEqual({"error": "Unknown service 'health'"}, resp) class AvailabilityApiTestCase(test.TestCase):