refactor(gw): use emqx_gateway_conf to update conf
This commit is contained in:
parent
b55a1f62c3
commit
f0ac62c513
|
@ -62,13 +62,13 @@ gateway(get, Request) ->
|
|||
gateway(post, Request) ->
|
||||
Body = maps:get(body, Request, #{}),
|
||||
try
|
||||
Name0 = maps:get(<<"name">>, Request),
|
||||
Name0 = maps:get(<<"name">>, Body),
|
||||
GwName = binary_to_existing_atom(Name0),
|
||||
case emqx_gateway_registry:lookup(GwName) of
|
||||
undefined -> error(badarg);
|
||||
_ ->
|
||||
GwConf = maps:without([<<"name">>], Body),
|
||||
case emqx_gateway:update_rawconf(Name0, GwConf) of
|
||||
case emqx_gateway_conf:load_gateway(GwName, GwConf) of
|
||||
ok ->
|
||||
{204};
|
||||
{error, Reason} ->
|
||||
|
@ -97,9 +97,9 @@ gateway_insta(get, #{bindings := #{name := Name0}}) ->
|
|||
gateway_insta(put, #{body := GwConf0,
|
||||
bindings := #{name := Name0}
|
||||
}) ->
|
||||
with_gateway(Name0, fun(_, _) ->
|
||||
with_gateway(Name0, fun(GwName, _) ->
|
||||
GwConf = maps:without([<<"authentication">>, <<"listeners">>], GwConf0),
|
||||
case emqx_gateway:update_rawconf(Name0, GwConf) of
|
||||
case emqx_gateway_conf:update_gateway(GwName, GwConf) of
|
||||
ok ->
|
||||
{200};
|
||||
{error, Reason} ->
|
||||
|
|
|
@ -72,8 +72,7 @@ authn(put, #{bindings := #{name := Name0},
|
|||
authn(post, #{bindings := #{name := Name0},
|
||||
body := Body}) ->
|
||||
with_gateway(Name0, fun(GwName, _) ->
|
||||
%% Exitence checking?
|
||||
case emqx_gateway_http:update_authn(GwName, Body) of
|
||||
case emqx_gateway_http:add_authn(GwName, Body) of
|
||||
ok -> {204};
|
||||
{error, Reason} ->
|
||||
return_http_error(500, Reason)
|
||||
|
|
|
@ -70,8 +70,7 @@ listeners(post, #{bindings := #{name := Name0}, body := LConf}) ->
|
|||
undefined ->
|
||||
ListenerId = emqx_gateway_utils:listener_id(
|
||||
GwName, Type, LName),
|
||||
case emqx_gateway_http:update_listener(
|
||||
ListenerId, LConf) of
|
||||
case emqx_gateway_http:add_listener(ListenerId, LConf) of
|
||||
ok ->
|
||||
{204};
|
||||
{error, Reason} ->
|
||||
|
|
|
@ -115,6 +115,7 @@ update(Req) ->
|
|||
res(emqx:update_config([gateway], Req)).
|
||||
|
||||
res({ok, _Result}) -> ok;
|
||||
res({error, {pre_config_update,emqx_gateway_conf,Reason}}) -> {error, Reason};
|
||||
res({error, Reason}) -> {error, Reason}.
|
||||
|
||||
bin({LType, LName}) ->
|
||||
|
|
|
@ -27,12 +27,14 @@
|
|||
%% Mgmt APIs - listeners
|
||||
-export([ listeners/1
|
||||
, listener/1
|
||||
, add_listener/2
|
||||
, remove_listener/1
|
||||
, update_listener/2
|
||||
, mapping_listener_m2l/2
|
||||
]).
|
||||
|
||||
-export([ authn/1
|
||||
, add_authn/2
|
||||
, update_authn/2
|
||||
, remove_authn/1
|
||||
]).
|
||||
|
@ -203,47 +205,47 @@ bind2str(LConf = #{bind := Bind}) when is_binary(Bind) ->
|
|||
bind2str(LConf = #{<<"bind">> := Bind}) when is_binary(Bind) ->
|
||||
LConf.
|
||||
|
||||
-spec remove_listener(binary()) -> ok | {error, not_found} | {error, any()}.
|
||||
remove_listener(ListenerId) ->
|
||||
-spec add_listener(atom() | binary(), map()) -> ok | {error, any()}.
|
||||
add_listener(ListenerId, NewConf0) ->
|
||||
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
||||
LConf = emqx:get_raw_config(
|
||||
[<<"gateway">>, GwName, <<"listeners">>, Type]
|
||||
),
|
||||
NLConf = maps:remove(Name, LConf),
|
||||
emqx_gateway:update_rawconf(
|
||||
GwName,
|
||||
#{<<"listeners">> => #{Type => NLConf}}
|
||||
).
|
||||
NewConf = maps:without([<<"id">>, <<"name">>,
|
||||
<<"type">>, <<"running">>], NewConf0),
|
||||
emqx_gateway_conf:add_listener(GwName, {Type, Name}, NewConf).
|
||||
|
||||
-spec update_listener(atom() | binary(), map()) -> ok | {error, any()}.
|
||||
update_listener(ListenerId, NewConf0) ->
|
||||
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
||||
|
||||
NewConf = maps:without([<<"id">>, <<"name">>,
|
||||
<<"type">>, <<"running">>], NewConf0),
|
||||
emqx_gateway:update_rawconf(
|
||||
GwName,
|
||||
#{<<"listeners">> => #{Type => #{Name => NewConf}}
|
||||
}).
|
||||
emqx_gateway_conf:update_listener(GwName, {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.
|
||||
authn(GwName) ->
|
||||
case emqx_map_lib:deep_get(
|
||||
authentication,
|
||||
[authentication],
|
||||
emqx:get_config([gateway, GwName]),
|
||||
undefined) of
|
||||
undefined -> undefined;
|
||||
AuthConf -> emqx_map_lib:jsonable_map(AuthConf)
|
||||
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()}.
|
||||
update_authn(GwName, AuthConf) ->
|
||||
emqx_gateway:update_rawconf(
|
||||
atom_to_binary(GwName),
|
||||
#{authentication => AuthConf}).
|
||||
emqx_gateway_conf:update_authn(GwName, AuthConf).
|
||||
|
||||
-spec remove_authn(gateway_name()) -> ok | {error, any()}.
|
||||
remove_authn(_GwName) ->
|
||||
{error, not_supported_now}.
|
||||
remove_authn(GwName) ->
|
||||
emqx_gateway_conf:remove_authn(GwName).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% Mgmt APIs - clients
|
||||
|
|
|
@ -117,13 +117,18 @@ format_listenon({Addr, Port}) when is_tuple(Addr) ->
|
|||
|
||||
parse_listenon(Port) when is_integer(Port) ->
|
||||
Port;
|
||||
parse_listenon(IpPort) when is_tuple(IpPort) ->
|
||||
IpPort;
|
||||
parse_listenon(Str) when is_binary(Str) ->
|
||||
parse_listenon(binary_to_list(Str));
|
||||
parse_listenon(Str) when is_list(Str) ->
|
||||
try list_to_integer(Str)
|
||||
catch _ : _ ->
|
||||
case emqx_schema:to_ip_port(Str) of
|
||||
{ok, R} -> R;
|
||||
{error, _} ->
|
||||
error({invalid_listenon_name, Str})
|
||||
end
|
||||
end.
|
||||
|
||||
listener_id(GwName, Type, LisName) ->
|
||||
|
|
Loading…
Reference in New Issue