From 8a1525d5a7415202bcd0b625f2fca551edc592ab Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Mon, 8 May 2017 19:17:31 -0700 Subject: [PATCH 01/18] Just declare one trigger type for file watch sensor --- contrib/examples/rules/sample_rule_file_watch.yaml | 2 +- contrib/linux/sensors/file_watch_sensor.yaml | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/contrib/examples/rules/sample_rule_file_watch.yaml b/contrib/examples/rules/sample_rule_file_watch.yaml index c1b6643a49..35feab8168 100644 --- a/contrib/examples/rules/sample_rule_file_watch.yaml +++ b/contrib/examples/rules/sample_rule_file_watch.yaml @@ -7,7 +7,7 @@ enabled: false trigger: parameters: file_path: /var/log/dmesg - type: linux.file_watch.file_path + type: linux.file_watch.line criteria: {} diff --git a/contrib/linux/sensors/file_watch_sensor.yaml b/contrib/linux/sensors/file_watch_sensor.yaml index fd095443e2..0a335b1319 100644 --- a/contrib/linux/sensors/file_watch_sensor.yaml +++ b/contrib/linux/sensors/file_watch_sensor.yaml @@ -5,9 +5,10 @@ description: "Sensor which monitors files for new lines" trigger_types: - - name: "file_watch.file_path" + name: "file_watch.line" pack: "linux" - description: "Trigger which represents a file path to be monitored" + description: "Trigger which indicates a new line has been detected" + # This sensor can be supplied a path to a file to tail via a rule. parameters_schema: type: "object" properties: @@ -16,10 +17,7 @@ type: "string" required: true additionalProperties: false - - - name: "file_watch.line" - pack: "linux" - description: "Trigger which indicates a new line has been detected" + # This is the schema of the trigger payload the sensor generates payload_schema: type: "object" properties: From 1a55bcbc603bc1225f1e351160aec649175514e3 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Mon, 8 May 2017 19:33:01 -0700 Subject: [PATCH 02/18] Deprecate linux.file_watch.line trigger --- .../rules/sample_rule_file_watch.yaml | 2 +- contrib/linux/sensors/README.md | 28 +++++++++++++++++++ contrib/linux/sensors/file_watch_sensor.py | 2 +- contrib/linux/sensors/file_watch_sensor.yaml | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 contrib/linux/sensors/README.md diff --git a/contrib/examples/rules/sample_rule_file_watch.yaml b/contrib/examples/rules/sample_rule_file_watch.yaml index 35feab8168..c1b6643a49 100644 --- a/contrib/examples/rules/sample_rule_file_watch.yaml +++ b/contrib/examples/rules/sample_rule_file_watch.yaml @@ -7,7 +7,7 @@ enabled: false trigger: parameters: file_path: /var/log/dmesg - type: linux.file_watch.line + type: linux.file_watch.file_path criteria: {} diff --git a/contrib/linux/sensors/README.md b/contrib/linux/sensors/README.md new file mode 100644 index 0000000000..416e7fb9b9 --- /dev/null +++ b/contrib/linux/sensors/README.md @@ -0,0 +1,28 @@ +## DEPRECARTION NOTICE + +File watch sensor has been updated to use trigger with parameters supplied via a rule approach. Tailing a file path supplied via a config file is now deprecated. + +An example rule to supply a file path is as follows: + +``` +--- +name: sample_rule_file_watch +pack: "examples" +description: Sample rule custom trigger type - add a file to be watched by file_watch_sensor in linux pack. +enabled: false + +trigger: + parameters: + file_path: /var/log/dmesg + type: linux.file_watch.file_path + +criteria: {} + +action: + parameters: + cmd: echo "{{trigger}}" + ref: core.local + +``` + +The new trigger emitted will have the trigger ref ``linux.file_watch.file_path``. Trigger ``linux.file_watch.line`` is no longer emitted. diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index 49f624700a..f8bc76479b 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -9,7 +9,7 @@ class FileWatchSensor(Sensor): def __init__(self, sensor_service, config=None): super(FileWatchSensor, self).__init__(sensor_service=sensor_service, config=config) - self._trigger_ref = 'linux.file_watch.line' + self._trigger_ref = 'linux.file_watch.file_path' self._logger = self._sensor_service.get_logger(__name__) self._file_paths = [] # stores a list of file paths we are monitoring diff --git a/contrib/linux/sensors/file_watch_sensor.yaml b/contrib/linux/sensors/file_watch_sensor.yaml index 0a335b1319..586ffdf95f 100644 --- a/contrib/linux/sensors/file_watch_sensor.yaml +++ b/contrib/linux/sensors/file_watch_sensor.yaml @@ -5,7 +5,7 @@ description: "Sensor which monitors files for new lines" trigger_types: - - name: "file_watch.line" + name: "file_watch.file_path" pack: "linux" description: "Trigger which indicates a new line has been detected" # This sensor can be supplied a path to a file to tail via a rule. From d377362c6393c97c9e28dc59af4d7a546f222c1b Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Mon, 8 May 2017 19:35:38 -0700 Subject: [PATCH 03/18] Remove unneeded file_paths variable --- contrib/linux/sensors/file_watch_sensor.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index f8bc76479b..4f4491ced1 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -11,8 +11,6 @@ def __init__(self, sensor_service, config=None): config=config) self._trigger_ref = 'linux.file_watch.file_path' self._logger = self._sensor_service.get_logger(__name__) - - self._file_paths = [] # stores a list of file paths we are monitoring self._tail = None def setup(self): From 5a22501c54cfc62e05b5712703a2fd4da8532fab Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Mon, 8 May 2017 19:36:27 -0700 Subject: [PATCH 04/18] Fix README.md --- contrib/linux/sensors/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contrib/linux/sensors/README.md b/contrib/linux/sensors/README.md index 416e7fb9b9..ca8d2e3368 100644 --- a/contrib/linux/sensors/README.md +++ b/contrib/linux/sensors/README.md @@ -25,4 +25,5 @@ action: ``` -The new trigger emitted will have the trigger ref ``linux.file_watch.file_path``. Trigger ``linux.file_watch.line`` is no longer emitted. +The new trigger emitted will have the trigger ref ``linux.file_watch.file_path``. +Trigger ``linux.file_watch.line`` is no longer emitted. From 7de681c9d56390862bd62ba58e0dc2d7d205e286 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Mon, 8 May 2017 19:40:09 -0700 Subject: [PATCH 05/18] Change path of the default file to tail --- contrib/examples/rules/sample_rule_file_watch.yaml | 2 +- contrib/linux/sensors/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/examples/rules/sample_rule_file_watch.yaml b/contrib/examples/rules/sample_rule_file_watch.yaml index c1b6643a49..aff84de3c2 100644 --- a/contrib/examples/rules/sample_rule_file_watch.yaml +++ b/contrib/examples/rules/sample_rule_file_watch.yaml @@ -6,7 +6,7 @@ enabled: false trigger: parameters: - file_path: /var/log/dmesg + file_path: /tmp/st2_test type: linux.file_watch.file_path criteria: {} diff --git a/contrib/linux/sensors/README.md b/contrib/linux/sensors/README.md index ca8d2e3368..940b6ccdb3 100644 --- a/contrib/linux/sensors/README.md +++ b/contrib/linux/sensors/README.md @@ -13,7 +13,7 @@ enabled: false trigger: parameters: - file_path: /var/log/dmesg + file_path: /tmp/st2_test type: linux.file_watch.file_path criteria: {} From 2c01e66cd44f39baf8cca63325a586a34243b5db Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Mon, 8 May 2017 20:30:04 -0700 Subject: [PATCH 06/18] Change trigger name to linux.file_watch.line --- contrib/examples/rules/sample_rule_file_watch.yaml | 2 +- contrib/linux/sensors/README.md | 8 ++++---- contrib/linux/sensors/file_watch_sensor.py | 4 ++-- contrib/linux/sensors/file_watch_sensor.yaml | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/contrib/examples/rules/sample_rule_file_watch.yaml b/contrib/examples/rules/sample_rule_file_watch.yaml index aff84de3c2..784c543c38 100644 --- a/contrib/examples/rules/sample_rule_file_watch.yaml +++ b/contrib/examples/rules/sample_rule_file_watch.yaml @@ -7,7 +7,7 @@ enabled: false trigger: parameters: file_path: /tmp/st2_test - type: linux.file_watch.file_path + type: linux.file_watch.line criteria: {} diff --git a/contrib/linux/sensors/README.md b/contrib/linux/sensors/README.md index 940b6ccdb3..7924e91e17 100644 --- a/contrib/linux/sensors/README.md +++ b/contrib/linux/sensors/README.md @@ -1,4 +1,4 @@ -## DEPRECARTION NOTICE +## NOTICE File watch sensor has been updated to use trigger with parameters supplied via a rule approach. Tailing a file path supplied via a config file is now deprecated. @@ -14,7 +14,7 @@ enabled: false trigger: parameters: file_path: /tmp/st2_test - type: linux.file_watch.file_path + type: linux.file_watch.line criteria: {} @@ -25,5 +25,5 @@ action: ``` -The new trigger emitted will have the trigger ref ``linux.file_watch.file_path``. -Trigger ``linux.file_watch.line`` is no longer emitted. +Trigger ``linux.file_watch.line`` still emits the same payload as it used to. +Just the way to provide the file_path to tail has changed. diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index 4f4491ced1..abf880a682 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -9,7 +9,7 @@ class FileWatchSensor(Sensor): def __init__(self, sensor_service, config=None): super(FileWatchSensor, self).__init__(sensor_service=sensor_service, config=config) - self._trigger_ref = 'linux.file_watch.file_path' + self._trigger_ref = 'linux.file_watch.line' self._logger = self._sensor_service.get_logger(__name__) self._tail = None @@ -62,4 +62,4 @@ def _handle_line(self, file_path, line): 'file_name': os.path.basename(file_path), 'line': line } - self.sensor_service.dispatch(trigger=trigger, payload=payload) + self._sensor_service.dispatch(trigger=trigger, payload=payload) diff --git a/contrib/linux/sensors/file_watch_sensor.yaml b/contrib/linux/sensors/file_watch_sensor.yaml index 586ffdf95f..0a335b1319 100644 --- a/contrib/linux/sensors/file_watch_sensor.yaml +++ b/contrib/linux/sensors/file_watch_sensor.yaml @@ -5,7 +5,7 @@ description: "Sensor which monitors files for new lines" trigger_types: - - name: "file_watch.file_path" + name: "file_watch.line" pack: "linux" description: "Trigger which indicates a new line has been detected" # This sensor can be supplied a path to a file to tail via a rule. From 506cd5538abcccff5a6edc8b317fa2c0639b7924 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Mon, 8 May 2017 20:35:01 -0700 Subject: [PATCH 07/18] Add debug lines to file_watch_sensor --- contrib/linux/sensors/file_watch_sensor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index abf880a682..f304e2afe1 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -62,4 +62,5 @@ def _handle_line(self, file_path, line): 'file_name': os.path.basename(file_path), 'line': line } - self._sensor_service.dispatch(trigger=trigger, payload=payload) + self._logger.info('Sending payload %s for trigger %s to sensor_service.', payload, trigger) + self.sensor_service.dispatch(trigger=trigger, payload=payload) From a1b9f79fa79a9e0e91f709e0fdb3013fa931d67b Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Mon, 5 Jun 2017 07:25:40 -0700 Subject: [PATCH 08/18] Checkpoint --- contrib/linux/sensors/file_watch_sensor.py | 6 ++++-- contrib/linux/sensors/file_watch_sensor.yaml | 2 +- st2reactor/st2reactor/container/sensor_wrapper.py | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index f304e2afe1..eea16f7003 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -9,7 +9,7 @@ class FileWatchSensor(Sensor): def __init__(self, sensor_service, config=None): super(FileWatchSensor, self).__init__(sensor_service=sensor_service, config=config) - self._trigger_ref = 'linux.file_watch.line' + self._trigger = None self._logger = self._sensor_service.get_logger(__name__) self._tail = None @@ -38,6 +38,7 @@ def add_trigger(self, trigger): return self._tail.add_file(filename=file_path) + self._trigger = trigger self._logger.info('Added file "%s"' % (file_path)) @@ -52,11 +53,12 @@ def remove_trigger(self, trigger): return self._tail.remove_file(filename=file_path) + self._trigger = None self._logger.info('Removed file "%s"' % (file_path)) def _handle_line(self, file_path, line): - trigger = self._trigger_ref + trigger = self._trigger payload = { 'file_path': file_path, 'file_name': os.path.basename(file_path), diff --git a/contrib/linux/sensors/file_watch_sensor.yaml b/contrib/linux/sensors/file_watch_sensor.yaml index 0a335b1319..ba622a9eb7 100644 --- a/contrib/linux/sensors/file_watch_sensor.yaml +++ b/contrib/linux/sensors/file_watch_sensor.yaml @@ -1,6 +1,6 @@ --- class_name: "FileWatchSensor" - enabled: false + enabled: true entry_point: "file_watch_sensor.py" description: "Sensor which monitors files for new lines" trigger_types: diff --git a/st2reactor/st2reactor/container/sensor_wrapper.py b/st2reactor/st2reactor/container/sensor_wrapper.py index c9b1868f46..ea3c3bad97 100644 --- a/st2reactor/st2reactor/container/sensor_wrapper.py +++ b/st2reactor/st2reactor/container/sensor_wrapper.py @@ -87,6 +87,7 @@ def dispatch(self, trigger, payload=None, trace_tag=None): """ # empty strings trace_context = TraceContext(trace_tag=trace_tag) if trace_tag else None + self._logger.info('Added trace_context %s to trigger %s.', trace_context, trigger) self.dispatch_with_context(trigger, payload=payload, trace_context=trace_context) def dispatch_with_context(self, trigger, payload=None, trace_context=None): @@ -118,6 +119,7 @@ def dispatch_with_context(self, trigger, payload=None, trace_context=None): 'dispatching a trigger "%s" (%s)' % (trigger, str(payload))) return None + self._logger.info('Dispatching trigger %s with payload %s.', trigger, payload) self._dispatcher.dispatch(trigger, payload=payload, trace_context=trace_context) ################################## From 09ca593a3a29557076ba5e56eb472a61a05fdc0f Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Mon, 5 Jun 2017 13:51:12 -0700 Subject: [PATCH 09/18] Checkpoint #2 --- st2common/st2common/models/db/trigger.py | 2 +- st2reactor/st2reactor/container/utils.py | 31 ++++++++++----- st2reactor/st2reactor/rules/engine.py | 50 ++++++++++++++++++++---- 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/st2common/st2common/models/db/trigger.py b/st2common/st2common/models/db/trigger.py index c75ec48670..aec32c2de3 100644 --- a/st2common/st2common/models/db/trigger.py +++ b/st2common/st2common/models/db/trigger.py @@ -108,7 +108,7 @@ class TriggerInstanceDB(stormbase.StormFoundationDB): payload (dict): payload specific to the occurrence. occurrence_time (datetime): time of occurrence of the trigger. """ - trigger = me.StringField() + trigger = me.DictField() payload = stormbase.EscapedDictField() occurrence_time = me.DateTimeField() status = me.StringField( diff --git a/st2reactor/st2reactor/container/utils.py b/st2reactor/st2reactor/container/utils.py index e893d99f39..66510116ac 100644 --- a/st2reactor/st2reactor/container/utils.py +++ b/st2reactor/st2reactor/container/utils.py @@ -40,6 +40,13 @@ def create_trigger_instance(trigger, payload, occurrence_time, raise_on_no_trigg # TODO: This is nasty, this should take a unique reference and not a dict if isinstance(trigger, six.string_types): trigger_db = TriggerService.get_trigger_db_by_ref(trigger) + if not trigger_db: + LOG.debug('No trigger in db for %s', trigger) + if raise_on_no_trigger: + raise StackStormDBObjectNotFoundError('Trigger not found for %s', trigger) + return None + else: + trigger_ptr = {'ref': trigger_db.get_reference().ref} else: # If id / uid is available we try to look up Trigger by id. This way we can avoid bug in # pymongo / mongoengine related to "parameters" dictionary lookups @@ -49,10 +56,10 @@ def create_trigger_instance(trigger, payload, occurrence_time, raise_on_no_trigg # TODO: Remove parameters dictionary look up when we can confirm each trigger dictionary # passed to this method always contains id or uid if trigger_id: - LOG.debug('Looking up TriggerDB by id: %s', trigger_id) + LOG.info('Looking up TriggerDB by id: %s', trigger_id) trigger_db = TriggerService.get_trigger_db_by_id(id=trigger_id) elif trigger_uid: - LOG.debug('Looking up TriggerDB by uid: %s', trigger_uid) + LOG.info('Looking up TriggerDB by uid: %s', trigger_uid) trigger_db = TriggerService.get_trigger_db_by_uid(uid=trigger_uid) else: # Last resort - look it up by parameters @@ -64,16 +71,20 @@ def create_trigger_instance(trigger, payload, occurrence_time, raise_on_no_trigg trigger_db = TriggerService.get_trigger_db_given_type_and_params(type=trigger_type, parameters=parameters) - if trigger_db is None: - LOG.debug('No trigger in db for %s', trigger) - if raise_on_no_trigger: - raise StackStormDBObjectNotFoundError('Trigger not found for %s', trigger) - return None - - trigger_ref = trigger_db.get_reference().ref + if not trigger_db: + LOG.debug('No trigger in db for %s', trigger) + if raise_on_no_trigger: + raise StackStormDBObjectNotFoundError('Trigger not found for %s', trigger) + return None + else: + trigger_ptr = { + 'ref': trigger_db.get_reference().ref, + 'type': trigger_db.type, + 'parameters': trigger_db.parameters + } trigger_instance = TriggerInstanceDB() - trigger_instance.trigger = trigger_ref + trigger_instance.trigger = trigger_ptr trigger_instance.payload = payload trigger_instance.occurrence_time = occurrence_time trigger_instance.status = TRIGGER_INSTANCE_PENDING diff --git a/st2reactor/st2reactor/rules/engine.py b/st2reactor/st2reactor/rules/engine.py index be86285293..d4185803ae 100644 --- a/st2reactor/st2reactor/rules/engine.py +++ b/st2reactor/st2reactor/rules/engine.py @@ -13,8 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import six + from st2common import log as logging from st2common.persistence.rule import Rule +from st2common.services.triggers import get_trigger_db_given_type_and_params from st2common.services.triggers import get_trigger_db_by_ref from st2reactor.rules.enforcer import RuleEnforcer from st2reactor.rules.matcher import RulesMatcher @@ -27,20 +30,53 @@ def handle_trigger_instance(self, trigger_instance): # Find matching rules for trigger instance. matching_rules = self.get_matching_rules_for_trigger(trigger_instance) - # Create rule enforcers. - enforcers = self.create_rule_enforcers(trigger_instance, matching_rules) + if matching_rules: + # Create rule enforcers. + enforcers = self.create_rule_enforcers(trigger_instance, matching_rules) - # Enforce the rules. - self.enforce_rules(enforcers) + # Enforce the rules. + self.enforce_rules(enforcers) + else: + LOG.info('No matching rules found for trigger instance %s.', trigger_instance) def get_matching_rules_for_trigger(self, trigger_instance): trigger = trigger_instance.trigger - trigger = get_trigger_db_by_ref(trigger_instance.trigger) - rules = Rule.query(trigger=trigger_instance.trigger, enabled=True) + + trigger_type_with_params = False + + if getattr(trigger, 'type', None) and getattr(trigger, 'parameters', None): + trigger_type_with_params = True + + LOG.info('Type(trigger) = %s', type(trigger)) + trigger_ptr = None + if trigger_type_with_params: + trigger_type = trigger.type + trigger_params = trigger.parameters + trigger_db = get_trigger_db_given_type_and_params( + type=trigger_type, + parameters=trigger_params + ) + if trigger_db: + trigger_ptr = { + 'ref': trigger_db.get_reference().ref, + 'type': trigger_type, + 'parameters': trigger_params + } + else: + trigger_db = get_trigger_db_by_ref(trigger_instance.trigger.ref) + if trigger_db: + trigger_ptr = trigger_db.get_reference().ref + + + if not trigger_ptr: + LOG.error('No matching trigger found for trigger instance %s.', trigger_instance) + return None + + rules = Rule.query(trigger=trigger_ptr, enabled=True) LOG.info('Found %d rules defined for trigger %s (type=%s)', len(rules), trigger['name'], trigger['type']) matcher = RulesMatcher(trigger_instance=trigger_instance, - trigger=trigger, rules=rules) + trigger=trigger_db, rules=rules) matching_rules = matcher.get_matching_rules() LOG.info('Matched %s rule(s) for trigger_instance %s (type=%s)', len(matching_rules), From 36df9fdacc35256c8aa566882e92838e8adc5117 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Tue, 6 Jun 2017 15:24:49 -0700 Subject: [PATCH 10/18] Checkpoint #3 --- st2common/st2common/services/executions.py | 3 +- st2common/st2common/services/rules.py | 59 ++++++++++++++++++++++ st2common/st2common/services/trace.py | 3 +- st2reactor/st2reactor/rules/engine.py | 41 +++++++-------- 4 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 st2common/st2common/services/rules.py diff --git a/st2common/st2common/services/executions.py b/st2common/st2common/services/executions.py index 07ad9530b4..7a31f56e1e 100644 --- a/st2common/st2common/services/executions.py +++ b/st2common/st2common/services/executions.py @@ -116,8 +116,9 @@ def create_execution_object(liveaction, publish=True): trigger_instance_id = liveaction.context.get('trigger_instance', {}) trigger_instance_id = trigger_instance_id.get('id', None) trigger_instance = TriggerInstance.get_by_id(trigger_instance_id) + trigger_ref = trigger_instance.trigger.get('ref', None) trigger = reference.get_model_by_resource_ref(db_api=Trigger, - ref=trigger_instance.trigger) + ref=trigger_ref) trigger_type = reference.get_model_by_resource_ref(db_api=TriggerType, ref=trigger.type) trigger_instance = reference.get_model_from_ref( diff --git a/st2common/st2common/services/rules.py b/st2common/st2common/services/rules.py new file mode 100644 index 0000000000..7ec8b5004b --- /dev/null +++ b/st2common/st2common/services/rules.py @@ -0,0 +1,59 @@ +# Licensed to the StackStorm, Inc ('StackStorm') under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import six + +from st2common import log as logging +from st2common.persistence.rule import Rule + + +LOG = logging.getLogger(__name__) + +__all__ = [ + 'get_rules_given_trigger', + 'get_rules_with_trigger_ref' +] + + +def get_rules_given_trigger(trigger): + + if isinstance(trigger, six.string_types): + return get_rules_with_trigger_ref(trigger_ref=trigger) + + if isinstance(trigger, dict): + trigger_ref = trigger.get('ref', None) + if trigger_ref: + return get_rules_with_trigger_ref(trigger_ref=trigger_ref) + else: + raise ValueError('Trigger dict %s is missing ``ref``.' % trigger) + + raise ValueError('Unknown type %s for trigger. Cannot do rule lookups.' % type(trigger)) + + +def get_rules_with_trigger_ref(trigger_ref=None, enabled=True): + """ + Get rules in DB corresponding to given trigger_ref as a string reference. + + :param trigger_ref: Reference to trigger. + :type trigger_ref: ``str`` + + :rtype: ``list`` of ``RuleDB`` + """ + + if not trigger_ref: + return None + + LOG.info('Querying rules with trigger %s', trigger_ref) + return Rule.query(trigger=trigger_ref, enabled=enabled) diff --git a/st2common/st2common/services/trace.py b/st2common/st2common/services/trace.py index 96c2f6feec..1099c96fd0 100644 --- a/st2common/st2common/services/trace.py +++ b/st2common/st2common/services/trace.py @@ -348,7 +348,7 @@ def get_trace_component_for_trigger_instance(trigger_instance_db): trace_component = {} trace_component = { 'id': str(trigger_instance_db.id), - 'ref': trigger_instance_db.trigger + 'ref': trigger_instance_db.trigger.get('ref', '') } caused_by = {} # Special handling for ACTION_SENSOR_TRIGGER and NOTIFY_TRIGGER where we @@ -380,4 +380,5 @@ def _to_trace_component_db(component): ref = component.get('ref', '') if isinstance(component, dict) else '' caused_by = component.get('caused_by', {}) if isinstance(component, dict) else {} + LOG.info('Ref in component %s is %s', component, ref) return TraceComponentDB(object_id=object_id, ref=ref, caused_by=caused_by) diff --git a/st2reactor/st2reactor/rules/engine.py b/st2reactor/st2reactor/rules/engine.py index d4185803ae..7358f89f44 100644 --- a/st2reactor/st2reactor/rules/engine.py +++ b/st2reactor/st2reactor/rules/engine.py @@ -13,10 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import six - from st2common import log as logging -from st2common.persistence.rule import Rule +from st2common.services.rules import get_rules_given_trigger from st2common.services.triggers import get_trigger_db_given_type_and_params from st2common.services.triggers import get_trigger_db_by_ref from st2reactor.rules.enforcer import RuleEnforcer @@ -37,50 +35,45 @@ def handle_trigger_instance(self, trigger_instance): # Enforce the rules. self.enforce_rules(enforcers) else: - LOG.info('No matching rules found for trigger instance %s.', trigger_instance) + LOG.info('No matching rules found for trigger instance %s.', trigger_instance['id']) def get_matching_rules_for_trigger(self, trigger_instance): trigger = trigger_instance.trigger trigger_type_with_params = False - if getattr(trigger, 'type', None) and getattr(trigger, 'parameters', None): + if trigger.get('type', None) and trigger.get('parameters', None): trigger_type_with_params = True - LOG.info('Type(trigger) = %s', type(trigger)) - trigger_ptr = None if trigger_type_with_params: - trigger_type = trigger.type - trigger_params = trigger.parameters + trigger_type = trigger['type'] + trigger_params = trigger['parameters'] trigger_db = get_trigger_db_given_type_and_params( type=trigger_type, parameters=trigger_params ) if trigger_db: - trigger_ptr = { - 'ref': trigger_db.get_reference().ref, - 'type': trigger_type, - 'parameters': trigger_params - } + rules = get_rules_given_trigger(trigger=trigger) else: - trigger_db = get_trigger_db_by_ref(trigger_instance.trigger.ref) + trigger_db = get_trigger_db_by_ref(trigger_instance.trigger.get('ref')) if trigger_db: - trigger_ptr = trigger_db.get_reference().ref - + rules = get_rules_given_trigger(trigger=trigger) - if not trigger_ptr: - LOG.error('No matching trigger found for trigger instance %s.', trigger_instance) + if not trigger_db: + LOG.error('No matching trigger found in db for trigger instance %s.', trigger_instance) return None - rules = Rule.query(trigger=trigger_ptr, enabled=True) - LOG.info('Found %d rules defined for trigger %s (type=%s)', len(rules), trigger['name'], - trigger['type']) + LOG.info('Found %d rules defined for trigger %s', len(rules), trigger['ref']) + + if len(rules) < 1: + return rules + matcher = RulesMatcher(trigger_instance=trigger_instance, trigger=trigger_db, rules=rules) matching_rules = matcher.get_matching_rules() - LOG.info('Matched %s rule(s) for trigger_instance %s (type=%s)', len(matching_rules), - trigger['name'], trigger['type']) + LOG.info('Matched %s rule(s) for trigger_instance %s (trigger=%s)', len(matching_rules), + trigger_instance['id'], trigger_db.ref) return matching_rules def create_rule_enforcers(self, trigger_instance, matching_rules): From c997969d57d9a093c7865f30231fc2a231029924 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Wed, 7 Jun 2017 08:42:59 -0700 Subject: [PATCH 11/18] Revert back trigger to StringField in trigger instance --- st2common/st2common/models/db/trigger.py | 2 +- st2common/st2common/services/executions.py | 2 +- st2common/st2common/services/trace.py | 2 +- st2reactor/st2reactor/container/utils.py | 27 +++++++--------------- st2reactor/st2reactor/rules/engine.py | 24 ++++--------------- 5 files changed, 16 insertions(+), 41 deletions(-) diff --git a/st2common/st2common/models/db/trigger.py b/st2common/st2common/models/db/trigger.py index aec32c2de3..c75ec48670 100644 --- a/st2common/st2common/models/db/trigger.py +++ b/st2common/st2common/models/db/trigger.py @@ -108,7 +108,7 @@ class TriggerInstanceDB(stormbase.StormFoundationDB): payload (dict): payload specific to the occurrence. occurrence_time (datetime): time of occurrence of the trigger. """ - trigger = me.DictField() + trigger = me.StringField() payload = stormbase.EscapedDictField() occurrence_time = me.DateTimeField() status = me.StringField( diff --git a/st2common/st2common/services/executions.py b/st2common/st2common/services/executions.py index 7a31f56e1e..901dab8d14 100644 --- a/st2common/st2common/services/executions.py +++ b/st2common/st2common/services/executions.py @@ -116,7 +116,7 @@ def create_execution_object(liveaction, publish=True): trigger_instance_id = liveaction.context.get('trigger_instance', {}) trigger_instance_id = trigger_instance_id.get('id', None) trigger_instance = TriggerInstance.get_by_id(trigger_instance_id) - trigger_ref = trigger_instance.trigger.get('ref', None) + trigger_ref = trigger_instance.trigger trigger = reference.get_model_by_resource_ref(db_api=Trigger, ref=trigger_ref) trigger_type = reference.get_model_by_resource_ref(db_api=TriggerType, diff --git a/st2common/st2common/services/trace.py b/st2common/st2common/services/trace.py index 1099c96fd0..b6e94be72e 100644 --- a/st2common/st2common/services/trace.py +++ b/st2common/st2common/services/trace.py @@ -348,7 +348,7 @@ def get_trace_component_for_trigger_instance(trigger_instance_db): trace_component = {} trace_component = { 'id': str(trigger_instance_db.id), - 'ref': trigger_instance_db.trigger.get('ref', '') + 'ref': trigger_instance_db.trigger } caused_by = {} # Special handling for ACTION_SENSOR_TRIGGER and NOTIFY_TRIGGER where we diff --git a/st2reactor/st2reactor/container/utils.py b/st2reactor/st2reactor/container/utils.py index 66510116ac..84beb4b333 100644 --- a/st2reactor/st2reactor/container/utils.py +++ b/st2reactor/st2reactor/container/utils.py @@ -40,13 +40,6 @@ def create_trigger_instance(trigger, payload, occurrence_time, raise_on_no_trigg # TODO: This is nasty, this should take a unique reference and not a dict if isinstance(trigger, six.string_types): trigger_db = TriggerService.get_trigger_db_by_ref(trigger) - if not trigger_db: - LOG.debug('No trigger in db for %s', trigger) - if raise_on_no_trigger: - raise StackStormDBObjectNotFoundError('Trigger not found for %s', trigger) - return None - else: - trigger_ptr = {'ref': trigger_db.get_reference().ref} else: # If id / uid is available we try to look up Trigger by id. This way we can avoid bug in # pymongo / mongoengine related to "parameters" dictionary lookups @@ -71,20 +64,16 @@ def create_trigger_instance(trigger, payload, occurrence_time, raise_on_no_trigg trigger_db = TriggerService.get_trigger_db_given_type_and_params(type=trigger_type, parameters=parameters) - if not trigger_db: - LOG.debug('No trigger in db for %s', trigger) - if raise_on_no_trigger: - raise StackStormDBObjectNotFoundError('Trigger not found for %s', trigger) - return None - else: - trigger_ptr = { - 'ref': trigger_db.get_reference().ref, - 'type': trigger_db.type, - 'parameters': trigger_db.parameters - } + if not trigger_db: + LOG.debug('No trigger in db for %s', trigger) + if raise_on_no_trigger: + raise StackStormDBObjectNotFoundError('Trigger not found for %s', trigger) + return None + + trigger_ref = trigger_db.get_reference().ref trigger_instance = TriggerInstanceDB() - trigger_instance.trigger = trigger_ptr + trigger_instance.trigger = trigger_ref trigger_instance.payload = payload trigger_instance.occurrence_time = occurrence_time trigger_instance.status = TRIGGER_INSTANCE_PENDING diff --git a/st2reactor/st2reactor/rules/engine.py b/st2reactor/st2reactor/rules/engine.py index 7358f89f44..5a73b7fd6f 100644 --- a/st2reactor/st2reactor/rules/engine.py +++ b/st2reactor/st2reactor/rules/engine.py @@ -40,30 +40,16 @@ def handle_trigger_instance(self, trigger_instance): def get_matching_rules_for_trigger(self, trigger_instance): trigger = trigger_instance.trigger - trigger_type_with_params = False - - if trigger.get('type', None) and trigger.get('parameters', None): - trigger_type_with_params = True - - if trigger_type_with_params: - trigger_type = trigger['type'] - trigger_params = trigger['parameters'] - trigger_db = get_trigger_db_given_type_and_params( - type=trigger_type, - parameters=trigger_params - ) - if trigger_db: - rules = get_rules_given_trigger(trigger=trigger) - else: - trigger_db = get_trigger_db_by_ref(trigger_instance.trigger.get('ref')) - if trigger_db: - rules = get_rules_given_trigger(trigger=trigger) + trigger_db = get_trigger_db_by_ref(trigger_instance.trigger) if not trigger_db: LOG.error('No matching trigger found in db for trigger instance %s.', trigger_instance) return None - LOG.info('Found %d rules defined for trigger %s', len(rules), trigger['ref']) + rules = get_rules_given_trigger(trigger=trigger) + + LOG.info('Found %d rules defined for trigger %s', len(rules), + trigger_db.get_reference().ref) if len(rules) < 1: return rules From f2e2b1f5da2159db216072010279394f5e3d5503 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Wed, 7 Jun 2017 08:43:23 -0700 Subject: [PATCH 12/18] Log debug rule query --- st2common/st2common/services/rules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/st2common/st2common/services/rules.py b/st2common/st2common/services/rules.py index 7ec8b5004b..fdc9f7aafb 100644 --- a/st2common/st2common/services/rules.py +++ b/st2common/st2common/services/rules.py @@ -55,5 +55,5 @@ def get_rules_with_trigger_ref(trigger_ref=None, enabled=True): if not trigger_ref: return None - LOG.info('Querying rules with trigger %s', trigger_ref) + LOG.debug('Querying rules with trigger %s', trigger_ref) return Rule.query(trigger=trigger_ref, enabled=enabled) From a0c95ebd545b2280fff517dec1583628a9e4670a Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Wed, 7 Jun 2017 08:51:31 -0700 Subject: [PATCH 13/18] Cleanup log changes --- contrib/examples/rules/sample_rule_file_watch.yaml | 2 +- contrib/linux/sensors/file_watch_sensor.py | 3 ++- st2reactor/st2reactor/container/sensor_wrapper.py | 4 ++-- st2reactor/st2reactor/container/utils.py | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/contrib/examples/rules/sample_rule_file_watch.yaml b/contrib/examples/rules/sample_rule_file_watch.yaml index 784c543c38..a88c224de1 100644 --- a/contrib/examples/rules/sample_rule_file_watch.yaml +++ b/contrib/examples/rules/sample_rule_file_watch.yaml @@ -2,7 +2,7 @@ name: sample_rule_file_watch pack: "examples" description: Sample rule custom trigger type - add a file to be watched by file_watch_sensor in linux pack. -enabled: false +enabled: true trigger: parameters: diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index eea16f7003..7c784c324a 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -64,5 +64,6 @@ def _handle_line(self, file_path, line): 'file_name': os.path.basename(file_path), 'line': line } - self._logger.info('Sending payload %s for trigger %s to sensor_service.', payload, trigger) + self._logger.debug('Sending payload %s for trigger %s to sensor_service.', + payload, trigger) self.sensor_service.dispatch(trigger=trigger, payload=payload) diff --git a/st2reactor/st2reactor/container/sensor_wrapper.py b/st2reactor/st2reactor/container/sensor_wrapper.py index ea3c3bad97..0fae409f94 100644 --- a/st2reactor/st2reactor/container/sensor_wrapper.py +++ b/st2reactor/st2reactor/container/sensor_wrapper.py @@ -87,7 +87,7 @@ def dispatch(self, trigger, payload=None, trace_tag=None): """ # empty strings trace_context = TraceContext(trace_tag=trace_tag) if trace_tag else None - self._logger.info('Added trace_context %s to trigger %s.', trace_context, trigger) + self._logger.debug('Added trace_context %s to trigger %s.', trace_context, trigger) self.dispatch_with_context(trigger, payload=payload, trace_context=trace_context) def dispatch_with_context(self, trigger, payload=None, trace_context=None): @@ -119,7 +119,7 @@ def dispatch_with_context(self, trigger, payload=None, trace_context=None): 'dispatching a trigger "%s" (%s)' % (trigger, str(payload))) return None - self._logger.info('Dispatching trigger %s with payload %s.', trigger, payload) + self._logger.debug('Dispatching trigger %s with payload %s.', trigger, payload) self._dispatcher.dispatch(trigger, payload=payload, trace_context=trace_context) ################################## diff --git a/st2reactor/st2reactor/container/utils.py b/st2reactor/st2reactor/container/utils.py index 84beb4b333..50cad5d0ee 100644 --- a/st2reactor/st2reactor/container/utils.py +++ b/st2reactor/st2reactor/container/utils.py @@ -49,10 +49,10 @@ def create_trigger_instance(trigger, payload, occurrence_time, raise_on_no_trigg # TODO: Remove parameters dictionary look up when we can confirm each trigger dictionary # passed to this method always contains id or uid if trigger_id: - LOG.info('Looking up TriggerDB by id: %s', trigger_id) + LOG.debug('Looking up TriggerDB by id: %s', trigger_id) trigger_db = TriggerService.get_trigger_db_by_id(id=trigger_id) elif trigger_uid: - LOG.info('Looking up TriggerDB by uid: %s', trigger_uid) + LOG.debug('Looking up TriggerDB by uid: %s', trigger_uid) trigger_db = TriggerService.get_trigger_db_by_uid(uid=trigger_uid) else: # Last resort - look it up by parameters From ad31b4ee9927c7330071cae1d382664b81eb520f Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Wed, 7 Jun 2017 08:53:47 -0700 Subject: [PATCH 14/18] Revert unneeded changes --- st2common/st2common/services/executions.py | 3 +-- st2common/st2common/services/trace.py | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/st2common/st2common/services/executions.py b/st2common/st2common/services/executions.py index 901dab8d14..07ad9530b4 100644 --- a/st2common/st2common/services/executions.py +++ b/st2common/st2common/services/executions.py @@ -116,9 +116,8 @@ def create_execution_object(liveaction, publish=True): trigger_instance_id = liveaction.context.get('trigger_instance', {}) trigger_instance_id = trigger_instance_id.get('id', None) trigger_instance = TriggerInstance.get_by_id(trigger_instance_id) - trigger_ref = trigger_instance.trigger trigger = reference.get_model_by_resource_ref(db_api=Trigger, - ref=trigger_ref) + ref=trigger_instance.trigger) trigger_type = reference.get_model_by_resource_ref(db_api=TriggerType, ref=trigger.type) trigger_instance = reference.get_model_from_ref( diff --git a/st2common/st2common/services/trace.py b/st2common/st2common/services/trace.py index b6e94be72e..96c2f6feec 100644 --- a/st2common/st2common/services/trace.py +++ b/st2common/st2common/services/trace.py @@ -380,5 +380,4 @@ def _to_trace_component_db(component): ref = component.get('ref', '') if isinstance(component, dict) else '' caused_by = component.get('caused_by', {}) if isinstance(component, dict) else {} - LOG.info('Ref in component %s is %s', component, ref) return TraceComponentDB(object_id=object_id, ref=ref, caused_by=caused_by) From 01283770f925e4cf0d1cd66236ac69413017d12d Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Wed, 7 Jun 2017 11:58:20 -0700 Subject: [PATCH 15/18] Trigger and TriggerType models should have ref field --- contrib/linux/sensors/file_watch_sensor.py | 7 +++++-- st2common/st2common/models/db/trigger.py | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/contrib/linux/sensors/file_watch_sensor.py b/contrib/linux/sensors/file_watch_sensor.py index 7c784c324a..d0a74c71a9 100644 --- a/contrib/linux/sensors/file_watch_sensor.py +++ b/contrib/linux/sensors/file_watch_sensor.py @@ -37,9 +37,12 @@ def add_trigger(self, trigger): self._logger.error('Received trigger type without "file_path" field.') return - self._tail.add_file(filename=file_path) - self._trigger = trigger + self._trigger = trigger.get('ref', None) + + if not self._trigger: + raise Exception('Trigger %s did not contain a ref.' % trigger) + self._tail.add_file(filename=file_path) self._logger.info('Added file "%s"' % (file_path)) def update_trigger(self, trigger): diff --git a/st2common/st2common/models/db/trigger.py b/st2common/st2common/models/db/trigger.py index c75ec48670..3497a77d9a 100644 --- a/st2common/st2common/models/db/trigger.py +++ b/st2common/st2common/models/db/trigger.py @@ -46,8 +46,9 @@ class TriggerTypeDB(stormbase.StormBaseDB, RESOURCE_TYPE = ResourceType.TRIGGER_TYPE UID_FIELDS = ['pack', 'name'] - name = me.StringField(required=True) + name = me.StringField(required=False) pack = me.StringField(required=True, unique_with='name') + ref = me.StringField(required=True) payload_schema = me.DictField() parameters_schema = me.DictField(default={}) @@ -75,6 +76,7 @@ class TriggerDB(stormbase.StormBaseDB, stormbase.ContentPackResourceMixin, RESOURCE_TYPE = ResourceType.TRIGGER UID_FIELDS = ['pack', 'name'] + ref = me.StringField(required=False) name = me.StringField(required=True) pack = me.StringField(required=True, unique_with='name') type = me.StringField() From b738eef6c948288914d17de3c2870a6c54e9cc3f Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Wed, 7 Jun 2017 12:01:45 -0700 Subject: [PATCH 16/18] Name is required, ref is optional --- st2common/st2common/models/db/trigger.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/st2common/st2common/models/db/trigger.py b/st2common/st2common/models/db/trigger.py index 3497a77d9a..1f09f76fce 100644 --- a/st2common/st2common/models/db/trigger.py +++ b/st2common/st2common/models/db/trigger.py @@ -46,9 +46,9 @@ class TriggerTypeDB(stormbase.StormBaseDB, RESOURCE_TYPE = ResourceType.TRIGGER_TYPE UID_FIELDS = ['pack', 'name'] - name = me.StringField(required=False) + ref = me.StringField(required=False) + name = me.StringField(required=True) pack = me.StringField(required=True, unique_with='name') - ref = me.StringField(required=True) payload_schema = me.DictField() parameters_schema = me.DictField(default={}) From 0096682fa1a1fe3fea47b3b9a3f21b2e69537332 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Thu, 8 Jun 2017 08:34:15 -0700 Subject: [PATCH 17/18] Enable trigger parameters validation check --- conf/st2.dev.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/st2.dev.conf b/conf/st2.dev.conf index e22404770e..d67e0ebbb4 100644 --- a/conf/st2.dev.conf +++ b/conf/st2.dev.conf @@ -58,6 +58,7 @@ api_url = http://127.0.0.1:9101/ [system] debug = True base_path = /opt/stackstorm +validate_trigger_parameters = True [garbagecollector] logging = st2reactor/conf/logging.garbagecollector.conf From c555786c4b8c779e1ed30581254aa1773b0e7e61 Mon Sep 17 00:00:00 2001 From: Lakshmi Kannan Date: Thu, 8 Jun 2017 11:30:14 -0700 Subject: [PATCH 18/18] Lint and unit tests fixes --- st2api/tests/unit/controllers/v1/test_webhooks.py | 2 ++ .../tests/unit/controllers/v1/test_webhooks_rbac.py | 12 ++++++------ st2reactor/st2reactor/rules/engine.py | 1 - 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/st2api/tests/unit/controllers/v1/test_webhooks.py b/st2api/tests/unit/controllers/v1/test_webhooks.py index fb3d9f46d3..76d40cbda3 100644 --- a/st2api/tests/unit/controllers/v1/test_webhooks.py +++ b/st2api/tests/unit/controllers/v1/test_webhooks.py @@ -25,6 +25,7 @@ from st2api.controllers.v1.webhooks import WebhooksController, HooksHolder from st2common.constants.triggers import WEBHOOK_TRIGGER_TYPES +from st2common.models.api.trigger import TriggerAPI from st2common.models.db.trigger import TriggerDB from st2common.transport.reactor import TriggerInstancePublisher @@ -44,6 +45,7 @@ DUMMY_TRIGGER = TriggerDB(name='pr-merged', pack='git') DUMMY_TRIGGER.type = WEBHOOK_TRIGGER_TYPES.keys()[0] +DUMMY_TRIGGER_API = TriggerAPI.from_model(DUMMY_TRIGGER) class TestWebhooksController(FunctionalTest): diff --git a/st2api/tests/unit/controllers/v1/test_webhooks_rbac.py b/st2api/tests/unit/controllers/v1/test_webhooks_rbac.py index 1051e7ced6..51b491c4f7 100644 --- a/st2api/tests/unit/controllers/v1/test_webhooks_rbac.py +++ b/st2api/tests/unit/controllers/v1/test_webhooks_rbac.py @@ -36,7 +36,7 @@ from st2tests.fixturesloader import FixturesLoader from tests.base import APIControllerWithRBACTestCase -from tests.unit.controllers.v1.test_webhooks import DUMMY_TRIGGER +from tests.unit.controllers.v1.test_webhooks import DUMMY_TRIGGER, DUMMY_TRIGGER_API http_client = six.moves.http_client @@ -107,7 +107,7 @@ def test_get_all_no_permissions(self): self.assertEqual(resp.json['faultstring'], expected_msg) @mock.patch.object(HooksHolder, 'get_triggers_for_hook', mock.MagicMock( - return_value=[vars(DUMMY_TRIGGER)])) + return_value=[DUMMY_TRIGGER_API])) def test_get_one_no_permissions(self): user_db = self.users['no_permissions'] self.use_user(user_db) @@ -124,9 +124,9 @@ def test_get_one_no_permissions(self): self.assertEqual(resp.json['faultstring'], expected_msg) @mock.patch.object(HooksHolder, 'get_all', mock.MagicMock( - return_value=[vars(DUMMY_TRIGGER)])) + return_value=[DUMMY_TRIGGER_API])) @mock.patch.object(HooksHolder, 'get_triggers_for_hook', mock.MagicMock( - return_value=[vars(DUMMY_TRIGGER)])) + return_value=[DUMMY_TRIGGER_API])) def test_get_all_permission_success_get_one_no_permission_failure(self): user_db = self.users['webhook_list'] self.use_user(user_db) @@ -148,9 +148,9 @@ def test_get_all_permission_success_get_one_no_permission_failure(self): self.assertEqual(resp.json['faultstring'], expected_msg) @mock.patch.object(HooksHolder, 'get_all', mock.MagicMock( - return_value=[vars(DUMMY_TRIGGER)])) + return_value=[DUMMY_TRIGGER_API])) @mock.patch.object(HooksHolder, 'get_triggers_for_hook', mock.MagicMock( - return_value=[vars(DUMMY_TRIGGER)])) + return_value=[DUMMY_TRIGGER_API])) def test_get_one_permission_success_get_all_no_permission_failure(self): user_db = self.users['webhook_view'] self.use_user(user_db) diff --git a/st2reactor/st2reactor/rules/engine.py b/st2reactor/st2reactor/rules/engine.py index 5a73b7fd6f..6100720e8a 100644 --- a/st2reactor/st2reactor/rules/engine.py +++ b/st2reactor/st2reactor/rules/engine.py @@ -15,7 +15,6 @@ from st2common import log as logging from st2common.services.rules import get_rules_given_trigger -from st2common.services.triggers import get_trigger_db_given_type_and_params from st2common.services.triggers import get_trigger_db_by_ref from st2reactor.rules.enforcer import RuleEnforcer from st2reactor.rules.matcher import RulesMatcher