Merge "Add a db_api interface for deleting actions"

This commit is contained in:
Jenkins 2016-06-08 07:47:28 +00:00 committed by Gerrit Code Review
commit 448fa35d51
3 changed files with 44 additions and 2 deletions

View File

@ -336,6 +336,10 @@ def action_check_status(context, action_id, timestamp):
return IMPL.action_check_status(context, action_id, timestamp)
def action_delete_by_target(context, target, exceptions=None):
return IMPL.action_check_status(context, target, exceptions=exceptions)
def dependency_add(context, depended, dependent):
return IMPL.dependency_add(context, depended, dependent)

View File

@ -862,6 +862,15 @@ def action_check_status(context, action_id, timestamp):
return action.status
def action_delete_by_target(context, target, exceptions=None):
with session_for_write() as session:
q = session.query(models.Action).\
filter(models.Action.target == target)
if exceptions:
q = q.filter(~models.Action.action.in_(exceptions))
return q.delete(synchronize_session='fetch')
def dependency_get_depended(context, action_id):
with session_for_read() as session:
q = session.query(models.ActionDependency).filter_by(

View File

@ -23,8 +23,8 @@ from senlin.tests.unit.common import utils
from senlin.tests.unit.db import shared
def _create_action(context, action=shared.sample_action, **kwargs):
data = parser.simple_parse(action)
def _create_action(context, action_json=shared.sample_action, **kwargs):
data = parser.simple_parse(action_json)
data['user'] = context.user
data['project'] = context.project
data['domain'] = context.domain
@ -405,3 +405,32 @@ class DBAPIActionTest(base.SenlinTestCase):
self.ctx, action.id)
self.assertEqual('The action (%s) is busy now.' % action.id,
six.text_type(ex))
def test_action_delete_by_target(self):
for name in ['CLUSTER_CREATE', 'CLUSTER_RESIZE', 'CLUSTER_DELETE']:
action = _create_action(self.ctx, action=name, target='CLUSTER_ID')
self.assertIsNotNone(action)
action = _create_action(self.ctx, action=name,
target='CLUSTER_ID_2')
self.assertIsNotNone(action)
actions = db_api.action_get_all(self.ctx)
self.assertEqual(6, len(actions))
db_api.action_delete_by_target(self.ctx, 'CLUSTER_ID')
actions = db_api.action_get_all(self.ctx)
self.assertEqual(3, len(actions))
def test_action_delete_by_target_with_exceptions(self):
for name in ['CLUSTER_CREATE', 'CLUSTER_RESIZE', 'CLUSTER_DELETE']:
action = _create_action(self.ctx, action=name, target='CLUSTER_ID')
self.assertIsNotNone(action)
actions = db_api.action_get_all(self.ctx)
self.assertEqual(3, len(actions))
db_api.action_delete_by_target(self.ctx, 'CLUSTER_ID',
['CLUSTER_DELETE'])
actions = db_api.action_get_all(self.ctx)
self.assertEqual(1, len(actions))
self.assertEqual('CLUSTER_DELETE', actions[0].action)