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

View File

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

View File

@ -69,12 +69,12 @@ update(Cmd, Rules) ->
pre_config_update({move, Id, <<"top">>}, Conf) when is_list(Conf) ->
{Index, _} = find_rule_by_id(Id),
{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) ->
{Index, _} = find_rule_by_id(Id),
{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) ->
{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),
{List1, List2} = lists:split(Index2, Conf),
lists:delete(Conf1, lists:droplast(List1))
++ [Conf1] ++ [Conf2]
++ lists:delete(Conf1, List2);
{ok, lists:delete(Conf1, lists:droplast(List1))
++ [Conf1] ++ [Conf2]
++ lists:delete(Conf1, List2)};
pre_config_update({move, Id, #{<<"after">> := AfterId}}, Conf) when is_list(Conf) ->
{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),
{List1, List2} = lists:split(Index2, Conf),
lists:delete(Conf1, List1)
++ [Conf1]
++ lists:delete(Conf1, List2);
{ok, lists:delete(Conf1, List1)
++ [Conf1]
++ lists:delete(Conf1, List2)};
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) ->
Conf ++ Rules;
{ok, Conf ++ Rules};
pre_config_update({{replace_once, Id}, Rule}, Conf) when is_map(Rule), is_list(Conf) ->
{Index, _} = find_rule_by_id(Id),
{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)->
%% overwrite the entire config!
Rules.
{ok, Rules}.
post_config_update(_, undefined, _Conf) ->
ok;

View File

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

View File

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

View File

@ -76,13 +76,13 @@ init_per_suite(Config) ->
ekka_mnesia:start(),
emqx_mgmt_auth:mnesia(boot),
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, enable], true),
{ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Config.
end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []),
{ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz, emqx_management]),
ok.
@ -155,7 +155,7 @@ t_api(_) ->
ok.
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 := Id2}},
#{annotations := #{id := Id3}},

View File

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

View File

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

View File

@ -35,8 +35,8 @@ init_per_suite(Config) ->
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, enable], true),
{ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{
<<"server">> => <<"127.0.0.1:27017">>,
<<"pool_size">> => 1,
@ -49,11 +49,11 @@ init_per_suite(Config) ->
<<"principal">> => <<"all">>,
<<"sql">> => <<"abcb">>,
<<"type">> => <<"mysql">> }],
{ok, _, _} = emqx_authz:update(replace, Rules),
{ok, _} = emqx_authz:update(replace, Rules),
Config.
end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []),
{ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz, 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:update_config([zones, default, authorization, cache, enable], false),
{ok, _, _} = emqx:update_config([zones, default, authorization, enable], true),
{ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{
<<"server">> => <<"127.0.0.1:27017">>,
<<"pool_size">> => 1,
@ -48,11 +48,11 @@ init_per_suite(Config) ->
},
<<"sql">> => <<"abcb">>,
<<"type">> => <<"pgsql">> }],
{ok, _, _} = emqx_authz:update(replace, Rules),
{ok, _} = emqx_authz:update(replace, Rules),
Config.
end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []),
{ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz, 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:update_config([zones, default, authorization, cache, enable], false),
{ok, _, _} = emqx:update_config([zones, default, authorization, enable], true),
{ok, _} = emqx:update_config([zones, default, authorization, cache, enable], false),
{ok, _} = emqx:update_config([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{
<<"server">> => <<"127.0.0.1:27017">>,
<<"pool_size">> => 1,
@ -47,11 +47,11 @@ init_per_suite(Config) ->
},
<<"cmd">> => <<"HGETALL mqtt_authz:%u">>,
<<"type">> => <<"redis">> }],
{ok, _, _} = emqx_authz:update(replace, Rules),
{ok, _} = emqx_authz:update(replace, Rules),
Config.
end_per_suite(_Config) ->
{ok, _, _} = emqx_authz:update(replace, []),
{ok, _} = emqx_authz:update(replace, []),
emqx_ct_helpers:stop_apps([emqx_authz, 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) ->
case emqx_data_bridge:update_config({update, ?BRIDGE(Name, BridgeType, Config)}) of
{ok, _, _} ->
{ok, _} ->
{200, #{code => 0, data => format_api_reply(
emqx_resource_api:format_data(Data))}};
{error, Reason} ->
@ -133,7 +133,7 @@ update_config_and_reply(Name, BridgeType, Config, Data) ->
delete_config_and_reply(Name) ->
case emqx_data_bridge:update_config({delete, Name}) of
{ok, _, _} -> {200, #{code => 0, data => #{}}};
{ok, _} -> {200, #{code => 0, data => #{}}};
{error, Reason} ->
{500, #{code => 102, message => emqx_resource_api:stringnify(Reason)}}
end.

View File

@ -32,12 +32,12 @@ stop(_State) ->
%% internal functions
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) ->
remove_bridge(Name, OldConf);
{ok, remove_bridge(Name, OldConf)};
pre_config_update(NewConf, _OldConf) when is_list(NewConf) ->
%% overwrite the entire config!
NewConf.
{ok, NewConf}.
remove_bridge(_Name, undefined) ->
[];

View File

@ -113,7 +113,7 @@ config(get, Req) ->
config(put, 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}),
{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'
Path = conf_path_reset(Req) ++ conf_path_from_querystr(Req),
case emqx:reset_config(Path, #{}) of
{ok, _, _} -> {200};
{ok, _} -> {200};
{error, Reason} ->
{400, ?ERR_MSG(Reason)}
end.

View File

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

View File

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