From 55b04f279bb2587b8e78667f386f582b347f4e03 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Fri, 31 May 2019 13:09:42 -0700 Subject: [PATCH] Fix l7rule API handling of None updates The current l7rule API does not properly handle clearing/reseting values on update. This patch corrects this to appropriately handle None/null updates to the l7rule parameters. Story: 2005374 Task: 36023 Change-Id: I4e8007ff98f585ce6622f7128ecdd53cdfa926a7 (cherry picked from commit e4e7b4a880c19054b63cd295cb19adee527517e5) --- octavia/api/v2/controllers/l7rule.py | 5 +++++ octavia/common/validate.py | 2 ++ octavia/tests/functional/api/v2/test_l7rule.py | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/octavia/api/v2/controllers/l7rule.py b/octavia/api/v2/controllers/l7rule.py index f37f794193..8d1c8a56e3 100644 --- a/octavia/api/v2/controllers/l7rule.py +++ b/octavia/api/v2/controllers/l7rule.py @@ -192,6 +192,11 @@ class L7RuleController(base.BaseController): context = pecan.request.context.get('octavia_context') db_l7rule = self._get_db_l7rule(context.session, id, show_deleted=False) + + # Handle the invert unset + if l7rule.invert is None: + l7rule.invert = False + new_l7rule = db_l7rule.to_dict() new_l7rule.update(l7rule.to_dict()) new_l7rule = data_models.L7Rule.from_dict(new_l7rule) diff --git a/octavia/common/validate.py b/octavia/common/validate.py index 947487b90b..33a3b96bdc 100644 --- a/octavia/common/validate.py +++ b/octavia/common/validate.py @@ -107,6 +107,8 @@ def regex(regex): # L7 rules must be internally consistent. def l7rule_data(l7rule): """Raises an error if the l7rule given is invalid in some way.""" + if not l7rule.value: + raise exceptions.InvalidL7Rule(msg=_('L7 rule type requires a value')) if l7rule.type == constants.L7RULE_TYPE_HEADER: if not l7rule.key: raise exceptions.InvalidL7Rule(msg='L7 rule type requires a key') diff --git a/octavia/tests/functional/api/v2/test_l7rule.py b/octavia/tests/functional/api/v2/test_l7rule.py index 0ba838f2d9..42c03aff67 100644 --- a/octavia/tests/functional/api/v2/test_l7rule.py +++ b/octavia/tests/functional/api/v2/test_l7rule.py @@ -1004,6 +1004,18 @@ class TestL7Rule(base.BaseAPITest): self._test_bad_cases_with_ssl_rule_types( is_create=False, rule_id=api_l7rule.get('id')) + def test_update_invert_none(self): + api_l7rule = self.create_l7rule( + self.l7policy_id, constants.L7RULE_TYPE_PATH, + constants.L7RULE_COMPARE_TYPE_STARTS_WITH, + '/api', tags=['old_tag'], invert=True).get(self.root_tag) + self.set_lb_status(self.lb_id) + new_l7rule = {'invert': None} + response = self.put(self.l7rule_path.format( + l7rule_id=api_l7rule.get('id')), + self._build_body(new_l7rule)).json.get(self.root_tag) + self.assertFalse(response.get('invert')) + def test_delete(self): api_l7rule = self.create_l7rule( self.l7policy_id, constants.L7RULE_TYPE_PATH,