From bbefdeb46870054f7bd30abab269bf7ed2f24306 Mon Sep 17 00:00:00 2001 From: Renat Akhmerov Date: Mon, 17 Feb 2014 19:13:18 +0700 Subject: [PATCH] Add context parameter in execution creation * Added parameter "context" when creating execution * Added "context" validation (dict or json compatible string) * Additional tests * Added exceptions.py module with Mistral client exceptions * Fixing typos Change-Id: Id569d2f953efce9b86988c7f8c8f92cd0c97ef17 --- mistralclient/api/base.py | 2 +- mistralclient/api/executions.py | 28 +++++++++++++++-- mistralclient/exceptions.py | 42 ++++++++++++++++++++++++++ mistralclient/tests/test_executions.py | 18 +++++++++-- 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 mistralclient/exceptions.py diff --git a/mistralclient/api/base.py b/mistralclient/api/base.py index fb4b92b4..4c3ef4e1 100644 --- a/mistralclient/api/base.py +++ b/mistralclient/api/base.py @@ -138,7 +138,7 @@ class ResourceManager(object): def get_json(response): """This method provided backward compatibility with old versions - of requests library + of requests library. """ json_field_or_function = getattr(response, 'json', None) diff --git a/mistralclient/api/executions.py b/mistralclient/api/executions.py index 24cf691c..be8037e1 100644 --- a/mistralclient/api/executions.py +++ b/mistralclient/api/executions.py @@ -14,7 +14,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json + from mistralclient.api import base +from mistralclient import exceptions as ex class Execution(base.Resource): @@ -24,10 +27,31 @@ class Execution(base.Resource): class ExecutionManager(base.ResourceManager): resource_class = Execution - def create(self, workbook_name, task): + def _validate_context_str(self, context): + msg = 'Context must be a dictionary or json compatible string.' + + if not isinstance(context, str): + raise ex.IllegalArgumentException(msg) + + try: + json.loads(context) + except Exception as e: + raise ex.IllegalArgumentException(msg + e) + + def create(self, workbook_name, task, context=None): self._ensure_not_empty(workbook_name=workbook_name, task=task) - data = {'workbook_name': workbook_name, 'task': task} + if isinstance(context, dict): + context_str = str(context) + else: + self._validate_context_str(context) + context_str = context + + data = { + 'workbook_name': workbook_name, + 'task': task, + 'context': context_str + } return self._create('/workbooks/%s/executions' % workbook_name, data) diff --git a/mistralclient/exceptions.py b/mistralclient/exceptions.py new file mode 100644 index 00000000..078c66ad --- /dev/null +++ b/mistralclient/exceptions.py @@ -0,0 +1,42 @@ +# -*- 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. + + +class MistralClientException(Exception): + """Base Exception for Mistral client + + To correctly use this class, inherit from it and define + a 'message' and 'code' properties. + """ + message = "An unknown exception occurred" + code = "UNKNOWN_EXCEPTION" + + def __str__(self): + return self.message + + def __init__(self, message=message): + self.message = message + super(MistralClientException, self).__init__( + '%s: %s' % (self.code, self.message)) + + +class IllegalArgumentException(MistralClientException): + message = "IllegalArgumentException occurred" + code = "ILLEGAL_ARGUMENT_EXCEPTION" + + def __init__(self, message=None): + if message: + self.message = message diff --git a/mistralclient/tests/test_executions.py b/mistralclient/tests/test_executions.py index 32e271a5..2643925d 100644 --- a/mistralclient/tests/test_executions.py +++ b/mistralclient/tests/test_executions.py @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import unittest2 + from mistralclient.tests import base # TODO: Later we need additional tests verifying all the errors etc. @@ -37,12 +39,12 @@ EXECS = [ class TestExecutions(base.BaseClientTest): - def test_create(self): self.mock_http_post(json=EXECS[0]) ex = self.executions.create(EXECS[0]['workbook_name'], - EXECS[0]['target_task']) + EXECS[0]['target_task'], + EXECS[0]['context']) self.assertIsNotNone(ex) self.assertEqual(EXECS[0]['id'], ex.id) @@ -51,6 +53,18 @@ class TestExecutions(base.BaseClientTest): self.assertEqual(EXECS[0]['state'], ex.state) self.assertEqual(EXECS[0]['context'], ex.context) + @unittest2.expectedFailure + def test_create_failure1(self): + self.executions.create(EXECS[0]['workbook_name'], + EXECS[0]['target_task'], + "sdfsdf") + + @unittest2.expectedFailure + def test_create_failure2(self): + self.executions.create(EXECS[0]['workbook_name'], + EXECS[0]['target_task'], + list('343', 'sdfsd')) + def test_update(self): self.mock_http_put(json=EXECS[0])