refactor(config): update the return values of config handlers

This commit is contained in:
Shawn 2021-08-17 19:49:13 +08:00
parent e5c3199d6e
commit bf6251e20f
16 changed files with 70 additions and 75 deletions

View File

@ -28,7 +28,7 @@
-boot_mnesia({mnesia, [boot]}). -boot_mnesia({mnesia, [boot]}).
-copy_mnesia({mnesia, [copy]}). -copy_mnesia({mnesia, [copy]}).
-export([pre_config_update/2]). -export([post_config_update/3]).
-export([ start_link/0 -export([ start_link/0
, stop/0 , stop/0
@ -151,14 +151,9 @@ get_alarms(activated) ->
get_alarms(deactivated) -> get_alarms(deactivated) ->
gen_server:call(?MODULE, {get_alarms, deactivated}). gen_server:call(?MODULE, {get_alarms, deactivated}).
pre_config_update(#{<<"validity_period">> := Period0} = NewConf, OldConf) -> post_config_update(_, #{validity_period := Period0}, _OldConf) ->
?MODULE ! {update_timer, hocon_postprocess:duration(Period0)}, ?MODULE ! {update_timer, Period0},
merge(OldConf, NewConf); ok.
pre_config_update(NewConf, OldConf) ->
merge(OldConf, NewConf).
merge(undefined, New) -> New;
merge(Old, New) -> maps:merge(Old, New).
format(#activated_alarm{name = Name, message = Message, activate_at = At, details = Details}) -> format(#activated_alarm{name = Name, message = Message, activate_at = At, details = Details}) ->
Now = erlang:system_time(microsecond), Now = erlang:system_time(microsecond),

View File

@ -28,14 +28,14 @@ all() -> emqx_ct:all(?MODULE).
init_per_testcase(t_size_limit, Config) -> init_per_testcase(t_size_limit, Config) ->
emqx_ct_helpers:boot_modules(all), emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]), emqx_ct_helpers:start_apps([]),
{ok, _, _} = emqx:update_config([alarm], #{ {ok, _} = emqx:update_config([alarm], #{
<<"size_limit">> => 2 <<"size_limit">> => 2
}), }),
Config; Config;
init_per_testcase(t_validity_period, Config) -> init_per_testcase(t_validity_period, Config) ->
emqx_ct_helpers:boot_modules(all), emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]), emqx_ct_helpers:start_apps([]),
{ok, _, _} = emqx:update_config([alarm], #{ {ok, _} = emqx:update_config([alarm], #{
<<"validity_period">> => <<"1s">> <<"validity_period">> => <<"1s">>
}), }),
Config; Config;

View File

