From 0fa08284322d7c30a2cc576b7712d21bbc0f03cd Mon Sep 17 00:00:00 2001 From: Brandon Logan Date: Thu, 28 Jul 2016 14:27:27 -0500 Subject: [PATCH] Allow deletion of ERRORed load balancers The API was not allowing the deletion of load balancers in ERROR state. Users should be able to delete their load balancers in ACTIVE or ERROR state. This is good for users and good for operators. Change-Id: Ie40b062e1b5cc6d90274190676fb81ee1c32e63e Closes-Bug: #1607502 --- octavia/common/constants.py | 1 + octavia/db/repositories.py | 7 ++++++- octavia/tests/functional/api/v1/test_load_balancer.py | 6 ++++++ octavia/tests/functional/db/test_repositories.py | 11 +++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/octavia/common/constants.py b/octavia/common/constants.py index 93f5a5a3fa..f1e4c58448 100644 --- a/octavia/common/constants.py +++ b/octavia/common/constants.py @@ -63,6 +63,7 @@ SUPPORTED_PROVISIONING_STATUSES = (ACTIVE, AMPHORA_ALLOCATED, PENDING_DELETE, PENDING_CREATE, PENDING_UPDATE, DELETED, ERROR) MUTABLE_STATUSES = (ACTIVE,) +DELETABLE_STATUSES = (ACTIVE, ERROR) SUPPORTED_AMPHORA_STATUSES = (AMPHORA_ALLOCATED, AMPHORA_BOOTING, AMPHORA_READY, DELETED, PENDING_DELETE) diff --git a/octavia/db/repositories.py b/octavia/db/repositories.py index 2a0e41791a..6d0da7399f 100644 --- a/octavia/db/repositories.py +++ b/octavia/db/repositories.py @@ -322,7 +322,12 @@ class LoadBalancerRepository(BaseRepository): with session.begin(subtransactions=True): lb = session.query(self.model_class).with_for_update().filter_by( id=id).one() - if lb.provisioning_status not in constants.MUTABLE_STATUSES: + is_delete = status == constants.PENDING_DELETE + acceptable_statuses = ( + constants.DELETABLE_STATUSES + if is_delete else constants.MUTABLE_STATUSES + ) + if lb.provisioning_status not in acceptable_statuses: return False lb.provisioning_status = status session.add(lb) diff --git a/octavia/tests/functional/api/v1/test_load_balancer.py b/octavia/tests/functional/api/v1/test_load_balancer.py index 48ac47c6a5..c44c9904a4 100644 --- a/octavia/tests/functional/api/v1/test_load_balancer.py +++ b/octavia/tests/functional/api/v1/test_load_balancer.py @@ -200,6 +200,12 @@ class TestLoadBalancer(base.BaseAPITest): self.put(self.LB_PATH.format(lb_id=lb.get('id')), lb_json) self.delete(self.LB_PATH.format(lb_id=lb.get('id')), status=409) + def test_delete_with_error_status(self): + lb = self.create_load_balancer({}, name='lb1', description='desc1', + enabled=False) + lb = self.set_lb_status(lb.get('id'), status=constants.ERROR) + self.delete(self.LB_PATH.format(lb_id=lb.get('id')), status=202) + def test_update_pending_delete(self): lb = self.create_load_balancer({}, name='lb1', description='desc1', enabled=False) diff --git a/octavia/tests/functional/db/test_repositories.py b/octavia/tests/functional/db/test_repositories.py index d7e700ee7f..dea6681219 100644 --- a/octavia/tests/functional/db/test_repositories.py +++ b/octavia/tests/functional/db/test_repositories.py @@ -1250,6 +1250,17 @@ class LoadBalancerRepositoryTest(BaseRepositoryTest): lb = self.lb_repo.get(self.session, id=lb_id) self.assertEqual(constants.PENDING_UPDATE, lb.provisioning_status) + def test_test_and_set_provisioning_status_error_on_delete(self): + lb_id = uuidutils.generate_uuid() + self.lb_repo.create(self.session, id=lb_id, + provisioning_status=constants.ERROR, + operating_status=constants.OFFLINE, + enabled=True) + self.lb_repo.test_and_set_provisioning_status( + self.session, lb_id, constants.PENDING_DELETE) + lb = self.lb_repo.get(self.session, id=lb_id) + self.assertEqual(constants.PENDING_DELETE, lb.provisioning_status) + def test_check_load_balancer_expired_default_exp_age(self): """When exp_age defaults to load_balancer_expiry_age.""" newdate = datetime.datetime.utcnow() - datetime.timedelta(minutes=10)