Passed
Push — master ( 2fdf18...019a6d )
by Lakshmi
04:12 queued 01:19
created

get_rules_given_trigger()   A

Complexity

Conditions 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
dl 0
loc 13
rs 9.2
c 1
b 0
f 0
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 import log as logging
19
from st2common.persistence.rule import Rule
20
21
22
LOG = logging.getLogger(__name__)
23
24
__all__ = [
25
    'get_rules_given_trigger',
26
    'get_rules_with_trigger_ref'
27
]
28
29
30
def get_rules_given_trigger(trigger):
31
32
    if isinstance(trigger, six.string_types):
33
        return get_rules_with_trigger_ref(trigger_ref=trigger)
34
35
    if isinstance(trigger, dict):
36
        trigger_ref = trigger.get('ref', None)
37
        if trigger_ref:
38
            return get_rules_with_trigger_ref(trigger_ref=trigger_ref)
39
        else:
40
            raise ValueError('Trigger dict %s is missing ``ref``.' % trigger)
41
42
    raise ValueError('Unknown type %s for trigger. Cannot do rule lookups.' % type(trigger))
43
44
45
def get_rules_with_trigger_ref(trigger_ref=None, enabled=True):
46
    """
47
    Get rules in DB corresponding to given trigger_ref as a string reference.
48
49
    :param trigger_ref: Reference to trigger.
50
    :type trigger_ref: ``str``
51
52
    :rtype: ``list`` of ``RuleDB``
53
    """
54
55
    if not trigger_ref:
56
        return None
57
58
    LOG.debug('Querying rules with trigger %s', trigger_ref)
59
    return Rule.query(trigger=trigger_ref, enabled=enabled)
60