3 from datetime import datetime
5 from oslo_serialization import jsonutils
7 from api import ApiResource
8 from api.database.v2.handlers import V2TaskHandler
9 from api.database.v2.handlers import V2ProjectHandler
10 from api.database.v2.handlers import V2EnvironmentHandler
11 from api.utils.thread import TaskThread
12 from yardstick.common.utils import result_handler
13 from yardstick.common.utils import change_obj_to_dict
14 from yardstick.common import constants as consts
15 from yardstick.benchmark.core.task import Task
16 from yardstick.benchmark.core import Param
18 LOG = logging.getLogger(__name__)
19 LOG.setLevel(logging.DEBUG)
22 class V2Tasks(ApiResource):
25 task_handler = V2TaskHandler()
26 tasks = [change_obj_to_dict(t) for t in task_handler.list_all()]
30 t['result'] = jsonutils.loads(result) if result else None
32 return result_handler(consts.API_SUCCESS, {'tasks': tasks})
35 return self._dispatch_post()
37 def create_task(self, args):
41 return result_handler(consts.API_ERROR, 'name must be provided')
44 project_id = args['project_id']
46 return result_handler(consts.API_ERROR, 'project_id must be provided')
48 task_id = str(uuid.uuid4())
49 create_time = datetime.now()
50 task_handler = V2TaskHandler()
52 LOG.info('create task in database')
55 'project_id': project_id,
60 task_handler.insert(task_init_data)
62 LOG.info('create task in project')
63 project_handler = V2ProjectHandler()
64 project_handler.append_attr(project_id, {'tasks': task_id})
66 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
69 class V2Task(ApiResource):
71 def get(self, task_id):
75 return result_handler(consts.API_ERROR, 'invalid task id')
77 task_handler = V2TaskHandler()
79 task = task_handler.get_by_uuid(task_id)
81 return result_handler(consts.API_ERROR, 'no such task id')
83 task_info = change_obj_to_dict(task)
84 result = task_info['result']
85 task_info['result'] = jsonutils.loads(result) if result else None
87 return result_handler(consts.API_SUCCESS, {'task': task_info})
89 def delete(self, task_id):
93 return result_handler(consts.API_ERROR, 'invalid task id')
95 task_handler = V2TaskHandler()
97 project_id = task_handler.get_by_uuid(task_id).project_id
99 return result_handler(consts.API_ERROR, 'no such task id')
101 LOG.info('delete task in database')
102 task_handler.delete_by_uuid(task_id)
104 project_handler = V2ProjectHandler()
105 project = project_handler.get_by_uuid(project_id)
108 LOG.info('update tasks in project')
109 new_task_list = project.tasks.split(',')
110 new_task_list.remove(task_id)
112 new_tasks = ','.join(new_task_list)
115 project_handler.update_attr(project_id, {'tasks': new_tasks})
117 return result_handler(consts.API_SUCCESS, {'task': task_id})
119 def put(self, task_id):
124 return result_handler(consts.API_ERROR, 'invalid task id')
126 task_handler = V2TaskHandler()
128 task_handler.get_by_uuid(task_id)
130 return result_handler(consts.API_ERROR, 'no such task id')
132 return self._dispatch_post(task_id=task_id)
134 def add_environment(self, args):
136 task_id = args['task_id']
138 environment_id = args['environment_id']
140 return result_handler(consts.API_ERROR, 'environment_id must be provided')
143 uuid.UUID(environment_id)
145 return result_handler(consts.API_ERROR, 'invalid environment id')
147 environment_handler = V2EnvironmentHandler()
149 environment_handler.get_by_uuid(environment_id)
151 return result_handler(consts.API_ERROR, 'no such environment id')
153 LOG.info('update environment_id in task')
154 task_handler = V2TaskHandler()
155 task_handler.update_attr(task_id, {'environment_id': environment_id})
157 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
159 def add_case(self, args):
160 task_id = args['task_id']
162 name = args['case_name']
164 return result_handler(consts.API_ERROR, 'case_name must be provided')
167 content = args['case_content']
169 return result_handler(consts.API_ERROR, 'case_content must be provided')
171 LOG.info('update case info in task')
172 task_handler = V2TaskHandler()
178 task_handler.update_attr(task_id, task_update_data)
180 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
182 def add_suite(self, args):
183 task_id = args['task_id']
185 name = args['suite_name']
187 return result_handler(consts.API_ERROR, 'suite_name must be provided')
190 content = args['suite_content']
192 return result_handler(consts.API_ERROR, 'suite_content must be provided')
194 LOG.info('update suite info in task')
195 task_handler = V2TaskHandler()
201 task_handler.update_attr(task_id, task_update_data)
203 return result_handler(consts.API_SUCCESS, {'uuid': task_id})
207 task_id = args['task_id']
209 return result_handler(consts.API_ERROR, 'task_id must be provided')
214 return result_handler(consts.API_ERROR, 'invalid task id')
216 task_handler = V2TaskHandler()
218 task = task_handler.get_by_uuid(task_id)
220 return result_handler(consts.API_ERROR, 'no such task id')
222 if not task.environment_id:
223 return result_handler(consts.API_ERROR, 'environment not set')
225 if not task.case_name or not task.content:
226 return result_handler(consts.API_ERROR, 'case not set')
229 return result_handler(consts.API_ERROR, 'task is already running')
231 with open('/tmp/{}.yaml'.format(task.case_name), 'w') as f:
232 f.write(task.content)
235 'inputfile': ['/tmp/{}.yaml'.format(task.case_name)],
239 data.update({'suite': True})
241 LOG.info('start task thread')
243 task_thread = TaskThread(Task().start, param, task_handler)
246 return result_handler(consts.API_SUCCESS, {'uuid': task_id})