fix(config): update the calls to emqx_config:update/2,3

This commit is contained in:
Shawn 2021-08-16 15:11:00 +08:00
parent b381b5d2b9
commit 7f03cd0e8b
14 changed files with 92 additions and 68 deletions

View File

@ -43,7 +43,7 @@
, put/2 , put/2
]). ]).
-export([ save_schema_mod/1 -export([ save_schema_mod_and_names/1
, get_schema_mod/0 , get_schema_mod/0
, get_schema_mod/1 , get_schema_mod/1
, get_root_names/0 , get_root_names/0
@ -76,7 +76,7 @@
-define(CONF, conf). -define(CONF, conf).
-define(RAW_CONF, raw_conf). -define(RAW_CONF, raw_conf).
-define(PERSIS_MOD_ROOTNAMES, {?MODULE, default_conf}). -define(PERSIS_SCHEMA_MODS, {?MODULE, schema_mods}).
-define(PERSIS_KEY(TYPE, ROOT), {?MODULE, TYPE, ROOT}). -define(PERSIS_KEY(TYPE, ROOT), {?MODULE, TYPE, ROOT}).
-define(ZONE_CONF_PATH(ZONE, PATH), [zones, ZONE | PATH]). -define(ZONE_CONF_PATH(ZONE, PATH), [zones, ZONE | PATH]).
-define(LISTENER_CONF_PATH(ZONE, LISTENER, PATH), [zones, ZONE, listeners, LISTENER | PATH]). -define(LISTENER_CONF_PATH(ZONE, LISTENER, PATH), [zones, ZONE, listeners, LISTENER | PATH]).
@ -183,17 +183,18 @@ put(Config) ->
put(KeyPath, Config) -> do_put(?CONF, KeyPath, Config). put(KeyPath, Config) -> do_put(?CONF, KeyPath, Config).
-spec update(emqx_map_lib:config_key_path(), update_request()) -> -spec update(emqx_map_lib:config_key_path(), update_request()) ->
ok | {error, term()}. {ok, config(), raw_config()} | {error, term()}.
update(KeyPath, UpdateReq) -> update(KeyPath, UpdateReq) ->
update(KeyPath, UpdateReq, #{}). update(KeyPath, UpdateReq, #{}).
-spec update(emqx_map_lib:config_key_path(), update_request(), -spec update(emqx_map_lib:config_key_path(), update_request(),
emqx_config_handler:update_opts()) -> ok | {error, term()}. emqx_config_handler:update_opts()) ->
{ok, config(), raw_config()} | {error, term()}.
update([RootName | _] = KeyPath, UpdateReq, Opts) -> update([RootName | _] = KeyPath, UpdateReq, Opts) ->
emqx_config_handler:update_config(get_schema_mod(RootName), KeyPath, emqx_config_handler:update_config(get_schema_mod(RootName), KeyPath,
{{update, UpdateReq}, Opts}). {{update, UpdateReq}, Opts}).
-spec remove(emqx_map_lib:config_key_path()) -> ok | {error, term()}. -spec remove(emqx_map_lib:config_key_path()) -> {ok, config(), raw_config()} | {error, term()}.
remove(KeyPath) -> remove(KeyPath) ->
remove(KeyPath, #{}). remove(KeyPath, #{}).
@ -203,7 +204,7 @@ remove([RootName | _] = KeyPath, Opts) ->
emqx_config_handler:update_config(get_schema_mod(RootName), KeyPath, {remove, Opts}). emqx_config_handler:update_config(get_schema_mod(RootName), KeyPath, {remove, Opts}).
-spec reset(emqx_map_lib:config_key_path(), emqx_config_handler:update_opts()) -> -spec reset(emqx_map_lib:config_key_path(), emqx_config_handler:update_opts()) ->
ok | {error, term()}. {ok, config(), raw_config()} | {error, term()}.
reset([RootName | _] = KeyPath, Opts) -> reset([RootName | _] = KeyPath, Opts) ->
case get_default_value(KeyPath) of case get_default_value(KeyPath) of
{ok, Default} -> {ok, Default} ->
@ -275,12 +276,12 @@ init_load(SchemaMod, RawRichConf) when is_map(RawRichConf) ->
}, },
%% this call throws exception in case of check failure %% this call throws exception in case of check failure
{_AppEnvs, CheckedConf} = hocon_schema:map_translate(SchemaMod, RawRichConf, Opts), {_AppEnvs, CheckedConf} = hocon_schema:map_translate(SchemaMod, RawRichConf, Opts),
ok = save_schema_mod(SchemaMod), ok = save_schema_mod_and_names(SchemaMod),
ok = save_to_config_map(emqx_map_lib:unsafe_atom_key_map(normalize_conf(CheckedConf)), ok = save_to_config_map(emqx_map_lib:unsafe_atom_key_map(normalize_conf(CheckedConf)),
normalize_conf(hocon_schema:richmap_to_map(RawRichConf))). normalize_conf(hocon_schema:richmap_to_map(RawRichConf))).
normalize_conf(Conf) -> normalize_conf(Conf) ->
maps:with(get_root_names(), Conf). maps:with(get_root_names(bin), Conf).
-spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf} -spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf}
when AppEnvs :: app_envs(), CheckedConf :: config(). when AppEnvs :: app_envs(), CheckedConf :: config().
@ -296,7 +297,7 @@ check_config(SchemaMod, RawConf) ->
-spec fill_defaults(raw_config()) -> map(). -spec fill_defaults(raw_config()) -> map().
fill_defaults(RawConf) -> fill_defaults(RawConf) ->
RootNames = get_root_names(), RootNames = get_root_names(bin),
maps:fold(fun(Key, Conf, Acc) -> maps:fold(fun(Key, Conf, Acc) ->
SubMap = #{Key => Conf}, SubMap = #{Key => Conf},
WithDefaults = case lists:member(Key, RootNames) of WithDefaults = case lists:member(Key, RootNames) of
@ -309,21 +310,26 @@ fill_defaults(RawConf) ->
-spec fill_defaults(module(), raw_config()) -> map(). -spec fill_defaults(module(), raw_config()) -> map().
fill_defaults(SchemaMod, RawConf) -> fill_defaults(SchemaMod, RawConf) ->
hocon_schema:check_plain(SchemaMod, RawConf, hocon_schema:check_plain(SchemaMod, RawConf,
#{nullable => true, no_conversion => true}, [str(K) || K <- maps:keys(RawConf)]). #{nullable => true, no_conversion => true}, root_names_from_conf(RawConf)).
-spec read_override_conf() -> raw_config(). -spec read_override_conf() -> raw_config().
read_override_conf() -> read_override_conf() ->
load_hocon_file(emqx_override_conf_name(), map). load_hocon_file(emqx_override_conf_name(), map).
-spec save_schema_mod(module()) -> ok. -spec save_schema_mod_and_names(module()) -> ok.
save_schema_mod(SchemaMod) -> save_schema_mod_and_names(SchemaMod) ->
RootNames = SchemaMod:structs(),
OldMods = get_schema_mod(), OldMods = get_schema_mod(),
NewMods = maps:from_list([{bin(RootName), SchemaMod} || RootName <- SchemaMod:structs()]), OldNames = get_root_names(),
persistent_term:put(?PERSIS_MOD_ROOTNAMES, maps:merge(OldMods, NewMods)). NewMods = maps:from_list([{bin(Name), SchemaMod} || Name <- RootNames]),
persistent_term:put(?PERSIS_SCHEMA_MODS, #{
mods => maps:merge(OldMods, NewMods),
names => lists:usort(OldNames ++ RootNames)
}).
-spec get_schema_mod() -> #{binary() => atom()}. -spec get_schema_mod() -> #{binary() => atom()}.
get_schema_mod() -> get_schema_mod() ->
persistent_term:get(?PERSIS_MOD_ROOTNAMES, #{}). maps:get(mods, persistent_term:get(?PERSIS_SCHEMA_MODS, #{mods => #{}})).
-spec get_schema_mod(atom() | binary()) -> module(). -spec get_schema_mod(atom() | binary()) -> module().
get_schema_mod(RootName) -> get_schema_mod(RootName) ->
@ -331,6 +337,9 @@ get_schema_mod(RootName) ->
-spec get_root_names() -> [binary()]. -spec get_root_names() -> [binary()].
get_root_names() -> get_root_names() ->
maps:get(names, persistent_term:get(?PERSIS_SCHEMA_MODS, #{names => []})).
get_root_names(bin) ->
maps:keys(get_schema_mod()). maps:keys(get_schema_mod()).
-spec save_configs(app_envs(), config(), raw_config(), raw_config()) -> ok | {error, term()}. -spec save_configs(app_envs(), config(), raw_config(), raw_config()) -> ok | {error, term()}.
@ -420,6 +429,17 @@ do_deep_put(?CONF, KeyPath, Map, Value) ->
do_deep_put(?RAW_CONF, KeyPath, Map, Value) -> do_deep_put(?RAW_CONF, KeyPath, Map, Value) ->
emqx_map_lib:deep_put([bin(Key) || Key <- KeyPath], Map, Value). emqx_map_lib:deep_put([bin(Key) || Key <- KeyPath], Map, Value).
root_names_from_conf(RawConf) ->
Keys = maps:keys(RawConf),
StrNames = [str(K) || K <- Keys],
AtomNames = lists:foldl(fun(K, Acc) ->
try [atom(K) | Acc]
catch error:badarg -> Acc
end
end, [], Keys),
PossibleNames = StrNames ++ AtomNames,
[Name || Name <- get_root_names(), lists:member(Name, PossibleNames)].
atom(Bin) when is_binary(Bin) -> atom(Bin) when is_binary(Bin) ->
binary_to_existing_atom(Bin, latin1); binary_to_existing_atom(Bin, latin1);
atom(Str) when is_list(Str) -> atom(Str) when is_list(Str) ->

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([]),
emqx_config:update([alarm], #{ {ok, _, _} = emqx_config:update([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([]),
emqx_config:update([alarm], #{ {ok, _, _} = emqx_config:update([alarm], #{
<<"validity_period">> => <<"1s">> <<"validity_period">> => <<"1s">>
}), }),
Config; Config;

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_config:update([zones, default, authorization, cache, enable], false), {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true), {ok, _, _} = emqx_config:update([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_config:update([zones, default, authorization, cache, enable], false), {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true), {ok, _, _} = emqx_config:update([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

@ -34,8 +34,8 @@ init_per_suite(Config) ->
ok = emqx_ct_helpers:start_apps([emqx_authz]), ok = emqx_ct_helpers:start_apps([emqx_authz]),
ok = emqx_config:update([zones, default, authorization, cache, enable], false), {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true), {ok, _, _} = emqx_config:update([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{ Rules = [#{ <<"config">> => #{
<<"url">> => <<"https://fake.com:443/">>, <<"url">> => <<"https://fake.com:443/">>,
<<"headers">> => #{}, <<"headers">> => #{},
@ -45,11 +45,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) ->
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_config:update([zones, default, authorization, cache, enable], false), {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true), {ok, _, _} = emqx_config:update([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) ->
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_config:update([zones, default, authorization, cache, enable], false), {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true), {ok, _, _} = emqx_config:update([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">> }],
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_config:update([zones, default, authorization, cache, enable], false), {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true), {ok, _, _} = emqx_config:update([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">> }],
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_config:update([zones, default, authorization, cache, enable], false), {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true), {ok, _, _} = emqx_config:update([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">> }],
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

@ -89,6 +89,9 @@ config_reset_api() ->
- For a config entry that has no default value, an error 400 will be returned">>, - For a config entry that has no default value, an error 400 will be returned">>,
parameters => ?PARAM_CONF_PATH, parameters => ?PARAM_CONF_PATH,
responses => #{ responses => #{
%% We only return "200" rather than the new configs that has been changed, as
%% the schema of the changed configs is depends on the request parameter
%% `conf_path`, it cannot be defined here.
<<"200">> => emqx_mgmt_util:response_schema(<<"Reset configs successfully">>), <<"200">> => emqx_mgmt_util:response_schema(<<"Reset configs successfully">>),
<<"400">> => emqx_mgmt_util:response_error_schema( <<"400">> => emqx_mgmt_util:response_error_schema(
<<"It's not able to reset the config">>, ['INVALID_OPERATION']) <<"It's not able to reset the config">>, ['INVALID_OPERATION'])
@ -110,14 +113,15 @@ config(get, Req) ->
config(put, Req) -> config(put, Req) ->
Path = conf_path(Req), Path = conf_path(Req),
ok = emqx_config:update(Path, http_body(Req)), {ok, _, RawConf} = emqx_config:update(Path, http_body(Req),
{200, emqx_map_lib:deep_get(Path, get_full_config())}. #{rawconf_with_defaults => true}),
{200, emqx_map_lib:deep_get(Path, emqx_map_lib:jsonable_map(RawConf))}.
config_reset(post, Req) -> 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_config:reset(Path) of case emqx_config:reset(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_config:update([prometheus], Params), {ok, _, _} = emqx_config:update([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_config:update([statsd], Params), {ok, _, _} = emqx_config:update([statsd], Params),
enable_statsd(Enable). enable_statsd(Enable).
enable_statsd(true) -> enable_statsd(true) ->