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(',')
118 new_task_list.remove(task_id)
120 new_tasks = ','.join(new_task_list)
123 project_handler.update_attr(project_id, {'tasks': new_tasks})
125 return result_handler(consts.API_SUCCESS, {'task': task_id})
127 def put(self, task_id):
132 return result_handler(consts.API_ERROR, 'invalid task id')
134 task_handler = V2TaskHandler()
136 task_handler.get_by_uuid(task_id)
138 return result_handler(consts.API_ERROR, 'no such task id')
140 return self._dispatch_post(task_id=task_id)
142 def add_environment(self, args):
144 task_id = args['task_id']
146 environment_id = args['environment_id']
148 return result_handler(consts.API_ERROR, 'environment_id must be provided')
151 uuid.UUID(environment_id)
153 return result_handler(consts.API_ERROR, 'invalid environment id')
155 environment_handler = V2EnvironmentHandler()
157 environment_handler.get_by_uuid(environment_id)
159 return result_handler(consts.API_ERROR, 'no such environment id')
161 LOG.info('update environment_id in task')
162 task_handler = V2TaskHandler()
163 task_handler.update_attr(task_id, {'environment_id': environment_id})
165 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
167 def add_case(self, args):
168 task_id = args['task_id']
170 name = args['case_name']
172 return result_handler(consts.API_ERROR, 'case_name must be provided')
175 content = args['case_content']
177 return result_handler(consts.API_ERROR, 'case_content must be provided')
179 LOG.info('update case info in task')
180 task_handler = V2TaskHandler()
186 task_handler.update_attr(task_id, task_update_data)
188 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
190 def add_suite(self, args):
191 task_id = args['task_id']
193 name = args['suite_name']
195 return result_handler(consts.API_ERROR, 'suite_name must be provided')
198 content = args['suite_content']
200 return result_handler(consts.API_ERROR, 'suite_content must be provided')
202 LOG.info('update suite info in task')
203 task_handler = V2TaskHandler()
209 task_handler.update_attr(task_id, task_update_data)
211 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
215 task_id = args['task_id']
217 return result_handler(consts.API_ERROR, 'task_id must be provided')
222 return result_handler(consts.API_ERROR, 'invalid task id')
224 task_handler = V2TaskHandler()
226 task = task_handler.get_by_uuid(task_id)
228 return result_handler(consts.API_ERROR, 'no such task id')
230 if not task.environment_id:
231 return result_handler(consts.API_ERROR, 'environment not set')
233 if not task.case_name or not task.content:
234 return result_handler(consts.API_ERROR, 'case not set')
237 return result_handler(consts.API_ERROR, 'task is already running')
239 with open('/tmp/{}.yaml'.format(task.case_name), 'w') as f:
240 f.write(task.content)
243 'inputfile': ['/tmp/{}.yaml'.format(task.case_name)],
247 data.update({'suite': True})
249 LOG.info('start task thread')
251 task_thread = TaskThread(Task().start, param, task_handler)
254 return result_handler(consts.API_SUCCESS, {'uuid': task_id})