execution.py 5.94 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
import tornado.escape
19

20
from zoe_api.rest_api.request_handler import ZoeAPIRequestHandler
21
from zoe_api.exceptions import ZoeException
22 23


24
class ExecutionAPI(ZoeAPIRequestHandler):
25 26
    """The Execution API endpoint."""

27
    def get(self, execution_id):
28
        """GET a single execution by its ID."""
29 30
        if self.current_user is None:
            return
31

32 33 34 35 36
        try:
            e = self.api_endpoint.execution_by_id(self.current_user, execution_id)
        except ZoeException as e:
            self.set_status(e.status_code, e.message)
            return
37

38
        self.write(e.serialize())
39 40 41

    def delete(self, execution_id: int):
        """
42 43
        Terminate an execution.

44
        :param execution_id: the execution to be terminated
45
        """
46 47
        if self.current_user is None:
            return
48

49
        try:
50
            self.api_endpoint.execution_terminate(self.current_user, execution_id, 'user {} request from API'.format(self.current_user))
51 52
        except ZoeException as e:
            self.set_status(e.status_code, e.message)
53 54
        else:
            self.set_status(204)
55 56


57
class ExecutionDeleteAPI(ZoeAPIRequestHandler):
58 59
    """The ExecutionDelete API endpoints."""

60 61
    def delete(self, execution_id: int):
        """
62 63
        Delete an execution.

64 65
        :param execution_id: the execution to be deleted
        """
66 67
        if self.current_user is None:
            return
68

69 70 71 72
        try:
            self.api_endpoint.execution_delete(self.current_user, execution_id)
        except ZoeException as e:
            self.set_status(e.status_code, e.message)
73 74
        else:
            self.set_status(204)
75 76


77
class ExecutionCollectionAPI(ZoeAPIRequestHandler):
78 79
    """The Execution Collection API endpoints."""

80 81 82 83
    def get(self):
        """
        Returns a list of all active executions.

84 85
        The list can be filtered by passing a non-empty JSON dictionary. Any combination of the following filters is supported:

86
        * status: one of submitted, queued, starting, error, running, cleaning up, terminated
87 88 89 90 91 92 93 94 95
        * name: execution mane
        * user_id: user_id owning the execution (admin only)
        * limit: limit the number of returned entries
        * earlier_than_submit: all execution that where submitted earlier than this timestamp
        * earlier_than_start: all execution that started earlier than this timestamp
        * earlier_than_end: all execution that ended earlier than this timestamp
        * later_than_submit: all execution that where submitted later than this timestamp
        * later_than_start: all execution that started later than this timestamp
        * later_than_end: all execution that started later than this timestamp
Daniele Venzano's avatar
Daniele Venzano committed
96

97
        All timestamps should be passed as number of seconds since the epoch (UTC timezone).
98

Daniele Venzano's avatar
Daniele Venzano committed
99
        example:  curl -u 'username:password' -X GET 'http://bf5:8080/api/0.6/execution?limit=1&status=terminated'
100

101 102
        :return:
        """
103 104
        if self.current_user is None:
            return
105

106 107
        filt_dict = {}

108 109 110 111 112 113 114 115 116 117 118 119
        filters = [
            ('status', str),
            ('name', str),
            ('user_id', str),
            ('limit', int),
            ('earlier_than_submit', int),
            ('earlier_than_start', int),
            ('earlier_than_end', int),
            ('later_than_submit', int),
            ('later_than_start', int),
            ('later_than_end', int)
        ]
Daniele Venzano's avatar
Daniele Venzano committed
120 121 122 123
        for filt in filters:
            if filt[0] in self.request.arguments:
                if filt[1] == str:
                    filt_dict[filt[0]] = self.request.arguments[filt[0]][0].decode('utf-8')
124
                else:
Daniele Venzano's avatar
Daniele Venzano committed
125
                    filt_dict[filt[0]] = filt[1](self.request.arguments[filt[0]][0])
126

127 128 129 130 131
        try:
            execs = self.api_endpoint.execution_list(self.current_user, **filt_dict)
        except ZoeException as e:
            self.set_status(e.status_code, e.message)
            return
132

133
        self.write({e.id: e.serialize() for e in execs})
134 135 136 137

    def post(self):
        """
        Starts an execution, given an application description. Takes a JSON object.
138

139 140
        :return: the new execution_id
        """
141 142
        if self.current_user is None:
            return
143 144

        try:
145 146
            data = tornado.escape.json_decode(self.request.body)
        except ValueError:
147 148
            self.set_status(400, 'Error decoding JSON data')
            return
149 150 151 152

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

153 154 155 156 157
        try:
            new_id = self.api_endpoint.execution_start(self.current_user, exec_name, application_description)
        except ZoeException as e:
            self.set_status(e.status_code, e.message)
            return
158

159 160
        self.set_status(201)
        self.write({'execution_id': new_id})
161

162

163
class ExecutionEndpointsAPI(ZoeAPIRequestHandler):
164 165 166 167 168 169 170 171
    """The ExecutionEndpoints API endpoint."""

    def get(self, execution_id: int):
        """
        Get a list of execution endpoints.

        :param execution_id: the execution to be deleted
        """
172 173
        if self.current_user is None:
            return
174

175 176 177 178 179 180
        try:
            execution = self.api_endpoint.execution_by_id(self.current_user, execution_id)
            services_, endpoints = self.api_endpoint.execution_endpoints(self.current_user, execution)
        except ZoeException as e:
            self.set_status(e.status_code, e.message)
            return
181 182

        self.write({'endpoints': endpoints})