Raise 409 when removing security group from instance

currently when attempting to remove a security group from instance, nova
api returns 500 when there are more than one secuity groups with such
name in the project.

This patch makes it return 409 Conflict, using the same logic as already
present when adding a security group to the instance in such scenario of
duplicate names for security groups.

Change-Id: I28ad999c49d4e11add54405b89185b3f56f165b5
Closes-Bug: 1889655
This commit is contained in:
Pavlo Shchelokovskyy
2020-06-15 14:59:46 +03:00
parent c57d52e197
commit 696c8a95f6
2 changed files with 18 additions and 0 deletions

View File

@@ -665,6 +665,8 @@ def remove_from_instance(context, instance, security_group_name):
neutron, 'security_group',
security_group_name,
context.project_id)
except n_exc.NeutronClientNoUniqueMatch as e:
raise exception.NoUniqueMatch(e)
except n_exc.NeutronClientException as e:
if e.status_code == 404:
msg = (_("Security group %(name)s is not found for "

View File

@@ -373,6 +373,22 @@ class TestNeutronDriver(test.NoDBTestCase):
self.mocked_client.update_port.assert_called_once_with(
port_id, {'port': {'security_groups': [sg_id]}})
def test_add_to_instance_duplicate_sg_name(self):
sg_name = 'web_server'
with mock.patch.object(neutronv20, 'find_resourceid_by_name_or_id',
side_effect=n_exc.NeutronClientNoUniqueMatch):
self.assertRaises(exception.NoUniqueMatch,
sg_api.add_to_instance, self.context,
objects.Instance(uuid=uuids.instance), sg_name)
def test_remove_from_instance_duplicate_sg_name(self):
sg_name = 'web_server'
with mock.patch.object(neutronv20, 'find_resourceid_by_name_or_id',
side_effect=n_exc.NeutronClientNoUniqueMatch):
self.assertRaises(exception.NoUniqueMatch,
sg_api.remove_from_instance, self.context,
objects.Instance(uuid=uuids.instance), sg_name)
class TestNeutronDriverWithoutMock(test.NoDBTestCase):