Merge "NSX|V: Support changing the position of LBAAS L7 policy"

This commit is contained in:
Jenkins 2017-02-26 15:03:28 +00:00 committed by Gerrit Code Review
commit 4407e06bbe
2 changed files with 54 additions and 11 deletions

View File

@ -177,6 +177,35 @@ class EdgeL7PolicyManager(base_mgr.EdgeLoadbalancerBaseManager):
# update the backend with the new configuration
self.vcns.update_vip(edge_id, vse_id, vse)
def _update_app_rule_possition_in_virtual_server(self, edge_id, vse_id,
app_rule_id,
policy_position):
"""Move the new nsx application rule to another position"""
# Get the current virtual server configuration
vse = self.vcns.get_vip(edge_id, vse_id)[1]
# delete the policy (= application rule) from the list
if app_rule_id in vse['applicationRuleId']:
vse['applicationRuleId'].remove(app_rule_id)
# Add the policy (=application rule) in the correct position
# (position begins at 1)
if len(vse['applicationRuleId']) < policy_position:
vse['applicationRuleId'].append(app_rule_id)
else:
vse['applicationRuleId'].insert(policy_position - 1, app_rule_id)
# update the backend with the new configuration
self.vcns.update_vip(edge_id, vse_id, vse)
def _get_vse_id(self, context, pol):
lb_id = pol.listener.loadbalancer_id
list_id = pol.listener.id
listener_binding = nsxv_db.get_nsxv_lbaas_listener_binding(
context.session, lb_id, list_id)
if listener_binding:
return listener_binding['vse_id']
@log_helpers.log_method_call
def create(self, context, pol):
# find out the edge to be updated, by the listener of this policy
@ -198,12 +227,10 @@ class EdgeL7PolicyManager(base_mgr.EdgeLoadbalancerBaseManager):
# add the nsx application rule (neutron policy) to the nsx
# virtual server (neutron listener)
listener_binding = nsxv_db.get_nsxv_lbaas_listener_binding(
context.session, lb_id, pol.listener.id)
if listener_binding:
vse_id = self._get_vse_id(context, pol)
if vse_id:
self._add_app_rule_to_virtual_server(
edge_id, listener_binding['vse_id'], app_rule_id,
pol.position)
edge_id, vse_id, app_rule_id, pol.position)
except Exception as e:
with excutils.save_and_reraise_exception():
self.lbv2_driver.l7policy.failed_completion(context, pol)
@ -234,6 +261,14 @@ class EdgeL7PolicyManager(base_mgr.EdgeLoadbalancerBaseManager):
with locking.LockManager.get_lock(edge_id):
# update the backend application rule for the new policy
self.vcns.update_app_rule(edge_id, app_rule_id, app_rule)
# if the position changed - update it too
if old_pol.position != new_pol.position:
vse_id = self._get_vse_id(context, new_pol)
if vse_id:
self._update_app_rule_possition_in_virtual_server(
edge_id, vse_id, app_rule_id, new_pol.position)
except Exception as e:
with excutils.save_and_reraise_exception():
self.lbv2_driver.l7policy.failed_completion(context, new_pol)
@ -259,11 +294,8 @@ class EdgeL7PolicyManager(base_mgr.EdgeLoadbalancerBaseManager):
with locking.LockManager.get_lock(edge_id):
try:
# remove the nsx application rule from the virtual server
lb_id = pol.listener.loadbalancer_id
listener_binding = nsxv_db.get_nsxv_lbaas_listener_binding(
context.session, lb_id, pol.listener.id)
if listener_binding:
vse_id = listener_binding['vse_id']
vse_id = self._get_vse_id(context, pol)
if vse_id:
self._del_app_rule_from_virtual_server(
edge_id, vse_id, app_rule_id)

View File

@ -711,16 +711,26 @@ class TestEdgeLbaasV2L7Policy(BaseTestEdgeLbaasV2):
action='REDIRECT_TO_URL',
redirect_url=url,
listener=self.listener,
position=1)
position=2)
with mock.patch.object(nsxv_db, 'get_nsxv_lbaas_l7policy_binding'
) as mock_get_l7policy_binding, \
mock.patch.object(nsxv_db, 'get_nsxv_lbaas_loadbalancer_binding'
) as mock_get_lb_binding, \
mock.patch.object(nsxv_db, 'get_nsxv_lbaas_listener_binding'
) as mock_get_listener_binding, \
mock.patch.object(self.edge_driver.vcns, 'get_vip'
) as mock_get_vip, \
mock.patch.object(self.edge_driver.vcns, 'update_vip'
) as mock_upd_vip, \
mock.patch.object(self.edge_driver.vcns, 'update_app_rule'
) as mock_update_rule:
mock_get_lb_binding.return_value = LB_BINDING
mock_get_l7policy_binding.return_value = L7POL_BINDING
mock_get_listener_binding.return_value = LISTENER_BINDING
edge_vip_def = EDGE_VIP_DEF.copy()
edge_vip_def['applicationRuleId'] = [EDGE_RULE_ID]
mock_get_vip.return_value = (None, edge_vip_def)
self.edge_driver.l7policy.update(self.context, self.l7policy,
new_pol)
@ -729,6 +739,7 @@ class TestEdgeLbaasV2L7Policy(BaseTestEdgeLbaasV2):
edge_rule_def['script'] = "redirect location %s if TRUE" % url
mock_update_rule.assert_called_with(
LB_EDGE_ID, EDGE_RULE_ID, edge_rule_def)
mock_upd_vip.assert_called()
mock_successful_completion = (
self.lbv2_driver.l7policy.successful_completion)
mock_successful_completion.assert_called_with(self.context,