fix(backup): fix data import/export problem

This commit is contained in:
wwhai 2021-03-15 14:53:44 +08:00 committed by tigercl
parent 72c1ee264c
commit 109b2f4e90
1 changed files with 36 additions and 9 deletions

View File

@ -17,6 +17,7 @@
-module(emqx_mgmt_data_backup).
-include("emqx_mgmt.hrl").
-include_lib("emqx_rule_engine/include/rule_engine.hrl").
-include_lib("emqx/include/emqx.hrl").
-include_lib("kernel/include/file.hrl").
@ -59,7 +60,11 @@
%%--------------------------------------------------------------------
export_rules() ->
lists:map(fun({_, RuleId, _, RawSQL, _, _, _, _, _, _, Actions, Enabled, Desc}) ->
lists:map(fun(#rule{id = RuleId,
rawsql = RawSQL,
actions = Actions,
enabled = Enabled,
description = Desc}) ->
[{id, RuleId},
{rawsql, RawSQL},
{actions, actions_to_prop_list(Actions)},
@ -68,7 +73,11 @@ export_rules() ->
end, emqx_rule_registry:get_rules()).
export_resources() ->
lists:map(fun({_, Id, Type, Config, CreatedAt, Desc}) ->
lists:map(fun(#resource{id = Id,
type = Type,
config = Config,
created_at = CreatedAt,
description = Desc}) ->
NCreatedAt = case CreatedAt of
undefined -> null;
_ -> CreatedAt
@ -236,18 +245,25 @@ import_resources_and_rules(Resources, Rules, FromVersion)
when FromVersion =:= "4.0" orelse FromVersion =:= "4.1" orelse FromVersion =:= "4.2" ->
Configs = lists:foldl(fun(#{<<"id">> := ID,
<<"type">> := <<"web_hook">>,
<<"config">> := #{<<"content_type">> := ContentType,
<<"config">> := #{<<"connect_timeout">> := ConnectTimeout,
<<"content_type">> := ContentType,
<<"headers">> := Headers,
<<"method">> := Method,
<<"pool_size">> := PoolSize,
<<"request_timeout">> := RequestTimeout,
<<"url">> := URL}} = Resource, Acc) ->
NConfig = #{<<"connect_timeout">> => 5,
<<"request_timeout">> => 5,
CovertFun = fun(Int) ->
list_to_binary(integer_to_list(Int) ++ "s")
end,
NConfig = #{<<"connect_timeout">> => CovertFun(ConnectTimeout),
<<"method">> => Method,
<<"pool_size">> => PoolSize,
<<"request_timeout">> => CovertFun(RequestTimeout),
<<"cacertfile">> => <<>>,
<<"certfile">> => <<>>,
<<"keyfile">> => <<>>,
<<"pool_size">> => 8,
<<"url">> => URL,
<<"verify">> => true},
<<"verify">> => true,
<<"url">> => URL},
NResource = Resource#{<<"config">> := NConfig},
{ok, _Resource} = import_resource(NResource),
NHeaders = maps:put(<<"content-type">>, ContentType, Headers),
@ -284,7 +300,18 @@ apply_new_config([Action = #{<<"name">> := <<"data_to_webserver">>,
<<"method">> => Method,
<<"path">> => Path},
apply_new_config(More, Configs, [Action#{<<"args">> := Args} | Acc])
end.
end;
apply_new_config([Action = #{<<"args">> := #{<<"$resource">> := ResourceId,
<<"forward_topic">> := ForwardTopic,
<<"payload_tmpl">> := PayloadTmpl},
<<"fallbacks">> := _Fallbacks,
<<"id">> := _Id,
<<"name">> := <<"data_to_mqtt_broker">>} | More], Configs, Acc) ->
Args = #{<<"$resource">> => ResourceId,
<<"payload_tmpl">> => PayloadTmpl,
<<"forward_topic">> => ForwardTopic},
apply_new_config(More, Configs, [Action#{<<"args">> := Args} | Acc]).
-endif.