Adding LB rule as last if the possition is too big

Change-Id: I3ead08b2dac1395aaaf998a4f47b8657e81c4c93
This commit is contained in:
Adit Sarfaty 2019-04-22 08:38:15 +03:00
parent ff1523d377
commit a92a32b619
2 changed files with 41 additions and 6 deletions

View File

@ -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'

View File

@ -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