diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index d5d7447b..c5ecd1a3 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -4696,7 +4696,25 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): api_get.assert_called_once() api_put.assert_called_once_with( "%s/segments/%s/ports/%s" % (TEST_TENANT, segment_id, port_id), - {'attachment': None}) + {'attachment': None, 'tags': tags}) + + def test_detach_with_vif(self): + segment_id = "segment" + port_id = "port" + vif_id = "abc" + tags = [{'scope': 'a', 'tag': 'b'}] + with mock.patch.object(self.policy_api.client, + "get", return_value={}) as api_get,\ + mock.patch.object(self.policy_api.client, + "update") as api_put: + self.resourceApi.detach( + segment_id, port_id, tags=tags, vif_id=vif_id, + tenant=TEST_TENANT) + + api_get.assert_called_once() + api_put.assert_called_once_with( + "%s/segments/%s/ports/%s" % (TEST_TENANT, segment_id, port_id), + {'attachment': {'id': vif_id}, 'tags': tags}) class TestPolicySegmentProfileBase(NsxPolicyLibTestCase): diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index cf67d4f6..6f8de49c 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -2188,8 +2188,9 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): tags=tags, tenant=tenant) - def detach(self, segment_id, port_id, tags=IGNORE, + def detach(self, segment_id, port_id, vif_id=None, tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): + """Reset the attachment with or without a vif_id""" # Due to platform limitation, PUT should be used here and not PATCH port_def = self.entry_def( segment_id=segment_id, @@ -2202,7 +2203,12 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): max_attempts=self.policy_api.client.max_attempts) def _detach(): port = self.policy_api.get(port_def) - port['attachment'] = None + if vif_id: + port['attachment'] = {'id': vif_id} + else: + port['attachment'] = None + if tags != IGNORE: + port['tags'] = tags self.policy_api.client.update(path, port) _detach() @@ -2218,19 +2224,17 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): - port_def = self._init_def(segment_id=segment_id, - port_id=port_id, - attachment_type=attachment_type, - allocate_addresses=allocate_addresses, - vif_id=vif_id, - app_id=app_id, - context_id=context_id, - traffic_tag=traffic_tag, - hyperbus_mode=hyperbus_mode, - tags=tags, - tenant=tenant) - - self.policy_api.create_or_update(port_def) + self._update(segment_id=segment_id, + port_id=port_id, + attachment_type=attachment_type, + allocate_addresses=allocate_addresses, + vif_id=vif_id, + app_id=app_id, + context_id=context_id, + traffic_tag=traffic_tag, + hyperbus_mode=hyperbus_mode, + tags=tags, + tenant=tenant) def get_realized_state(self, segment_id, port_id, entity_type=None, tenant=constants.POLICY_INFRA_TENANT,