diff --git a/mistral/engine/actions/action_factory.py b/mistral/engine/actions/action_factory.py index 635fc27dd..d9041bbdd 100644 --- a/mistral/engine/actions/action_factory.py +++ b/mistral/engine/actions/action_factory.py @@ -22,32 +22,45 @@ import mistral.exceptions as exc def create_action(task): action_type = task['service_dsl']['type'] - if action_type == action_types.REST_API: - return get_rest_action(task) - elif action_type == action_types.OSLO_RPC: - return get_amqp_action(task) - else: + if not action_types.is_valid(action_type): raise exc.InvalidActionException("Action type is not supported: %s" % action_type) + return _get_mapping()[action_type](task) + + +def _get_mapping(): + return { + action_types.REST_API: get_rest_action, + action_types.MISTRAL_REST_API: get_mistral_rest_action, + action_types.OSLO_RPC: get_amqp_action + } def get_rest_action(task): action_name = task['task_dsl']['action'].split(':')[1] - action_params = task['service_dsl']['actions'][action_name]['parameters'] + action_dsl = task['service_dsl']['actions'][action_name] task_params = task['task_dsl'].get('parameters', None) url = task['service_dsl']['parameters']['baseUrl'] +\ - action_params['url'] + action_dsl['parameters']['url'] - headers = { + headers = {} + headers.update(task['task_dsl'].get('headers', {})) + headers.update(action_dsl.get('headers', {})) + + method = action_dsl['parameters'].get('method', "GET") + return actions.RestAction(url, params=task_params, + method=method, headers=headers) + + +def get_mistral_rest_action(task): + mistral_headers = { 'Mistral-Workbook-Name': task['workbook_name'], 'Mistral-Execution-Id': task['execution_id'], 'Mistral-Task-Id': task['id'], } - - return actions.RestAction(url=url, - params=task_params, - method=action_params['method'], - headers=headers) + action = get_rest_action(task) + action.headers.update(mistral_headers) + return action def get_amqp_action(task): diff --git a/mistral/engine/actions/action_types.py b/mistral/engine/actions/action_types.py index 002be5683..8e0aac181 100644 --- a/mistral/engine/actions/action_types.py +++ b/mistral/engine/actions/action_types.py @@ -19,8 +19,9 @@ REST_API = 'REST_API' OSLO_RPC = 'OSLO_RPC' +MISTRAL_REST_API = 'MISTRAL_REST_API' -_ALL = [REST_API, OSLO_RPC] +_ALL = [REST_API, OSLO_RPC, MISTRAL_REST_API] def is_valid(action_type): diff --git a/mistral/engine/actions/actions.py b/mistral/engine/actions/actions.py index b6a829762..b4cdf1c2e 100644 --- a/mistral/engine/actions/actions.py +++ b/mistral/engine/actions/actions.py @@ -24,12 +24,12 @@ LOG = logging.getLogger(__name__) class BaseAction(object): - def do_action(self): + def run(self): pass class RestAction(BaseAction): - def __init__(self, url, params={}, method="GET", headers=None): + def __init__(self, url, params={}, method="GET", headers={}): self.url = url self.params = params self.method = method @@ -43,6 +43,12 @@ class RestAction(BaseAction): headers=self.headers) LOG.info("Received HTTP response:\n%s\n%s" % (resp.status_code, resp.content)) + # Return rather json than text, but response can contain text also. + try: + return resp.json() + except: + LOG.debug("HTTP response content is not json") + return resp.content class OsloRPCAction(BaseAction): diff --git a/mistral/tests/unit/engine/actions/__init__.py b/mistral/tests/unit/engine/actions/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/mistral/tests/unit/engine/actions/test_action_factory.py b/mistral/tests/unit/engine/actions/test_action_factory.py new file mode 100644 index 000000000..7476ad6bf --- /dev/null +++ b/mistral/tests/unit/engine/actions/test_action_factory.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2013 - Mirantis, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest2 + +from mistral.engine.actions import action_factory +from mistral.engine.actions import action_types + + +SAMPLE_TASK = { + 'task_dsl': { + 'action': 'MyRest:create-vm', + 'parameters': { + 'a': 'b' + }, + 'headers': { + 'Cookie': 'abc' + } + }, + 'service_dsl': { + 'parameters': { + 'baseUrl': 'http://some_host' + }, + 'actions': { + 'create-vm': { + 'parameters': { + 'url': '/task1' + } + } + } + }, + 'workbook_name': 'wb', + 'execution_id': '1234', + 'id': '123' +} + + +class ActionFactoryTest(unittest2.TestCase): + def test_get_mistral_rest(self): + task = dict(SAMPLE_TASK) + task['service_dsl'].update({'type': action_types.MISTRAL_REST_API}) + action = action_factory.create_action(task) + + self.assertIn("Mistral-Workbook-Name", action.headers) + self.assertEqual(action.method, "GET") + + def test_get_rest(self): + task = dict(SAMPLE_TASK) + task['service_dsl'].update({'type': action_types.REST_API}) + action = action_factory.create_action(task) + + self.assertNotIn("Mistral-Workbook-Name", action.headers) + self.assertEqual(action.method, "GET")