Skip to content
Permalink
Browse files

Remove 'from' from republish actions params

  • Loading branch information...
terry-xiaoyu committed Apr 25, 2019
1 parent dc8669d commit 8721ebe583d5426f239b5b1f044fe381bf4ea0b7
Showing with 35 additions and 41 deletions.
  1. +4 −4 docs/api_examples.md
  2. +0 −1 docs/cli_examples.md
  3. +29 −34 src/emqx_rule_actions.erl
  4. +1 −1 src/emqx_rule_validator.erl
  5. +1 −1 test/emqx_rule_engine_SUITE.erl
@@ -54,15 +54,15 @@ $ curl -XDELETE -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/rules/
```shell
$ curl -v --basic -u $APPSECRET -k http://localhost:8080/api/v3/actions
{"code":0,"data":[{"app":"emqx_rule_engine","description":"Debug Action","name":"built_in:inspect_action","params":{"$resource":"built_in"}},{"app":"emqx_rule_engine","description":"Republish a MQTT message","name":"built_in:republish_action","params":{"$resource":"built_in","from":"topic","to":"topic"}}]}
{"code":0,"data":[{"app":"emqx_rule_engine","description":"Debug Action","name":"built_in:inspect_action","params":{"$resource":"built_in"}},{"app":"emqx_rule_engine","description":"Republish a MQTT message","name":"built_in:republish_action","params":{"$resource":"built_in","target_topic":"topic"}}]}
```

### list all actions of a resource type

```shell
$ curl -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/resource_types/built_in/actions'
{"code":0,"data":[{"app":"emqx_rule_engine","description":"Debug Action","name":"built_in:inspect_action","params":{},"type":"built_in"},{"app":"emqx_rule_engine","description":"Republish a MQTT message","name":"built_in:republish_action","params":{"from":"topic","to":"topic"},"type":"built_in"}]}
{"code":0,"data":[{"app":"emqx_rule_engine","description":"Debug Action","name":"built_in:inspect_action","params":{},"type":"built_in"},{"app":"emqx_rule_engine","description":"Republish a MQTT message","name":"built_in:republish_action","params":{"target_topic":"topic"},"type":"built_in"}]}
```


@@ -72,11 +72,11 @@ $ curl -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/resource_types/
```shell
$ curl -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/actions?for=message.publish'
{"app":"emqx_rule_engine","description":"Republish a MQTT message","name":"built_in:republish_action","params":{"from":"topic","to":"topic"},"type":"built_in"}]}
{"app":"emqx_rule_engine","description":"Republish a MQTT message","name":"built_in:republish_action","params":{"target_topic":"topic"},"type":"built_in"}]}
$ curl -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/actions?for=$messages'
{"code":0,"data":[{"app":"emqx_rule_engine","description":"Debug Action","for":"$any","name":"built_in:inspect_action","params":{},"type":"built_in"},{"app":"emqx_rule_engine","description":"Republish a MQTT message","for":"message.publish","name":"built_in:republish_action","params":{"from":"topic","to":"topic"},"type":"built_in"},{"app":"emqx_web_hook","description":"Forward Messages to Web Server","for":"message.publish","name":"web_hook:publish_action","params":{"$resource":"web_hook"},"type":"web_hook"}]}
{"code":0,"data":[{"app":"emqx_rule_engine","description":"Debug Action","for":"$any","name":"built_in:inspect_action","params":{},"type":"built_in"},{"app":"emqx_rule_engine","description":"Republish a MQTT message","for":"message.publish","name":"built_in:republish_action","params":{"target_topic":"topic"},"type":"built_in"},{"app":"emqx_web_hook","description":"Forward Messages to Web Server","for":"message.publish","name":"web_hook:publish_action","params":{"$resource":"web_hook"},"type":"web_hook"}]}
$ curl -v --basic -u $APPSECRET -k 'http://localhost:8080/api/v3/actions?for=$events'
@@ -44,7 +44,6 @@ $ ./bin/emqx_ctl rule-actions list
action(name=built_in:inspect_action, app=emqx_rule_engine, params=#{'$resource' => built_in}, description=Debug Action)
action(name=web_hook:publish_action, app=emqx_web_hook, params=#{'$resource' => web_hook,url => string}, description=Forward a MQTT message)
action(name=built_in:republish_action, app=emqx_rule_engine, params=#{'$resource' => built_in,from => topic,to => topic}, description=Republish a MQTT message)
```
### show
@@ -19,38 +19,38 @@
-include_lib("emqx/include/logger.hrl").

