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
This commit is contained in:
Renat Akhmerov 2014-02-17 19:13:18 +07:00
parent 5dd0f21d03
commit bbefdeb468
4 changed files with 85 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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