@ -69,12 +69,12 @@ update(Cmd, Rules) ->
pre_config_update({move, Id, <<"top">>}, Conf) when is_list(Conf) -> pre_config_update({move, Id, <<"top">>}, Conf) when is_list(Conf) ->
{Index, _} = find_rule_by_id(Id), {Index, _} = find_rule_by_id(Id),
{List1, List2} = lists:split(Index, Conf), {List1, List2} = lists:split(Index, Conf),
[lists:nth(Index, Conf)] ++ lists:droplast(List1) ++ List2; {ok, [lists:nth(Index, Conf)] ++ lists:droplast(List1) ++ List2};
pre_config_update({move, Id, <<"bottom">>}, Conf) when is_list(Conf) -> pre_config_update({move, Id, <<"bottom">>}, Conf) when is_list(Conf) ->
{Index, _} = find_rule_by_id(Id), {Index, _} = find_rule_by_id(Id),
{List1, List2} = lists:split(Index, Conf), {List1, List2} = lists:split(Index, Conf),
lists:droplast(List1) ++ List2 ++ [lists:nth(Index, Conf)]; {ok, lists:droplast(List1) ++ List2 ++ [lists:nth(Index, Conf)]};
pre_config_update({move, Id, #{<<"before">> := BeforeId}}, Conf) when is_list(Conf) -> pre_config_update({move, Id, #{<<"before">> := BeforeId}}, Conf) when is_list(Conf) ->
{Index1, _} = find_rule_by_id(Id), {Index1, _} = find_rule_by_id(Id),
@ -83,9 +83,9 @@ pre_config_update({move, Id, #{<<"before">> := BeforeId}}, Conf) when is_list(Co
Conf2 = lists:nth(Index2, Conf), Conf2 = lists:nth(Index2, Conf),
{List1, List2} = lists:split(Index2, Conf), {List1, List2} = lists:split(Index2, Conf),
lists:delete(Conf1, lists:droplast(List1)) {ok, lists:delete(Conf1, lists:droplast(List1))
++ [Conf1] ++ [Conf2] ++ [Conf1] ++ [Conf2]
++ lists:delete(Conf1, List2); ++ lists:delete(Conf1, List2)};
pre_config_update({move, Id, #{<<"after">> := AfterId}}, Conf) when is_list(Conf) -> pre_config_update({move, Id, #{<<"after">> := AfterId}}, Conf) when is_list(Conf) ->
{Index1, _} = find_rule_by_id(Id), {Index1, _} = find_rule_by_id(Id),
@ -93,21 +93,21 @@ pre_config_update({move, Id, #{<<"after">> := AfterId}}, Conf) when is_list(Conf
{Index2, _} = find_rule_by_id(AfterId), {Index2, _} = find_rule_by_id(AfterId),
{List1, List2} = lists:split(Index2, Conf), {List1, List2} = lists:split(Index2, Conf),
lists:delete(Conf1, List1) {ok, lists:delete(Conf1, List1)
++ [Conf1] ++ [Conf1]
++ lists:delete(Conf1, List2); ++ lists:delete(Conf1, List2)};
pre_config_update({head, Rules}, Conf) when is_list(Rules), is_list(Conf) -> pre_config_update({head, Rules}, Conf) when is_list(Rules), is_list(Conf) ->
Rules ++ Conf; {ok, Rules ++ Conf};
pre_config_update({tail, Rules}, Conf) when is_list(Rules), is_list(Conf) -> pre_config_update({tail, Rules}, Conf) when is_list(Rules), is_list(Conf) ->
Conf ++ Rules; {ok, Conf ++ Rules};
pre_config_update({{replace_once, Id}, Rule}, Conf) when is_map(Rule), is_list(Conf) -> pre_config_update({{replace_once, Id}, Rule}, Conf) when is_map(Rule), is_list(Conf) ->
{Index, _} = find_rule_by_id(Id), {Index, _} = find_rule_by_id(Id),
{List1, List2} = lists:split(Index, Conf), {List1, List2} = lists:split(Index, Conf),
lists:droplast(List1) ++ [Rule] ++ List2; {ok, lists:droplast(List1) ++ [Rule] ++ List2};
pre_config_update({_, Rules}, _Conf) when is_list(Rules)-> pre_config_update({_, Rules}, _Conf) when is_list(Rules)->
%% overwrite the entire config! %% overwrite the entire config!
Rules. {ok, Rules}.
post_config_update(_, undefined, _Conf) -> post_config_update(_, undefined, _Conf) ->
ok; ok;

View File

@ -449,7 +449,7 @@ rules(post, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
RawConfig = jsx:decode(Body, [return_maps]), RawConfig = jsx:decode(Body, [return_maps]),
case emqx_authz:update(head, [RawConfig]) of case emqx_authz:update(head, [RawConfig]) of
{ok, _, _} -> {204}; {ok, _} -> {204};
{error, Reason} -> {error, Reason} ->
{400, #{code => <<"BAD_REQUEST">>, {400, #{code => <<"BAD_REQUEST">>,
messgae => atom_to_binary(Reason)}} messgae => atom_to_binary(Reason)}}
@ -458,7 +458,7 @@ rules(put, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
RawConfig = jsx:decode(Body, [return_maps]), RawConfig = jsx:decode(Body, [return_maps]),
case emqx_authz:update(replace, RawConfig) of case emqx_authz:update(replace, RawConfig) of
{ok, _, _} -> {204}; {ok, _} -> {204};
{error, Reason} -> {error, Reason} ->
{400, #{code => <<"BAD_REQUEST">>, {400, #{code => <<"BAD_REQUEST">>,
messgae => atom_to_binary(Reason)}} messgae => atom_to_binary(Reason)}}
@ -486,7 +486,7 @@ rule(put, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
RawConfig = jsx:decode(Body, [return_maps]), RawConfig = jsx:decode(Body, [return_maps]),
case emqx_authz:update({replace_once, RuleId}, RawConfig) of case emqx_authz:update({replace_once, RuleId}, RawConfig) of
{ok, _, _} -> {204}; {ok, _} -> {204};
{error, not_found_rule} -> {error, not_found_rule} ->
{404, #{code => <<"NOT_FOUND">>, {404, #{code => <<"NOT_FOUND">>,
messgae => <<"rule ", RuleId/binary, " not found">>}}; messgae => <<"rule ", RuleId/binary, " not found">>}};
@ -497,7 +497,7 @@ rule(put, Request) ->
rule(delete, Request) -> rule(delete, Request) ->
RuleId = cowboy_req:binding(id, Request), RuleId = cowboy_req:binding(id, Request),
case emqx_authz:update({replace_once, RuleId}, #{}) of case emqx_authz:update({replace_once, RuleId}, #{}) of
{ok, _, _} -> {204}; {ok, _} -> {204};
{error, Reason} -> {error, Reason} ->
{400, #{code => <<"BAD_REQUEST">>, {400, #{code => <<"BAD_REQUEST">>,
messgae => atom_to_binary(Reason)}} messgae => atom_to_binary(Reason)}}
@ -507,7 +507,7 @@ move_rule(post, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
#{<<"position">> := Position} = jsx:decode(Body, [return_maps]), #{<<"position">> := Position} = jsx:decode(Body, [return_maps]),
case emqx_authz:move(RuleId, Position) of case emqx_authz:move(RuleId, Position) of
{ok, _, _} -> {204}; {ok, _} -> {204};
{error, not_found_rule} -> {error, not_found_rule} ->
{404, #{code => <<"NOT_FOUND">>, {404, #{code => <<"NOT_FOUND">>,
messgae => <<"rule ", RuleId/binary, " not found">>}}; messgae => <<"rule ", RuleId/binary, " not found">>}};

