|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
from mongoengine import ValidationError |
|
17
|
|
|
import six |
|
18
|
|
|
|
|
19
|
|
|
from st2api.controllers import resource |
|
20
|
|
|
from st2common.exceptions.db import StackStormDBObjectNotFoundError |
|
21
|
|
|
from st2common import log as logging |
|
22
|
|
|
from st2common.content import utils |
|
23
|
|
|
from st2common.models.api.action import ActionAPI |
|
24
|
|
|
from st2common.models.utils import action_param_utils |
|
25
|
|
|
from st2common.persistence.action import Action |
|
26
|
|
|
from st2common.persistence.runner import RunnerType |
|
27
|
|
|
from st2common.router import abort |
|
28
|
|
|
|
|
29
|
|
|
http_client = six.moves.http_client |
|
30
|
|
|
|
|
31
|
|
|
LOG = logging.getLogger(__name__) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class LookupUtils(object): |
|
35
|
|
|
|
|
36
|
|
|
@staticmethod |
|
37
|
|
|
def _get_action_by_id(id): |
|
|
|
|
|
|
38
|
|
|
try: |
|
39
|
|
|
return Action.get_by_id(id) |
|
40
|
|
|
except Exception as e: |
|
41
|
|
|
msg = 'Database lookup for id="%s" resulted in exception. %s' % (id, e) |
|
42
|
|
|
LOG.exception(msg) |
|
43
|
|
|
abort(http_client.NOT_FOUND, msg) |
|
44
|
|
|
|
|
45
|
|
|
@staticmethod |
|
46
|
|
|
def _get_runner_by_id(id): |
|
|
|
|
|
|
47
|
|
|
try: |
|
48
|
|
|
return RunnerType.get_by_id(id) |
|
49
|
|
|
except (ValueError, ValidationError) as e: |
|
50
|
|
|
msg = 'Database lookup for id="%s" resulted in exception. %s' % (id, e) |
|
51
|
|
|
LOG.exception(msg) |
|
52
|
|
|
abort(http_client.NOT_FOUND, msg) |
|
53
|
|
|
|
|
54
|
|
|
@staticmethod |
|
55
|
|
|
def _get_runner_by_name(name): |
|
56
|
|
|
try: |
|
57
|
|
|
return RunnerType.get_by_name(name) |
|
58
|
|
|
except (ValueError, ValidationError) as e: |
|
59
|
|
|
msg = 'Database lookup for name="%s" resulted in exception. %s' % (id, e) |
|
60
|
|
|
LOG.exception(msg) |
|
61
|
|
|
abort(http_client.NOT_FOUND, msg) |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
class ParametersViewController(object): |
|
65
|
|
|
|
|
66
|
|
|
def get_one(self, action_id): |
|
67
|
|
|
return self._get_one(action_id) |
|
68
|
|
|
|
|
69
|
|
|
@staticmethod |
|
70
|
|
|
def _get_one(action_id): |
|
71
|
|
|
""" |
|
72
|
|
|
List merged action & runner parameters by action id. |
|
73
|
|
|
|
|
74
|
|
|
Handle: |
|
75
|
|
|
GET /actions/views/parameters/1 |
|
76
|
|
|
""" |
|
77
|
|
|
action_db = LookupUtils._get_action_by_id(action_id) |
|
|
|
|
|
|
78
|
|
|
LOG.info('Found action: %s, runner: %s', action_db, action_db.runner_type['name']) |
|
79
|
|
|
runner_db = LookupUtils._get_runner_by_name(action_db.runner_type['name']) |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
all_params = action_param_utils.get_params_view( |
|
82
|
|
|
action_db=action_db, runner_db=runner_db, merged_only=True) |
|
83
|
|
|
|
|
84
|
|
|
return {'parameters': all_params} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
class OverviewController(resource.ContentPackResourceController): |
|
88
|
|
|
model = ActionAPI |
|
89
|
|
|
access = Action |
|
90
|
|
|
supported_filters = {} |
|
91
|
|
|
|
|
92
|
|
|
query_options = { |
|
93
|
|
|
'sort': ['pack', 'name'] |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
include_reference = True |
|
97
|
|
|
|
|
98
|
|
|
def get_one(self, ref_or_id): |
|
|
|
|
|
|
99
|
|
|
""" |
|
100
|
|
|
List action by id. |
|
101
|
|
|
|
|
102
|
|
|
Handle: |
|
103
|
|
|
GET /actions/views/overview/1 |
|
104
|
|
|
""" |
|
105
|
|
|
resp = super(OverviewController, self)._get_one(ref_or_id) |
|
106
|
|
|
action_api = ActionAPI(**resp.json) |
|
107
|
|
|
result = self._transform_action_api(action_api) |
|
108
|
|
|
resp.json = result |
|
109
|
|
|
return resp |
|
110
|
|
|
|
|
111
|
|
|
def get_all(self, **kwargs): |
|
112
|
|
|
""" |
|
113
|
|
|
List all actions. |
|
114
|
|
|
|
|
115
|
|
|
Handles requests: |
|
116
|
|
|
GET /actions/views/overview |
|
117
|
|
|
""" |
|
118
|
|
|
resp = super(OverviewController, self)._get_all(**kwargs) |
|
119
|
|
|
result = [] |
|
120
|
|
|
for item in resp.json: |
|
121
|
|
|
action_api = ActionAPI(**item) |
|
122
|
|
|
result.append(self._transform_action_api(action_api)) |
|
123
|
|
|
resp.json = result |
|
124
|
|
|
return resp |
|
125
|
|
|
|
|
126
|
|
|
@staticmethod |
|
127
|
|
|
def _transform_action_api(action_api): |
|
128
|
|
|
action_id = action_api.id |
|
129
|
|
|
action_api.parameters = ParametersViewController._get_one(action_id).get('parameters') |
|
|
|
|
|
|
130
|
|
|
return action_api |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
class EntryPointController(resource.ContentPackResourceController): |
|
134
|
|
|
model = ActionAPI |
|
135
|
|
|
access = Action |
|
136
|
|
|
|
|
137
|
|
|
supported_filters = {} |
|
138
|
|
|
|
|
139
|
|
|
def get_all(self, **kwargs): |
|
140
|
|
|
return abort(404) |
|
141
|
|
|
|
|
142
|
|
|
def get_one(self, ref_or_id): |
|
|
|
|
|
|
143
|
|
|
""" |
|
144
|
|
|
Outputs the file associated with action entry_point |
|
145
|
|
|
|
|
146
|
|
|
Handles requests: |
|
147
|
|
|
GET /actions/views/entry_point/1 |
|
148
|
|
|
""" |
|
149
|
|
|
LOG.info('GET /actions/views/overview with ref_or_id=%s', ref_or_id) |
|
150
|
|
|
action_db = self._get_by_ref_or_id(ref_or_id=ref_or_id) |
|
151
|
|
|
|
|
152
|
|
|
pack = getattr(action_db, 'pack', None) |
|
153
|
|
|
entry_point = getattr(action_db, 'entry_point', None) |
|
154
|
|
|
|
|
155
|
|
|
abs_path = utils.get_entry_point_abs_path(pack, entry_point) |
|
156
|
|
|
|
|
157
|
|
|
if not abs_path: |
|
158
|
|
|
raise StackStormDBObjectNotFoundError('Action ref_or_id=%s has no entry_point to output' |
|
159
|
|
|
% ref_or_id) |
|
160
|
|
|
|
|
161
|
|
|
with open(abs_path) as file: |
|
162
|
|
|
content = file.read() |
|
163
|
|
|
|
|
164
|
|
|
return content |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
class ActionViewsController(object): |
|
168
|
|
|
parameters = ParametersViewController() |
|
169
|
|
|
overview = OverviewController() |
|
170
|
|
|
entry_point = EntryPointController() |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
parameters_view_controller = ParametersViewController() |
|
174
|
|
|
overview_controller = OverviewController() |
|
175
|
|
|
entry_point_controller = EntryPointController() |
|
176
|
|
|
|
It is generally discouraged to redefine built-ins as this makes code very hard to read.