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
This commit is contained in:
ghanshyam 2018-09-30 12:45:34 +00:00 committed by Ghanshyam Mann
parent ce520ee789
commit 7c808fa702
6 changed files with 8 additions and 167 deletions

View File

@ -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)

View File

@ -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(

View File

@ -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),

View File

@ -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)

View File

@ -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": [{

View File

@ -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