Completed
Push — master ( 16c195...49ca7e )
by W
07:23
created

st2common.validators.api._validate_parameters()   F

Complexity

Conditions 12

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 2.7856
cc 12

How to fix   Complexity   

Complexity

Complex classes like st2common.validators.api._validate_parameters() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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.action import InvalidActionParameterException
19
from st2common.exceptions.apivalidation import ValueValidationException
20
from st2common.exceptions.db import StackStormDBObjectNotFoundError
21
from st2common import log as logging
22
from st2common.util.action_db import get_runnertype_by_name
23
from st2common.util import schema as util_schema
24
from st2common.content.utils import get_packs_base_paths
25
from st2common.content.utils import check_pack_content_directory_exists
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
    _validate_parameters(action_api.parameters, runner_db.runner_parameters)
45
46
47
def _get_runner_model(action_api):
48
    runner_db = None
49
    # Check if runner exists.
50
    try:
51
        runner_db = get_runnertype_by_name(action_api.runner_type)
52
    except StackStormDBObjectNotFoundError:
53
        msg = 'RunnerType %s is not found.' % action_api.runner_type
54
        raise ValueValidationException(msg)
55
    return runner_db
56
57
58
def _is_valid_pack(pack):
59
    return check_pack_content_directory_exists(pack=pack, content_type='actions')
60
61
62
def _validate_parameters(action_params=None, runner_params=None):
63
    for action_param, action_param_meta in six.iteritems(action_params):
64
        # Check if overridden runner parameters are permitted.
65
        if action_param in runner_params:
66
            for action_param_attr, value in six.iteritems(action_param_meta):
67
                if (action_param_attr not in util_schema.RUNNER_PARAM_OVERRIDABLE_ATTRS and
68
                        runner_params[action_param].get(action_param_attr) != value):
69
                    raise InvalidActionParameterException(
70
                        'The attribute "%s" for the runner parameter "%s" cannot '
71
                        'be overridden.' % (action_param_attr, action_param))
72
73
        if 'immutable' in action_param_meta:
74
            if action_param in runner_params:
75
                runner_param_meta = runner_params[action_param]
76
                if 'immutable' in runner_param_meta:
77
                    msg = 'Param %s is declared immutable in runner. ' % action_param + \
78
                          'Cannot override in action.'
79
                    raise ValueValidationException(msg)
80
                if 'default' not in action_param_meta and 'default' not in runner_param_meta:
81
                    msg = 'Immutable param %s requires a default value.' % action_param
82
                    raise ValueValidationException(msg)
83
            else:
84
                if 'default' not in action_param_meta:
85
                    msg = 'Immutable param %s requires a default value.' % action_param
86
                    raise ValueValidationException(msg)
87