View File

@ -33,17 +33,17 @@ groups() ->
init_per_suite(Config) -> init_per_suite(Config) ->
ok = emqx_config:init_load(emqx_authz_schema, ?CONF_DEFAULT), ok = emqx_config:init_load(emqx_authz_schema, ?CONF_DEFAULT),
ok = emqx_ct_helpers:start_apps([emqx_authz]), ok = emqx_ct_helpers:start_apps([emqx_authz]),
{ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), {ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), {ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []), {ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz]), emqx_ct_helpers:stop_apps([emqx_authz]),
ok. ok.
init_per_testcase(_, Config) -> init_per_testcase(_, Config) ->
{ok, _, _} = emqx_authz:update(replace, []), {ok, _} = emqx_authz:update(replace, []),
Config. Config.
-define(RULE1, #{<<"principal">> => <<"all">>, -define(RULE1, #{<<"principal">> => <<"all">>,
@ -82,9 +82,9 @@ init_per_testcase(_, Config) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
t_update_rule(_) -> t_update_rule(_) ->
{ok, _, _} = emqx_authz:update(replace, [?RULE2]), {ok, _} = emqx_authz:update(replace, [?RULE2]),
{ok, _, _} = emqx_authz:update(head, [?RULE1]), {ok, _} = emqx_authz:update(head, [?RULE1]),
{ok, _, _} = emqx_authz:update(tail, [?RULE3]), {ok, _} = emqx_authz:update(tail, [?RULE3]),
Lists1 = emqx_authz:check_rules([?RULE1, ?RULE2, ?RULE3]), Lists1 = emqx_authz:check_rules([?RULE1, ?RULE2, ?RULE3]),
?assertMatch(Lists1, emqx_config:get([authorization, rules], [])), ?assertMatch(Lists1, emqx_config:get([authorization, rules], [])),
@ -107,7 +107,7 @@ t_update_rule(_) ->
} }
] = emqx_authz:lookup(), ] = emqx_authz:lookup(),
{ok, _, _} = emqx_authz:update({replace_once, Id3}, ?RULE4), {ok, _} = emqx_authz:update({replace_once, Id3}, ?RULE4),
Lists2 = emqx_authz:check_rules([?RULE1, ?RULE2, ?RULE4]), Lists2 = emqx_authz:check_rules([?RULE1, ?RULE2, ?RULE4]),
?assertMatch(Lists2, emqx_config:get([authorization, rules], [])), ?assertMatch(Lists2, emqx_config:get([authorization, rules], [])),
@ -132,38 +132,38 @@ t_update_rule(_) ->
} }
] = emqx_authz:lookup(), ] = emqx_authz:lookup(),
{ok, _, _} = emqx_authz:update(replace, []). {ok, _} = emqx_authz:update(replace, []).
t_move_rule(_) -> t_move_rule(_) ->
{ok, _, _} = emqx_authz:update(replace, [?RULE1, ?RULE2, ?RULE3, ?RULE4]), {ok, _} = emqx_authz:update(replace, [?RULE1, ?RULE2, ?RULE3, ?RULE4]),
[#{annotations := #{id := Id1}}, [#{annotations := #{id := Id1}},
#{annotations := #{id := Id2}}, #{annotations := #{id := Id2}},
#{annotations := #{id := Id3}}, #{annotations := #{id := Id3}},
#{annotations := #{id := Id4}} #{annotations := #{id := Id4}}
] = emqx_authz:lookup(), ] = emqx_authz:lookup(),
{ok, _, _} = emqx_authz:move(Id4, <<"top">>), {ok, _} = emqx_authz:move(Id4, <<"top">>),
?assertMatch([#{annotations := #{id := Id4}}, ?assertMatch([#{annotations := #{id := Id4}},
#{annotations := #{id := Id1}}, #{annotations := #{id := Id1}},
#{annotations := #{id := Id2}}, #{annotations := #{id := Id2}},
#{annotations := #{id := Id3}} #{annotations := #{id := Id3}}
], emqx_authz:lookup()), ], emqx_authz:lookup()),
{ok, _, _} = emqx_authz:move(Id1, <<"bottom">>), {ok, _} = emqx_authz:move(Id1, <<"bottom">>),
?assertMatch([#{annotations := #{id := Id4}}, ?assertMatch([#{annotations := #{id := Id4}},
#{annotations := #{id := Id2}}, #{annotations := #{id := Id2}},
#{annotations := #{id := Id3}}, #{annotations := #{id := Id3}},
#{annotations := #{id := Id1}} #{annotations := #{id := Id1}}
], emqx_authz:lookup()), ], emqx_authz:lookup()),
{ok, _, _} = emqx_authz:move(Id3, #{<<"before">> => Id4}), {ok, _} = emqx_authz:move(Id3, #{<<"before">> => Id4}),
?assertMatch([#{annotations := #{id := Id3}}, ?assertMatch([#{annotations := #{id := Id3}},
#{annotations := #{id := Id4}}, #{annotations := #{id := Id4}},
#{annotations := #{id := Id2}}, #{annotations := #{id := Id2}},
#{annotations := #{id := Id1}} #{annotations := #{id := Id1}}
], emqx_authz:lookup()), ], emqx_authz:lookup()),
{ok, _, _} = emqx_authz:move(Id2, #{<<"after">> => Id1}), {ok, _} = emqx_authz:move(Id2, #{<<"after">> => Id1}),
?assertMatch([#{annotations := #{id := Id3}}, ?assertMatch([#{annotations := #{id := Id3}},
#{annotations := #{id := Id4}}, #{annotations := #{id := Id4}},
#{annotations := #{id := Id1}}, #{annotations := #{id := Id1}},

View File

@ -76,13 +76,13 @@ init_per_suite(Config) ->
ekka_mnesia:start(), ekka_mnesia:start(),
emqx_mgmt_auth:mnesia(boot), emqx_mgmt_auth:mnesia(boot),
ok = emqx_ct_helpers:start_apps([emqx_management, emqx_authz], fun set_special_configs/1), ok = emqx_ct_helpers:start_apps([emqx_management, emqx_authz], fun set_special_configs/1),
{ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), {ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), {ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []), {ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz, emqx_management]), emqx_ct_helpers:stop_apps([emqx_authz, emqx_management]),
ok. ok.
@ -155,7 +155,7 @@ t_api(_) ->
ok. ok.
t_move_rule(_) -> t_move_rule(_) ->
{ok, _, _} = emqx_authz:update(replace, [?RULE1, ?RULE2, ?RULE3, ?RULE4]), {ok, _} = emqx_authz:update(replace, [?RULE1, ?RULE2, ?RULE3, ?RULE4]),
[#{annotations := #{id := Id1}}, [#{annotations := #{id := Id1}},
#{annotations := #{id := Id2}}, #{annotations := #{id := Id2}},
#{annotations := #{id := Id3}}, #{annotations := #{id := Id3}},

View File

@ -35,8 +35,8 @@ init_per_suite(Config) ->
ok = emqx_ct_helpers:start_apps([emqx_authz]), ok = emqx_ct_helpers:start_apps([emqx_authz]),
{ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), {ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), {ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{ Rules = [#{ <<"config">> => #{
<<"url">> => <<"https://fake.com:443/">>, <<"url">> => <<"https://fake.com:443/">>,
<<"headers">> => #{}, <<"headers">> => #{},
@ -46,11 +46,11 @@ init_per_suite(Config) ->
<<"principal">> => <<"all">>, <<"principal">> => <<"all">>,
<<"type">> => <<"http">>} <<"type">> => <<"http">>}
], ],
{ok, _, _} = emqx_authz:update(replace, Rules), {ok, _} = emqx_authz:update(replace, Rules),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []), {ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]), emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]),
meck:unload(emqx_resource), meck:unload(emqx_resource),
ok. ok.

