chore(gw): http-api for loading gateway
This commit is contained in:
parent
92c02c0c8b
commit
aa03023811
|
@ -48,6 +48,7 @@ apis() ->
|
||||||
, {"/gateway/:name", gateway_insta}
|
, {"/gateway/:name", gateway_insta}
|
||||||
, {"/gateway/:name/stats", gateway_insta_stats}
|
, {"/gateway/:name/stats", gateway_insta_stats}
|
||||||
].
|
].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% http handlers
|
%% http handlers
|
||||||
|
|
||||||
|
@ -57,7 +58,29 @@ gateway(get, Request) ->
|
||||||
undefined -> all;
|
undefined -> all;
|
||||||
S0 -> binary_to_existing_atom(S0, utf8)
|
S0 -> binary_to_existing_atom(S0, utf8)
|
||||||
end,
|
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}}) ->
|
gateway_insta(delete, #{bindings := #{name := Name0}}) ->
|
||||||
with_gateway(Name0, fun(GwName, _) ->
|
with_gateway(Name0, fun(GwName, _) ->
|
||||||
|
@ -69,7 +92,7 @@ gateway_insta(get, #{bindings := #{name := Name0}}) ->
|
||||||
GwConf = filled_raw_confs([<<"gateway">>, Name0]),
|
GwConf = filled_raw_confs([<<"gateway">>, Name0]),
|
||||||
LisConf = maps:get(<<"listeners">>, GwConf, #{}),
|
LisConf = maps:get(<<"listeners">>, GwConf, #{}),
|
||||||
NLisConf = emqx_gateway_http:mapping_listener_m2l(Name0, LisConf),
|
NLisConf = emqx_gateway_http:mapping_listener_m2l(Name0, LisConf),
|
||||||
{200, GwConf#{<<"listeners">> => NLisConf}}
|
{200, GwConf#{<<"name">> => Name0, <<"listeners">> => NLisConf}}
|
||||||
end);
|
end);
|
||||||
gateway_insta(put, #{body := GwConf0,
|
gateway_insta(put, #{body := GwConf0,
|
||||||
bindings := #{name := Name0}
|
bindings := #{name := Name0}
|
||||||
|
@ -79,8 +102,6 @@ gateway_insta(put, #{body := GwConf0,
|
||||||
case emqx_gateway:update_rawconf(Name0, GwConf) of
|
case emqx_gateway:update_rawconf(Name0, GwConf) of
|
||||||
ok ->
|
ok ->
|
||||||
{200};
|
{200};
|
||||||
{error, not_found} ->
|
|
||||||
return_http_error(404, "Gateway not found");
|
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
return_http_error(500, Reason)
|
return_http_error(500, Reason)
|
||||||
end
|
end
|
||||||
|
@ -122,6 +143,16 @@ swagger("/gateway", get) ->
|
||||||
, responses =>
|
, responses =>
|
||||||
#{ <<"200">> => schema_gateway_overview_list() }
|
#{ <<"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) ->
|
swagger("/gateway/:name", get) ->
|
||||||
#{ description => <<"Get the gateway configurations">>
|
#{ description => <<"Get the gateway configurations">>
|
||||||
, parameters => params_gateway_name_in_path()
|
, parameters => params_gateway_name_in_path()
|
||||||
|
@ -189,7 +220,7 @@ schema_gateway_overview_list() ->
|
||||||
#{ type => object
|
#{ type => object
|
||||||
, properties => properties_gateway_overview()
|
, properties => properties_gateway_overview()
|
||||||
},
|
},
|
||||||
<<"Gateway Overview list">>
|
<<"Gateway list">>
|
||||||
).
|
).
|
||||||
|
|
||||||
%% XXX: This is whole confs for all type gateways. It is used to fill the
|
%% 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">>,
|
<<"name">> => <<"authenticator1">>,
|
||||||
<<"server_type">> => <<"built-in-database">>,
|
<<"server_type">> => <<"built-in-database">>,
|
||||||
<<"user_id_type">> => <<"clientid">>},
|
<<"user_id_type">> => <<"clientid">>},
|
||||||
|
<<"name">> => <<"coap">>,
|
||||||
<<"enable">> => true,
|
<<"enable">> => true,
|
||||||
<<"enable_stats">> => true,<<"heartbeat">> => <<"30s">>,
|
<<"enable_stats">> => true,<<"heartbeat">> => <<"30s">>,
|
||||||
<<"idle_timeout">> => <<"30s">>,
|
<<"idle_timeout">> => <<"30s">>,
|
||||||
|
@ -219,6 +251,7 @@ schema_gateway_overview_list() ->
|
||||||
|
|
||||||
-define(EXPROTO_GATEWAY_CONFS,
|
-define(EXPROTO_GATEWAY_CONFS,
|
||||||
#{<<"enable">> => true,
|
#{<<"enable">> => true,
|
||||||
|
<<"name">> => <<"exproto">>,
|
||||||
<<"enable_stats">> => true,
|
<<"enable_stats">> => true,
|
||||||
<<"handler">> =>
|
<<"handler">> =>
|
||||||
#{<<"address">> => <<"http://127.0.0.1:9001">>},
|
#{<<"address">> => <<"http://127.0.0.1:9001">>},
|
||||||
|
@ -236,6 +269,7 @@ schema_gateway_overview_list() ->
|
||||||
|
|
||||||
-define(LWM2M_GATEWAY_CONFS,
|
-define(LWM2M_GATEWAY_CONFS,
|
||||||
#{<<"auto_observe">> => false,
|
#{<<"auto_observe">> => false,
|
||||||
|
<<"name">> => <<"lwm2m">>,
|
||||||
<<"enable">> => true,
|
<<"enable">> => true,
|
||||||
<<"enable_stats">> => true,
|
<<"enable_stats">> => true,
|
||||||
<<"idle_timeout">> => <<"30s">>,
|
<<"idle_timeout">> => <<"30s">>,
|
||||||
|
@ -264,6 +298,7 @@ schema_gateway_overview_list() ->
|
||||||
#{<<"password">> => <<"abc">>,
|
#{<<"password">> => <<"abc">>,
|
||||||
<<"username">> => <<"mqtt_sn_user">>},
|
<<"username">> => <<"mqtt_sn_user">>},
|
||||||
<<"enable">> => true,
|
<<"enable">> => true,
|
||||||
|
<<"name">> => <<"mqtt-sn">>,
|
||||||
<<"enable_qos3">> => true,<<"enable_stats">> => true,
|
<<"enable_qos3">> => true,<<"enable_stats">> => true,
|
||||||
<<"gateway_id">> => 1,<<"idle_timeout">> => <<"30s">>,
|
<<"gateway_id">> => 1,<<"idle_timeout">> => <<"30s">>,
|
||||||
<<"listeners">> => [
|
<<"listeners">> => [
|
||||||
|
@ -290,6 +325,7 @@ schema_gateway_overview_list() ->
|
||||||
#{<<"password">> => <<"${Packet.headers.passcode}">>,
|
#{<<"password">> => <<"${Packet.headers.passcode}">>,
|
||||||
<<"username">> => <<"${Packet.headers.login}">>},
|
<<"username">> => <<"${Packet.headers.login}">>},
|
||||||
<<"enable">> => true,
|
<<"enable">> => true,
|
||||||
|
<<"name">> => <<"stomp">>,
|
||||||
<<"enable_stats">> => true,
|
<<"enable_stats">> => true,
|
||||||
<<"frame">> =>
|
<<"frame">> =>
|
||||||
#{<<"max_body_length">> => 8192,<<"max_headers">> => 10,
|
#{<<"max_body_length">> => 8192,<<"max_headers">> => 10,
|
||||||
|
|
|
@ -81,6 +81,7 @@ listeners(post, #{bindings := #{name := Name0}, body := LConf}) ->
|
||||||
end
|
end
|
||||||
end).
|
end).
|
||||||
|
|
||||||
|
%% FIXME: not working
|
||||||
listeners_insta(delete, #{bindings := #{name := Name0, id := ListenerId0}}) ->
|
listeners_insta(delete, #{bindings := #{name := Name0, id := ListenerId0}}) ->
|
||||||
ListenerId = emqx_mgmt_util:urldecode(ListenerId0),
|
ListenerId = emqx_mgmt_util:urldecode(ListenerId0),
|
||||||
with_gateway(Name0, fun(_GwName, _) ->
|
with_gateway(Name0, fun(_GwName, _) ->
|
||||||
|
@ -301,7 +302,6 @@ raw_properties_common_listener() ->
|
||||||
<<"Listener type. Enum: tcp, udp, ssl, dtls">>,
|
<<"Listener type. Enum: tcp, udp, ssl, dtls">>,
|
||||||
[<<"tcp">>, <<"ssl">>, <<"udp">>, <<"dtls">>]}
|
[<<"tcp">>, <<"ssl">>, <<"udp">>, <<"dtls">>]}
|
||||||
, {running, boolean, <<"Listener running status">>}
|
, {running, boolean, <<"Listener running status">>}
|
||||||
%% FIXME:
|
|
||||||
, {bind, string, <<"Listener bind address or port">>}
|
, {bind, string, <<"Listener bind address or port">>}
|
||||||
, {acceptors, integer, <<"Listener acceptors number">>}
|
, {acceptors, integer, <<"Listener acceptors number">>}
|
||||||
, {access_rules, {array, string}, <<"Listener Access rules for client">>}
|
, {access_rules, {array, string}, <<"Listener Access rules for client">>}
|
||||||
|
|
|
@ -171,12 +171,13 @@ listener(GwName, Type, Conf) ->
|
||||||
[begin
|
[begin
|
||||||
ListenerId = emqx_gateway_utils:listener_id(GwName, Type, LName),
|
ListenerId = emqx_gateway_utils:listener_id(GwName, Type, LName),
|
||||||
Running = is_running(ListenerId, LConf),
|
Running = is_running(ListenerId, LConf),
|
||||||
|
bind2str(
|
||||||
LConf#{
|
LConf#{
|
||||||
id => ListenerId,
|
id => ListenerId,
|
||||||
type => Type,
|
type => Type,
|
||||||
name => LName,
|
name => LName,
|
||||||
running => Running
|
running => Running
|
||||||
}
|
})
|
||||||
end || {LName, LConf} <- Conf, is_map(LConf)].
|
end || {LName, LConf} <- Conf, is_map(LConf)].
|
||||||
|
|
||||||
is_running(ListenerId, #{<<"bind">> := ListenOn0}) ->
|
is_running(ListenerId, #{<<"bind">> := ListenOn0}) ->
|
||||||
|
@ -188,6 +189,15 @@ is_running(ListenerId, #{<<"bind">> := ListenOn0}) ->
|
||||||
false
|
false
|
||||||
end.
|
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()}.
|
-spec remove_listener(binary()) -> ok | {error, not_found} | {error, any()}.
|
||||||
remove_listener(ListenerId) ->
|
remove_listener(ListenerId) ->
|
||||||
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
||||||
|
|
Loading…
Reference in New Issue