summaryrefslogtreecommitdiffstats
path: root/api/resources/v1/asynctasks.py
blob: 759df214cfe330c4769c95f49001736067101065 (plain)
@media only all and (prefers-color-scheme: dark) { .highlight .hll { background-color: #49483e } .highlight .c { color: #75715e } /* Comment */ .highlight .err { color: #960050; background-color: #1e0010 } /* Error */ .highlight .k { color: #66d9ef } /* Keyword */ .highlight .l { color: #ae81ff } /* Literal */ .highlight .n { color: #f8f8f2 } /* Name */ .highlight .o { color: #f92672 } /* Operator */ .highlight .p { color: #f8f8f2 } /* Punctuation */ .highlight .ch { color: #75715e } /* Comment.Hashbang */ .highlight .cm { color: #75715e } /* Comment.Multiline */ .highlight .cp { color: #75715e } /* Comment.Preproc */ .highlight .cpf { color: #75715e } /* Comment.PreprocFile */ .highlight .c1 { color: #75715e } /* Comment.Single */ .highlight .cs { color: #75715e } /* Comment.Special */ .highlight .gd { color: #f92672 } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gi { color: #a6e22e } /* Generic.Inserted */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #75715e } /* Generic.Subheading */ .highlight .kc { color: #66d9ef } /* Keyword.Constant */ .highlight .kd { color: #66d9ef } /* Keyword.Declaration */ .highlight .kn { color: #f92672 } /* Keyword.Namespace */ .highlight .kp { color: #66d9ef } /* Keyword.Pseudo */ .highlight .kr { color: #66d9ef } /* Keyword.Reserved */ .highlight .kt { color: #66d9ef } /* Keyword.Type */ .highlight .ld { color: #e6db74 } /* Literal.Date */ .highlight .m { color: #ae81ff } /* Literal.Number */ .highlight .s { color: #e6db74 } /* Literal.String */ .highlight .na { color: #a6e22e } /* Name.Attribute */ .highlight .nb { color: #f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8
# ############################################################################
# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
# ############################################################################
import uuid
import logging

from api import ApiResource
from api.database.v1.handlers import AsyncTaskHandler
from yardstick.common import constants as consts
from yardstick.common.utils import result_handler

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)


class V1AsyncTask(ApiResource):

    def get(self):
        args = self._get_args()

        try:
            task_id = args['task_id']
        except KeyError:
            return result_handler(consts.API_ERROR, 'task_id must be provided')

        try:
            uuid.UUID(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid task_id')

        asynctask_handler = AsyncTaskHandler()
        try:
            asynctask = asynctask_handler.get_task_by_taskid(task_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid task_id')

        def _unfinished():
            return result_handler(consts.TASK_NOT_DONE, {})

        def _finished():
            return result_handler(consts.TASK_DONE, {})

        def _error():
            return result_handler(consts.TASK_FAILED, asynctask.error)

        status = asynctask.status
        LOG.debug('Task status is: %s', status)

        if status not in [consts.TASK_NOT_DONE,
                          consts.TASK_DONE,
                          consts.TASK_FAILED]:
            return result_handler(consts.API_ERROR, 'internal server error')

        switcher = {
            consts.TASK_NOT_DONE: _unfinished,
            consts.TASK_DONE: _finished,
            consts.TASK_FAILED: _error
        }

        return switcher.get(status)()