1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
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 ##############################################################################
11 from datetime import datetime
13 from oslo_serialization import jsonutils
15 from api import ApiResource
16 from api.database.v2.handlers import V2TaskHandler
17 from api.database.v2.handlers import V2ProjectHandler
18 from api.database.v2.handlers import V2EnvironmentHandler
19 from api.utils.thread import TaskThread
20 from yardstick.common.utils import result_handler
21 from yardstick.common.utils import change_obj_to_dict
22 from yardstick.common import constants as consts
23 from yardstick.benchmark.core.task import Task
24 from yardstick.benchmark.core import Param
26 LOG = logging.getLogger(__name__)
27 LOG.setLevel(logging.DEBUG)
30 class V2Tasks(ApiResource):
33 task_handler = V2TaskHandler()
34 tasks = [change_obj_to_dict(t) for t in task_handler.list_all()]
38 t['result'] = jsonutils.loads(result) if result else None
40 return result_handler(consts.API_SUCCESS, {'tasks': tasks})
43 return self._dispatch_post()
45 def create_task(self, args):
49 return result_handler(consts.API_ERROR, 'name must be provided')
52 project_id = args['project_id']
54 return result_handler(consts.API_ERROR, 'project_id must be provided')
56 task_id = str(uuid.uuid4())
57 create_time = datetime.now()
58 task_handler = V2TaskHandler()
60 LOG.info('create task in database')
63 'project_id': project_id,
68 task_handler.insert(task_init_data)
70 LOG.info('create task in project')
71 project_handler = V2ProjectHandler()
72 project_handler.append_attr(project_id, {'tasks': task_id})
74 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
77 class V2Task(ApiResource):
79 def get(self, task_id):
83 return result_handler(consts.API_ERROR, 'invalid task id')
85 task_handler = V2TaskHandler()
87 task = task_handler.get_by_uuid(task_id)
89 return result_handler(consts.API_ERROR, 'no such task id')
91 task_info = change_obj_to_dict(task)
92 result = task_info['result']
93 task_info['result'] = jsonutils.loads(result) if result else None
95 return result_handler(consts.API_SUCCESS, {'task': task_info})
97 def delete(self, task_id):
101 return result_handler(consts.API_ERROR, 'invalid task id')
103 task_handler = V2TaskHandler()
105 project_id = task_handler.get_by_uuid(task_id).project_id
107 return result_handler(consts.API_ERROR, 'no such task id')
109 LOG.info('delete task in database')
110 task_handler.delete_by_uuid(task_id)
112 project_handler = V2ProjectHandler()
113 project = project_handler.get_by_uuid(project_id)
116 LOG.info('update tasks in project')
117 new_task_list = project.tasks.split(',').remove(task_id)
119 new_tasks = ','.join(new_task_list)
122 project_handler.update_attr(project_id, {'tasks': new_tasks})
124 return result_handler(consts.API_SUCCESS, {'task': task_id})
126 def put(self, task_id):
131 return result_handler(consts.API_ERROR, 'invalid task id')
133 task_handler = V2TaskHandler()
135 task_handler.get_by_uuid(task_id)
137 return result_handler(consts.API_ERROR, 'no such task id')
139 return self._dispatch_post(task_id=task_id)
141 def add_environment(self, args):
143 task_id = args['task_id']
145 environment_id = args['environment_id']
147 return result_handler(consts.API_ERROR, 'environment_id must be provided')
150 uuid.UUID(environment_id)
152 return result_handler(consts.API_ERROR, 'invalid environment id')
154 environment_handler = V2EnvironmentHandler()
156 environment_handler.get_by_uuid(environment_id)
158 return result_handler(consts.API_ERROR, 'no such environment id')
160 LOG.info('update environment_id in task')
161 task_handler = V2TaskHandler()
162 task_handler.update_attr(task_id, {'environment_id': environment_id})
164 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
166 def add_case(self, args):
167 task_id = args['task_id']
169 name = args['case_name']
171 return result_handler(consts.API_ERROR, 'case_name must be provided')
174 content = args['case_content']
176 return result_handler(consts.API_ERROR, 'case_content must be provided')
178 LOG.info('update case info in task')
179 task_handler = V2TaskHandler()
185 task_handler.update_attr(task_id, task_update_data)
187 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
189 def add_suite(self, args):
190 task_id = args['task_id']
192 name = args['suite_name']
194 return result_handler(consts.API_ERROR, 'suite_name must be provided')
197 content = args['suite_content']
199 return result_handler(consts.API_ERROR, 'suite_content must be provided')
201 LOG.info('update suite info in task')
202 task_handler = V2TaskHandler()
208 task_handler.update_attr(task_id, task_update_data)
210 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
214 task_id = args['task_id']
216 return result_handler(consts.API_ERROR, 'task_id must be provided')
221 return result_handler(consts.API_ERROR, 'invalid task id')
223 task_handler = V2TaskHandler()
225 task = task_handler.get_by_uuid(task_id)
227 return result_handler(consts.API_ERROR, 'no such task id')
229 if not task.environment_id:
230 return result_handler(consts.API_ERROR, 'environment not set')
232 if not task.case_name or not task.content:
233 return result_handler(consts.API_ERROR, 'case not set')
236 return result_handler(consts.API_ERROR, 'task is already running')
238 with open('/tmp/{}.yaml'.format(task.case_name), 'w') as f:
239 f.write(task.content)
242 'inputfile': ['/tmp/{}.yaml'.format(task.case_name)],
246 data.update({'suite': True})
248 LOG.info('start task thread')
250 task_thread = TaskThread(Task().start, param, task_handler)
253 return result_handler(consts.API_SUCCESS, {'uuid': task_id})