Raise explicit exception when deleting config which has been referenced

This change aims to raise an explicit exception when trying to delete
a software config which has been referenced by deployment.

Change-Id: Ifa9492f003910b07fce4891fc88a566d6340f5a3
Closes-Bug: #1509915
This commit is contained in:
xiaolihope 2016-03-18 12:31:42 +08:00
parent 559a4830dc
commit e767eb48b1
2 changed files with 27 additions and 0 deletions

View File

@ -915,6 +915,13 @@ def software_config_get_all(context, limit=None, marker=None,
def software_config_delete(context, config_id):
config = software_config_get(context, config_id)
# Query if the software config has been referenced by deployment.
result = model_query(context, models.SoftwareDeployment).filter_by(
config_id=config_id).first()
if result:
msg = (_("Software config with id %s can not be deleted as "
"it is referenced.") % config_id)
raise exception.InvalidRestrictedAction(message=msg)
session = orm_session.Session.object_session(config)
with session.begin():
session.delete(config)

View File

@ -1105,6 +1105,26 @@ class SqlAlchemyTest(common.HeatTestCase):
self.ctx, config_id)
self.assertIn(config_id, six.text_type(err))
def test_software_config_delete_not_allowed(self):
tenant_id = self.ctx.tenant_id
config = db_api.software_config_create(
self.ctx, {'name': 'config_mysql',
'tenant': tenant_id})
config_id = config.id
values = {
'tenant': tenant_id,
'stack_user_project_id': str(uuid.uuid4()),
'config_id': config_id,
'server_id': str(uuid.uuid4()),
}
db_api.software_deployment_create(self.ctx, values)
err = self.assertRaises(
exception.InvalidRestrictedAction, db_api.software_config_delete,
self.ctx, config_id)
msg = ("Software config with id %s can not be deleted as it is "
"referenced" % config_id)
self.assertIn(msg, six.text_type(err))
def _deployment_values(self):
tenant_id = self.ctx.tenant_id
stack_user_project_id = str(uuid.uuid4())