Remove the caching the resource on Request object
All extensions are now merged into their main controller view builder. Request object used to have the cache mechanism to store the db resources for extensions to extend those. Now those caching is not needed and this commit removes the caching and get methods from Request object and their usage. Partially implements: blueprint api-extensions-merge-stein Change-Id: I444c2be539031a9ace0b3602c5c6f9f4132b6f23
This commit is contained in:
parent
7c808fa702
commit
d9237e159c
|
@ -106,7 +106,6 @@ class FlavorManageController(wsgi.Controller):
|
|||
description=description)
|
||||
# NOTE(gmann): For backward compatibility, non public flavor
|
||||
# access is not being added for created tenant. Ref -bug/1209101
|
||||
req.cache_db_flavor(flavor)
|
||||
except (exception.FlavorExists,
|
||||
exception.FlavorIdExists) as err:
|
||||
raise webob.exc.HTTPConflict(explanation=err.format_message())
|
||||
|
@ -141,9 +140,6 @@ class FlavorManageController(wsgi.Controller):
|
|||
except exception.FlavorNotFound as e:
|
||||
raise webob.exc.HTTPNotFound(explanation=e.format_message())
|
||||
|
||||
# Cache the flavor so the flavor_access and flavor_rxtx extensions
|
||||
# can add stuff to the response.
|
||||
req.cache_db_flavor(flavor)
|
||||
include_extra_specs = False
|
||||
if api_version_request.is_supported(
|
||||
req, flavors_view.FLAVOR_EXTRA_SPECS_MICROVERSION):
|
||||
|
|
|
@ -50,7 +50,6 @@ class FlavorsController(wsgi.Controller):
|
|||
"""Return all flavors in detail."""
|
||||
context = req.environ['nova.context']
|
||||
limited_flavors = self._get_flavors(req)
|
||||
req.cache_db_flavors(limited_flavors)
|
||||
include_extra_specs = False
|
||||
if api_version_request.is_supported(
|
||||
req, flavors_view.FLAVOR_EXTRA_SPECS_MICROVERSION):
|
||||
|
@ -65,7 +64,6 @@ class FlavorsController(wsgi.Controller):
|
|||
context = req.environ['nova.context']
|
||||
try:
|
||||
flavor = flavors.get_flavor_by_flavor_id(id, ctxt=context)
|
||||
req.cache_db_flavor(flavor)
|
||||
except exception.FlavorNotFound as e:
|
||||
raise webob.exc.HTTPNotFound(explanation=e.format_message())
|
||||
|
||||
|
|
|
@ -89,7 +89,6 @@ class ImagesController(wsgi.Controller):
|
|||
explanation = _("Image not found.")
|
||||
raise webob.exc.HTTPNotFound(explanation=explanation)
|
||||
|
||||
req.cache_db_items('images', [image], 'id')
|
||||
return self._view_builder.show(req, image)
|
||||
|
||||
@wsgi.Controller.api_version("2.1", MAX_PROXY_API_SUPPORT_VERSION)
|
||||
|
@ -149,5 +148,4 @@ class ImagesController(wsgi.Controller):
|
|||
except exception.Invalid as e:
|
||||
raise webob.exc.HTTPBadRequest(explanation=e.format_message())
|
||||
|
||||
req.cache_db_items('images', images, 'id')
|
||||
return self._view_builder.detail(req, images)
|
||||
|
|
|
@ -286,14 +286,13 @@ class ServersController(wsgi.Controller):
|
|||
response = self._view_builder.detail(req, instance_list)
|
||||
else:
|
||||
response = self._view_builder.index(req, instance_list)
|
||||
req.cache_db_instances(instance_list)
|
||||
return response
|
||||
|
||||
def _get_server(self, context, req, instance_uuid, is_detail=False):
|
||||
"""Utility function for looking up an instance by uuid.
|
||||
|
||||
:param context: request context for auth
|
||||
:param req: HTTP request. The instance is cached in this request.
|
||||
:param req: HTTP request.
|
||||
:param instance_uuid: UUID of the server instance to get
|
||||
:param is_detail: True if you plan on showing the details of the
|
||||
instance in the response, False otherwise.
|
||||
|
@ -309,7 +308,6 @@ class ServersController(wsgi.Controller):
|
|||
instance = common.get_instance(self.compute_api, context,
|
||||
instance_uuid,
|
||||
expected_attrs=expected_attrs)
|
||||
req.cache_db_instance(instance)
|
||||
return instance
|
||||
|
||||
@staticmethod
|
||||
|
@ -683,7 +681,6 @@ class ServersController(wsgi.Controller):
|
|||
if return_reservation_id:
|
||||
return wsgi.ResponseObject({'reservation_id': resv_id})
|
||||
|
||||
req.cache_db_instances(instances)
|
||||
server = self._view_builder.create(req, instances[0])
|
||||
|
||||
if CONF.api.enable_instance_password:
|
||||
|
|
|
@ -483,8 +483,6 @@ class ViewBuilder(common.ViewBuilder):
|
|||
return fault_dict
|
||||
|
||||
def _add_security_grps(self, req, servers, instances):
|
||||
# TODO(arosen) this function should be refactored to reduce duplicate
|
||||
# code and use get_instance_security_groups instead of get_db_instance.
|
||||
if not len(servers):
|
||||
return
|
||||
if not openstack_driver.is_neutron_security_groups():
|
||||
|
|
|
@ -74,78 +74,14 @@ def get_supported_content_types():
|
|||
return _SUPPORTED_CONTENT_TYPES
|
||||
|
||||
|
||||
# NOTE(rlrossit): This function allows a get on both a dict-like and an
|
||||
# object-like object. cache_db_items() is used on both versioned objects and
|
||||
# dicts, so the function can't be totally changed over to [] syntax, nor
|
||||
# can it be changed over to use getattr().
|
||||
def item_get(item, item_key):
|
||||
if hasattr(item, '__getitem__'):
|
||||
return item[item_key]
|
||||
else:
|
||||
return getattr(item, item_key)
|
||||
|
||||
|
||||
class Request(wsgi.Request):
|
||||
"""Add some OpenStack API-specific logic to the base webob.Request."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Request, self).__init__(*args, **kwargs)
|
||||
self._extension_data = {'db_items': {}}
|
||||
if not hasattr(self, 'api_version_request'):
|
||||
self.api_version_request = api_version.APIVersionRequest()
|
||||
|
||||
def cache_db_items(self, key, items, item_key='id'):
|
||||
"""Allow API methods to store objects from a DB query to be
|
||||
used by API extensions within the same API request.
|
||||
|
||||
An instance of this class only lives for the lifetime of a
|
||||
single API request, so there's no need to implement full
|
||||
cache management.
|
||||
"""
|
||||
db_items = self._extension_data['db_items'].setdefault(key, {})
|
||||
for item in items:
|
||||
db_items[item_get(item, item_key)] = item
|
||||
|
||||
def get_db_items(self, key):
|
||||
"""Allow an API extension to get previously stored objects within
|
||||
the same API request.
|
||||
|
||||
Note that the object data will be slightly stale.
|
||||
"""
|
||||
return self._extension_data['db_items'][key]
|
||||
|
||||
def get_db_item(self, key, item_key):
|
||||
"""Allow an API extension to get a previously stored object
|
||||
within the same API request.
|
||||
|
||||
Note that the object data will be slightly stale.
|
||||
"""
|
||||
return self.get_db_items(key).get(item_key)
|
||||
|
||||
def cache_db_instances(self, instances):
|
||||
self.cache_db_items('instances', instances, 'uuid')
|
||||
|
||||
def cache_db_instance(self, instance):
|
||||
self.cache_db_items('instances', [instance], 'uuid')
|
||||
|
||||
def get_db_instances(self):
|
||||
return self.get_db_items('instances')
|
||||
|
||||
def get_db_instance(self, instance_uuid):
|
||||
return self.get_db_item('instances', instance_uuid)
|
||||
|
||||
def cache_db_flavors(self, flavors):
|
||||
self.cache_db_items('flavors', flavors, 'flavorid')
|
||||
|
||||
def cache_db_flavor(self, flavor):
|
||||
self.cache_db_items('flavors', [flavor], 'flavorid')
|
||||
|
||||
def get_db_flavors(self):
|
||||
return self.get_db_items('flavors')
|
||||
|
||||
def get_db_flavor(self, flavorid):
|
||||
return self.get_db_item('flavors', flavorid)
|
||||
|
||||
def best_match_content_type(self):
|
||||
"""Determine the requested response content-type."""
|
||||
if 'nova.best_content_type' not in self.environ:
|
||||
|
|
|
@ -103,9 +103,6 @@ class FakeRequest(object):
|
|||
environ = {"nova.context": context.get_admin_context()}
|
||||
api_version_request = api_version.APIVersionRequest("2.1")
|
||||
|
||||
def get_db_flavor(self, flavor_id):
|
||||
return INSTANCE_TYPES[flavor_id]
|
||||
|
||||
def is_legacy_v2(self):
|
||||
return False
|
||||
|
||||
|
|
|
@ -18,10 +18,6 @@ from nova import test
|
|||
from nova.tests.unit.api.openstack import fakes
|
||||
|
||||
|
||||
def fake_get_db_flavor(req, flavorid):
|
||||
return fakes.FLAVORS[flavorid]
|
||||
|
||||
|
||||
class FlavorDisabledTestV21(test.NoDBTestCase):
|
||||
base_url = '/v2/fake/flavors'
|
||||
content_type = 'application/json'
|
||||
|
@ -32,8 +28,6 @@ class FlavorDisabledTestV21(test.NoDBTestCase):
|
|||
fakes.stub_out_nw_api(self)
|
||||
fakes.stub_out_flavor_get_all(self)
|
||||
fakes.stub_out_flavor_get_by_flavor_id(self)
|
||||
self.stub_out('nova.api.openstack.wsgi.Request.get_db_flavor',
|
||||
fake_get_db_flavor)
|
||||
|
||||
def _make_request(self, url):
|
||||
req = fakes.HTTPRequest.blank(url)
|
||||
|
|
|
@ -76,27 +76,6 @@ class RequestTest(MicroversionedTest):
|
|||
result = request.best_match_content_type()
|
||||
self.assertEqual(result, "application/json")
|
||||
|
||||
def test_cache_and_retrieve_instances(self):
|
||||
request = wsgi.Request.blank('/foo')
|
||||
instances = []
|
||||
for x in range(3):
|
||||
instances.append({'uuid': 'uuid%s' % x})
|
||||
# Store 2
|
||||
request.cache_db_instances(instances[:2])
|
||||
# Store 1
|
||||
request.cache_db_instance(instances[2])
|
||||
self.assertEqual(request.get_db_instance('uuid0'),
|
||||
instances[0])
|
||||
self.assertEqual(request.get_db_instance('uuid1'),
|
||||
instances[1])
|
||||
self.assertEqual(request.get_db_instance('uuid2'),
|
||||
instances[2])
|
||||
self.assertIsNone(request.get_db_instance('uuid3'))
|
||||
self.assertEqual(request.get_db_instances(),
|
||||
{'uuid0': instances[0],
|
||||
'uuid1': instances[1],
|
||||
'uuid2': instances[2]})
|
||||
|
||||
def test_from_request(self):
|
||||
request = wsgi.Request.blank('/')
|
||||
accepted = 'bogus;q=1, en-gb;q=0.7,en-us,en;q=0.5,*;q=0.7'
|
||||
|
|
Loading…
Reference in New Issue