Open storperf testcase to huawei-pod2
[yardstick.git] / api / resources / asynctask.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.utils.common import result_handler
13 from api.database.v1.handlers import AsyncTaskHandler
14 from yardstick.common import constants as consts
15
16 LOG = logging.getLogger(__name__)
17 LOG.setLevel(logging.DEBUG)
18
19
20 def default(args):
21     return _get_status(args)
22
23
24 def _get_status(args):
25     try:
26         task_id = args['task_id']
27     except KeyError:
28         return result_handler(consts.API_ERROR, 'task_id must be provided')
29
30     try:
31         uuid.UUID(task_id)
32     except ValueError:
33         return result_handler(consts.API_ERROR, 'invalid task_id')
34
35     asynctask_handler = AsyncTaskHandler()
36     try:
37         asynctask = asynctask_handler.get_task_by_taskid(task_id)
38     except ValueError:
39         return result_handler(consts.API_ERROR, 'invalid task_id')
40
41     def _unfinished():
42         return result_handler(consts.TASK_NOT_DONE, {})
43
44     def _finished():
45         return result_handler(consts.TASK_DONE, {})
46
47     def _error():
48         return result_handler(consts.TASK_FAILED, asynctask.error)
49
50     status = asynctask.status
51     LOG.debug('Task status is: %s', status)
52
53     if status not in [consts.TASK_NOT_DONE,
54                       consts.TASK_DONE,
55                       consts.TASK_FAILED]:
56         return result_handler(consts.API_ERROR, 'internal server error')
57
58     switcher = {
59         consts.TASK_NOT_DONE: _unfinished,
60         consts.TASK_DONE: _finished,
61         consts.TASK_FAILED: _error
62     }
63
64     return switcher.get(status)()