Handle a flavor create failed better

Catch FlavorCreateFailed exceptions which when flavor creation fails.
This type of flavor creation failure is caused by an internal nova problem.
Return an internal server error (500) to the user so they know
to contact their cloud provider.

Closes-Bug: #1292309

Change-Id: I268c84dae290b0170448d789509af781555df9b0
This commit is contained in:
jichenjc 2014-04-13 13:29:56 +08:00
parent f92cf2099f
commit e0844f257c
4 changed files with 59 additions and 1 deletions

View File

@ -77,6 +77,9 @@ class FlavorManageController(wsgi.Controller):
raise webob.exc.HTTPConflict(explanation=err.format_message())
except exception.InvalidInput as exc:
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
except exception.FlavorCreateFailed as exc:
raise webob.exc.HTTPInternalServerError(explanation=
exc.format_message())
return self._view_builder.show(req, flavor)

View File

@ -50,7 +50,7 @@ class FlavorManageController(wsgi.Controller):
@wsgi.response(201)
@wsgi.action("create")
@extensions.expected_errors((400, 409))
@extensions.expected_errors((400, 409, 500))
@validation.schema(flavor_manage.create)
def _create(self, req, body):
context = req.environ['nova.context']
@ -81,6 +81,9 @@ class FlavorManageController(wsgi.Controller):
except (exception.FlavorExists,
exception.FlavorIdExists) as err:
raise webob.exc.HTTPConflict(explanation=err.format_message())
except exception.FlavorCreateFailed as err:
raise webob.exc.HTTPInternalServerError(explanation=
err.format_message())
return self._view_builder.show(req, flavor)

View File

@ -15,6 +15,7 @@
import datetime
import mock
import webob
from nova.api.openstack.compute.contrib import flavor_access
@ -426,6 +427,31 @@ class FlavorManageTest(test.NoDBTestCase):
res = req.get_response(self.app)
self.assertEqual(res.status_int, 409)
@mock.patch('nova.compute.flavors.create',
side_effect=exception.FlavorCreateFailed)
def test_flavor_create_db_failed(self, mock_create):
request_dict = {
"flavor": {
"name": " test ",
'id': "12345",
"ram": 512,
"vcpus": 2,
"disk": 1,
"OS-FLV-EXT-DATA:ephemeral": 1,
"swap": 512,
"rxtx_factor": 1,
"os-flavor-access:is_public": True,
}
}
url = '/v2/fake/flavors'
req = webob.Request.blank(url)
req.headers['Content-Type'] = 'application/json'
req.method = 'POST'
req.body = jsonutils.dumps(request_dict)
res = req.get_response(self.app)
self.assertEqual(res.status_int, 500)
self.assertIn('Unable to create flavor', res.body)
def test_invalid_memory_mb(self):
"""Check negative and decimal number can't be accepted."""

View File

@ -15,6 +15,7 @@
import datetime
import mock
import webob
from nova.api.openstack.compute.plugins.v3 import flavor_access
@ -210,6 +211,31 @@ class FlavorManageTest(test.NoDBTestCase):
self.base_request_dict['flavor']['id'] = id
self._test_create_bad_request(self.base_request_dict)
@mock.patch('nova.compute.flavors.create',
side_effect=exception.FlavorCreateFailed)
def test_flavor_create_db_failed(self, mock_create):
request_dict = {
"flavor": {
"name": "test",
"ram": 512,
"vcpus": 2,
"disk": 1,
"ephemeral": 1,
"id": unicode('1234'),
"swap": 512,
"%s:rxtx_factor" % flavor_rxtx.ALIAS: 1,
"flavor-access:is_public": True,
}
}
url = '/v3/flavors'
req = webob.Request.blank(url)
req.headers['Content-Type'] = 'application/json'
req.method = 'POST'
req.body = jsonutils.dumps(request_dict)
res = req.get_response(self.app)
self.assertEqual(res.status_int, 500)
self.assertIn('Unable to create flavor', res.body)
def test_create_without_name(self):
del self.base_request_dict['flavor']['name']
self._test_create_bad_request(self.base_request_dict)