refactor(gw): use emqx_gateway_conf to update conf

This commit is contained in:
JianBo He 2021-09-18 14:36:53 +08:00
parent b55a1f62c3
commit f0ac62c513
6 changed files with 38 additions and 32 deletions

View File

@ -62,13 +62,13 @@ gateway(get, Request) ->
gateway(post, Request) -> gateway(post, Request) ->
Body = maps:get(body, Request, #{}), Body = maps:get(body, Request, #{}),
try try
Name0 = maps:get(<<"name">>, Request), Name0 = maps:get(<<"name">>, Body),
GwName = binary_to_existing_atom(Name0), GwName = binary_to_existing_atom(Name0),
case emqx_gateway_registry:lookup(GwName) of case emqx_gateway_registry:lookup(GwName) of
undefined -> error(badarg); undefined -> error(badarg);
_ -> _ ->
GwConf = maps:without([<<"name">>], Body), GwConf = maps:without([<<"name">>], Body),
case emqx_gateway:update_rawconf(Name0, GwConf) of case emqx_gateway_conf:load_gateway(GwName, GwConf) of
ok -> ok ->
{204}; {204};
{error, Reason} -> {error, Reason} ->
@ -97,9 +97,9 @@ gateway_insta(get, #{bindings := #{name := Name0}}) ->
gateway_insta(put, #{body := GwConf0, gateway_insta(put, #{body := GwConf0,
bindings := #{name := Name0} bindings := #{name := Name0}
}) -> }) ->
with_gateway(Name0, fun(_, _) -> with_gateway(Name0, fun(GwName, _) ->
GwConf = maps:without([<<"authentication">>, <<"listeners">>], GwConf0), GwConf = maps:without([<<"authentication">>, <<"listeners">>], GwConf0),
case emqx_gateway:update_rawconf(Name0, GwConf) of case emqx_gateway_conf:update_gateway(GwName, GwConf) of
ok -> ok ->
{200}; {200};
{error, Reason} -> {error, Reason} ->

View File

@ -72,8 +72,7 @@ authn(put, #{bindings := #{name := Name0},
authn(post, #{bindings := #{name := Name0}, authn(post, #{bindings := #{name := Name0},
body := Body}) -> body := Body}) ->
with_gateway(Name0, fun(GwName, _) -> with_gateway(Name0, fun(GwName, _) ->
%% Exitence checking? case emqx_gateway_http:add_authn(GwName, Body) of
case emqx_gateway_http:update_authn(GwName, Body) of
ok -> {204}; ok -> {204};
{error, Reason} -> {error, Reason} ->
return_http_error(500, Reason) return_http_error(500, Reason)

View File

@ -70,8 +70,7 @@ listeners(post, #{bindings := #{name := Name0}, body := LConf}) ->
undefined -> undefined ->
ListenerId = emqx_gateway_utils:listener_id( ListenerId = emqx_gateway_utils:listener_id(
GwName, Type, LName), GwName, Type, LName),
case emqx_gateway_http:update_listener( case emqx_gateway_http:add_listener(ListenerId, LConf) of
ListenerId, LConf) of
ok -> ok ->
{204}; {204};
{error, Reason} -> {error, Reason} ->

View File

@ -115,6 +115,7 @@ update(Req) ->
res(emqx:update_config([gateway], Req)). res(emqx:update_config([gateway], Req)).
res({ok, _Result}) -> ok; res({ok, _Result}) -> ok;
res({error, {pre_config_update,emqx_gateway_conf,Reason}}) -> {error, Reason};
res({error, Reason}) -> {error, Reason}. res({error, Reason}) -> {error, Reason}.
bin({LType, LName}) -> bin({LType, LName}) ->

View File

@ -27,12 +27,14 @@
%% Mgmt APIs - listeners %% Mgmt APIs - listeners
-export([ listeners/1 -export([ listeners/1
, listener/1 , listener/1
, add_listener/2
, remove_listener/1 , remove_listener/1
, update_listener/2 , update_listener/2
, mapping_listener_m2l/2 , mapping_listener_m2l/2
]). ]).
-export([ authn/1 -export([ authn/1
, add_authn/2
, update_authn/2 , update_authn/2
, remove_authn/1 , remove_authn/1
]). ]).
@ -203,47 +205,47 @@ bind2str(LConf = #{bind := Bind}) when is_binary(Bind) ->
bind2str(LConf = #{<<"bind">> := Bind}) when is_binary(Bind) -> bind2str(LConf = #{<<"bind">> := Bind}) when is_binary(Bind) ->
LConf. LConf.
-spec remove_listener(binary()) -> ok | {error, not_found} | {error, any()}. -spec add_listener(atom() | binary(), map()) -> ok | {error, any()}.
remove_listener(ListenerId) -> add_listener(ListenerId, NewConf0) ->
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId), {GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
LConf = emqx:get_raw_config( NewConf = maps:without([<<"id">>, <<"name">>,
[<<"gateway">>, GwName, <<"listeners">>, Type] <<"type">>, <<"running">>], NewConf0),
), emqx_gateway_conf:add_listener(GwName, {Type, Name}, NewConf).
NLConf = maps:remove(Name, LConf),
emqx_gateway:update_rawconf(
GwName,
#{<<"listeners">> => #{Type => NLConf}}
).
-spec update_listener(atom() | binary(), map()) -> ok | {error, any()}. -spec update_listener(atom() | binary(), map()) -> ok | {error, any()}.
update_listener(ListenerId, NewConf0) -> update_listener(ListenerId, NewConf0) ->
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId), {GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
NewConf = maps:without([<<"id">>, <<"name">>, NewConf = maps:without([<<"id">>, <<"name">>,
<<"type">>, <<"running">>], NewConf0), <<"type">>, <<"running">>], NewConf0),
emqx_gateway:update_rawconf( emqx_gateway_conf:update_listener(GwName, {Type, Name}, NewConf).
GwName,
#{<<"listeners">> => #{Type => #{Name => NewConf}} -spec remove_listener(binary()) -> ok | {error, not_found} | {error, any()}.
}). remove_listener(ListenerId) ->
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
emqx_gateway_conf:remove_listener(GwName, {Type, Name}).
-spec authn(gateway_name()) -> map() | undefined. -spec authn(gateway_name()) -> map() | undefined.
authn(GwName) -> authn(GwName) ->
case emqx_map_lib:deep_get( case emqx_map_lib:deep_get(
authentication, [authentication],
emqx:get_config([gateway, GwName]), emqx:get_config([gateway, GwName]),
undefined) of undefined) of
undefined -> undefined; undefined -> undefined;
AuthConf -> emqx_map_lib:jsonable_map(AuthConf) AuthConf -> emqx_map_lib:jsonable_map(AuthConf)
end. end.
-spec add_authn(gateway_name(), map()) -> ok | {error, any()}.
add_authn(GwName, AuthConf) ->
emqx_gateway_conf:add_authn(GwName, AuthConf).
-spec update_authn(gateway_name(), map()) -> ok | {error, any()}. -spec update_authn(gateway_name(), map()) -> ok | {error, any()}.
update_authn(GwName, AuthConf) -> update_authn(GwName, AuthConf) ->
emqx_gateway:update_rawconf( emqx_gateway_conf:update_authn(GwName, AuthConf).
atom_to_binary(GwName),
#{authentication => AuthConf}).
-spec remove_authn(gateway_name()) -> ok | {error, any()}. -spec remove_authn(gateway_name()) -> ok | {error, any()}.
remove_authn(_GwName) -> remove_authn(GwName) ->
{error, not_supported_now}. emqx_gateway_conf:remove_authn(GwName).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Mgmt APIs - clients %% Mgmt APIs - clients

View File

@ -117,13 +117,18 @@ format_listenon({Addr, Port}) when is_tuple(Addr) ->
parse_listenon(Port) when is_integer(Port) -> parse_listenon(Port) when is_integer(Port) ->
Port; Port;
parse_listenon(IpPort) when is_tuple(IpPort) ->
IpPort;
parse_listenon(Str) when is_binary(Str) -> parse_listenon(Str) when is_binary(Str) ->
parse_listenon(binary_to_list(Str)); parse_listenon(binary_to_list(Str));
parse_listenon(Str) when is_list(Str) -> parse_listenon(Str) when is_list(Str) ->
case emqx_schema:to_ip_port(Str) of try list_to_integer(Str)
{ok, R} -> R; catch _ : _ ->
{error, _} -> case emqx_schema:to_ip_port(Str) of
error({invalid_listenon_name, Str}) {ok, R} -> R;
{error, _} ->
error({invalid_listenon_name, Str})
end
end. end.
listener_id(GwName, Type, LisName) -> listener_id(GwName, Type, LisName) ->