其他分享
首页 > 其他分享> > 3 stackstorm rule

3 stackstorm rule

作者:互联网

stackstorm rule
Rule是映射Trigger到Action(或者Workflow),即当事件触发后,通过Rule定义的标准(Criteria)进行匹配,当匹配成功将执行Action(或者Workflow)。
https://docs.stackstorm.com/rules.html

Rule的定义格式:

---
    name: "rule_name"                      # required
    pack: "examples"                       # optional
    description: "Rule description."       # optional
    enabled: true                          # required
 
    trigger:                               # required
        type: "trigger_type_ref"
 
    criteria:                              # optional
        trigger.payload_parameter_name1:
            type: "regex"
            pattern : "^value$"
        trigger.payload_parameter_name2:
            type: "iequals"
            pattern : "watchevent"
 
    action:                                # required
        ref: "action_ref"
        parameters:                        # optional
            foo: "bar"
            baz: "{{ trigger.payload_parameter_1 }}"
 

name: Rule的名称
pack: Rule归属的Pack
enabled: Rule的启用开关
trigger: 配置Trigger的类型和参数
criteria:Rule的匹配规则,当 Trigger的参数满足的条件触发Action
action: Rule匹配需要执行的Action配置

rule的主要格式如下:
name: 规则名称
pack: 规则的属于方,如果没有指定pack则为default
description: 规则的描述
enabled: 规则是否开启(true或者false)
trigger:emitted from sensors to monitor, and optionally parameters associated with that trigger.
criteria:可选择的集合,包含
    一个trigger的payload的属性
    criteria比较的类型
    pattern: to match against.
action:当一个规则被满足时,执行的动作,包含:
    ref:动作或工作流的名称
    parameters: 执行动作的参数(可选)


我们可以通过命令行创建Rule
$ st2 rule create /usr/share/doc/st2/examples/rules/sample_rule_with_webhook.yaml
查询Rule
$ st2 rule list
$ st2 rule get examples.sample_rule_with_webhook

Criteria
Rule的Criteria 提供了一系列方法,可以设置各种匹配规则:
equals Values are equal (for values of arbitrary type).
nequals Values are not equal (for values of arbitrary type).
lessthan Trigger value is less than the provided value.
greaterthan Trigger value is greater than the provided value.
matchwildcard Trigger value matches the provided wildcard-like string. This operator provides support for Unix shell-style wildcards which means you can use characters such as * and ?. This operator is preferred over regex for simple string matches.
regex Trigger value matches the provided regular expression pattern. This operator behaves like re.search('pattern', trigger_value).
iregex Trigger value matches the provided regular expression pattern case insensitively. This operator behaves like re.search('pattern', trigger_value, re.IGNORECASE).
matchregex Trigger value matches the provided regular expression pattern. This operator is deprecated in favor of regex and iregex
iequals String trigger value equals the provided value case insensitively.
contains Trigger value contains the provided value. Keep in mind that the trigger value can be either a string or an array (list).
ncontains Trigger value does not contain the provided value. Keep in mind that the trigger value can be either a string or an array (list).
icontains String trigger value contains the provided value case insensitively.
incontains String trigger value does not contain the provided string value case insensitively.
startswith Beginning of the string trigger value matches the provided string value.
istartswith Beginning of the string trigger value matches the provided string value case insensitively.
endswith End of the string trigger value matches the provided string value.
iendswith End of the string trigger value matches the provided string value case insensitively.
timediff_lt Time difference between trigger value and current time is less than the provided value.
timediff_gt Time difference between trigger value and current time is greater than the provided value.
exists Key exists in payload.
nexists Key doesn't exist in payload.
inside Trigger payload is inside provided value. (e.g. testing if "trigger.payload in provided_value"). Reverse of contains. (where contains would test for "trigger.payload contains provided_value").
ninside Trigger payload is not inside provided value. (e.g. testing if "trigger.payload not in provided_value"). Reverse of ncontains (where contains would test for "trigger.payload does not contain provided_value").
search Search an array (list) in the trigger payload that matches child criteria. See the Advanced Comparison section for more information and examples.



