Merge "Divide RestAction on two separated actions in DSL"

This commit is contained in:
Jenkins 2014-01-20 15:48:33 +00:00 committed by Gerrit Code Review
commit 4f7ba4402d
5 changed files with 102 additions and 16 deletions

View File

@ -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):

View File

@ -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):

View File

@ -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):

View File

@ -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")