diff --git a/apps/emqx_gateway/src/emqx_gateway_api.erl b/apps/emqx_gateway/src/emqx_gateway_api.erl index 9037518c5..9259ff3b6 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api.erl @@ -48,6 +48,7 @@ apis() -> , {"/gateway/:name", gateway_insta} , {"/gateway/:name/stats", gateway_insta_stats} ]. + %%-------------------------------------------------------------------- %% http handlers @@ -57,7 +58,29 @@ gateway(get, Request) -> undefined -> all; S0 -> binary_to_existing_atom(S0, utf8) end, - {200, emqx_gateway_http:gateways(Status)}. + {200, emqx_gateway_http:gateways(Status)}; +gateway(post, Request) -> + Body = maps:get(body, Request, #{}), + try + Name0 = maps:get(<<"name">>, Request), + 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 + ok -> + {204}; + {error, Reason} -> + return_http_error(500, Reason) + end + end + catch + error : {badkey, K} -> + return_http_error(400, [K, " is required"]); + error : badarg -> + return_http_error(404, "Bad gateway name") + end. gateway_insta(delete, #{bindings := #{name := Name0}}) -> with_gateway(Name0, fun(GwName, _) -> @@ -69,7 +92,7 @@ gateway_insta(get, #{bindings := #{name := Name0}}) -> GwConf = filled_raw_confs([<<"gateway">>, Name0]), LisConf = maps:get(<<"listeners">>, GwConf, #{}), NLisConf = emqx_gateway_http:mapping_listener_m2l(Name0, LisConf), - {200, GwConf#{<<"listeners">> => NLisConf}} + {200, GwConf#{<<"name">> => Name0, <<"listeners">> => NLisConf}} end); gateway_insta(put, #{body := GwConf0, bindings := #{name := Name0} @@ -79,8 +102,6 @@ gateway_insta(put, #{body := GwConf0, case emqx_gateway:update_rawconf(Name0, GwConf) of ok -> {200}; - {error, not_found} -> - return_http_error(404, "Gateway not found"); {error, Reason} -> return_http_error(500, Reason) end @@ -122,6 +143,16 @@ swagger("/gateway", get) -> , responses => #{ <<"200">> => schema_gateway_overview_list() } }; +swagger("/gateway", post) -> + #{ description => <<"Load a gateway">> + , requestBody => schema_gateway_conf() + , responses => + #{ <<"400">> => schema_bad_request() + , <<"404">> => schema_not_found() + , <<"500">> => schema_internal_error() + , <<"204">> => schema_no_content() + } + }; swagger("/gateway/:name", get) -> #{ description => <<"Get the gateway configurations">> , parameters => params_gateway_name_in_path() @@ -189,7 +220,7 @@ schema_gateway_overview_list() -> #{ type => object , properties => properties_gateway_overview() }, - <<"Gateway Overview list">> + <<"Gateway list">> ). %% XXX: This is whole confs for all type gateways. It is used to fill the @@ -202,6 +233,7 @@ schema_gateway_overview_list() -> <<"name">> => <<"authenticator1">>, <<"server_type">> => <<"built-in-database">>, <<"user_id_type">> => <<"clientid">>}, + <<"name">> => <<"coap">>, <<"enable">> => true, <<"enable_stats">> => true,<<"heartbeat">> => <<"30s">>, <<"idle_timeout">> => <<"30s">>, @@ -219,6 +251,7 @@ schema_gateway_overview_list() -> -define(EXPROTO_GATEWAY_CONFS, #{<<"enable">> => true, + <<"name">> => <<"exproto">>, <<"enable_stats">> => true, <<"handler">> => #{<<"address">> => <<"http://127.0.0.1:9001">>}, @@ -236,6 +269,7 @@ schema_gateway_overview_list() -> -define(LWM2M_GATEWAY_CONFS, #{<<"auto_observe">> => false, + <<"name">> => <<"lwm2m">>, <<"enable">> => true, <<"enable_stats">> => true, <<"idle_timeout">> => <<"30s">>, @@ -264,6 +298,7 @@ schema_gateway_overview_list() -> #{<<"password">> => <<"abc">>, <<"username">> => <<"mqtt_sn_user">>}, <<"enable">> => true, + <<"name">> => <<"mqtt-sn">>, <<"enable_qos3">> => true,<<"enable_stats">> => true, <<"gateway_id">> => 1,<<"idle_timeout">> => <<"30s">>, <<"listeners">> => [ @@ -290,6 +325,7 @@ schema_gateway_overview_list() -> #{<<"password">> => <<"${Packet.headers.passcode}">>, <<"username">> => <<"${Packet.headers.login}">>}, <<"enable">> => true, + <<"name">> => <<"stomp">>, <<"enable_stats">> => true, <<"frame">> => #{<<"max_body_length">> => 8192,<<"max_headers">> => 10, diff --git a/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl b/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl index 374f2841d..a3136e365 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl @@ -81,6 +81,7 @@ listeners(post, #{bindings := #{name := Name0}, body := LConf}) -> end end). +%% FIXME: not working listeners_insta(delete, #{bindings := #{name := Name0, id := ListenerId0}}) -> ListenerId = emqx_mgmt_util:urldecode(ListenerId0), with_gateway(Name0, fun(_GwName, _) -> @@ -301,7 +302,6 @@ raw_properties_common_listener() -> <<"Listener type. Enum: tcp, udp, ssl, dtls">>, [<<"tcp">>, <<"ssl">>, <<"udp">>, <<"dtls">>]} , {running, boolean, <<"Listener running status">>} - %% FIXME: , {bind, string, <<"Listener bind address or port">>} , {acceptors, integer, <<"Listener acceptors number">>} , {access_rules, {array, string}, <<"Listener Access rules for client">>} diff --git a/apps/emqx_gateway/src/emqx_gateway_http.erl b/apps/emqx_gateway/src/emqx_gateway_http.erl index ed8e511c7..ad690d065 100644 --- a/apps/emqx_gateway/src/emqx_gateway_http.erl +++ b/apps/emqx_gateway/src/emqx_gateway_http.erl @@ -171,12 +171,13 @@ listener(GwName, Type, Conf) -> [begin ListenerId = emqx_gateway_utils:listener_id(GwName, Type, LName), Running = is_running(ListenerId, LConf), - LConf#{ - id => ListenerId, - type => Type, - name => LName, - running => Running - } + bind2str( + LConf#{ + id => ListenerId, + type => Type, + name => LName, + running => Running + }) end || {LName, LConf} <- Conf, is_map(LConf)]. is_running(ListenerId, #{<<"bind">> := ListenOn0}) -> @@ -188,6 +189,15 @@ is_running(ListenerId, #{<<"bind">> := ListenOn0}) -> false end. +bind2str(LConf = #{bind := Bind}) when is_integer(Bind) -> + maps:put(bind, integer_to_binary(Bind), LConf); +bind2str(LConf = #{<<"bind">> := Bind}) when is_integer(Bind) -> + maps:put(<<"bind">>, integer_to_binary(Bind), LConf); +bind2str(LConf = #{bind := Bind}) when is_binary(Bind) -> + LConf; +bind2str(LConf = #{<<"bind">> := Bind}) when is_binary(Bind) -> + LConf. + -spec remove_listener(binary()) -> ok | {error, not_found} | {error, any()}. remove_listener(ListenerId) -> {GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),