From e11f788ca76807cb1c723e1f1d3bb574ca12db87 Mon Sep 17 00:00:00 2001 From: Ryan Rossiter Date: Mon, 11 Jan 2016 19:08:03 +0000 Subject: [PATCH] Add helper shim for getting items Within the wsgi module, the wsgi request object has a cache_db_items() function that does a dictionary __getitem__ on the item passed in. Because the objects are being changed over to remove dictionary syntax, __getitem__ on these objects now fails. But the item being passed in is not always a versioned object, so we can't just totally drop the __getitem__ to use getattr(). To keep compatibility between them, a function is added to check if the item has __getitem__. If it does, we'll use it as a dictionary, otherwise, we'll use getattr(). Change-Id: I84b76d2fd344e0f53ffaada3ed74d3b97de4f962 Partially-Implements: bp rm-object-dict-compat --- nova/api/openstack/wsgi.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index 256d1bfc3419..5ac812f96611 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -86,6 +86,17 @@ def get_media_map(): return dict(_MEDIA_TYPE_MAP.items()) +# 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.""" @@ -105,7 +116,7 @@ class Request(wsgi.Request): """ db_items = self._extension_data['db_items'].setdefault(key, {}) for item in items: - db_items[item[item_key]] = item + db_items[item_get(item, item_key)] = item def get_db_items(self, key): """Allow an API extension to get previously stored objects within