From 0539f612bc7979031ad41dcffbfa78fe2cdaa631 Mon Sep 17 00:00:00 2001 From: Erno Kuvaja Date: Mon, 10 Jul 2017 16:07:32 +0100 Subject: [PATCH] Add Discovery stub for Image Import This change adds discovery stub for Image Import MVP returning ['glance-direct'] as import-methods if Image Import has been enabled in config. The default behavior for Pike is disabled so the GET call to /v2/info/import returns empty list as default. The stub is required for the client work to support the Minimum Viable Product testing. This change is based on the work started by Cyril and Nikhil on https://review.openstack.org/#/c/270980 dropping any components that are depending of the parts of IIR not implemented yet. Change-Id: I4bc4c1123fed7ea8a838ea3d06e9da966ae18f58 Co-Authored-By: Cyril Roelandt Co-Authored-By: Nikhil Komawar --- glance/api/v2/discovery.py | 46 +++++++++++++++++++ glance/api/v2/router.py | 13 ++++++ .../unit/v2/test_discovery_image_import.py | 31 +++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 glance/api/v2/discovery.py create mode 100644 glance/tests/unit/v2/test_discovery_image_import.py diff --git a/glance/api/v2/discovery.py b/glance/api/v2/discovery.py new file mode 100644 index 0000000000..0166ed9d9c --- /dev/null +++ b/glance/api/v2/discovery.py @@ -0,0 +1,46 @@ +# Copyright (c) 2017 RedHat, Inc. +# +# 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 oslo_config import cfg + +from glance.common import wsgi + +CONF = cfg.CONF + + +class InfoController(object): + def get_image_import(self, req): + # TODO(jokke): All the rest of the boundaries should be implemented. + # TODO(jokke): Once we have the rest of the methods implemented + # the value should be inherited from the CONF rather than hard- + # coded. + import_methods = { + 'description': 'Import methods available.', + 'type': 'array', + 'value': ['glance-direct'] + } + + # TODO(jokke): Will be removed after the config option + # is removed. (deprecated) + if not CONF.enable_image_import: + import_methods['value'] = [] + + return { + 'import-methods': import_methods + } + + +def create_resource(): + return wsgi.Resource(InfoController()) diff --git a/glance/api/v2/router.py b/glance/api/v2/router.py index 3334f6a5a2..a398a7c851 100644 --- a/glance/api/v2/router.py +++ b/glance/api/v2/router.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +from glance.api.v2 import discovery from glance.api.v2 import image_actions from glance.api.v2 import image_data from glance.api.v2 import image_members @@ -553,4 +554,16 @@ class API(wsgi.Router): action='reject', allowed_methods='GET, DELETE') + # Discovery API + info_resource = discovery.create_resource() + mapper.connect('info/import', + controller=info_resource, + action='get_image_import', + conditions={'method': ['GET']}, + body_reject=True) + mapper.connect('info/import', + controller=reject_method_resource, + action='reject', + allowed_methods='GET') + super(API, self).__init__(mapper) diff --git a/glance/tests/unit/v2/test_discovery_image_import.py b/glance/tests/unit/v2/test_discovery_image_import.py new file mode 100644 index 0000000000..82d3a18986 --- /dev/null +++ b/glance/tests/unit/v2/test_discovery_image_import.py @@ -0,0 +1,31 @@ +# Copyright (c) 2017 RedHat, Inc. +# +# 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. + +import glance.api.v2.discovery +import glance.tests.unit.utils as unit_test_utils +import glance.tests.utils as test_utils + + +class TestInfoControllers(test_utils.BaseTestCase): + + def setUp(self): + super(TestInfoControllers, self).setUp() + self.controller = glance.api.v2.discovery.InfoController() + + def test_get_image_import(self): + req = unit_test_utils.get_fake_request() + output = self.controller.get_image_import(req) + self.assertIn('import-methods', output) + self.assertEqual([], output['import-methods']['value'])