Merge "Give a list of links to wget"
[functest.git] / functest / api / resources / v1 / tasks.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """
11 Resources to retrieve the task results
12 """
13
14
15 import json
16 import logging
17 import uuid
18
19 from flask import jsonify
20
21 from functest.api.base import ApiResource
22 from functest.api.common import api_utils
23 from functest.api.database.v1.handlers import TasksHandler
24
25
26 LOGGER = logging.getLogger(__name__)
27
28
29 class V1Tasks(ApiResource):
30     """ V1Tasks Resource class"""
31
32     def get(self, task_id):  # pylint: disable=no-self-use
33         """ GET the result of the task id """
34         try:
35             uuid.UUID(task_id)
36         except ValueError:
37             return api_utils.result_handler(status=1, data='Invalid task id')
38
39         task_handler = TasksHandler()
40         try:
41             task = task_handler.get_task_by_taskid(task_id)
42         except ValueError:
43             return api_utils.result_handler(status=1, data='No such task id')
44
45         status = task.status
46         LOGGER.debug('Task status is: %s', status)
47
48         if status not in ['IN PROGRESS', 'FAIL', 'FINISHED']:
49             return api_utils.result_handler(status=1,
50                                             data='internal server error')
51         if status == 'IN PROGRESS':
52             result = {'status': status, 'result': ''}
53         elif status == 'FAIL':
54             result = {'status': status, 'error': task.error}
55         else:
56             result = {'status': status, 'result': json.loads(task.result)}
57
58         return jsonify(result)