diff --git a/nova/api/openstack/compute/contrib/consoles.py b/nova/api/openstack/compute/contrib/consoles.py index e755ae391f3c..b1402eee81a6 100644 --- a/nova/api/openstack/compute/contrib/consoles.py +++ b/nova/api/openstack/compute/contrib/consoles.py @@ -48,7 +48,10 @@ class ConsolesController(wsgi.Controller): except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict( explanation=_('Instance not yet ready')) - except exception.ConsoleTypeUnavailable as e: + except exception.InstanceNotFound as e: + raise webob.exc.HTTPNotFound(explanation=e.format_message()) + except (exception.ConsoleTypeUnavailable, + exception.ConsoleTypeInvalid) as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) except NotImplementedError: msg = _("Unable to get vnc console, functionality not implemented") @@ -71,8 +74,11 @@ class ConsolesController(wsgi.Controller): output = self.compute_api.get_spice_console(context, instance, console_type) - except exception.ConsoleTypeUnavailable as e: + except (exception.ConsoleTypeUnavailable, + exception.ConsoleTypeInvalid) as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) + except exception.InstanceNotFound as e: + raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except NotImplementedError: @@ -97,8 +103,11 @@ class ConsolesController(wsgi.Controller): output = self.compute_api.get_rdp_console(context, instance, console_type) - except exception.ConsoleTypeUnavailable as e: + except (exception.ConsoleTypeUnavailable, + exception.ConsoleTypeInvalid) as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) + except exception.InstanceNotFound as e: + raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except NotImplementedError: @@ -123,8 +132,16 @@ class ConsolesController(wsgi.Controller): console_type) except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) + except exception.InstanceNotFound as e: + raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) + except (exception.ConsoleTypeUnavailable, + exception.ConsoleTypeInvalid, + exception.ImageSerialPortNumberInvalid, + exception.ImageSerialPortNumberExceedFlavorValue, + exception.SocketPortRangeExhaustedException) as e: + raise webob.exc.HTTPBadRequest(explanation=e.format_message()) except NotImplementedError: msg = _("Unable to get serial console, " "functionality not implemented") diff --git a/nova/api/openstack/compute/plugins/v3/remote_consoles.py b/nova/api/openstack/compute/plugins/v3/remote_consoles.py index 02fabd9c54b4..d99d221896ed 100644 --- a/nova/api/openstack/compute/plugins/v3/remote_consoles.py +++ b/nova/api/openstack/compute/plugins/v3/remote_consoles.py @@ -123,7 +123,7 @@ class RemoteConsolesController(wsgi.Controller): return {'console': {'type': console_type, 'url': output['url']}} - @extensions.expected_errors((404, 409, 501)) + @extensions.expected_errors((400, 404, 409, 501)) @wsgi.action('os-getSerialConsole') @validation.schema(remote_consoles.get_serial_console) def get_serial_console(self, req, id, body): @@ -142,6 +142,11 @@ class RemoteConsolesController(wsgi.Controller): raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) + except (exception.ConsoleTypeUnavailable, + exception.ImageSerialPortNumberInvalid, + exception.ImageSerialPortNumberExceedFlavorValue, + exception.SocketPortRangeExhaustedException) as e: + raise webob.exc.HTTPBadRequest(explanation=e.format_message()) except NotImplementedError: msg = _("Unable to get serial console, " "functionality not implemented") diff --git a/nova/tests/unit/api/openstack/compute/contrib/test_consoles.py b/nova/tests/unit/api/openstack/compute/contrib/test_consoles.py index debd1e7f5f45..5196e4995040 100644 --- a/nova/tests/unit/api/openstack/compute/contrib/test_consoles.py +++ b/nova/tests/unit/api/openstack/compute/contrib/test_consoles.py @@ -431,25 +431,6 @@ class ConsolesExtensionTestV21(test.NoDBTestCase): res = req.get_response(self.app) self.assertEqual(400, res.status_int) - -class ConsolesExtensionTestV2(ConsolesExtensionTestV21): - - def _setup_wsgi(self): - self.flags( - osapi_compute_extension=[ - 'nova.api.openstack.compute.contrib.select_extensions'], - osapi_compute_ext_list=['Consoles']) - self.app = fakes.wsgi_app(init_only=('servers',)) - - def test_get_vnc_console_with_undefined_param(self): - pass - - def test_get_spice_console_with_undefined_param(self): - pass - - def test_get_rdp_console_with_undefined_param(self): - pass - def test_get_serial_console(self): body = {'os-getSerialConsole': {'type': 'serial'}} req = webob.Request.blank(self.url) @@ -492,7 +473,6 @@ class ConsolesExtensionTestV2(ConsolesExtensionTestV21): res = req.get_response(self.app) self.assertEqual(res.status_int, 400) - self.assertTrue(get_serial_console.called) @mock.patch.object(compute_api.API, 'get_serial_console') def test_get_serial_console_no_type(self, get_serial_console): @@ -507,7 +487,6 @@ class ConsolesExtensionTestV2(ConsolesExtensionTestV21): res = req.get_response(self.app) self.assertEqual(res.status_int, 400) - self.assertTrue(get_serial_console.called) @mock.patch.object(compute_api.API, 'get_serial_console') def test_get_serial_console_no_instance(self, get_serial_console): @@ -552,7 +531,7 @@ class ConsolesExtensionTestV2(ConsolesExtensionTestV21): req.headers["content-type"] = "application/json" res = req.get_response(self.app) - self.assertEqual(res.status_int, 500) + self.assertEqual(res.status_int, 400) self.assertTrue(get_serial_console.called) @mock.patch.object(compute_api.API, 'get_serial_console') @@ -585,3 +564,22 @@ class ConsolesExtensionTestV2(ConsolesExtensionTestV21): res = req.get_response(self.app) self.assertEqual(res.status_int, 400) self.assertTrue(get_serial_console.called) + + +class ConsolesExtensionTestV2(ConsolesExtensionTestV21): + + def _setup_wsgi(self): + self.flags( + osapi_compute_extension=[ + 'nova.api.openstack.compute.contrib.select_extensions'], + osapi_compute_ext_list=['Consoles']) + self.app = fakes.wsgi_app(init_only=('servers',)) + + def test_get_vnc_console_with_undefined_param(self): + pass + + def test_get_spice_console_with_undefined_param(self): + pass + + def test_get_rdp_console_with_undefined_param(self): + pass