View File

@ -34,8 +34,8 @@ init_per_suite(Config) ->
meck:expect(emqx_resource, remove, fun(_) -> ok end ), meck:expect(emqx_resource, remove, fun(_) -> ok end ),
ok = emqx_ct_helpers:start_apps([emqx_authz]), ok = emqx_ct_helpers:start_apps([emqx_authz]),
{ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), {ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), {ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{ Rules = [#{ <<"config">> => #{
<<"mongo_type">> => <<"single">>, <<"mongo_type">> => <<"single">>,
<<"server">> => <<"127.0.0.1:27017">>, <<"server">> => <<"127.0.0.1:27017">>,
@ -47,11 +47,11 @@ init_per_suite(Config) ->
<<"find">> => #{<<"a">> => <<"b">>}, <<"find">> => #{<<"a">> => <<"b">>},
<<"type">> => <<"mongo">>} <<"type">> => <<"mongo">>}
], ],
{ok, _, _} = emqx_authz:update(replace, Rules), {ok, _} = emqx_authz:update(replace, Rules),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []), {ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]), emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]),
meck:unload(emqx_resource), meck:unload(emqx_resource),
ok. ok.

View File

@ -35,8 +35,8 @@ init_per_suite(Config) ->
ok = emqx_ct_helpers:start_apps([emqx_authz]), ok = emqx_ct_helpers:start_apps([emqx_authz]),
{ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), {ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), {ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{ Rules = [#{ <<"config">> => #{
<<"server">> => <<"127.0.0.1:27017">>, <<"server">> => <<"127.0.0.1:27017">>,
<<"pool_size">> => 1, <<"pool_size">> => 1,
@ -49,11 +49,11 @@ init_per_suite(Config) ->
<<"principal">> => <<"all">>, <<"principal">> => <<"all">>,
<<"sql">> => <<"abcb">>, <<"sql">> => <<"abcb">>,
<<"type">> => <<"mysql">> }], <<"type">> => <<"mysql">> }],
{ok, _, _} = emqx_authz:update(replace, Rules), {ok, _} = emqx_authz:update(replace, Rules),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []), {ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]), emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]),
meck:unload(emqx_resource). meck:unload(emqx_resource).

