Fixed 500 error in get_result API handler

If there is no task in DB with environemnt id and task id passed in
handler, a server error was returned by the API.

This patch fixes this bug.

Closes-Bug: #162206
Change-Id: I9fd84a963a1eacf8ab2aa428cfb4365f80fff02b
This commit is contained in:
Andrew Pashkin 2015-03-06 19:07:50 +03:00
parent d6f1971756
commit 2106494d41
3 changed files with 74 additions and 3 deletions

View File

@ -20,7 +20,7 @@ from murano.db import models
from murano.db.services import environments as envs
from murano.db.services import sessions
from murano.db import session as db_session
from murano.common.i18n import _LI, _LE
from murano.common.i18n import _LI, _LE, _
from murano.openstack.common import log as logging
from murano.services import actions
from murano.services import states
@ -79,7 +79,17 @@ class Controller(object):
unit = db_session.get_session()
self._validate_environment(unit, request, environment_id)
return actions.ActionServices.get_result(environment_id, task_id, unit)
result = actions.ActionServices.get_result(environment_id, task_id,
unit)
if result is not None:
return result
msg = (_('Result for task with environment_id: {} and '
'task_id: {} was not found.')
.format(environment_id, task_id))
LOG.error(msg)
raise exc.HTTPNotFound(msg)
def create_resource():

View File

@ -111,4 +111,8 @@ class ActionServices(object):
def get_result(environment_id, task_id, unit):
task = unit.query(models.Task).filter_by(
id=task_id, environment_id=environment_id).first()
if task is not None:
return task.result
return None

View File

@ -95,3 +95,60 @@ class TestActionsApi(tb.ControllerTest, tb.MuranoApiTestCase):
self.mock_engine_rpc.handle_task.assert_called_once_with(rpc_task)
self.assertIn('task_id', result)
def test_get_result(self, _):
"""Result of task with given id and environment id is returned."""
now = timeutils.utcnow()
expected_environment_id = 'test_environment'
expected_task_id = 'test_task'
expected_result = {'test_result': 'test_result'}
environment = models.Environment(
id=expected_environment_id,
name='test_environment', created=now, updated=now,
tenant_id=self.tenant
)
task = models.Task(
id=expected_task_id,
started=now,
finished=now,
result=expected_result,
environment_id=expected_environment_id
)
test_utils.save_models(environment, task)
request = self._get(
'/environments/{environment_id}/actions/{task_id}'
.format(environment_id=expected_environment_id,
task_id=expected_task_id),
)
response = request.get_response(self.api)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, expected_result)
def test_get_result_not_found(self, _):
"""If task does not exists, it should be handled correctly
and API should return 404.
"""
expected_environment_id = 'test_environment'
environment = models.Environment(
id=expected_environment_id,
name='test_environment',
tenant_id=self.tenant
)
test_utils.save_models(environment)
request = self._get(
'/environments/{environment_id}/actions/{task_id}'
.format(environment_id=expected_environment_id,
task_id='not_existent_task_id'),
)
response = request.get_response(self.api)
self.assertEqual(response.status_code, 404)