Merge "Add test case file and document of Tardstick TC056(HA_TC013)"
[yardstick.git] / api / resources / v1 / results.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
10 import logging
11 import uuid
12 import json
13 import os
14
15 from flasgger.utils import swag_from
16
17 from api import ApiResource
18 from api.database.v1.handlers import TasksHandler
19 from yardstick.common import constants as consts
20 from yardstick.common.utils import result_handler
21 from api.swagger import models
22
23 LOG = logging.getLogger(__name__)
24 LOG.setLevel(logging.DEBUG)
25
26
27 ResultModel = models.ResultModel
28
29
30 class V1Result(ApiResource):
31
32     @swag_from(os.path.join(consts.REPOS_DIR, 'api/swagger/docs/results.yaml'))
33     def get(self):
34         args = self._get_args()
35
36         try:
37             task_id = args['task_id']
38         except KeyError:
39             return result_handler(consts.API_ERROR, 'task_id must be provided')
40
41         try:
42             uuid.UUID(task_id)
43         except ValueError:
44             return result_handler(consts.API_ERROR, 'invalid task_id')
45
46         task_handler = TasksHandler()
47         try:
48             task = task_handler.get_task_by_taskid(task_id)
49         except ValueError:
50             return result_handler(consts.API_ERROR, 'invalid task_id')
51
52         def _unfinished():
53             return result_handler(consts.TASK_NOT_DONE, {})
54
55         def _finished():
56             if task.result:
57                 return result_handler(consts.TASK_DONE, json.loads(task.result))
58             else:
59                 return result_handler(consts.TASK_DONE, {})
60
61         def _error():
62             return result_handler(consts.TASK_FAILED, task.error)
63
64         status = task.status
65         LOG.debug('Task status is: %s', status)
66
67         if status not in [consts.TASK_NOT_DONE,
68                           consts.TASK_DONE,
69                           consts.TASK_FAILED]:
70             return result_handler(consts.API_ERROR, 'internal server error')
71
72         switcher = {
73             consts.TASK_NOT_DONE: _unfinished,
74             consts.TASK_DONE: _finished,
75             consts.TASK_FAILED: _error
76         }
77
78         return switcher.get(status)()