[cleanup]catch 404 exception while there is not any neutron security group

In some environment, the security group is not configured, so we need to catch
404 exception which is raised by security group list.

Change-Id: Iaa5d2af6040a54011b8cbf788797c2c199f30ab4
This commit is contained in:
chenhb 2018-11-13 20:34:46 +08:00
parent 833bd3d704
commit 726f343832
2 changed files with 28 additions and 5 deletions

View File

@ -466,11 +466,16 @@ class NeutronRouter(NeutronMixin):
tenant_resource=True)
class NeutronSecurityGroup(NeutronMixin):
def list(self):
try:
tenant_sgs = super(NeutronSecurityGroup, self).list()
# NOTE(pirsriva): Filter out "default" security group deletion
# by non-admin role user
return filter(lambda r: r["name"] != "default",
tenant_sgs)
except Exception as e:
if getattr(e, "status_code", 400) == 404:
return []
raise
@base.resource("neutron", "quota", order=next(_neutron_order),

View File

@ -576,6 +576,24 @@ class NeutronSecurityGroupTestCase(test.TestCase):
neut.user.neutron().list_security_groups.assert_called_once_with(
tenant_id=neut.tenant_uuid)
def test_list_with_not_found(self):
class NotFound(Exception):
status_code = 404
neut = resources.NeutronSecurityGroup()
neut.user = mock.MagicMock()
neut._resource = "security_group"
neut.tenant_uuid = "user_tenant"
neut.user.neutron().list_security_groups.side_effect = NotFound()
expected_result = []
self.assertEqual(expected_result, list(neut.list()))
neut.user.neutron().list_security_groups.assert_called_once_with(
tenant_id=neut.tenant_uuid)
class NeutronQuotaTestCase(test.TestCase):