From cfe4ed8e274d9322065a1a6f2c8bd8c74f6ca81f Mon Sep 17 00:00:00 2001 From: Shawn Wang Date: Tue, 8 Feb 2022 14:50:45 -0800 Subject: [PATCH] Add force update of Policy T1 Adv Rules In certain cases, caller would need to add route advertisment rules on Policy Tier1 owned by other accounts. This change adds the support by propagating the "force" param to include X-Allow-Overwrite header in the final API call. The same operation is already allowed in MP counterpart. Change-Id: Ic09fb16dd2403f33323c179d68fd2f1f3ce4bb42 --- .../tests/unit/v3/policy/test_resources.py | 81 ++++++++++--------- vmware_nsxlib/v3/policy/core_resources.py | 14 ++-- 2 files changed, 53 insertions(+), 42 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index 43ee6136..07113c67 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -3567,30 +3567,33 @@ class TestPolicyTier1(NsxPolicyLibTestCase): rule_pfx_operator = 'GE' rule_adv_types = ['A'] rule_subnets = ['x', 'y', 'z'] - with mock.patch.object(self.policy_api, - "get", - return_value={'id': tier1_id, - 'resource_type': 'Tier1'}),\ - mock.patch.object(self.policy_api, - 'create_or_update') as api_call: - self.resourceApi.add_advertisement_rule( - tier1_id, rule_name, action=rule_action, - prefix_operator=rule_pfx_operator, - route_advertisement_types=rule_adv_types, subnets=rule_subnets, - tenant=TEST_TENANT) + for force in [True, False]: + with mock.patch.object(self.policy_api, + "get", + return_value={'id': tier1_id, + 'resource_type': 'Tier1'}),\ + mock.patch.object(self.policy_api, + 'create_or_update') as api_call: + self.resourceApi.add_advertisement_rule( + tier1_id, rule_name, action=rule_action, + prefix_operator=rule_pfx_operator, + route_advertisement_types=rule_adv_types, + subnets=rule_subnets, force=force, tenant=TEST_TENANT) - expected_def = core_defs.Tier1Def( - tier1_id=tier1_id, - route_advertisement_rules=[ - core_defs.RouteAdvertisementRule( - rule_name, - action=rule_action, - prefix_operator=rule_pfx_operator, - route_advertisement_types=rule_adv_types, - subnets=rule_subnets)], - tenant=TEST_TENANT) + expected_def = core_defs.Tier1Def( + tier1_id=tier1_id, + route_advertisement_rules=[ + core_defs.RouteAdvertisementRule( + rule_name, + action=rule_action, + prefix_operator=rule_pfx_operator, + route_advertisement_types=rule_adv_types, + subnets=rule_subnets)], + tenant=TEST_TENANT) - self.assert_called_with_def(api_call, expected_def) + self.assert_called_with_def_and_kwargs( + api_call, expected_def, force=force, + partial_updates=self.partial_updates) def test_remove_advertisement_rule(self): tier1_id = '111' @@ -3604,14 +3607,16 @@ class TestPolicyTier1(NsxPolicyLibTestCase): mock.patch.object(self.policy_api, 'create_or_update') as api_call: self.resourceApi.remove_advertisement_rule( - tier1_id, rule_name, tenant=TEST_TENANT) + tier1_id, rule_name, force=True, tenant=TEST_TENANT) expected_def = core_defs.Tier1Def( tier1_id=tier1_id, route_advertisement_rules=[], tenant=TEST_TENANT) - self.assert_called_with_def(api_call, expected_def) + self.assert_called_with_def_and_kwargs( + api_call, expected_def, force=True, + partial_updates=self.partial_updates) def test_update_advertisement_rules(self): tier1_id = '111' @@ -3621,19 +3626,23 @@ class TestPolicyTier1(NsxPolicyLibTestCase): 'id': tier1_id, 'route_advertisement_rules': [{'name': old_rule}]} rules = [{'name': new_rule}] - with mock.patch.object(self.policy_api, - "get", - return_value=get_retval),\ - mock.patch.object(self.policy_api, - 'create_or_update') as api_call: - self.resourceApi.update_advertisement_rules( - tier1_id, rules, name_prefix=None, tenant=TEST_TENANT) + for force in [True, False]: + with mock.patch.object(self.policy_api, + "get", + return_value=get_retval),\ + mock.patch.object(self.policy_api, + 'create_or_update') as api_call: + self.resourceApi.update_advertisement_rules( + tier1_id, rules, name_prefix=None, force=force, + tenant=TEST_TENANT) - expected_def = core_defs.Tier1Def( - tier1_id=tier1_id, - route_advertisement_rules=rules, - tenant=TEST_TENANT) - self.assert_called_with_def(api_call, expected_def) + expected_def = core_defs.Tier1Def( + tier1_id=tier1_id, + route_advertisement_rules=rules, + tenant=TEST_TENANT) + self.assert_called_with_def_and_kwargs( + api_call, expected_def, force=force, + partial_updates=self.partial_updates) def test_update_advertisement_rules_with_replace(self): tier1_id = '111' diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 61944b93..68363eaf 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -1158,6 +1158,7 @@ class NsxPolicyTier1Api(NsxPolicyResourceBase): route_advertisement=IGNORE, route_advertisement_rules=IGNORE, pool_allocation=IGNORE, + force=False, tenant=constants.POLICY_INFRA_TENANT, current_body=None): @@ -1174,6 +1175,7 @@ class NsxPolicyTier1Api(NsxPolicyResourceBase): route_advertisement=route_advertisement, route_advertisement_rules=route_advertisement_rules, pool_allocation=pool_allocation, + force=force, tags=tags, tenant=tenant) @@ -1204,7 +1206,7 @@ class NsxPolicyTier1Api(NsxPolicyResourceBase): def add_advertisement_rule( self, tier1_id, name, action=None, prefix_operator=None, - route_advertisement_types=None, subnets=None, + route_advertisement_types=None, subnets=None, force=False, tenant=constants.POLICY_INFRA_TENANT): tier1_dict = self.get(tier1_id, tenant) adv_rules = tier1_dict.get('route_advertisement_rules', []) @@ -1217,10 +1219,10 @@ class NsxPolicyTier1Api(NsxPolicyResourceBase): adv_rules.append(adv_rule) self.update(tier1_id, route_advertisement_rules=adv_rules, - tenant=tenant, + force=force, tenant=tenant, current_body=tier1_dict) - def remove_advertisement_rule(self, tier1_id, name, + def remove_advertisement_rule(self, tier1_id, name, force=False, tenant=constants.POLICY_INFRA_TENANT): tier1_dict = self.get(tier1_id, tenant) adv_rules = tier1_dict.get('route_advertisement_rules', []) @@ -1228,7 +1230,7 @@ class NsxPolicyTier1Api(NsxPolicyResourceBase): if updated_adv_rules != adv_rules: self.update(tier1_id, route_advertisement_rules=updated_adv_rules, - tenant=tenant, + force=force, tenant=tenant, current_body=tier1_dict) def build_advertisement_rule(self, name, action=None, prefix_operator=None, @@ -1239,7 +1241,7 @@ class NsxPolicyTier1Api(NsxPolicyResourceBase): subnets=subnets) def update_advertisement_rules(self, tier1_id, rules=None, - name_prefix=None, + name_prefix=None, force=False, tenant=constants.POLICY_INFRA_TENANT): """Update the router advertisement rules @@ -1265,7 +1267,7 @@ class NsxPolicyTier1Api(NsxPolicyResourceBase): self.update(tier1_id, route_advertisement_rules=new_rules, - tenant=tenant, + force=force, tenant=tenant, current_body=tier1_dict) @staticmethod