-define(REPUBLISH_PARAMS_SPEC, #{
from => #{type => string,
format => topic,
required => true,
title => <<"From Topic">>,
description => <<"Wait messages from which topic">>},
to => #{type => string,
format => topic,
required => true,
title => <<"To Topic">>,
description => <<"Repubilsh to which topic">>}
target_topic => #{
type => string,
format => topic,
required => true,
title => <<"To Which Topic">>,
description => <<"Repubilsh the message to which topic">>
}
}).

-resource_type(#{name => built_in,
create => on_resource_create,
params => #{},
description => "Debug resource type"
title => <<"Built-In Resource Type">>,
description => "The built in resource type for debug purpose"
}).

-rule_action(#{name => inspect_action,
for => '$any',
type => built_in,
func => inspect_action,
params => #{},
description => "Debug Action"
title => <<"Inspect Action">>,
description => <<"Inspect the details of action params for debug purpose">>
}).

-rule_action(#{name => republish_action,
for => 'message.publish',
type => built_in,
func => republish_action,
params => ?REPUBLISH_PARAMS_SPEC,
description => "Republish a MQTT message"
title => <<"Republish Action">>,
description => "Republish a MQTT message to a another topic"
}).

-type(action_fun() :: fun((SelectedData::map(), Envs::map()) -> Result::any())).
@@ -81,29 +81,24 @@ inspect_action(Params) ->
end.

%% A Demo Action.
-spec(republish_action(#{binary() := emqx_topic:topic(),
binary() := emqx_topic:topic()})
-spec(republish_action(#{binary() := emqx_topic:topic()})
-> action_fun()).
republish_action(#{<<"from">> := SrcTopic, <<"to">> := TargetTopic}) ->
fun(Selected, #{topic := OriginTopic, qos := QoS, from := Client,
republish_action(#{<<"target_topic">> := TargetTopic}) ->
fun(Selected, #{qos := QoS, from := Client,
flags := Flags, headers := Headers}) ->
case emqx_topic:match(OriginTopic, SrcTopic) of
true ->
?LOG(debug, "[built_in:republish_action] republish to: ~p, Payload: ~p",
[TargetTopic, Selected]),
emqx_broker:safe_publish(
#message{
id = emqx_guid:gen(),
qos = QoS,
from = republish_from(Client),
flags = Flags,
headers = Headers,
topic = TargetTopic,
payload = jsx:encode(Selected),
timestamp = erlang:timestamp()
});
false -> ok
end
?LOG(debug, "[built_in:republish_action] republish to: ~p, Payload: ~p",
[TargetTopic, Selected]),
emqx_broker:safe_publish(
#message{
id = emqx_guid:gen(),
qos = QoS,
from = republish_from(Client),
flags = Flags,
headers = Headers,
topic = TargetTopic,
payload = jsx:encode(Selected),
timestamp = erlang:timestamp()
})
end.

republish_from(Client) ->
@@ -109,7 +109,7 @@ validate_boolean(false) -> ok;
validate_boolean(Val) -> error({invalid_data_type, {boolean, Val}}).

reg_exp(url) -> "^https?://\\w+(\.\\w+)*(:[0-9]+)?";
reg_exp(topic) -> "^/?(\\w|\\#|\\+)+(/(\\w|\\#|\\+))*$";
reg_exp(topic) -> "^/?(\\w|\\#|\\+)+(/?(\\w|\\#|\\+))*/?$";
reg_exp(resource_type) -> "[a-zA-Z0-9_:-]";
reg_exp(any) -> ".*";
reg_exp(RegExp) -> RegExp.
@@ -649,7 +649,7 @@ create_simple_repub_rule(RuleName, TargetTopic, SQL) ->
for => 'message.publish',
rawsql => SQL,
actions => [{'built_in:republish_action',
#{<<"from">> => <<"#">>, <<"to">> => TargetTopic}}],
#{<<"target_topic">> => TargetTopic}}],
description => RuleName}),
Rule.

0 comments on commit 8721ebe

Please sign in to comment.
You can’t perform that action at this time.