Skip to content

Commit 696c8a9

Browse files
committed
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
1 parent c57d52e commit 696c8a9

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

nova/network/security_group_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,8 @@ def remove_from_instance(context, instance, security_group_name):
665665
neutron, 'security_group',
666666
security_group_name,
667667
context.project_id)
668+
except n_exc.NeutronClientNoUniqueMatch as e:
669+
raise exception.NoUniqueMatch(e)
668670
except n_exc.NeutronClientException as e:
669671
if e.status_code == 404:
670672
msg = (_("Security group %(name)s is not found for "

nova/tests/unit/network/test_security_group.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,22 @@ def test_add_to_instance_with_bad_request(self):
373373
self.mocked_client.update_port.assert_called_once_with(
374374
port_id, {'port': {'security_groups': [sg_id]}})
375375

376+
def test_add_to_instance_duplicate_sg_name(self):
377+
sg_name = 'web_server'
378+
with mock.patch.object(neutronv20, 'find_resourceid_by_name_or_id',
379+
side_effect=n_exc.NeutronClientNoUniqueMatch):
380+
self.assertRaises(exception.NoUniqueMatch,
381+
sg_api.add_to_instance, self.context,
382+
objects.Instance(uuid=uuids.instance), sg_name)
383+
384+
def test_remove_from_instance_duplicate_sg_name(self):
385+
sg_name = 'web_server'
386+
with mock.patch.object(neutronv20, 'find_resourceid_by_name_or_id',
387+
side_effect=n_exc.NeutronClientNoUniqueMatch):
388+
self.assertRaises(exception.NoUniqueMatch,
389+
sg_api.remove_from_instance, self.context,
390+
objects.Instance(uuid=uuids.instance), sg_name)
391+
376392

377393
class TestNeutronDriverWithoutMock(test.NoDBTestCase):
378394

0 commit comments

Comments
 (0)