From 3b4e702518ecb1247c6c3a7bf23e3160e05bcee6 Mon Sep 17 00:00:00 2001 From: Krysta Knight Date: Thu, 2 Nov 2017 16:22:21 -0500 Subject: [PATCH] Add Versions API to Drydock This ps adds a VersionsResource that returns a list of API versions available and includes unit tests for get_versions and get_health Change-Id: Ibcbc2af79cf5e441b59d64da9a1c7f951eff0563 --- drydock_provisioner/control/api.py | 18 +++++++++++++++- drydock_provisioner/control/base.py | 6 ++++++ tests/unit/test_api_health.py | 30 ++++++++++++++++++++++++++ tests/unit/test_api_versions.py | 33 +++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_api_health.py create mode 100644 tests/unit/test_api_versions.py diff --git a/drydock_provisioner/control/api.py b/drydock_provisioner/control/api.py index a30f5e6a..a4b3ab31 100644 --- a/drydock_provisioner/control/api.py +++ b/drydock_provisioner/control/api.py @@ -24,7 +24,7 @@ from .bootdata import BootdataResource from .nodes import NodesResource from .health import HealthResource -from .base import DrydockRequest +from .base import DrydockRequest, BaseResource from .middleware import AuthMiddleware, ContextMiddleware, LoggingMiddleware @@ -46,6 +46,8 @@ def start_api(state_manager=None, ingester=None, orchestrator=None): LoggingMiddleware() ]) + control_api.add_route('/versions', VersionsResource()) + # v1.0 of Drydock API v1_0_routes = [ # API for managing orchestrator tasks @@ -77,3 +79,17 @@ def start_api(state_manager=None, ingester=None, orchestrator=None): control_api.add_route('/api/v1.0' + path, res) return control_api + + +class VersionsResource(BaseResource): + """ + Lists the versions supported by this API + """ + + def on_get(self, req, resp): + resp.body = self.to_json({ + 'v1.0': { + 'path': '/api/v1.0', + 'status': 'stable' + }}) + resp.status = falcon.HTTP_200 diff --git a/drydock_provisioner/control/base.py b/drydock_provisioner/control/base.py index dc84d635..42afb72f 100644 --- a/drydock_provisioner/control/base.py +++ b/drydock_provisioner/control/base.py @@ -82,6 +82,12 @@ class BaseResource(object): self.logger.log(level, msg, extra=extra) + def to_json(self, body_dict): + """ + Thin wrapper around json.dumps, providing the default=str config + """ + return json.dumps(body_dict, default=str) + def debug(self, ctx, msg): self.log_error(ctx, logging.DEBUG, msg) diff --git a/tests/unit/test_api_health.py b/tests/unit/test_api_health.py new file mode 100644 index 00000000..ddbd1c1b --- /dev/null +++ b/tests/unit/test_api_health.py @@ -0,0 +1,30 @@ +# Copyright 2017 AT&T Intellectual Property. All other 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. +"""Test Health API""" + +from drydock_provisioner.control.health import HealthResource + +import falcon + + +def test_get_health(mocker): + api = HealthResource() + + # Configure mocked request and response + req = mocker.MagicMock(spec=falcon.Request) + resp = mocker.MagicMock(spec=falcon.Response) + + api.on_get(req, resp) + + assert resp.status == falcon.HTTP_204 diff --git a/tests/unit/test_api_versions.py b/tests/unit/test_api_versions.py new file mode 100644 index 00000000..6fae06e1 --- /dev/null +++ b/tests/unit/test_api_versions.py @@ -0,0 +1,33 @@ +# Copyright 2017 AT&T Intellectual Property. All other 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. +"""Test Versions API""" + +from drydock_provisioner.control.api import VersionsResource + +import falcon + + +def test_get_versions(mocker): + api = VersionsResource() + + # Configure mocked request and response + req = mocker.MagicMock(spec=falcon.Request) + resp = mocker.MagicMock(spec=falcon.Response) + + api.on_get(req, resp) + + expected = api.to_json({'v1.0': {'path': '/api/v1.0', 'status': 'stable'}}) + + assert resp.body == expected + assert resp.status == falcon.HTTP_200