From 559cb7ac58200ab6e8f32a6914bf96d66ce8ea7f Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Tue, 4 Sep 2012 14:48:20 -0700 Subject: [PATCH] Optimizes flavor_access to not make a db request We are now caching the flavor object in the request so we don't need to make another db request. Note that there is further refactoring possible but I wanted to keep the changes small to minimize chance for regression. Also, no need to check for not found since we will have errored much earlier if the flavor doesn't exist. This change is necessary to create a failing test for bug 1046040 Change-Id: I718203b33a192575288057dd4631a9aa3086b567 --- .../compute/contrib/flavor_access.py | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/nova/api/openstack/compute/contrib/flavor_access.py b/nova/api/openstack/compute/contrib/flavor_access.py index 433d0c75d5b..6fd2fc4609d 100644 --- a/nova/api/openstack/compute/contrib/flavor_access.py +++ b/nova/api/openstack/compute/contrib/flavor_access.py @@ -139,14 +139,9 @@ def show(self, req, resp_obj, id): if authorize(context): # Attach our slave template to the response object resp_obj.attach(xml=FlavorextradatumTemplate()) + db_flavor = req.get_db_flavor(id) - try: - flavor_ref = instance_types.get_instance_type_by_flavor_id(id) - except exception.FlavorNotFound: - explanation = _("Flavor not found.") - raise webob.exc.HTTPNotFound(explanation=explanation) - - self._extend_flavor(resp_obj.obj['flavor'], flavor_ref) + self._extend_flavor(resp_obj.obj['flavor'], db_flavor) @wsgi.extends def detail(self, req, resp_obj): @@ -156,11 +151,9 @@ def detail(self, req, resp_obj): resp_obj.attach(xml=FlavorextradataTemplate()) flavors = list(resp_obj.obj['flavors']) - flavor_refs = self._get_flavor_refs(context) - for flavor_rval in flavors: - flavor_ref = flavor_refs[flavor_rval['id']] - self._extend_flavor(flavor_rval, flavor_ref) + db_flavor = req.get_db_flavor(flavor_rval['id']) + self._extend_flavor(flavor_rval, db_flavor) @wsgi.extends(action='create') def create(self, req, body, resp_obj): @@ -169,14 +162,9 @@ def create(self, req, body, resp_obj): # Attach our slave template to the response object resp_obj.attach(xml=FlavorextradatumTemplate()) - try: - fid = resp_obj.obj['flavor']['id'] - flavor_ref = instance_types.get_instance_type_by_flavor_id(fid) - except exception.FlavorNotFound: - explanation = _("Flavor not found.") - raise webob.exc.HTTPNotFound(explanation=explanation) + db_flavor = req.get_db_flavor(resp_obj.obj['flavor']['id']) - self._extend_flavor(resp_obj.obj['flavor'], flavor_ref) + self._extend_flavor(resp_obj.obj['flavor'], db_flavor) @wsgi.serializers(xml=FlavorAccessTemplate) @wsgi.action("addTenantAccess")