View File

@ -35,8 +35,8 @@ init_per_suite(Config) ->
ok = emqx_ct_helpers:start_apps([emqx_authz]), ok = emqx_ct_helpers:start_apps([emqx_authz]),
{ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), {ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), {ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{ Rules = [#{ <<"config">> => #{
<<"server">> => <<"127.0.0.1:27017">>, <<"server">> => <<"127.0.0.1:27017">>,
<<"pool_size">> => 1, <<"pool_size">> => 1,
@ -48,11 +48,11 @@ init_per_suite(Config) ->
}, },
<<"sql">> => <<"abcb">>, <<"sql">> => <<"abcb">>,
<<"type">> => <<"pgsql">> }], <<"type">> => <<"pgsql">> }],
{ok, _, _} = emqx_authz:update(replace, Rules), {ok, _} = emqx_authz:update(replace, Rules),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []), {ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]), emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]),
meck:unload(emqx_resource). meck:unload(emqx_resource).

View File

@ -35,8 +35,8 @@ init_per_suite(Config) ->
ok = emqx_ct_helpers:start_apps([emqx_authz]), ok = emqx_ct_helpers:start_apps([emqx_authz]),
{ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), {ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), {ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{ Rules = [#{ <<"config">> => #{
<<"server">> => <<"127.0.0.1:27017">>, <<"server">> => <<"127.0.0.1:27017">>,
<<"pool_size">> => 1, <<"pool_size">> => 1,
@ -47,11 +47,11 @@ init_per_suite(Config) ->
}, },
<<"cmd">> => <<"HGETALL mqtt_authz:%u">>, <<"cmd">> => <<"HGETALL mqtt_authz:%u">>,
<<"type">> => <<"redis">> }], <<"type">> => <<"redis">> }],
{ok, _, _} = emqx_authz:update(replace, Rules), {ok, _} = emqx_authz:update(replace, Rules),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []), {ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]), emqx_ct_helpers:stop_apps([emqx_authz, emqx_resource]),
meck:unload(emqx_resource). meck:unload(emqx_resource).

