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 686be4ad..ad7c012c 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_lb_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_lb_resources.py @@ -864,6 +864,45 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase): lb_rule]) self.assert_called_with_def(update_call, expected_def) + def test_add_lb_rule_last_over(self): + vs_obj_id = '111' + vs_name = 'name-name' + vs_ports = [80] + vs_ip_address = '1.1.1.1' + app_prof_id = 'xxxx' + app_prof_path = '/test/lb-app-profiles/' + app_prof_id + rule_actions = 'test1' + rule_match_conditions = 'test2' + rule_name = 'dummy_rule' + rule_match_strategy = 'test3' + rule_phase = 'test4' + with mock.patch.object( + self.policy_api, "get", return_value={ + 'ip_address': vs_ip_address, + 'ports': vs_ports, + 'display_name': vs_name, + 'rules': [{'display_name': 'xx'}, {'display_name': 'yy'}], + 'application_profile_path': app_prof_path}), \ + mock.patch.object(self.policy_api, + "create_or_update") as update_call: + self.resourceApi.add_lb_rule( + vs_obj_id, actions=rule_actions, name=rule_name, + match_conditions=rule_match_conditions, + match_strategy=rule_match_strategy, phase=rule_phase, + position=999) + lb_rule = lb_defs.LBRuleDef( + rule_actions, rule_match_conditions, rule_name, + rule_match_strategy, rule_phase) + + expected_def = lb_defs.LBVirtualServerDef( + virtual_server_id=vs_obj_id, name=vs_name, + ip_address=vs_ip_address, application_profile_id=app_prof_id, + ports=vs_ports, + rules=[{'display_name': 'xx'}, + {'display_name': 'yy'}, + lb_rule]) + self.assert_called_with_def(update_call, expected_def) + def test_add_lb_rule_mid(self): vs_obj_id = '111' vs_name = 'name-name' diff --git a/vmware_nsxlib/v3/policy/lb_resources.py b/vmware_nsxlib/v3/policy/lb_resources.py index cba43c38..85efd5b7 100644 --- a/vmware_nsxlib/v3/policy/lb_resources.py +++ b/vmware_nsxlib/v3/policy/lb_resources.py @@ -820,15 +820,11 @@ class NsxPolicyLoadBalancerVirtualServerAPI(NsxPolicyResourceBase): def _add_rule_in_position(self, body, lb_rule, position): lb_rules = body.get('rules', []) - if position < 0: + if position < 0 or position > len(lb_rules): + # Add as the last one lb_rules.append(lb_rule) elif position <= len(lb_rules): lb_rules.insert(position, lb_rule) - else: - raise nsxlib_exc.InvalidInput( - operation='Insert rule in position', - arg_val=position, - arg_name='position') return lb_rules