Fix L7 repository create methods

SQLAlchemy can get confused if the parent database objects are not
linked when creating new records using the repository.
For example, when creating an L7 policy, even if you specify the
listener_id, sqlalchemy will drop the ID from the final SQL INSERT
parameter.
This patch corrects the L7 policy and rule create methods to have
the required parent objects linked.

Story: 2006305
Task: 36021

Change-Id: I4380605bdb346eee824b2ce05fa25175c4cc3034
(cherry picked from commit 36afa82d0c)
This commit is contained in:
Michael Johnson 2019-07-27 15:41:57 -07:00 committed by Carlos Goncalves
parent 4b7fe7f80c
commit bdd0d44e99
2 changed files with 26 additions and 0 deletions

View File

@ -1531,6 +1531,10 @@ class L7RuleRepository(BaseRepository):
with session.begin(subtransactions=True):
if not model_kwargs.get('id'):
model_kwargs.update(id=uuidutils.generate_uuid())
if model_kwargs.get('l7policy_id'):
l7policy_db = session.query(models.L7Policy).filter_by(
id=model_kwargs.get('l7policy_id')).first()
model_kwargs.update(l7policy=l7policy_db)
l7rule = self.model_class(**model_kwargs)
validate.l7rule_data(l7rule)
session.add(l7rule)
@ -1679,6 +1683,10 @@ class L7PolicyRepository(BaseRepository):
pool_db = session.query(models.Pool).filter_by(
id=model_kwargs.get('redirect_pool_id')).first()
model_kwargs.update(redirect_pool=pool_db)
if model_kwargs.get('listener_id'):
listener_db = session.query(models.Listener).filter_by(
id=model_kwargs.get('listener_id')).first()
model_kwargs.update(listener=listener_db)
l7policy = self.model_class(
**validate.sanitize_l7policy_api_args(model_kwargs,
create=True))

View File

@ -3582,6 +3582,14 @@ class L7PolicyRepositoryTest(BaseRepositoryTest):
new_l7policy.action)
self.assertEqual(1, new_l7policy.position)
def test_l7policy_create_no_listener_id(self):
self.assertRaises(
db_exception.DBError, self.l7policy_repo.create,
self.session, action=constants.L7POLICY_ACTION_REJECT,
operating_status=constants.ONLINE,
provisioning_status=constants.ACTIVE,
enabled=True)
def test_update(self):
new_url = 'http://www.example.com/'
listener = self.create_listener(uuidutils.generate_uuid(), 80)
@ -3951,6 +3959,16 @@ class L7RuleRepositoryTest(BaseRepositoryTest):
self.assertEqual('something', new_l7rule.value)
self.assertFalse(new_l7rule.invert)
def test_l7rule_create_wihout_l7policy_id(self):
self.assertRaises(
db_exception.DBError, self.l7rule_repo.create,
self.session, id=None, type=constants.L7RULE_TYPE_PATH,
compare_type=constants.L7RULE_COMPARE_TYPE_CONTAINS,
provisioning_status=constants.ACTIVE,
operating_status=constants.ONLINE,
value='something',
enabled=True)
def test_update(self):
l7rule = self.create_l7rule(uuidutils.generate_uuid(),
self.l7policy.id,