executions.py 4.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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.

"""
17
This module contains all execution-related API calls for Zoe clients.
18
19
20
21
"""

import logging

22
from zoe_cmd.api_lib.api_base import ZoeAPIBase
23
24
25
26
27
28
from zoe_lib.exceptions import ZoeAPIException

log = logging.getLogger(__name__)


class ZoeExecutionsAPI(ZoeAPIBase):
29
30
31
    """
    The execution API class.
    """
32
    def terminate(self, execution_id):
33
34
35
        """
        Terminates an execution.

36
        :param execution_id: the execution to terminate
Daniele Venzano's avatar
Daniele Venzano committed
37
        :return: True if the operation was successful, False otherwise
38
39
40

        :type execution_id: int
        :rtype: bool
41
42
43
44
45
        """
        data, status_code = self._rest_delete('/execution/' + str(execution_id))
        if status_code == 204:
            return
        else:
46
            raise ZoeAPIException(data)
47

48
49
50
51
52
53
54
55
56
57
58
59
60
61
    def delete(self, execution_id):
        """
        Deletes an execution.

        :param execution_id: the execution to delete
        :return: True if the operation was successful, False otherwise

        :type execution_id: int
        :rtype: bool
        """
        data, status_code = self._rest_delete('/execution/delete/' + str(execution_id))
        if status_code == 204:
            return
        else:
62
            raise ZoeAPIException(data)
63

Daniele Venzano's avatar
Daniele Venzano committed
64
    def list(self, **kwargs):
65
66
67
        """
        Returns a list of all executions for the calling user, all of them if the user is admin.

Daniele Venzano's avatar
Daniele Venzano committed
68
69
70
71
72
73
74
75
76
77
78
79
        The list can be filtered by passing arguments. Any combination of the following filters is supported:

        * status: one of submitted, scheduled, starting, error, running, cleaning up, terminated
        * 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
80

81
82
        :return:
        """
Daniele Venzano's avatar
Daniele Venzano committed
83
        data, status_code = self._rest_get('/execution', kwargs)
84
        if status_code == 200:
85
            return list(data.values())
86
        else:
87
            raise ZoeAPIException(data)
88

89
    def get(self, execution_id):
90
91
92
        """
        Retrieve the Execution object for an existing execution.

93
        :param execution_id: the execution to load from the master
94
        :return: the Execution object, or None
95
96
97

        :type execution_id: int
        :rtype: dict
98
99
100
101
        """
        data, status_code = self._rest_get('/execution/' + str(execution_id))
        if status_code == 200:
            return data
102
        elif status_code == 404:
103
            return None
104
105
        else:
            raise ZoeAPIException(data)
106

107
    def start(self, name, application_description):
108
        """
109
        Submit an application to the master to start a new execution.
110
111

        :param name: user-provided name of the execution
112
        :param application_description: the application to start
113
        :return: the new Execution object, or None in case of error
114
115
116
117

        :type name: str
        :type application_description: dict
        :rtype: int
118
119
        """
        execution = {
120
            "application": application_description,
121
122
123
124
            'name': name
        }
        data, status_code = self._rest_post('/execution', execution)
        if status_code != 201:
125
            raise ZoeAPIException(data)
126
127
        else:
            return data['execution_id']
128
129
130
131
132
133
134
135
136
137
138
139
140

    def endpoints(self, execution_id):
        """
        Retrieve the public endpoints exposed by a running execution.

        :param execution_id: the execution to inspect
        :return:
        """
        data, status_code = self._rest_get('/execution/endpoints/' + str(execution_id))
        if status_code == 200:
            return data['endpoints']
        else:
            return None