Remove unnecessary ValueError exception

Agent object save method doesn't raise ValueError hence removed
unnecessary ValueError exception block while calling Agent object
save method.

ApiImpact: 400 BadRequest response will be returned in case invalid
agent id is provided during agent-delete, earlier it was returning
500 InternalServerError response.

Closes-Bug: 1463696
Change-Id: I600e70aff2d271e22ccad3b0302fc45a942c98bb
This commit is contained in:
Rajesh Tailor 2015-06-10 00:29:06 -07:00
parent 651bd02b4f
commit 68c4e0aa27
3 changed files with 30 additions and 4 deletions

View File

@ -21,6 +21,7 @@ from nova.api.openstack import wsgi
from nova.api import validation from nova.api import validation
from nova import exception from nova import exception
from nova import objects from nova import objects
from nova import utils
ALIAS = "os-agents" ALIAS = "os-agents"
@ -88,6 +89,11 @@ class AgentController(wsgi.Controller):
md5hash = para['md5hash'] md5hash = para['md5hash']
version = para['version'] version = para['version']
try:
utils.validate_integer(id, 'id')
except exception.InvalidInput as exc:
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
agent = objects.Agent(context=context, id=id) agent = objects.Agent(context=context, id=id)
agent.obj_reset_changes() agent.obj_reset_changes()
agent.version = version agent.version = version
@ -109,13 +115,18 @@ class AgentController(wsgi.Controller):
# TODO(oomichi): Here should be 204(No Content) instead of 200 by v2.1 # TODO(oomichi): Here should be 204(No Content) instead of 200 by v2.1
# +microversions because the resource agent has been deleted completely # +microversions because the resource agent has been deleted completely
# when returning a response. # when returning a response.
@extensions.expected_errors(404) @extensions.expected_errors((400, 404))
@wsgi.response(200) @wsgi.response(200)
def delete(self, req, id): def delete(self, req, id):
"""Deletes an existing agent build.""" """Deletes an existing agent build."""
context = req.environ['nova.context'] context = req.environ['nova.context']
authorize(context) authorize(context)
try:
utils.validate_integer(id, 'id')
except exception.InvalidInput as exc:
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
try: try:
agent = objects.Agent(context=context, id=id) agent = objects.Agent(context=context, id=id)
agent.destroy() agent.destroy()

View File

@ -89,6 +89,7 @@ class AgentController(object):
raise webob.exc.HTTPBadRequest(explanation=msg) raise webob.exc.HTTPBadRequest(explanation=msg)
try: try:
utils.validate_integer(id, 'id')
utils.check_string_length(url, 'url', max_length=255) utils.check_string_length(url, 'url', max_length=255)
utils.check_string_length(md5hash, 'md5hash', max_length=255) utils.check_string_length(md5hash, 'md5hash', max_length=255)
utils.check_string_length(version, 'version', max_length=255) utils.check_string_length(version, 'version', max_length=255)
@ -102,9 +103,6 @@ class AgentController(object):
agent.url = url agent.url = url
agent.md5hash = md5hash agent.md5hash = md5hash
agent.save() agent.save()
except ValueError as ex:
msg = _("Invalid request body: %s") % ex
raise webob.exc.HTTPBadRequest(explanation=msg)
except exception.AgentBuildNotFound as ex: except exception.AgentBuildNotFound as ex:
raise webob.exc.HTTPNotFound(explanation=ex.format_message()) raise webob.exc.HTTPNotFound(explanation=ex.format_message())
@ -123,6 +121,11 @@ class AgentController(object):
# NOTE(alex_xu): back-compatible with db layer hard-code admin # NOTE(alex_xu): back-compatible with db layer hard-code admin
# permission checks. # permission checks.
nova_context.require_admin_context(context) nova_context.require_admin_context(context)
try:
utils.validate_integer(id, 'id')
except exception.InvalidInput as exc:
raise webob.exc.HTTPBadRequest(explanation=exc.format_message())
try: try:
agent = objects.Agent(context=context, id=id) agent = objects.Agent(context=context, id=id)
agent.destroy() agent.destroy()

View File

@ -205,6 +205,10 @@ class AgentsTestV21(test.NoDBTestCase):
self.assertRaises(webob.exc.HTTPNotFound, self.assertRaises(webob.exc.HTTPNotFound,
self.controller.delete, self.req, 1) self.controller.delete, self.req, 1)
def test_agents_delete_string_id(self):
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.delete, self.req, 'string_id')
def test_agents_list(self): def test_agents_list(self):
res_dict = self.controller.index(self.req) res_dict = self.controller.index(self.req)
agents_list = [{'hypervisor': 'kvm', 'os': 'win', agents_list = [{'hypervisor': 'kvm', 'os': 'win',
@ -298,6 +302,14 @@ class AgentsTestV21(test.NoDBTestCase):
self.assertRaises(self.validation_error, self.assertRaises(self.validation_error,
self.controller.update, self.req, 1, body=body) self.controller.update, self.req, 1, body=body)
def test_agents_update_with_string_id(self):
body = {'para': {'version': '7.0',
'url': 'http://example.com/path/to/resource',
'md5hash': 'add6bb58e139be103324d04d82d8f545'}}
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.update, self.req,
'string_id', body=body)
def _test_agents_update_with_invalid_length(self, key): def _test_agents_update_with_invalid_length(self, key):
body = {'para': {'version': '7.0', body = {'para': {'version': '7.0',
'url': 'http://example.com/path/to/resource', 'url': 'http://example.com/path/to/resource',