Completed
Push — master ( d1f0a7...3b2ece )
by Edward
21:04 queued 05:38
created

st2common.validators.api.transform_definition()   F

Complexity

Conditions 11

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 3.1765
cc 11

How to fix   Complexity   

Complexity

Complex classes like st2common.validators.api.transform_definition() 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.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.content.utils import get_packs_base_paths
23
from st2common.content.utils import check_pack_content_directory_exists
24
25
26
LOG = logging.getLogger(__name__)
27
28
29
def validate_action(action_api):
30
    runner_db = _get_runner_model(action_api)
31
32
    # Check if pack is valid.
33
    if not _is_valid_pack(action_api.pack):
34
        packs_base_paths = get_packs_base_paths()
35
        packs_base_paths = ','.join(packs_base_paths)
36
        msg = ('Content pack "%s" is not found or doesn\'t contain actions directory. '
37
               'Searched in: %s' %
38
               (action_api.pack, packs_base_paths))
39
        raise ValueValidationException(msg)
40
41
    # Check if parameters defined are valid.
42
    _validate_parameters(action_api.parameters, runner_db.runner_parameters)
43
44
45
def _get_runner_model(action_api):
46
    runner_db = None
47
    # Check if runner exists.
48
    try:
49
        runner_db = get_runnertype_by_name(action_api.runner_type)
50
    except StackStormDBObjectNotFoundError:
51
        msg = 'RunnerType %s is not found.' % action_api.runner_type
52
        raise ValueValidationException(msg)
53
    return runner_db
54
55
56
def _is_valid_pack(pack):
57
    return check_pack_content_directory_exists(pack=pack, content_type='actions')
58
59
60
def _validate_parameters(action_params=None, runner_params=None):
61
    for param, action_param_meta in six.iteritems(action_params):
62
        if 'immutable' in action_param_meta:
63
            if param in runner_params:
64
                runner_param_meta = runner_params[param]
65
                if 'immutable' in runner_param_meta:
66
                    msg = 'Param %s is declared immutable in runner. ' % param + \
67
                          'Cannot override in action.'
68
                    raise ValueValidationException(msg)
69
                if 'default' not in action_param_meta and 'default' not in runner_param_meta:
70
                    msg = 'Immutable param %s requires a default value.' % param
71
                    raise ValueValidationException(msg)
72
            else:
73
                if 'default' not in action_param_meta:
74
                    msg = 'Immutable param %s requires a default value.' % param
75
                    raise ValueValidationException(msg)
76