Switch from CONST to CONF
[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 import errno
15 import json
16 import logging
17 import os
18 import uuid
19
20 from flask import jsonify
21 from flasgger.utils import swag_from
22 import pkg_resources
23
24 from functest.api.base import ApiResource
25 from functest.api.common import api_utils
26 from functest.api.database.v1.handlers import TasksHandler
27 from functest.utils import config
28
29
30 LOGGER = logging.getLogger(__name__)
31
32
33 class V1Task(ApiResource):
34     """ V1Task Resource class"""
35
36     @swag_from(pkg_resources.resource_filename(
37         'functest', 'api/swagger/task.yaml'))
38     def get(self, task_id):  # pylint: disable=no-self-use
39         """ GET the result of the task id """
40         try:
41             uuid.UUID(task_id)
42         except ValueError:
43             return api_utils.result_handler(status=1, data='Invalid task id')
44
45         task_handler = TasksHandler()
46         try:
47             task = task_handler.get_task_by_taskid(task_id)
48         except ValueError:
49             return api_utils.result_handler(status=1, data='No such task id')
50
51         status = task.status
52         LOGGER.debug('Task status is: %s', status)
53
54         if status not in ['IN PROGRESS', 'FAIL', 'FINISHED']:
55             return api_utils.result_handler(status=1,
56                                             data='internal server error')
57
58         switcher = {'IN PROGRESS': 0, 'FAIL': 1, 'FINISHED': 2}
59         if status == 'IN PROGRESS':
60             result = {'status': switcher.get(status), 'result': ''}
61         elif status == 'FAIL':
62             result = {'status': switcher.get(status), 'error': task.error}
63         else:
64             result = {'status': switcher.get(status),
65                       'result': json.loads(task.result)}
66
67         return jsonify(result)
68
69
70 class V1TaskLog(ApiResource):
71     """ V1TaskLog Resource class"""
72
73     @swag_from(pkg_resources.resource_filename(
74         'functest', 'api/swagger/task_log.yaml'))
75     def get(self, task_id):  # pylint: disable=no-self-use
76         """ GET the log of the task id """
77         try:
78             uuid.UUID(task_id)
79         except ValueError:
80             return api_utils.result_handler(status=1, data='Invalid task id')
81
82         task_handler = TasksHandler()
83         try:
84             task = task_handler.get_task_by_taskid(task_id)
85         except ValueError:
86             return api_utils.result_handler(status=1, data='No such task id')
87
88         task_log_dir = getattr(config.CONF, 'dir_results')
89         # pylint: disable=maybe-no-member
90         index = int(self._get_args().get('index', 0))
91
92         try:
93             with open(os.path.join(task_log_dir,
94                                    '{}.log'.format(task_id)), 'r') as log_file:
95                 log_file.seek(index)
96                 data = log_file.readlines()
97                 index = log_file.tell()
98         except OSError as err:
99             if err.errno == errno.ENOENT:
100                 return api_utils.result_handler(
101                     status=1, data='Log file does not exist')
102
103             return api_utils.result_handler(
104                 status=1, data='Error with log file')
105
106         return_data = {'data': data, 'index': index}
107
108         switcher = {'IN PROGRESS': 0, 'FAIL': 1, 'FINISHED': 2}
109
110         return api_utils.result_handler(status=switcher.get(task.status),
111                                         data=return_data)