Add validation on 'with-items' input

Partially implements blueprint mistral-with-items

Change-Id: I5c2446133ab6447ffd8f9c6336b11cc8fb340804
This commit is contained in:
Nikolay Mahotkin 2015-01-12 16:37:56 +03:00
parent 6472c74a04
commit 3202940f1a
3 changed files with 62 additions and 1 deletions

View File

@ -89,6 +89,10 @@ class WorkflowInputException(MistralException):
http_code = 400
class InputException(MistralException):
http_code = 400
class ApplicationContextNotFoundException(MistralException):
http_code = 400
message = "Application context not found"

View File

@ -15,6 +15,7 @@
# limitations under the License.
from mistral.db.v2.sqlalchemy import models
from mistral import exceptions as exc
from mistral.tests import base
from mistral.workbook.v2 import tasks
from mistral.workflow import utils
@ -114,4 +115,41 @@ class WithItemsCalculationsTest(base.BaseTest):
{'name_info': {'name': 'Mistral'}, 'server_info': 'server3'},
],
action_input_collection
)
)
def test_calculate_input_wrong_array_length(self):
with_items_input = {
'name_info': [
{'name': 'John'},
{'name': 'Ivan'},
{'name': 'Mistral'}
],
'server_info': [
'server1',
'server2'
]
}
exception = self.assertRaises(
exc.InputException,
with_items.calc_input,
with_items_input
)
self.assertIn("the same length", exception.message)
def test_calculate_input_not_list(self):
with_items_input = {
'name_info': [
{'name': 'John'},
{'name': 'Ivan'},
{'name': 'Mistral'}
],
'server_info': 'some_string'
}
exception = self.assertRaises(
exc.InputException,
with_items.calc_input,
with_items_input
)
self.assertIn("List type", exception.message)

View File

@ -14,6 +14,7 @@
import copy
from mistral import exceptions as exc
from mistral import expressions as expr
@ -112,6 +113,8 @@ def calc_input(with_items_input):
:param with_items_input: Dict containing mapped variables to their arrays.
:return: list containing dicts of each action input.
"""
validate_input(with_items_input)
action_input_collection = []
for key, value in with_items_input.items():
@ -126,6 +129,22 @@ def calc_input(with_items_input):
return action_input_collection
def validate_input(with_items_input):
# Take only mapped values and check them.
values = with_items_input.values()
if not all([isinstance(v, list) for v in values]):
raise exc.InputException(
"Wrong input format for: %s. List type is"
" expected for each value." % with_items_input)
required_len = len(values[0])
if not all(len(v) == required_len for v in values):
raise exc.InputException(
"Wrong input format for: %s. All arrays must"
" have the same length." % with_items_input)
def _get_output_key(task_spec):
return (task_spec.get_publish().keys()[0]
if task_spec.get_publish() else None)