From 7c808fa702d553b6c7a4e1fed8d137ba0ee43e8d Mon Sep 17 00:00:00 2001 From: ghanshyam Date: Sun, 30 Sep 2018 12:45:34 +0000 Subject: [PATCH] Merge image_size extension response into image view builder As nova extensions has been deprecated already and goal is to merge all scattered code into main controller side. Currently schema and request/response extended code are there among all extensions. This commit merges the image_size extension response into image view builder. Partially implements: blueprint api-extensions-merge-stein Change-Id: Ifca9d9b766c87d4e1107e9be07223f8d4a0d6794 --- nova/api/openstack/compute/image_size.py | 43 ------- nova/api/openstack/compute/routes.py | 3 +- nova/api/openstack/compute/views/images.py | 1 + .../api/openstack/compute/test_image_size.py | 118 ------------------ .../unit/api/openstack/compute/test_images.py | 2 + nova/tests/unit/image_fixtures.py | 8 +- 6 files changed, 8 insertions(+), 167 deletions(-) delete mode 100644 nova/api/openstack/compute/image_size.py delete mode 100644 nova/tests/unit/api/openstack/compute/test_image_size.py diff --git a/nova/api/openstack/compute/image_size.py b/nova/api/openstack/compute/image_size.py deleted file mode 100644 index c6569c690601..000000000000 --- a/nova/api/openstack/compute/image_size.py +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright 2013 Rackspace Hosting -# 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 nova.api.openstack import wsgi - - -class ImageSizeController(wsgi.Controller): - - def _extend_image(self, image, image_cache): - # NOTE(mriedem): The OS-EXT-* prefix should not be used for new - # attributes after v2.1. They are only in v2.1 for backward compat - # with v2.0. - key = "OS-EXT-IMG-SIZE:size" - image[key] = image_cache['size'] - - @wsgi.extends - def show(self, req, resp_obj, id): - image_resp = resp_obj.obj['image'] - # image guaranteed to be in the cache due to the core API adding - # it in its 'show' method - image_cached = req.get_db_item('images', image_resp['id']) - self._extend_image(image_resp, image_cached) - - @wsgi.extends - def detail(self, req, resp_obj): - images_resp = list(resp_obj.obj['images']) - # images guaranteed to be in the cache due to the core API adding - # it in its 'detail' method - for image in images_resp: - image_cached = req.get_db_item('images', image['id']) - self._extend_image(image, image_cached) diff --git a/nova/api/openstack/compute/routes.py b/nova/api/openstack/compute/routes.py index d4fa3c171888..6ee8f6fa777a 100644 --- a/nova/api/openstack/compute/routes.py +++ b/nova/api/openstack/compute/routes.py @@ -49,7 +49,6 @@ from nova.api.openstack.compute import fping from nova.api.openstack.compute import hosts from nova.api.openstack.compute import hypervisors from nova.api.openstack.compute import image_metadata -from nova.api.openstack.compute import image_size from nova.api.openstack.compute import images from nova.api.openstack.compute import instance_actions from nova.api.openstack.compute import instance_usage_audit_log @@ -198,7 +197,7 @@ hypervisors_controller = functools.partial( images_controller = functools.partial( _create_controller, images.ImagesController, - [image_size.ImageSizeController], []) + [], []) image_metadata_controller = functools.partial( diff --git a/nova/api/openstack/compute/views/images.py b/nova/api/openstack/compute/views/images.py index 5cf7cab95bb4..1eb8ed5fb790 100644 --- a/nova/api/openstack/compute/views/images.py +++ b/nova/api/openstack/compute/views/images.py @@ -49,6 +49,7 @@ class ViewBuilder(common.ViewBuilder): "updated": self._format_date(image.get("updated_at")), "status": self._get_status(image), "progress": self._get_progress(image), + "OS-EXT-IMG-SIZE:size": image.get("size"), "links": self._get_links(request, image["id"], self._collection_name), diff --git a/nova/tests/unit/api/openstack/compute/test_image_size.py b/nova/tests/unit/api/openstack/compute/test_image_size.py deleted file mode 100644 index b6977f3d0643..000000000000 --- a/nova/tests/unit/api/openstack/compute/test_image_size.py +++ /dev/null @@ -1,118 +0,0 @@ -# Copyright 2013 Rackspace Hosting -# 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 oslo_serialization import jsonutils - -from nova import test -from nova.tests.unit.api.openstack import fakes - -NOW_API_FORMAT = "2010-10-11T10:30:22Z" -IMAGES = [{ - 'id': '123', - 'name': 'public image', - 'metadata': {'key1': 'value1'}, - 'updated': NOW_API_FORMAT, - 'created': NOW_API_FORMAT, - 'status': 'ACTIVE', - 'progress': 100, - 'minDisk': 10, - 'minRam': 128, - 'size': 12345678, - "links": [{ - "rel": "self", - "href": "http://localhost/v2/fake/images/123", - }, - { - "rel": "bookmark", - "href": "http://localhost/fake/images/123", - }], - }, - { - 'id': '124', - 'name': 'queued snapshot', - 'updated': NOW_API_FORMAT, - 'created': NOW_API_FORMAT, - 'status': 'SAVING', - 'progress': 25, - 'minDisk': 0, - 'minRam': 0, - 'size': 87654321, - "links": [{ - "rel": "self", - "href": "http://localhost/v2/fake/images/124", - }, - { - "rel": "bookmark", - "href": "http://localhost/fake/images/124", - }], - }] - - -def fake_show(*args, **kwargs): - return IMAGES[0] - - -def fake_detail(*args, **kwargs): - return IMAGES - - -class ImageSizeTestV21(test.NoDBTestCase): - content_type = 'application/json' - prefix = 'OS-EXT-IMG-SIZE' - - def setUp(self): - super(ImageSizeTestV21, self).setUp() - - self.stub_out('nova.image.glance.GlanceImageServiceV2.show', - fake_show) - self.stub_out('nova.image.glance.GlanceImageServiceV2.detail', - fake_detail) - - self.flags(api_servers=['http://localhost:9292'], group='glance') - - def _make_request(self, url): - req = fakes.HTTPRequest.blank(url) - req.headers['Accept'] = self.content_type - res = req.get_response(self._get_app()) - return res - - def _get_app(self): - return fakes.wsgi_app_v21() - - def _get_image(self, body): - return jsonutils.loads(body).get('image') - - def _get_images(self, body): - return jsonutils.loads(body).get('images') - - def assertImageSize(self, image, size): - self.assertEqual(size, image.get('%s:size' % self.prefix)) - - def test_show(self): - url = '/v2/fake/images/1' - res = self._make_request(url) - - self.assertEqual(200, res.status_int) - image = self._get_image(res.body) - self.assertImageSize(image, 12345678) - - def test_detail(self): - url = '/v2/fake/images/detail' - res = self._make_request(url) - - self.assertEqual(200, res.status_int) - images = self._get_images(res.body) - self.assertImageSize(images[0], 12345678) - self.assertImageSize(images[1], 87654321) diff --git a/nova/tests/unit/api/openstack/compute/test_images.py b/nova/tests/unit/api/openstack/compute/test_images.py index 7a807cf461e8..ef2152dd4a88 100644 --- a/nova/tests/unit/api/openstack/compute/test_images.py +++ b/nova/tests/unit/api/openstack/compute/test_images.py @@ -79,6 +79,7 @@ class ImagesControllerTestV21(test.NoDBTestCase): 'minDisk': 10, 'progress': 100, 'minRam': 128, + 'OS-EXT-IMG-SIZE:size': 25165824, "links": [{ "rel": "self", "href": "%s/123" % self.url_prefix @@ -111,6 +112,7 @@ class ImagesControllerTestV21(test.NoDBTestCase): 'progress': 25, 'minDisk': 0, 'minRam': 0, + 'OS-EXT-IMG-SIZE:size': 25165824, 'server': { 'id': self.server_uuid, "links": [{ diff --git a/nova/tests/unit/image_fixtures.py b/nova/tests/unit/image_fixtures.py index 9ab09b989a79..76cf1a21fb25 100644 --- a/nova/tests/unit/image_fixtures.py +++ b/nova/tests/unit/image_fixtures.py @@ -51,7 +51,7 @@ def get_image_fixtures(): # Public image add_fixture(id=str(image_id), name='public image', is_public=True, status='active', properties={'key1': 'value1'}, - min_ram="128", min_disk="10", size='25165824') + min_ram="128", min_disk="10", size=25165824) image_id += 1 # Snapshot for User 1 @@ -64,16 +64,16 @@ def get_image_fixtures(): add_fixture(id=str(image_id), name='%s snapshot' % status, is_public=False, status=status, - properties=snapshot_properties, size='25165824', + properties=snapshot_properties, size=25165824, deleted=deleted, deleted_at=deleted_at) image_id += 1 # Image without a name add_fixture(id=str(image_id), is_public=True, status='active', - properties={}) + properties={}, size=25165824) # Image for permission tests image_id += 1 add_fixture(id=str(image_id), is_public=True, status='active', - properties={}, owner='authorized_fake') + properties={}, owner='authorized_fake', size=25165824) return fixtures