测试
StackStorm提供一个工具st2-rule-tester可以用于快速测试Rule。
首先创建Rule:
st2-rule-tester
--------------------------------------------------------------------------------
name: "relayed_matched_irc_message"
pack: "irc"
description: "Relay IRC message to Slack if the message contains word StackStorm"
enabled: true
trigger:
type: "irc.pubmsg"
parameters: {}
criteria:
trigger.message:
type: "icontains"
pattern: "StackStorm"
action:
ref: "slack.post_message"
parameters:
message: "{{ trigger.source.nick }} on {{ trigger.channel }}: {{ trigger.message }}"
channel: "#irc-relay"
这个Rule是匹配Trigger(irc.pubmsg)的参数message是否含有字符串StackStorm(不区分大小写),匹配后执行Action(slack.post_message)。
然后创建用于测试的Trigger Instance:
trigger_instance_1.yaml: message包含字符串StackStorm,匹配Rule
trigger: "irc.pubmsg"
payload:
  source:
      nick: "Kami_"
      host: "gateway/web/irccloud.com/x-uvv"
  channel: "#stackstorm"
  timestamp: 1419166748,
  message: "stackstorm is cool!"

trigger_instance_2.yaml:不匹配Rule
trigger: "irc.pubmsg"
payload:
  source:
      nick: "Kami_"
      host: "gateway/web/irccloud.com/x-uvv"
  channel: "#stackstorm"
  timestamp: 1419166748,
  message: "blah blah"

执行测试
Trigger Instace 1匹配Rule并执行Action
$ st2-rule-tester --rule=./my_rule.yaml --trigger-instance=./trigger_instance_1.yaml --config-file=/etc/st2/st2.conf
2018-05-17 06:44:17,972 INFO [-] Connecting to database "st2" @ "mongo:27017" as user "None".
2018-05-17 06:44:18,144 INFO [-] Validating rule irc.relayed_matched_irc_message for pubmsg.
2018-05-17 06:44:18,169 INFO [-] 1 rule(s) found to enforce for pubmsg.
2018-05-17 06:44:18,170 INFO [-] Failed to retrieve config for pack <Mock name='mock.pack' id='140111251335056'> and user stanley: 'Mock' object is not iterable
2018-05-17 06:44:18,173 INFO [-] Action parameters resolved to:
2018-05-17 06:44:18,173 INFO [-]    message: Kami_ on #stackstorm: stackstorm is cool!
2018-05-17 06:44:18,174 INFO [-]    channel: #irc-relay
2018-05-17 06:44:18,174 INFO [-] === RULE MATCHES ===
Trigger Instace 2不匹配Rule
$ st2-rule-tester --rule=./my_rule.yaml --trigger-instance=./trigger_instance_2.yaml --config-file=/etc/st2/st2.conf
2018-05-17 06:47:27,426 INFO [-] Connecting to database "st2" @ "mongo:27017" as user "None".
2018-05-17 06:47:27,605 INFO [-] Validating rule irc.relayed_matched_irc_message for pubmsg.
2018-05-17 06:47:27,627 INFO [-] Validation for rule irc.relayed_matched_irc_message failed on criteria -
key: trigger.message
pattern: StackStorm
type: icontains
payload: blah blah
2018-05-17 06:47:27,627 INFO [-] 0 rule(s) found to enforce for pubmsg.
2018-05-17 06:47:27,628 INFO [-] === RULE DOES NOT MATCH ===
另外也可以根据实际的Trigger Instance去测试:
$ st2-rule-tester --rule-ref=my_pack.fire_on_execution --trigger-instance-id=566b4be632ed352a09cd347d --config-file=/etc/st2/st2.conf

 

标签:provided,rule,Rule,Trigger,stackstorm,trigger,value
来源: https://www.cnblogs.com/skyzy/p/16549249.html