Allow to use both name and id to update action definitions

Also added some database tests for get and delete action
definition by id.

Change-Id: I71dde2b91423ba1850653a076e241787d1f79572
Partially-Implements: blueprint mistral-actions-identifier
This commit is contained in:
hardik 2016-07-25 17:40:17 +05:30
parent d844aad75b
commit c3e776e10a
5 changed files with 68 additions and 17 deletions

View File

@ -61,7 +61,7 @@ class ActionsController(rest.RestController, hooks.HookController):
@rest_utils.wrap_pecan_controller_exception
@pecan.expose(content_type="text/plain")
def put(self):
def put(self, identifier=None):
"""Update one or more actions.
NOTE: This text is allowed to have definitions
@ -79,7 +79,11 @@ class ActionsController(rest.RestController, hooks.HookController):
)
with db_api.transaction():
db_acts = actions.update_actions(definition, scope=scope)
db_acts = actions.update_actions(
definition,
scope=scope,
identifier=identifier
)
models_dicts = [db_act.to_dict() for db_act in db_acts]
action_list = [resources.Action.from_dict(act) for act in models_dicts]

View File

@ -185,8 +185,8 @@ def create_action_definition(values):
return IMPL.create_action_definition(values)
def update_action_definition(name, values):
return IMPL.update_action_definition(name, values)
def update_action_definition(identifier, values):
return IMPL.update_action_definition(identifier, values)
def create_or_update_action_definition(name, values):

View File

@ -526,13 +526,8 @@ def create_action_definition(values, session=None):
@b.session_aware()
def update_action_definition(name, values, session=None):
a_def = _get_db_object_by_name(models.ActionDefinition, name)
if not a_def:
raise exc.DBEntityNotFoundError(
"Action definition not found [action_name=%s]" % name
)
def update_action_definition(identifier, values, session=None):
a_def = get_action_definition(identifier)
a_def.update(values.copy())

View File

@ -30,13 +30,25 @@ def create_actions(definition, scope='private'):
return db_actions
def update_actions(definition, scope='private'):
def update_actions(definition, scope='private', identifier=None):
action_list_spec = spec_parser.get_action_list_spec_from_yaml(definition)
actions = action_list_spec.get_actions()
if identifier and len(actions) > 1:
raise exc.InputException(
"More than one actions are not supported for update with UUID "
"provided."
)
db_actions = []
for action_spec in action_list_spec.get_actions():
db_actions.append(update_action(action_spec, definition, scope))
db_actions.append(update_action(
action_spec,
definition,
scope,
identifier=identifier
))
return db_actions
@ -60,7 +72,7 @@ def create_action(action_spec, definition, scope):
)
def update_action(action_spec, definition, scope):
def update_action(action_spec, definition, scope, identifier=None):
action = db_api.load_action_definition(action_spec.get_name())
if action and action.is_system:
@ -71,7 +83,10 @@ def update_action(action_spec, definition, scope):
values = _get_action_values(action_spec, definition, scope)
return db_api.update_action_definition(values['name'], values)
return db_api.update_action_definition(
identifier if identifier else values['name'],
values
)
def create_or_update_action(action_spec, definition, scope):

View File

@ -589,6 +589,12 @@ class ActionDefinitionTest(SQLAlchemyTest):
self.assertIsNone(db_api.load_action_definition("not-existing-id"))
def test_get_action_definition_with_uuid(self):
created = db_api.create_action_definition(ACTION_DEFINITIONS[0])
fetched = db_api.get_action_definition(created.id)
self.assertEqual(created, fetched)
def test_create_action_definition_duplicate_without_auth(self):
cfg.CONF.set_default('auth_enable', False, group='pecan')
db_api.create_action_definition(ACTION_DEFINITIONS[0])
@ -599,7 +605,7 @@ class ActionDefinitionTest(SQLAlchemyTest):
ACTION_DEFINITIONS[0]
)
def test_update_action_definition(self):
def test_update_action_definition_with_name(self):
created = db_api.create_action_definition(ACTION_DEFINITIONS[0])
self.assertIsNone(created.updated_at)
@ -616,6 +622,22 @@ class ActionDefinitionTest(SQLAlchemyTest):
self.assertEqual(updated, fetched)
self.assertIsNotNone(fetched.updated_at)
def test_update_action_definition_with_uuid(self):
created = db_api.create_action_definition(ACTION_DEFINITIONS[0])
self.assertIsNone(created.updated_at)
updated = db_api.update_action_definition(
created.id,
{'description': 'my new desc'}
)
self.assertEqual('my new desc', updated.description)
fetched = db_api.get_action_definition(created.id)
self.assertEqual(updated, fetched)
def test_create_or_update_action_definition(self):
name = 'not-existing-id'
@ -654,7 +676,7 @@ class ActionDefinitionTest(SQLAlchemyTest):
self.assertEqual(created0, fetched[0])
self.assertEqual(created1, fetched[1])
def test_delete_action_definition(self):
def test_delete_action_definition_with_name(self):
created = db_api.create_action_definition(ACTION_DEFINITIONS[0])
fetched = db_api.get_action_definition(created.name)
@ -669,6 +691,21 @@ class ActionDefinitionTest(SQLAlchemyTest):
created.name
)
def test_delete_action_definition_with_uuid(self):
created = db_api.create_action_definition(ACTION_DEFINITIONS[0])
fetched = db_api.get_action_definition(created.id)
self.assertEqual(created, fetched)
db_api.delete_action_definition(created.id)
self.assertRaises(
exc.DBEntityNotFoundError,
db_api.get_action_definition,
created.id
)
def test_action_definition_repr(self):
s = db_api.create_action_definition(ACTION_DEFINITIONS[0]).__repr__()