execution.py 3.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2016, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16 17
"""The Execution API endpoints."""

18 19 20
from flask_restful import Resource, request
from werkzeug.exceptions import BadRequest

21 22 23
from zoe_api.rest_api.utils import catch_exceptions, get_auth
import zoe_api.exceptions
import zoe_api.api_endpoint
24 25 26


class ExecutionAPI(Resource):
27 28
    """The Execution API endpoint."""

29 30 31
    def __init__(self, api_endpoint: zoe_api.api_endpoint.APIEndpoint):
        self.api_endpoint = api_endpoint

32 33
    @catch_exceptions
    def get(self, execution_id):
34
        """GET a single execution by its ID."""
35 36
        uid, role = get_auth(request)

37
        e = self.api_endpoint.execution_by_id(uid, role, execution_id)
38 39 40 41 42 43

        return e.serialize()

    @catch_exceptions
    def delete(self, execution_id: int):
        """
44 45
        Terminate an execution.

46
        :param execution_id: the execution to be terminated
47 48 49 50
        :return:
        """
        uid, role = get_auth(request)

51
        success, message = self.api_endpoint.execution_terminate(uid, role, execution_id)
52
        if not success:
53
            raise zoe_api.exceptions.ZoeRestAPIException(message, 400)
54 55 56 57

        return '', 204


58
class ExecutionDeleteAPI(Resource):
59 60
    """The ExecutionDelete API endpoints."""

61 62 63 64 65 66
    def __init__(self, api_endpoint: zoe_api.api_endpoint.APIEndpoint):
        self.api_endpoint = api_endpoint

    @catch_exceptions
    def delete(self, execution_id: int):
        """
67 68
        Delete an execution.

69 70 71 72 73 74 75 76 77 78 79 80
        :param execution_id: the execution to be deleted
        :return:
        """
        uid, role = get_auth(request)

        success, message = self.api_endpoint.execution_delete(uid, role, execution_id)
        if not success:
            raise zoe_api.exceptions.ZoeRestAPIException(message, 400)

        return '', 204


81
class ExecutionCollectionAPI(Resource):
82 83
    """The Execution Collection API endpoints."""

84 85 86
    def __init__(self, api_endpoint: zoe_api.api_endpoint.APIEndpoint):
        self.api_endpoint = api_endpoint

87 88 89 90 91 92 93 94 95
    @catch_exceptions
    def get(self):
        """
        Returns a list of all active executions.

        :return:
        """
        uid, role = get_auth(request)

96
        execs = self.api_endpoint.execution_list(uid, role)
97 98 99 100 101 102 103 104 105 106 107 108 109
        return [e.serialize() for e in execs]

    @catch_exceptions
    def post(self):
        """
        Starts an execution, given an application description. Takes a JSON object.
        :return: the new execution_id
        """
        uid, role = get_auth(request)

        try:
            data = request.get_json()
        except BadRequest:
110
            raise zoe_api.exceptions.ZoeRestAPIException('Error decoding JSON data')
111 112 113 114

        application_description = data['application']
        exec_name = data['name']

115
        new_id = self.api_endpoint.execution_start(uid, role, exec_name, application_description)
116 117

        return {'execution_id': new_id}, 201