Fix AttributeError in check allowed port fields

When the 'local_link_connection' field is None an
AttributeError is raised when attempting to get the
keys.

Add a check for None before trying to get the keys.

Closes-Bug: #1871346
Change-Id: I5855aea79d6322f70f95709e1a87b8bda3282435
This commit is contained in:
Harald Jensås 2020-04-07 15:55:54 +02:00
parent ef3c8ff799
commit 78f904978d
2 changed files with 12 additions and 3 deletions

View File

@ -443,10 +443,10 @@ class PortsController(rest.RestController):
if ('local_link_connection/network_type' in fields
and not api_utils.allow_local_link_connection_network_type()):
raise exception.NotAcceptable()
if isinstance(fields, dict):
if (isinstance(fields, dict)
and fields.get('local_link_connection') is not None):
if (not api_utils.allow_local_link_connection_network_type()
and 'network_type' in fields.get('local_link_connection',
{}).keys()):
and 'network_type' in fields['local_link_connection']):
raise exception.NotAcceptable()
@METRICS.timer('PortsController.get_all')

View File

@ -185,6 +185,15 @@ class TestPortsController__CheckAllowedPortFields(base.TestCase):
self.assertFalse(mock_allow_portgroup.called)
mock_allow_physnet.assert_called_once_with()
def test__check_allowed_port_fields_local_link_connection_none_type(
self, mock_allow_port, mock_allow_portgroup, mock_allow_physnet):
mock_allow_port.return_value = True
mock_allow_physnet.return_value = True
self.assertIsNone(
self.controller._check_allowed_port_fields(
{'local_link_connection': None}))
mock_allow_port.assert_called_once_with()
class TestListPorts(test_api_base.BaseApiTest):