diff --git a/vmware_nsx/plugins/nsx_p/plugin.py b/vmware_nsx/plugins/nsx_p/plugin.py index b1abf9db59..0ff0c60ed7 100644 --- a/vmware_nsx/plugins/nsx_p/plugin.py +++ b/vmware_nsx/plugins/nsx_p/plugin.py @@ -1698,7 +1698,11 @@ class NsxPolicyPlugin(nsx_plugin_common.NsxPluginV3Base): cidr1 = netaddr.IPNetwork(binding1.ip_address) cidr2 = netaddr.IPNetwork(binding2.ip_address) if cidr1 != cidr2 and cidr1 in cidr2: - address_bindings.remove(binding1) + try: + address_bindings.remove(binding1) + except ValueError: + # Item was already removed + pass return address_bindings def _get_network_nsx_id(self, context, network_id): diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index 57e4f5cc00..2b6f5a25aa 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -1205,7 +1205,11 @@ class NsxV3Plugin(nsx_plugin_common.NsxPluginV3Base, cidr1 = netaddr.IPNetwork(binding1.ip_address) cidr2 = netaddr.IPNetwork(binding2.ip_address) if cidr1 != cidr2 and cidr1 in cidr2: - address_bindings.remove(binding1) + try: + address_bindings.remove(binding1) + except ValueError: + # item already removed + pass return address_bindings diff --git a/vmware_nsx/tests/unit/nsx_p/test_plugin.py b/vmware_nsx/tests/unit/nsx_p/test_plugin.py index 8a403655ea..b4e60033fa 100644 --- a/vmware_nsx/tests/unit/nsx_p/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_p/test_plugin.py @@ -1334,6 +1334,48 @@ class NsxPTestPorts(common_v3.NsxV3TestPorts, set([fixed_ip, '1.2.3.0/24']), addresses) + def test_update_port_allowed_pair_cidr(self): + with self.subnet() as subnet: + post_data = { + 'port': { + 'network_id': subnet['subnet']['network_id'], + 'tenant_id': subnet['subnet']['tenant_id'], + 'allowed_address_pairs': [ + {'ip_address': '10.4.0.32', + 'mac_address': '00:00:5e:00:01:fa'}, + {'ip_address': '10.40.1.125', + 'mac_address': 'fa:16:3e:ef:b1:be'}], + 'device_owner': 'compute:meh', + 'fixed_ips': [{'subnet_id': + subnet['subnet']['id']}]}} + post_req = self.new_create_request('ports', post_data) + res = post_req.get_response(self.api) + self.assertEqual(201, res.status_int) + port = self.deserialize('json', res) + fixed_ip = ( + port['port']['fixed_ips'][0]['ip_address']) + with mock.patch.object( + self.plugin.nsxpolicy.segment_port, + 'create_or_overwrite') as mock_port: + put_data = { + 'port': { + 'allowed_address_pairs': [ + {'ip_address': '10.4.0.0/24'} + ] + } + } + put_req = self.new_update_request( + 'ports', put_data, port['port']['id']) + put_res = put_req.get_response(self.api) + self.assertEqual(200, put_res.status_int) + self.assertEqual(1, len(mock_port.mock_calls)) + _n, _a, kwargs = mock_port.mock_calls[0] + actual_bindings = kwargs['address_bindings'] + addresses = set([b.ip_address for b in actual_bindings]) + self.assertEqual( + set([fixed_ip, '10.4.0.0/24']), + addresses) + class NsxPTestSubnets(common_v3.NsxV3TestSubnets, NsxPPluginTestCaseMixin):