From a94e5af899936e3fb7e554cfccb4d66903f34c6b Mon Sep 17 00:00:00 2001 From: Andrew Karpow Date: Fri, 2 Oct 2020 17:32:52 +0200 Subject: [PATCH] Support deletion of all tags Add support for removal of all tags by PUTing empty tags array. Also moved assignment after initial session query for the object in the listener update path. Conflicts: octavia/db/repositories.py Task: #41009 Story: #2008220 Change-Id: I7488f2fae61917f6d4a56cedd05bace7c5e2bc70 Signed-off-by: Andrew Karpow (cherry picked from commit 7ad022379ff2e366d3dfd73113d4619b0137551c) (cherry picked from commit adb4e78e4439181f57c17f277bad0cd325f219cf) (cherry picked from commit 95a97cf1eba85222e4ff683b88878f0f030a2f11) --- octavia/db/repositories.py | 10 +++++----- .../tests/functional/api/v2/test_load_balancer.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/octavia/db/repositories.py b/octavia/db/repositories.py index 080bb21263..8917e506f3 100644 --- a/octavia/db/repositories.py +++ b/octavia/db/repositories.py @@ -106,7 +106,7 @@ class BaseRepository(object): """ with session.begin(subtransactions=True): tags = model_kwargs.pop('tags', None) - if tags: + if tags is not None: resource = session.query(self.model_class).get(id) resource.tags = tags session.query(self.model_class).filter_by( @@ -1061,12 +1061,12 @@ class ListenerRepository(BaseRepository): def update(self, session, id, **model_kwargs): with session.begin(subtransactions=True): - tags = model_kwargs.pop('tags', None) - if tags: - resource = session.query(self.model_class).get(id) - resource.tags = tags listener_db = session.query(self.model_class).filter_by( id=id).first() + tags = model_kwargs.pop('tags', None) + if tags is not None: + resource = session.query(self.model_class).get(id) + resource.tags = tags # Verify any newly specified default_pool_id exists default_pool_id = model_kwargs.get('default_pool_id') if default_pool_id: diff --git a/octavia/tests/functional/api/v2/test_load_balancer.py b/octavia/tests/functional/api/v2/test_load_balancer.py index 9193934eec..53d78440d1 100644 --- a/octavia/tests/functional/api/v2/test_load_balancer.py +++ b/octavia/tests/functional/api/v2/test_load_balancer.py @@ -1780,6 +1780,21 @@ class TestLoadBalancer(base.BaseAPITest): self.assert_correct_lb_status(api_lb.get('id'), constants.ONLINE, constants.PENDING_UPDATE) + def test_update_delete_tag(self): + project_id = uuidutils.generate_uuid() + lb = self.create_load_balancer(uuidutils.generate_uuid(), + name='lb1', + project_id=project_id, + admin_state_up=False, + tags=['test_tag1'],) + lb_dict = lb.get(self.root_tag) + lb_json = self._build_body({'tags': []}) + self.set_lb_status(lb_dict.get('id')) + response = self.put(self.LB_PATH.format(lb_id=lb_dict.get('id')), + lb_json) + api_lb = response.json.get(self.root_tag) + self.assertEqual([], api_lb.get('tags')) + def test_update_with_vip(self): project_id = uuidutils.generate_uuid() lb = self.create_load_balancer(uuidutils.generate_uuid(),