Add common function for v2.1 API flavor_get.

There are objects.Flaovr.get_by_flavor_id function in API layer
we need to catch FlavorNotFound every time. This patch make a
common function and catch FlavorNotFound in V2.1 API

Change-Id: I900debd2e096405ab91f7eb041359c2830c03c2d
changes/68/136568/20
jichenjc 8 years ago committed by Ken'ichi Ohmichi
parent c0625a4e93
commit 793bcc07b9
  1. 6
      nova/api/openstack/compute/plugins/v3/flavor_access.py
  2. 14
      nova/api/openstack/compute/plugins/v3/flavors_extraspecs.py

@ -17,6 +17,7 @@
import webob
from nova.api.openstack import common
from nova.api.openstack.compute.schemas.v3 import flavor_access
from nova.api.openstack import extensions
from nova.api.openstack import wsgi
@ -50,10 +51,7 @@ class FlavorAccessController(wsgi.Controller):
context = req.environ['nova.context']
authorize(context)
try:
flavor = objects.Flavor.get_by_flavor_id(context, flavor_id)
except exception.FlavorNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message())
flavor = common.get_flavor(context, flavor_id)
# public flavor to all projects
if flavor.is_public:

@ -16,13 +16,13 @@
import six
import webob
from nova.api.openstack import common
from nova.api.openstack.compute.schemas.v3 import flavors_extraspecs
from nova.api.openstack import extensions
from nova.api.openstack import wsgi
from nova.api import validation
from nova import exception
from nova.i18n import _
from nova import objects
from nova import utils
ALIAS = 'os-flavor-extra-specs'
@ -36,7 +36,7 @@ class FlavorExtraSpecsController(wsgi.Controller):
super(FlavorExtraSpecsController, self).__init__(*args, **kwargs)
def _get_extra_specs(self, context, flavor_id):
flavor = objects.Flavor.get_by_flavor_id(context, flavor_id)
flavor = common.get_flavor(context, flavor_id)
return dict(extra_specs=flavor.extra_specs)
# NOTE(gmann): Max length for numeric value is being checked
@ -70,8 +70,8 @@ class FlavorExtraSpecsController(wsgi.Controller):
specs = body['extra_specs']
self._check_extra_specs_value(specs)
flavor = common.get_flavor(context, flavor_id)
try:
flavor = objects.Flavor.get_by_flavor_id(context, flavor_id)
flavor.extra_specs = dict(flavor.extra_specs, **specs)
flavor.save()
except exception.FlavorExtraSpecUpdateCreateFailed as e:
@ -90,8 +90,8 @@ class FlavorExtraSpecsController(wsgi.Controller):
if id not in body:
expl = _('Request body and URI mismatch')
raise webob.exc.HTTPBadRequest(explanation=expl)
flavor = common.get_flavor(context, flavor_id)
try:
flavor = objects.Flavor.get_by_flavor_id(context, flavor_id)
flavor.extra_specs = dict(flavor.extra_specs, **body)
flavor.save()
except exception.FlavorExtraSpecUpdateCreateFailed as e:
@ -105,11 +105,9 @@ class FlavorExtraSpecsController(wsgi.Controller):
"""Return a single extra spec item."""
context = req.environ['nova.context']
authorize(context, action='show')
flavor = common.get_flavor(context, flavor_id)
try:
flavor = objects.Flavor.get_by_flavor_id(context, flavor_id)
return {id: flavor.extra_specs[id]}
except exception.FlavorNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message())
except KeyError:
msg = _("Flavor %(flavor_id)s has no extra specs with "
"key %(key)s.") % dict(flavor_id=flavor_id,
@ -124,8 +122,8 @@ class FlavorExtraSpecsController(wsgi.Controller):
"""Deletes an existing extra spec."""
context = req.environ['nova.context']
authorize(context, action='delete')
flavor = common.get_flavor(context, flavor_id)
try:
flavor = objects.Flavor.get_by_flavor_id(context, flavor_id)
del flavor.extra_specs[id]
flavor.save()
except (exception.FlavorExtraSpecsNotFound,

Loading…
Cancel
Save