Bugfix: missing license in api directory
[yardstick.git] / api / resources / v2 / tasks.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
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 uuid
10 import logging
11 from datetime import datetime
12
13 from oslo_serialization import jsonutils
14
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
25
26 LOG = logging.getLogger(__name__)
27 LOG.setLevel(logging.DEBUG)
28
29
30 class V2Tasks(ApiResource):
31
32     def get(self):
33         task_handler = V2TaskHandler()
34         tasks = [change_obj_to_dict(t) for t in task_handler.list_all()]
35
36         for t in tasks:
37             result = t['result']
38             t['result'] = jsonutils.loads(result) if result else None
39
40         return result_handler(consts.API_SUCCESS, {'tasks': tasks})
41
42     def post(self):
43         return self._dispatch_post()
44
45     def create_task(self, args):
46         try:
47             name = args['name']
48         except KeyError:
49             return result_handler(consts.API_ERROR, 'name must be provided')
50
51         try:
52             project_id = args['project_id']
53         except KeyError:
54             return result_handler(consts.API_ERROR, 'project_id must be provided')
55
56         task_id = str(uuid.uuid4())
57         create_time = datetime.now()
58         task_handler = V2TaskHandler()
59
60         LOG.info('create task in database')
61         task_init_data = {
62             'uuid': task_id,
63             'project_id': project_id,
64             'name': name,
65             'time': create_time,
66             'status': -1
67         }
68         task_handler.insert(task_init_data)
69
70         LOG.info('create task in project')
71         project_handler = V2ProjectHandler()
72         project_handler.append_attr(project_id, {'tasks': task_id})
73
74         return result_handler(consts.API_SUCCESS, {'uuid': task_id})
75
76
77 class V2Task(ApiResource):
78
79     def get(self, task_id):
80         try:
81             uuid.UUID(task_id)
82         except ValueError:
83             return result_handler(consts.API_ERROR, 'invalid task id')
84
85         task_handler = V2TaskHandler()
86         try:
87             task = task_handler.get_by_uuid(task_id)
88         except ValueError:
89             return result_handler(consts.API_ERROR, 'no such task id')
90
91         task_info = change_obj_to_dict(task)
92         result = task_info['result']
93         task_info['result'] = jsonutils.loads(result) if result else None
94
95         return result_handler(consts.API_SUCCESS, {'task': task_info})
96
97     def delete(self, task_id):
98         try:
99             uuid.UUID(task_id)
100         except ValueError:
101             return result_handler(consts.API_ERROR, 'invalid task id')
102
103         task_handler = V2TaskHandler()
104         try:
105             project_id = task_handler.get_by_uuid(task_id).project_id
106         except ValueError:
107             return result_handler(consts.API_ERROR, 'no such task id')
108
109         LOG.info('delete task in database')
110         task_handler.delete_by_uuid(task_id)
111
112         project_handler = V2ProjectHandler()
113         project = project_handler.get_by_uuid(project_id)
114
115         if project.tasks:
116             LOG.info('update tasks in project')
117             new_task_list = project.tasks.split(',').remove(task_id)
118             if new_task_list:
119                 new_tasks = ','.join(new_task_list)
120             else:
121                 new_tasks = None
122             project_handler.update_attr(project_id, {'tasks': new_tasks})
123
124         return result_handler(consts.API_SUCCESS, {'task': task_id})
125
126     def put(self, task_id):
127
128         try:
129             uuid.UUID(task_id)
130         except ValueError:
131             return result_handler(consts.API_ERROR, 'invalid task id')
132
133         task_handler = V2TaskHandler()
134         try:
135             task_handler.get_by_uuid(task_id)
136         except ValueError:
137             return result_handler(consts.API_ERROR, 'no such task id')
138
139         return self._dispatch_post(task_id=task_id)
140
141     def add_environment(self, args):
142
143         task_id = args['task_id']
144         try:
145             environment_id = args['environment_id']
146         except KeyError:
147             return result_handler(consts.API_ERROR, 'environment_id must be provided')
148
149         try:
150             uuid.UUID(environment_id)
151         except ValueError:
152             return result_handler(consts.API_ERROR, 'invalid environment id')
153
154         environment_handler = V2EnvironmentHandler()
155         try:
156             environment_handler.get_by_uuid(environment_id)
157         except ValueError:
158             return result_handler(consts.API_ERROR, 'no such environment id')
159
160         LOG.info('update environment_id in task')
161         task_handler = V2TaskHandler()
162         task_handler.update_attr(task_id, {'environment_id': environment_id})
163
164         return result_handler(consts.API_SUCCESS, {'uuid': task_id})
165
166     def add_case(self, args):
167         task_id = args['task_id']
168         try:
169             name = args['case_name']
170         except KeyError:
171             return result_handler(consts.API_ERROR, 'case_name must be provided')
172
173         try:
174             content = args['case_content']
175         except KeyError:
176             return result_handler(consts.API_ERROR, 'case_content must be provided')
177
178         LOG.info('update case info in task')
179         task_handler = V2TaskHandler()
180         task_update_data = {
181             'case_name': name,
182             'content': content,
183             'suite': False
184         }
185         task_handler.update_attr(task_id, task_update_data)
186
187         return result_handler(consts.API_SUCCESS, {'uuid': task_id})
188
189     def add_suite(self, args):
190         task_id = args['task_id']
191         try:
192             name = args['suite_name']
193         except KeyError:
194             return result_handler(consts.API_ERROR, 'suite_name must be provided')
195
196         try:
197             content = args['suite_content']
198         except KeyError:
199             return result_handler(consts.API_ERROR, 'suite_content must be provided')
200
201         LOG.info('update suite info in task')
202         task_handler = V2TaskHandler()
203         task_update_data = {
204             'case_name': name,
205             'content': content,
206             'suite': True
207         }
208         task_handler.update_attr(task_id, task_update_data)
209
210         return result_handler(consts.API_SUCCESS, {'uuid': task_id})
211
212     def run(self, args):
213         try:
214             task_id = args['task_id']
215         except KeyError:
216             return result_handler(consts.API_ERROR, 'task_id must be provided')
217
218         try:
219             uuid.UUID(task_id)
220         except ValueError:
221             return result_handler(consts.API_ERROR, 'invalid task id')
222
223         task_handler = V2TaskHandler()
224         try:
225             task = task_handler.get_by_uuid(task_id)
226         except ValueError:
227             return result_handler(consts.API_ERROR, 'no such task id')
228
229         if not task.environment_id:
230             return result_handler(consts.API_ERROR, 'environment not set')
231
232         if not task.case_name or not task.content:
233             return result_handler(consts.API_ERROR, 'case not set')
234
235         if task.status == 0:
236             return result_handler(consts.API_ERROR, 'task is already running')
237
238         with open('/tmp/{}.yaml'.format(task.case_name), 'w') as f:
239             f.write(task.content)
240
241         data = {
242             'inputfile': ['/tmp/{}.yaml'.format(task.case_name)],
243             'task_id': task_id
244         }
245         if task.suite:
246             data.update({'suite': True})
247
248         LOG.info('start task thread')
249         param = Param(data)
250         task_thread = TaskThread(Task().start, param, task_handler)
251         task_thread.start()
252
253         return result_handler(consts.API_SUCCESS, {'uuid': task_id})