diff --git a/ceagle/api/client.py b/ceagle/api/client.py index 6fdcb13..c77de58 100644 --- a/ceagle/api/client.py +++ b/ceagle/api/client.py @@ -16,11 +16,15 @@ import requests from ceagle.api import base +from ceagle.api_fake_data import availability from ceagle.api_fake_data import fake_security +from ceagle.api_fake_data import health from ceagle import config FAKE_CLIENT_MAP = { "security": fake_security.Client, + "health": health.Client, + "availability": availability.Client, } diff --git a/ceagle/api/v1/regions.py b/ceagle/api/v1/regions.py index 91942df..554e856 100644 --- a/ceagle/api/v1/regions.py +++ b/ceagle/api/v1/regions.py @@ -16,7 +16,6 @@ import flask from ceagle.api import client -from ceagle.api_fake_data import fake_regions from ceagle import config bp = flask.Blueprint("regions", __name__) @@ -24,7 +23,6 @@ @bp.route("", defaults={"detailed": False}) @bp.route("/detailed", defaults={"detailed": True}) -@fake_regions.get_regions def get_regions(detailed): regions = {} diff --git a/ceagle/api_fake_data/availability.py b/ceagle/api_fake_data/availability.py new file mode 100644 index 0000000..053b8df --- /dev/null +++ b/ceagle/api_fake_data/availability.py @@ -0,0 +1,23 @@ +# Copyright 2016: Mirantis Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# 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. + +from ceagle.api_fake_data import base + + +class Client(base.FakeClient): + + @base.route("/api/v1/availability/(?Pday|week|month)$") + def availability(self, query, period): + return {"availability": {}}, 200 diff --git a/ceagle/api_fake_data/base.py b/ceagle/api_fake_data/base.py index d52eaf1..96f3f1c 100644 --- a/ceagle/api_fake_data/base.py +++ b/ceagle/api_fake_data/base.py @@ -15,11 +15,77 @@ import functools import random +import re from ceagle import config - USE_FAKE_DATA = config.get_config().get("use_fake_api_data", True) +FAKE_REGIONS = [ + "west-1.hooli.net", + "north-1.piedpiper.net", + "east-1.hooli.net", + "south-1.piedpiper.net", +] + + +def route(reg): + def decorator(method): + method._route = re.compile(reg) + return method + return decorator + + +class BaseFakeClient(object): + """Base fake client. + + Usage: + + >>> from ceagle.api_fake_data import base + >>> class MyClient(base.FakeClient): + ... @base.route(r"/api/(?P)") + ... def _method(self, query, method): + ... return {"query": query}, 200 + ... + >>> c = MyClient(name="name", endpoint="endpoint") + >>> resp, code = c.get("/api/foo", foo="bar") + >>> assert code == 200 + >>> assert resp == {"query": {"foo": "bar"}} + + """ + + def __init__(self, name, endpoint): + self._setup_routing() + + def _setup_routing(self): + self._routes = [] + for attr in dir(self): + method = getattr(self, attr) + route = getattr(method, "_route", None) + if route: + self._routes.append((route, method)) + + def _find_route(self, path): + for reg, method in self._routes: + match = reg.match(path) + if match: + return method, match + return None, None + + def default(self, path, *args, **kwargs): + return ("not found", 404) + + def get(self, path, **kwargs): + method, match = self._find_route(path) + if method is None: + return self.default(path, **kwargs) + return method(kwargs, **match.groupdict()) + + +class FakeClient(BaseFakeClient): + + @route("/api/(?P\w+)/regions") + def _regions(self, query, api): + return FAKE_REGIONS, 200 def api_handler(fake): diff --git a/ceagle/api_fake_data/health.py b/ceagle/api_fake_data/health.py new file mode 100644 index 0000000..a29e2ed --- /dev/null +++ b/ceagle/api_fake_data/health.py @@ -0,0 +1,23 @@ +# Copyright 2016: Mirantis Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# 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. + +from ceagle.api_fake_data import base + + +class Client(base.FakeClient): + + @base.route("/api/v1/health/(?Pday|week|month)$") + def health(self, query, period): + return {"health": {}}, 200 diff --git a/ceagle/api_fake_data/security.py b/ceagle/api_fake_data/security.py new file mode 100644 index 0000000..6a0cead --- /dev/null +++ b/ceagle/api_fake_data/security.py @@ -0,0 +1,23 @@ +# Copyright 2016: Mirantis Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# 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. + +from ceagle.api_fake_data import base + + +class Client(base.FakeClient): + + @base.route("/api/v1/security/issues/(?Pday|week|month)") + def health(self, query, period): + return {"issues": {}}, 200 diff --git a/tests/ci/fake-clients-config.json b/tests/ci/fake-clients-config.json index b868ce3..01f27d2 100644 --- a/tests/ci/fake-clients-config.json +++ b/tests/ci/fake-clients-config.json @@ -5,6 +5,8 @@ "DEBUG": true }, "services": { - "security": "http://example.org/api/security" + "security": "http://security.example.org/", + "health": "http://health.example.org/", + "availability": "http://availability.example.org/" } } diff --git a/tests/unit/api/v1/test_regions.py b/tests/unit/api/v1/test_regions.py index 2be081f..cd9e1d3 100644 --- a/tests/unit/api/v1/test_regions.py +++ b/tests/unit/api/v1/test_regions.py @@ -21,15 +21,6 @@ from tests.unit import test -class ApiTestCase(test.TestCase): - - def test_api_response_code(self): - code, resp = self.get("/api/v1/regions") - self.assertEqual(code, 200) - code, resp = self.get("/api/v1/regions/detailed") - self.assertEqual(code, 200) - - class RegionsApiTestCase(test.TestCase): def setUp(self):