-
-
Notifications
You must be signed in to change notification settings - Fork 782
Just declare one trigger type for file watch sensor #3398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8a1525d
1a55bcb
d377362
5a22501
7de681c
2c01e66
506cd55
a1b9f79
24f7a63
09ca593
36df9fd
c997969
f2e2b1f
a0c95eb
ad31b4e
0128377
b738eef
0096682
c555786
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| ## 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: /tmp/st2_test | ||
| type: linux.file_watch.line | ||
|
|
||
| criteria: {} | ||
|
|
||
| action: | ||
| parameters: | ||
| cmd: echo "{{trigger}}" | ||
| ref: core.local | ||
|
|
||
| ``` | ||
|
|
||
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ class TriggerTypeDB(stormbase.StormBaseDB, | |
| RESOURCE_TYPE = ResourceType.TRIGGER_TYPE | ||
| UID_FIELDS = ['pack', 'name'] | ||
|
|
||
| ref = me.StringField(required=False) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made it required=False so people upgrading don't have issues. At some point, we should make it required.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also see why this "ref" change is required. #3454 |
||
| name = me.StringField(required=True) | ||
| pack = me.StringField(required=True, unique_with='name') | ||
| payload_schema = me.DictField() | ||
|
|
@@ -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() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parenthesis around |
||
|
|
||
| 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.debug('Querying rules with trigger %s', trigger_ref) | ||
| return Rule.query(trigger=trigger_ref, enabled=enabled) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kami @dzimine This turned out to the root cause of all problems. Basically, I was using the trigger type as the "ref" which is incorrect. I should either be using "type"and "parameters" or the actual ref for the trigger object we create for this instance of type and parameters. For example,
Trigger type:
Trigger:
And if you look at the rule, it's tied to the trigger ref we created and it has no knowledge of type and parameters.
So for a single trigger type, for each instance of parameters, we end up creating a rule with a different "trigger". The trigger type information is only used to verify that the parameters someone supplies matches the schema. I have to verify if we throw an error if the parameters don't match. I'll validate that part.
In essence, the sensor should either use the actual "ref" for the trigger or a dict containing "type" and "parameters" so we can do a lookup and get the actual trigger ref ourselves. Everything from trigger instance to rules engine only understands trigger ref. They don't understand type and parameters.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I verified by swapping "file_path" to "file_route" and the rule registered successfully without throwing errors :/. See trigger below
This is a bug and it should be fixed. #3453