View File

@ -124,7 +124,7 @@ format_api_reply(#{resource_type := Type, id := Id, config := Conf, status := St
update_config_and_reply(Name, BridgeType, Config, Data) -> update_config_and_reply(Name, BridgeType, Config, Data) ->
case emqx_data_bridge:update_config({update, ?BRIDGE(Name, BridgeType, Config)}) of case emqx_data_bridge:update_config({update, ?BRIDGE(Name, BridgeType, Config)}) of
{ok, _, _} -> {ok, _} ->
{200, #{code => 0, data => format_api_reply( {200, #{code => 0, data => format_api_reply(
emqx_resource_api:format_data(Data))}}; emqx_resource_api:format_data(Data))}};
{error, Reason} -> {error, Reason} ->
@ -133,7 +133,7 @@ update_config_and_reply(Name, BridgeType, Config, Data) ->
delete_config_and_reply(Name) -> delete_config_and_reply(Name) ->
case emqx_data_bridge:update_config({delete, Name}) of case emqx_data_bridge:update_config({delete, Name}) of
{ok, _, _} -> {200, #{code => 0, data => #{}}}; {ok, _} -> {200, #{code => 0, data => #{}}};
{error, Reason} -> {error, Reason} ->
{500, #{code => 102, message => emqx_resource_api:stringnify(Reason)}} {500, #{code => 102, message => emqx_resource_api:stringnify(Reason)}}
end. end.

View File

@ -32,12 +32,12 @@ stop(_State) ->
%% internal functions %% internal functions
pre_config_update({update, Bridge = #{<<"name">> := Name}}, OldConf) -> pre_config_update({update, Bridge = #{<<"name">> := Name}}, OldConf) ->
[Bridge | remove_bridge(Name, OldConf)]; {ok, [Bridge | remove_bridge(Name, OldConf)]};
pre_config_update({delete, Name}, OldConf) -> pre_config_update({delete, Name}, OldConf) ->
remove_bridge(Name, OldConf); {ok, remove_bridge(Name, OldConf)};
pre_config_update(NewConf, _OldConf) when is_list(NewConf) -> pre_config_update(NewConf, _OldConf) when is_list(NewConf) ->
%% overwrite the entire config! %% overwrite the entire config!
NewConf. {ok, NewConf}.
remove_bridge(_Name, undefined) -> remove_bridge(_Name, undefined) ->
[]; [];

View File

@ -113,7 +113,7 @@ config(get, Req) ->
config(put, Req) -> config(put, Req) ->
Path = conf_path(Req), Path = conf_path(Req),
{ok, _, RawConf} = emqx:update_config(Path, http_body(Req), {ok, #{raw_config := RawConf}} = emqx:update_config(Path, http_body(Req),
#{rawconf_with_defaults => true}), #{rawconf_with_defaults => true}),
{200, emqx_map_lib:deep_get(Path, emqx_map_lib:jsonable_map(RawConf))}. {200, emqx_map_lib:deep_get(Path, emqx_map_lib:jsonable_map(RawConf))}.
@ -121,7 +121,7 @@ config_reset(post, Req) ->
%% reset the config specified by the query string param 'conf_path' %% reset the config specified by the query string param 'conf_path'
Path = conf_path_reset(Req) ++ conf_path_from_querystr(Req), Path = conf_path_reset(Req) ++ conf_path_from_querystr(Req),
case emqx:reset_config(Path, #{}) of case emqx:reset_config(Path, #{}) of
{ok, _, _} -> {200}; {ok, _} -> {200};
{error, Reason} -> {error, Reason} ->
{400, ?ERR_MSG(Reason)} {400, ?ERR_MSG(Reason)}
end. end.

View File

@ -113,7 +113,7 @@ prometheus(put, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
Params = emqx_json:decode(Body, [return_maps]), Params = emqx_json:decode(Body, [return_maps]),
Enable = maps:get(<<"enable">>, Params), Enable = maps:get(<<"enable">>, Params),
{ok, _, _} = emqx:update_config([prometheus], Params), {ok, _} = emqx:update_config([prometheus], Params),
enable_prometheus(Enable). enable_prometheus(Enable).
% stats(_Bindings, Params) -> % stats(_Bindings, Params) ->

View File

@ -91,7 +91,7 @@ statsd(put, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
Params = emqx_json:decode(Body, [return_maps]), Params = emqx_json:decode(Body, [return_maps]),
Enable = maps:get(<<"enable">>, Params), Enable = maps:get(<<"enable">>, Params),
{ok, _, _} = emqx:update_config([statsd], Params), {ok, _} = emqx:update_config([statsd], Params),
enable_statsd(Enable). enable_statsd(Enable).
enable_statsd(true) -> enable_statsd(true) ->