Merge "Add host&targer in scenario['options']['server_name'] support"
[yardstick.git] / api / resources / v1 / asynctasks.py
1 # ############################################################################
2 # Copyright (c) 2017 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 import uuid
10 import logging
11
12 from api import ApiResource
13 from api.database.v1.handlers import AsyncTaskHandler
14 from yardstick.common import constants as consts
15 from yardstick.common.utils import result_handler
16
17 LOG = logging.getLogger(__name__)
18 LOG.setLevel(logging.DEBUG)
19
20
21 class V1AsyncTask(ApiResource):
22
23     def get(self):
24         args = self._get_args()
25
26         try:
27             task_id = args['task_id']
28         except KeyError:
29             return result_handler(consts.API_ERROR, 'task_id must be provided')
30
31         try:
32             uuid.UUID(task_id)
33         except ValueError:
34             return result_handler(consts.API_ERROR, 'invalid task_id')
35
36         asynctask_handler = AsyncTaskHandler()
37         try:
38             asynctask = asynctask_handler.get_task_by_taskid(task_id)
39         except ValueError:
40             return result_handler(consts.API_ERROR, 'invalid task_id')
41
42         def _unfinished():
43             return result_handler(consts.TASK_NOT_DONE, {})
44
45         def _finished():
46             return result_handler(consts.TASK_DONE, {})
47
48         def _error():
49             return result_handler(consts.TASK_FAILED, asynctask.error)
50
51         status = asynctask.status
52         LOG.debug('Task status is: %s', status)
53
54         if status not in [consts.TASK_NOT_DONE,
55                           consts.TASK_DONE,
56                           consts.TASK_FAILED]:
57             return result_handler(consts.API_ERROR, 'internal server error')
58
59         switcher = {
60             consts.TASK_NOT_DONE: _unfinished,
61             consts.TASK_DONE: _finished,
62             consts.TASK_FAILED: _error
63         }
64
65         return switcher.get(status)()