From 600d8562e5e240e3432b59cfae8c28eb26d67e7c Mon Sep 17 00:00:00 2001 From: Danting Liu Date: Tue, 7 Apr 2020 20:14:03 -0700 Subject: [PATCH] Add 'X-Allow-Overwrite' in header for policy API force update Change-Id: I8449bb9313b7f2e14484ca58b307e80419356246 --- .../tests/unit/v3/policy/test_lb_resources.py | 3 ++- vmware_nsxlib/tests/unit/v3/policy/test_resources.py | 5 +++-- vmware_nsxlib/v3/policy/core_defs.py | 8 +++++++- vmware_nsxlib/v3/policy/core_resources.py | 10 ++++++---- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_lb_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_lb_resources.py index 7e93a14f..954cc770 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_lb_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_lb_resources.py @@ -1115,7 +1115,8 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase): virtual_server_id=obj_id, name=vs_name, max_concurrent_connections=None, tenant=TEST_TENANT) - update_call.assert_called_with(mock.ANY, partial_updates=False) + update_call.assert_called_with(mock.ANY, partial_updates=False, + force=False) self.assert_called_with_def(update_call, expected_def) def test_add_lb_rule(self): diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index f245f351..66031700 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -5881,14 +5881,15 @@ class TestPolicyTier0RouteMap(NsxPolicyLibTestCase): with self.mock_get(tier0_id, name), \ self.mock_create_update() as update_call: self.resourceApi.update(name, tier0_id, route_map_id, entries, - tenant=TEST_TENANT) + tenant=TEST_TENANT, force=True) expected_def = core_defs.Tier0RouteMapDef( tier0_id=tier0_id, route_map_id=route_map_id, name=name, entries=entries, tenant=TEST_TENANT) - + update_call.assert_called_with(mock.ANY, partial_updates=True, + force=True) self.assert_called_with_def(update_call, expected_def) def test_build_route_map_entry(self): diff --git a/vmware_nsxlib/v3/policy/core_defs.py b/vmware_nsxlib/v3/policy/core_defs.py index 71112d63..469cfc9d 100644 --- a/vmware_nsxlib/v3/policy/core_defs.py +++ b/vmware_nsxlib/v3/policy/core_defs.py @@ -2365,7 +2365,8 @@ class NsxPolicyApi(object): def partial_updates_supported(self): return self.partial_updates - def create_or_update(self, resource_def, partial_updates=False): + def create_or_update(self, resource_def, partial_updates=False, + force=False): """Create or update a policy object. This api will update an existing object, or create a new one if it @@ -2380,6 +2381,11 @@ class NsxPolicyApi(object): headers = None if partial_updates: headers = {'nsx-enable-partial-patch': 'true'} + if force: + if headers: + headers['X-Allow-Overwrite'] = 'true' + else: + headers = {'X-Allow-Overwrite': 'true'} self.client.patch(path, body, headers=headers) def create_with_parent(self, parent_def, resource_def): diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 07a81fe3..8b8aed12 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -143,7 +143,7 @@ class NsxPolicyResourceBase(object): return resource_def - def _update(self, allow_partial_updates=True, **kwargs): + def _update(self, allow_partial_updates=True, force=False, **kwargs): """Helper for update function - ignore attrs without explicit value""" if (allow_partial_updates and self.policy_api.partial_updates_supported()): @@ -157,7 +157,7 @@ class NsxPolicyResourceBase(object): # Nothing to update - only keys provided in kwargs return self.policy_api.create_or_update( - policy_def, partial_updates=partial_updates) + policy_def, partial_updates=partial_updates, force=force) @staticmethod def _init_obj_uuid(obj_uuid): @@ -4480,14 +4480,16 @@ class NsxPolicyTier0RouteMapApi(NsxPolicyResourceBase): entries, description=IGNORE, tags=IGNORE, - tenant=constants.POLICY_INFRA_TENANT): + tenant=constants.POLICY_INFRA_TENANT, + force=False): self._update(tier0_id=tier0_id, route_map_id=route_map_id, name=name, entries=entries, description=description, tags=tags, - tenant=tenant) + tenant=tenant, + force=force) def build_route_map_entry(self, action, community_list_matches=None, prefix_list_matches=None, entry_set=None):