Add API(v2) to delete certain task 75/37775/2
authorchenjiankun <chenjiankun1@huawei.com>
Wed, 19 Jul 2017 10:01:05 +0000 (10:01 +0000)
committerchenjiankun <chenjiankun1@huawei.com>
Wed, 19 Jul 2017 10:07:14 +0000 (10:07 +0000)
JIRA: YARDSTICK-738

API: /api/v2/yardstick/tasks/<task_id>
METHOD: DELETE

Change-Id: I28215d2cae63551455abacc4142cff3c4aaed281
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
api/resources/v2/tasks.py

index 7cc4f5d..2c4ff2c 100644 (file)
@@ -73,6 +73,35 @@ class V2Task(ApiResource):
 
         return result_handler(consts.API_SUCCESS, {'task': task_info})
 
+    def delete(self, task_id):
+        try:
+            uuid.UUID(task_id)
+        except ValueError:
+            return result_handler(consts.API_ERROR, 'invalid task id')
+
+        task_handler = V2TaskHandler()
+        try:
+            project_id = task_handler.get_by_uuid(task_id).project_id
+        except ValueError:
+            return result_handler(consts.API_ERROR, 'no such task id')
+
+        LOG.info('delete task in database')
+        task_handler.delete_by_uuid(task_id)
+
+        project_handler = V2ProjectHandler()
+        project = project_handler.get_by_uuid(project_id)
+
+        if project.tasks:
+            LOG.info('update tasks in project')
+            new_task_list = project.tasks.split(',').remove(task_id)
+            if new_task_list:
+                new_tasks = ','.join(new_task_list)
+            else:
+                new_tasks = None
+            project_handler.update_attr(project_id, {'tasks': new_tasks})
+
+        return result_handler(consts.API_SUCCESS, {'task': task_id})
+
     def put(self, task_id):
 
         try: