fix(api): update config APIs for emqx core

This commit is contained in:
Shawn 2021-08-02 19:26:02 +08:00
parent c9911a3b5f
commit c745120453
1 changed files with 65 additions and 73 deletions

View File

@ -20,93 +20,85 @@
-export([api_spec/0]). -export([api_spec/0]).
-export([brokers/2]). -export([ config/2
]).
-define(CONFIG_NAMES, [log, rpc, broker, zones, sysmon, alarm]).
-define(PARAM_CONF_PATH, [#{
name => conf_path,
in => query,
description => <<"The config path in jq syntax">>,
required => false,
schema => #{type => string, default => <<".">>}
}]).
api_spec() -> api_spec() ->
{[config_apis()], config_schemas()}. {config_apis(), config_schemas()}.
config_schema() -> [ config_schemas() ->
#{ [#{RootName => #{type => object}} %% TODO: generate this from hocon schema
broker => #{ || RootName <- ?CONFIG_NAMES].
type => object,
}
}
|| RootKey <- ].
config_apis() -> config_apis() ->
[config_api(RootName) || RootName <- ?CONFIG_NAMES].
config_api(RootName) when is_atom(RootName) ->
RootNameStr = atom_to_list(RootName),
Descr = fun(Prefix) -> list_to_binary(Prefix ++ " " ++ RootNameStr) end,
Metadata = #{ Metadata = #{
get => #{ get => #{
description => <<"EMQ X configs">>, description => Descr("Get configs for"),
parameters => [#{ parameters => ?PARAM_CONF_PATH,
name => activated,
in => query,
description => <<"All configs, if not specified">>,
required => false,
schema => #{type => boolean, default => true}
}],
responses => #{ responses => #{
<<"200">> => <<"200">> => emqx_mgmt_util:response_schema(<<"The config value for log">>,
emqx_mgmt_util:response_array_schema(<<"List all configs">>, config)}}, RootName)
}
},
delete => #{ delete => #{
description => <<"Remove all deactivated configs">>, description => Descr("Remove configs for"),
parameters => ?PARAM_CONF_PATH,
responses => #{ responses => #{
<<"200">> => <<"200">> => emqx_mgmt_util:response_schema(<<"Remove configs successfully">>),
emqx_mgmt_util:response_schema(<<"Remove all deactivated configs ok">>)}}}, <<"400">> => emqx_mgmt_util:response_error_schema(
{"/configs", Metadata, configs}. <<"It's not able to remove the config">>, ['INVALID_OPERATION'])
}
},
put => #{
description => Descr("Update configs for"),
'requestBody' => emqx_mgmt_util:request_body_schema(RootName),
responses => #{
<<"200">> => emqx_mgmt_util:response_schema(<<"Update configs successfully">>,
RootName),
<<"400">> => emqx_mgmt_util:response_error_schema(
<<"Update configs failed">>, ['UPDATE_FAILED'])
}
}
},
{"/configs/" ++ RootNameStr, Metadata, config}.
%%%============================================================================================== %%%==============================================================================================
%% parameters trans %% parameters trans
configs(get, Request) -> config(get, Req) ->
case proplists:get_value(<<"activated">>, cowboy_req:parse_qs(Request), undefined) of %% TODO: query the config specified by the query string param 'conf_path'
undefined -> Conf = emqx_config:get_raw([root_name_from_path(Req)]),
list(#{activated => undefined}); {200, Conf};
<<"true">> ->
list(#{activated => true});
<<"false">> ->
list(#{activated => false})
end;
configs(delete, _Request) -> config(delete, Req) ->
delete(). %% TODO: remove the config specified by the query string param 'conf_path'
emqx_config:remove([binary_to_existing_atom(root_name_from_path(Req), latin1)]),
{200};
%%%============================================================================================== config(put, Req) ->
%% api apply RootName = root_name_from_path(Req),
list(#{activated := true}) -> AtomRootName = binary_to_existing_atom(RootName, latin1),
do_list(activated); ok = emqx_config:update([AtomRootName], http_body(Req)),
list(#{activated := false}) -> {200, emqx_config:get_raw([RootName])}.
do_list(deactivated);
list(#{activated := undefined}) ->
do_list(activated).
delete() -> root_name_from_path(Req) ->
_ = emqx_mgmt:delete_all_deactivated_configs(), <<"/api/v5/configs/", RootName/binary>> = cowboy_req:path(Req),
{200}. RootName.
%%%============================================================================================== http_body(Req) ->
%% internal {ok, Body, _} = cowboy_req:read_body(Req),
do_list(Type) -> Body.
{Table, Function} =
case Type of
activated ->
{?ACTIVATED_ALARM, query_activated};
deactivated ->
{?DEACTIVATED_ALARM, query_deactivated}
end,
Response = emqx_mgmt_api:cluster_query([], {Table, []}, {?MODULE, Function}),
{200, Response}.
query_activated(_, Start, Limit) ->
query(?ACTIVATED_ALARM, Start, Limit).
query_deactivated(_, Start, Limit) ->
query(?DEACTIVATED_ALARM, Start, Limit).
query(Table, Start, Limit) ->
Ms = [{'$1',[],['$1']}],
emqx_mgmt_api:select_table(Table, Ms, Start, Limit, fun format_config/1).
format_config(Alarms) when is_list(Alarms) ->
[emqx_config:format(Alarm) || Alarm <- Alarms];
format_config(Alarm) ->
emqx_config:format(Alarm).