fix(emqx_data_bridge): correct the format of HTTP responses

This commit is contained in:
Shawn 2021-06-07 19:52:08 +08:00
parent 4914b003ac
commit e9fe345ac8
4 changed files with 83 additions and 15 deletions

View File

@ -2,7 +2,10 @@
-export([ load_bridges/0
, resource_type/1
, resource_id/1
, name_to_resource_id/1
, resource_id_to_name/1
, list_bridges/0
, is_bridge/1
]).
load_bridges() ->
@ -12,5 +15,16 @@ load_bridges() ->
resource_type(<<"mysql">>) -> emqx_connector_mysql.
resource_id(BridgeName) ->
name_to_resource_id(BridgeName) ->
<<"bridge:", BridgeName/binary>>.
resource_id_to_name(<<"bridge:", BridgeName/binary>> = _ResourceId) ->
BridgeName.
list_bridges() ->
emqx_resource_api:list_instances(fun emqx_data_bridge:is_bridge/1).
is_bridge(#{id := <<"bridge:", _/binary>>}) ->
true;
is_bridge(_Data) ->
false.

View File

@ -43,12 +43,13 @@
]).
list_bridges(_Binding, _Params) ->
{200, #{code => 0, data => emqx_resource_api:list_instances(fun is_bridge/1)}}.
{200, #{code => 0, data => [format_api_reply(Data) ||
Data <- emqx_data_bridge:list_bridges()]}}.
get_bridge(#{name := Name}, _Params) ->
case emqx_resource:get_instance(emqx_data_bridge:resource_id(Name)) of
case emqx_resource:get_instance(emqx_data_bridge:name_to_resource_id(Name)) of
{ok, Data} ->
{200, #{code => 0, data => emqx_resource_api:format_data(Data)}};
{200, #{code => 0, data => format_api_reply(emqx_resource_api:format_data(Data))}};
{error, not_found} ->
{404, #{code => 102, message => <<"not_found: ", Name/binary>>}}
end.
@ -57,10 +58,10 @@ create_bridge(#{name := Name}, Params) ->
Config = proplists:get_value(<<"config">>, Params),
BridgeType = proplists:get_value(<<"type">>, Params),
case emqx_resource:check_and_create(
emqx_data_bridge:resource_id(Name),
emqx_data_bridge:name_to_resource_id(Name),
emqx_data_bridge:resource_type(BridgeType), Config) of
{ok, Data} ->
{200, #{code => 0, data => emqx_resource_api:format_data(Data)}};
{200, #{code => 0, data => format_api_reply(emqx_resource_api:format_data(Data))}};
{error, already_created} ->
{400, #{code => 102, message => <<"bridge already created: ", Name/binary>>}};
{error, Reason0} ->
@ -73,10 +74,10 @@ update_bridge(#{name := Name}, Params) ->
Config = proplists:get_value(<<"config">>, Params),
BridgeType = proplists:get_value(<<"type">>, Params),
case emqx_resource:check_and_update(
emqx_data_bridge:resource_id(Name),
emqx_data_bridge:name_to_resource_id(Name),
emqx_data_bridge:resource_type(BridgeType), Config, []) of
{ok, Data} ->
{200, #{code => 0, data => emqx_resource_api:format_data(Data)}};
{200, #{code => 0, data => format_api_reply(emqx_resource_api:format_data(Data))}};
{error, not_found} ->
{400, #{code => 102, message => <<"bridge not_found: ", Name/binary>>}};
{error, Reason0} ->
@ -86,13 +87,12 @@ update_bridge(#{name := Name}, Params) ->
end.
delete_bridge(#{name := Name}, _Params) ->
case emqx_resource:remove(emqx_data_bridge:resource_id(Name)) of
case emqx_resource:remove(emqx_data_bridge:name_to_resource_id(Name)) of
ok -> {200, #{code => 0, data => #{}}};
{error, Reason} ->
{500, #{code => 102, message => emqx_resource_api:stringnify(Reason)}}
end.
is_bridge(#{id := <<"bridge:", _/binary>>}) ->
true;
is_bridge(_Data) ->
false.
format_api_reply(#{resource_type := Type, id := Id, config := Conf, status := Status}) ->
#{type => Type, name => emqx_data_bridge:resource_id_to_name(Id),
config => Conf, status => Status}.

View File

@ -0,0 +1,54 @@
-module(emqx_data_bridge_config_handler).
-behaviour(gen_server).
%% API functions
-export([ start_link/0
, notify_updated/0
]).
%% gen_server callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
-record(state, {}).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
notify_updated() ->
gen_server:cast(?MODULE, updated).
init([]) ->
{ok, #state{}}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(updated, State) ->
Configs = [format_conf(Data) || Data <- emqx_data_bridge:list_bridges()],
emqx_config_handler ! {emqx_data_bridge, Configs},
{noreply, State};
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%============================================================================
format_conf(#{resource_type := Type, id := Id, config := Conf}) ->
#{type => Type, name => emqx_data_bridge:resource_id_to_name(Id),
config => Conf}.

View File

@ -55,7 +55,7 @@ load_bridges(Configs) ->
load_bridge(#{<<"name">> := Name, <<"type">> := Type,
<<"config">> := Config}) ->
case emqx_resource:check_and_create_local(
emqx_data_bridge:resource_id(Name),
emqx_data_bridge:name_to_resource_id(Name),
emqx_data_bridge:resource_type(Type), Config) of
{ok, _} -> ok;
{error, already_created} -> ok;