Browse Source

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)
changes/63/678563/1
Michael Johnson 1 month ago
parent
commit
bdd0d44e99
2 changed files with 26 additions and 0 deletions
  1. 8
    0
      octavia/db/repositories.py
  2. 18
    0
      octavia/tests/functional/db/test_repositories.py

+ 8
- 0
octavia/db/repositories.py View File

@@ -1531,6 +1531,10 @@ class L7RuleRepository(BaseRepository):
1531 1531
         with session.begin(subtransactions=True):
1532 1532
             if not model_kwargs.get('id'):
1533 1533
                 model_kwargs.update(id=uuidutils.generate_uuid())
1534
+            if model_kwargs.get('l7policy_id'):
1535
+                l7policy_db = session.query(models.L7Policy).filter_by(
1536
+                    id=model_kwargs.get('l7policy_id')).first()
1537
+                model_kwargs.update(l7policy=l7policy_db)
1534 1538
             l7rule = self.model_class(**model_kwargs)
1535 1539
             validate.l7rule_data(l7rule)
1536 1540
             session.add(l7rule)
@@ -1679,6 +1683,10 @@ class L7PolicyRepository(BaseRepository):
1679 1683
                 pool_db = session.query(models.Pool).filter_by(
1680 1684
                     id=model_kwargs.get('redirect_pool_id')).first()
1681 1685
                 model_kwargs.update(redirect_pool=pool_db)
1686
+            if model_kwargs.get('listener_id'):
1687
+                listener_db = session.query(models.Listener).filter_by(
1688
+                    id=model_kwargs.get('listener_id')).first()
1689
+                model_kwargs.update(listener=listener_db)
1682 1690
             l7policy = self.model_class(
1683 1691
                 **validate.sanitize_l7policy_api_args(model_kwargs,
1684 1692
                                                       create=True))

+ 18
- 0
octavia/tests/functional/db/test_repositories.py View File

@@ -3582,6 +3582,14 @@ class L7PolicyRepositoryTest(BaseRepositoryTest):
3582 3582
                          new_l7policy.action)
3583 3583
         self.assertEqual(1, new_l7policy.position)
3584 3584
 
3585
+    def test_l7policy_create_no_listener_id(self):
3586
+        self.assertRaises(
3587
+            db_exception.DBError, self.l7policy_repo.create,
3588
+            self.session, action=constants.L7POLICY_ACTION_REJECT,
3589
+            operating_status=constants.ONLINE,
3590
+            provisioning_status=constants.ACTIVE,
3591
+            enabled=True)
3592
+
3585 3593
     def test_update(self):
3586 3594
         new_url = 'http://www.example.com/'
3587 3595
         listener = self.create_listener(uuidutils.generate_uuid(), 80)
@@ -3951,6 +3959,16 @@ class L7RuleRepositoryTest(BaseRepositoryTest):
3951 3959
         self.assertEqual('something', new_l7rule.value)
3952 3960
         self.assertFalse(new_l7rule.invert)
3953 3961
 
3962
+    def test_l7rule_create_wihout_l7policy_id(self):
3963
+        self.assertRaises(
3964
+            db_exception.DBError, self.l7rule_repo.create,
3965
+            self.session, id=None, type=constants.L7RULE_TYPE_PATH,
3966
+            compare_type=constants.L7RULE_COMPARE_TYPE_CONTAINS,
3967
+            provisioning_status=constants.ACTIVE,
3968
+            operating_status=constants.ONLINE,
3969
+            value='something',
3970
+            enabled=True)
3971
+
3954 3972
     def test_update(self):
3955 3973
         l7rule = self.create_l7rule(uuidutils.generate_uuid(),
3956 3974
                                     self.l7policy.id,

Loading…
Cancel
Save