diff --git a/mistral/api/controllers/v2/action.py b/mistral/api/controllers/v2/action.py index 0ebab5f90..108f5da37 100644 --- a/mistral/api/controllers/v2/action.py +++ b/mistral/api/controllers/v2/action.py @@ -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] diff --git a/mistral/db/v2/api.py b/mistral/db/v2/api.py index b66e25d6a..1dcdfdded 100644 --- a/mistral/db/v2/api.py +++ b/mistral/db/v2/api.py @@ -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): diff --git a/mistral/db/v2/sqlalchemy/api.py b/mistral/db/v2/sqlalchemy/api.py index 2b7a63e44..d1089f9c6 100644 --- a/mistral/db/v2/sqlalchemy/api.py +++ b/mistral/db/v2/sqlalchemy/api.py @@ -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()) diff --git a/mistral/services/actions.py b/mistral/services/actions.py index d570e8d01..37d787742 100644 --- a/mistral/services/actions.py +++ b/mistral/services/actions.py @@ -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): diff --git a/mistral/tests/unit/db/v2/test_sqlalchemy_db_api.py b/mistral/tests/unit/db/v2/test_sqlalchemy_db_api.py index ca74e4ab5..a126af5da 100644 --- a/mistral/tests/unit/db/v2/test_sqlalchemy_db_api.py +++ b/mistral/tests/unit/db/v2/test_sqlalchemy_db_api.py @@ -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__()