Use POST API when restore vif

When restore vif, we need to use POST api with init_state:RESTORE_VIF
for NSX to persist all properties including tags and address_bindings.

Change-Id: I0d49a3860349a4d021cc9c881fb60543936feefc
This commit is contained in:
lxiaopei 2023-01-03 15:26:19 +08:00
parent 349c4c3999
commit 67a72e6baf
2 changed files with 65 additions and 8 deletions

View File

@ -5368,7 +5368,6 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase):
tags = [{'scope': 'a', 'tag': 'b'}]
hyperbus_mode = 'DISABLE'
admin_state = True
init_state = 'RESTORE_VIF'
with mock.patch.object(
self.policy_api, "create_or_update") as api_call, \
@ -5382,8 +5381,7 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase):
allocate_addresses=allocate_addresses,
hyperbus_mode=hyperbus_mode, admin_state=admin_state,
tags=tags,
tenant=TEST_TENANT,
init_state=init_state)
tenant=TEST_TENANT)
expected_def = core_defs.SegmentPortDef(
nsx_version=nsxlib_testcase.LATEST_VERSION,
@ -5401,10 +5399,66 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase):
admin_state=admin_state,
tags=tags,
tenant=TEST_TENANT,
hyperbus_mode=hyperbus_mode)
self.assert_called_with_def(api_call, expected_def)
self.assertIsNotNone(result)
def test_create_with_restore_vif(self):
name = 'test'
description = 'desc'
segment_id = "segment"
port_id = "port"
address_bindings = []
attachment_type = "CHILD"
vif_id = "vif"
app_id = "app"
context_id = "context"
traffic_tag = 10
allocate_addresses = "BOTH"
tags = [{'scope': 'a', 'tag': 'b'}]
hyperbus_mode = 'DISABLE'
admin_state = True
init_state = 'RESTORE_VIF'
with mock.patch.object(
self.policy_api.client, "url_post") as api_post, \
mock.patch.object(self.resourceApi, 'version',
nsxlib_testcase.LATEST_VERSION):
result = self.resourceApi.create_or_overwrite(
name, segment_id, port_id=port_id, description=description,
address_bindings=address_bindings,
attachment_type=attachment_type, vif_id=vif_id, app_id=app_id,
context_id=context_id, traffic_tag=traffic_tag,
allocate_addresses=allocate_addresses,
hyperbus_mode=hyperbus_mode, admin_state=admin_state,
tags=tags,
tenant=TEST_TENANT,
init_state=init_state)
expected_def = core_defs.SegmentPortDef(
nsx_version=nsxlib_testcase.LATEST_VERSION,
segment_id=segment_id,
port_id=port_id,
name=name,
description=description,
address_bindings=address_bindings,
attachment_type=attachment_type,
vif_id=vif_id,
app_id=app_id,
context_id=context_id,
traffic_tag=traffic_tag,
allocate_addresses=allocate_addresses,
admin_state=admin_state,
tags=tags,
tenant=TEST_TENANT,
hyperbus_mode=hyperbus_mode,
init_state=init_state)
self.assert_called_with_def(api_call, expected_def)
api_post.assert_called_once_with(
expected_def.get_resource_path(),
expected_def.get_obj_dict(), headers=None,
expected_results=None, retry_confirm=False)
self.assertIsNotNone(result)
def test_create_with_unsupported_attribute(self):
@ -5420,7 +5474,6 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase):
allocate_addresses = "BOTH"
tags = [{'scope': 'a', 'tag': 'b'}]
hyperbus_mode = 'DISABLE'
init_state = 'RESTORE_VIF'
with mock.patch.object(
self.policy_api, "create_or_update") as api_call, \
@ -5431,8 +5484,7 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase):
attachment_type=attachment_type, vif_id=vif_id, app_id=app_id,
context_id=context_id, traffic_tag=traffic_tag,
allocate_addresses=allocate_addresses, tags=tags,
tenant=TEST_TENANT, hyperbus_mode=hyperbus_mode,
init_state=init_state)
tenant=TEST_TENANT, hyperbus_mode=hyperbus_mode)
expected_def = core_defs.SegmentPortDef(
nsx_version=self.policy_lib.get_version(),
segment_id=segment_id,

View File

@ -2546,7 +2546,12 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase):
extra_configs=extra_configs,
tags=tags,
tenant=tenant)
self._create_or_store(port_def)
if init_state == nsx_constants.INIT_STATE_RESTORE_VIF:
path = port_def.get_resource_path()
body = port_def.get_obj_dict()
self.policy_api.client.create(path, body=body)
else:
self._create_or_store(port_def)
return port_id
def delete(self, segment_id, port_id,