From 1d1f93bd3b3fe2533289955c1fc47b2d215dd4ae Mon Sep 17 00:00:00 2001 From: chenjiankun Date: Wed, 19 Jul 2017 08:47:10 +0000 Subject: Add API(v2) to create task JIRA: YARDSTICK-735 API: /api/v2/yardstick/tasks/action METHOD: POST PARAMS: { 'action': 'create_task', 'args': { 'name': 'task1', 'project_id': project_id } } Change-Id: I1f9c743f32bbcff999e37cf53ebfa96b41c61e5e Signed-off-by: chenjiankun --- api/resources/v2/tasks.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 api/resources/v2/tasks.py (limited to 'api') diff --git a/api/resources/v2/tasks.py b/api/resources/v2/tasks.py new file mode 100644 index 000000000..6366be847 --- /dev/null +++ b/api/resources/v2/tasks.py @@ -0,0 +1,49 @@ +import uuid +import logging +from datetime import datetime + +from api import ApiResource +from api.database.v2.handlers import V2TaskHandler +from api.database.v2.handlers import V2ProjectHandler +from yardstick.common.utils import result_handler +from yardstick.common import constants as consts + +LOG = logging.getLogger(__name__) +LOG.setLevel(logging.DEBUG) + + +class V2Tasks(ApiResource): + + def post(self): + return self._dispatch_post() + + def create_task(self, args): + try: + name = args['name'] + except KeyError: + return result_handler(consts.API_ERROR, 'name must be provided') + + try: + project_id = args['project_id'] + except KeyError: + return result_handler(consts.API_ERROR, 'project_id must be provided') + + task_id = str(uuid.uuid4()) + create_time = datetime.now() + task_handler = V2TaskHandler() + + LOG.info('create task in database') + task_init_data = { + 'uuid': task_id, + 'project_id': project_id, + 'name': name, + 'time': create_time, + 'status': -1 + } + task_handler.insert(task_init_data) + + LOG.info('create task in project') + project_handler = V2ProjectHandler() + project_handler.append_attr(project_id, {'tasks': task_id}) + + return result_handler(consts.API_SUCCESS, {'uuid': task_id}) -- cgit 1.2.3-korg