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'])