Merge "Make "Scenario" class abstract"
[yardstick.git] / api / resources / v1 / tasks.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 import os
10 import errno
11 import uuid
12
13 from api import ApiResource
14 from api.database.v1.handlers import TasksHandler
15 from yardstick.common import constants as consts
16 from yardstick.common.utils import result_handler
17
18
19 class V1TaskLog(ApiResource):
20     def get(self, task_id):
21
22         try:
23             uuid.UUID(task_id)
24         except ValueError:
25             return result_handler(consts.API_ERROR, 'invalid task_id')
26
27         task_handler = TasksHandler()
28         try:
29             task = task_handler.get_task_by_taskid(task_id)
30         except ValueError:
31             return result_handler(consts.API_ERROR, 'invalid task_id')
32
33         index = int(self._get_args().get('index', 0))
34
35         try:
36             with open(os.path.join(consts.TASK_LOG_DIR, '{}.log'.format(task_id))) as f:
37                 f.seek(index)
38                 data = f.readlines()
39                 index = f.tell()
40         except OSError as e:
41             if e.errno == errno.ENOENT:
42                 return result_handler(consts.API_ERROR, 'log file does not exist')
43             return result_handler(consts.API_ERROR, 'error with log file')
44
45         return_data = {
46             'index': index,
47             'data': data
48         }
49
50         return result_handler(task.status, return_data)