668f7648e19c09519e32830697ad96c8bc6c36d5
[yardstick.git] / api / resources / v2 / tasks.py
1 import uuid
2 import logging
3 from datetime import datetime
4
5 from oslo_serialization import jsonutils
6
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 yardstick.common.utils import result_handler
12 from yardstick.common.utils import change_obj_to_dict
13 from yardstick.common import constants as consts
14
15 LOG = logging.getLogger(__name__)
16 LOG.setLevel(logging.DEBUG)
17
18
19 class V2Tasks(ApiResource):
20
21     def post(self):
22         return self._dispatch_post()
23
24     def create_task(self, args):
25         try:
26             name = args['name']
27         except KeyError:
28             return result_handler(consts.API_ERROR, 'name must be provided')
29
30         try:
31             project_id = args['project_id']
32         except KeyError:
33             return result_handler(consts.API_ERROR, 'project_id must be provided')
34
35         task_id = str(uuid.uuid4())
36         create_time = datetime.now()
37         task_handler = V2TaskHandler()
38
39         LOG.info('create task in database')
40         task_init_data = {
41             'uuid': task_id,
42             'project_id': project_id,
43             'name': name,
44             'time': create_time,
45             'status': -1
46         }
47         task_handler.insert(task_init_data)
48
49         LOG.info('create task in project')
50         project_handler = V2ProjectHandler()
51         project_handler.append_attr(project_id, {'tasks': task_id})
52
53         return result_handler(consts.API_SUCCESS, {'uuid': task_id})
54
55
56 class V2Task(ApiResource):
57
58     def get(self, task_id):
59         try:
60             uuid.UUID(task_id)
61         except ValueError:
62             return result_handler(consts.API_ERROR, 'invalid task id')
63
64         task_handler = V2TaskHandler()
65         try:
66             task = task_handler.get_by_uuid(task_id)
67         except ValueError:
68             return result_handler(consts.API_ERROR, 'no such task id')
69
70         task_info = change_obj_to_dict(task)
71         result = task_info['result']
72         task_info['result'] = jsonutils.loads(result) if result else None
73
74         return result_handler(consts.API_SUCCESS, {'task': task_info})
75
76     def delete(self, task_id):
77         try:
78             uuid.UUID(task_id)
79         except ValueError:
80             return result_handler(consts.API_ERROR, 'invalid task id')
81
82         task_handler = V2TaskHandler()
83         try:
84             project_id = task_handler.get_by_uuid(task_id).project_id
85         except ValueError:
86             return result_handler(consts.API_ERROR, 'no such task id')
87
88         LOG.info('delete task in database')
89         task_handler.delete_by_uuid(task_id)
90
91         project_handler = V2ProjectHandler()
92         project = project_handler.get_by_uuid(project_id)
93
94         if project.tasks:
95             LOG.info('update tasks in project')
96             new_task_list = project.tasks.split(',').remove(task_id)
97             if new_task_list:
98                 new_tasks = ','.join(new_task_list)
99             else:
100                 new_tasks = None
101             project_handler.update_attr(project_id, {'tasks': new_tasks})
102
103         return result_handler(consts.API_SUCCESS, {'task': task_id})
104
105     def put(self, task_id):
106
107         try:
108             uuid.UUID(task_id)
109         except ValueError:
110             return result_handler(consts.API_ERROR, 'invalid task id')
111
112         task_handler = V2TaskHandler()
113         try:
114             task_handler.get_by_uuid(task_id)
115         except ValueError:
116             return result_handler(consts.API_ERROR, 'no such task id')
117
118         return self._dispatch_post(task_id=task_id)
119
120     def add_environment(self, args):
121
122         task_id = args['task_id']
123         try:
124             environment_id = args['environment_id']
125         except KeyError:
126             return result_handler(consts.API_ERROR, 'environment_id must be provided')
127
128         try:
129             uuid.UUID(environment_id)
130         except ValueError:
131             return result_handler(consts.API_ERROR, 'invalid environment id')
132
133         environment_handler = V2EnvironmentHandler()
134         try:
135             environment_handler.get_by_uuid(environment_id)
136         except ValueError:
137             return result_handler(consts.API_ERROR, 'no such environment id')
138
139         LOG.info('update environment_id in task')
140         task_handler = V2TaskHandler()
141         task_handler.update_attr(task_id, {'environment_id': environment_id})
142
143         return result_handler(consts.API_SUCCESS, {'uuid': task_id})
144
145     def add_case(self, args):
146         task_id = args['task_id']
147         try:
148             name = args['case_name']
149         except KeyError:
150             return result_handler(consts.API_ERROR, 'case_name must be provided')
151
152         try:
153             content = args['case_content']
154         except KeyError:
155             return result_handler(consts.API_ERROR, 'case_content must be provided')
156
157         LOG.info('update case info in task')
158         task_handler = V2TaskHandler()
159         task_update_data = {
160             'case_name': name,
161             'content': content,
162             'suite': False
163         }
164         task_handler.update_attr(task_id, task_update_data)
165
166         return result_handler(consts.API_SUCCESS, {'uuid': task_id})
167
168     def add_suite(self, args):
169         task_id = args['task_id']
170         try:
171             name = args['suite_name']
172         except KeyError:
173             return result_handler(consts.API_ERROR, 'suite_name must be provided')
174
175         try:
176             content = args['suite_content']
177         except KeyError:
178             return result_handler(consts.API_ERROR, 'suite_content must be provided')
179
180         LOG.info('update suite info in task')
181         task_handler = V2TaskHandler()
182         task_update_data = {
183             'case_name': name,
184             'content': content,
185             'suite': True
186         }
187         task_handler.update_attr(task_id, task_update_data)
188
189         return result_handler(consts.API_SUCCESS, {'uuid': task_id})