From dba6713e83967be513b5b2bfc39408f15c0d0fe6 Mon Sep 17 00:00:00 2001 From: Takashi NATSUME Date: Tue, 31 May 2016 21:08:35 +0900 Subject: [PATCH] Return 400 when SecurityGroupCannotBeApplied is raised Return 400 when SecurityGroupCannotBeApplied is raised in attaching an interface to a VM instance. Change-Id: I6cc0e2b43b82dc3b16a581b1f8ec75b35995934e Closes-Bug: #1584676 --- nova/api/openstack/compute/attach_interfaces.py | 3 ++- .../openstack/compute/test_attach_interfaces.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/nova/api/openstack/compute/attach_interfaces.py b/nova/api/openstack/compute/attach_interfaces.py index c81389497f3d..04a8164807b5 100644 --- a/nova/api/openstack/compute/attach_interfaces.py +++ b/nova/api/openstack/compute/attach_interfaces.py @@ -120,7 +120,8 @@ class InterfaceAttachmentController(wsgi.Controller): exception.NetworkAmbiguous, exception.NoMoreFixedIps, exception.PortNotUsable, - exception.AttachInterfaceNotSupported) as e: + exception.AttachInterfaceNotSupported, + exception.SecurityGroupCannotBeApplied) as e: raise exc.HTTPBadRequest(explanation=e.format_message()) except (exception.InstanceIsLocked, exception.FixedIpAlreadyInUse, diff --git a/nova/tests/unit/api/openstack/compute/test_attach_interfaces.py b/nova/tests/unit/api/openstack/compute/test_attach_interfaces.py index 4fc3e956d7ec..5ea7a4317c4f 100644 --- a/nova/tests/unit/api/openstack/compute/test_attach_interfaces.py +++ b/nova/tests/unit/api/openstack/compute/test_attach_interfaces.py @@ -425,6 +425,23 @@ class InterfaceAttachTestsV21(test.NoDBTestCase): get_mock.assert_called_once_with(ctxt, FAKE_UUID1, expected_attrs=None) + @mock.patch.object(compute_api.API, 'get') + @mock.patch.object(compute_api.API, 'attach_interface') + def test_attach_interface_failed_securitygroup_cannot_be_applied( + self, attach_mock, get_mock): + fake_instance = objects.Instance(uuid=FAKE_UUID1, + project_id=FAKE_UUID2) + get_mock.return_value = fake_instance + attach_mock.side_effect = ( + exception.SecurityGroupCannotBeApplied()) + self.assertRaises(exc.HTTPBadRequest, self.attachments.create, + self.req, FAKE_UUID1, body={}) + ctxt = self.req.environ['nova.context'] + attach_mock.assert_called_once_with(ctxt, fake_instance, None, + None, None) + get_mock.assert_called_once_with(ctxt, FAKE_UUID1, + expected_attrs=None) + def _test_attach_interface_with_invalid_parameter(self, param): self.stubs.Set(compute_api.API, 'attach_interface', fake_attach_interface)