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
|
|
|
import six |
17
|
|
|
|
18
|
|
|
from st2common.exceptions.apivalidation import ValueValidationException |
19
|
|
|
from st2common.exceptions.db import StackStormDBObjectNotFoundError |
20
|
|
|
from st2common import log as logging |
21
|
|
|
from st2common.util.action_db import get_runnertype_by_name |
22
|
|
|
from st2common.util import schema as util_schema |
23
|
|
|
from st2common.content.utils import get_packs_base_paths |
24
|
|
|
from st2common.content.utils import check_pack_content_directory_exists |
25
|
|
|
from st2common.models.system.common import ResourceReference |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
LOG = logging.getLogger(__name__) |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def validate_action(action_api): |
32
|
|
|
runner_db = _get_runner_model(action_api) |
33
|
|
|
|
34
|
|
|
# Check if pack is valid. |
35
|
|
|
if not _is_valid_pack(action_api.pack): |
36
|
|
|
packs_base_paths = get_packs_base_paths() |
37
|
|
|
packs_base_paths = ','.join(packs_base_paths) |
38
|
|
|
msg = ('Content pack "%s" is not found or doesn\'t contain actions directory. ' |
39
|
|
|
'Searched in: %s' % |
40
|
|
|
(action_api.pack, packs_base_paths)) |
41
|
|
|
raise ValueValidationException(msg) |
42
|
|
|
|
43
|
|
|
# Check if parameters defined are valid. |
44
|
|
|
action_ref = ResourceReference.to_string_reference(pack=action_api.pack, name=action_api.name) |
45
|
|
|
_validate_parameters(action_ref, action_api.parameters, runner_db.runner_parameters) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def _get_runner_model(action_api): |
49
|
|
|
runner_db = None |
50
|
|
|
# Check if runner exists. |
51
|
|
|
try: |
52
|
|
|
runner_db = get_runnertype_by_name(action_api.runner_type) |
53
|
|
|
except StackStormDBObjectNotFoundError: |
54
|
|
|
msg = 'RunnerType %s is not found.' % action_api.runner_type |
55
|
|
|
raise ValueValidationException(msg) |
56
|
|
|
return runner_db |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
def _is_valid_pack(pack): |
60
|
|
|
return check_pack_content_directory_exists(pack=pack, content_type='actions') |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def _validate_parameters(action_ref, action_params=None, runner_params=None): |
64
|
|
|
for action_param, action_param_meta in six.iteritems(action_params): |
65
|
|
|
# Check if overridden runner parameters are permitted. |
66
|
|
|
if action_param in runner_params: |
67
|
|
|
for action_param_attr, value in six.iteritems(action_param_meta): |
68
|
|
|
util_schema.validate_runner_parameter_attribute_override( |
69
|
|
|
action_ref, action_param, action_param_attr, |
70
|
|
|
value, runner_params[action_param].get(action_param_attr)) |
71
|
|
|
|
72
|
|
|
if 'immutable' in action_param_meta: |
73
|
|
|
if action_param in runner_params: |
74
|
|
|
runner_param_meta = runner_params[action_param] |
75
|
|
|
if 'immutable' in runner_param_meta: |
76
|
|
|
msg = 'Param %s is declared immutable in runner. ' % action_param + \ |
77
|
|
|
'Cannot override in action.' |
78
|
|
|
raise ValueValidationException(msg) |
79
|
|
|
if 'default' not in action_param_meta and 'default' not in runner_param_meta: |
80
|
|
|
msg = 'Immutable param %s requires a default value.' % action_param |
81
|
|
|
raise ValueValidationException(msg) |
82
|
|
|
else: |
83
|
|
|
if 'default' not in action_param_meta: |
84
|
|
|
msg = 'Immutable param %s requires a default value.' % action_param |
85
|
|
|
raise ValueValidationException(msg) |
86
|
|
|
|