feat(limiter): add limiter view/update api
This commit is contained in:
parent
8ff552c9cf
commit
f2e7563b0c
|
@ -29,7 +29,8 @@
|
||||||
to_initial/1,
|
to_initial/1,
|
||||||
namespace/0,
|
namespace/0,
|
||||||
get_bucket_cfg_path/2,
|
get_bucket_cfg_path/2,
|
||||||
desc/1
|
desc/1,
|
||||||
|
types/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-define(KILOBYTE, 1024).
|
-define(KILOBYTE, 1024).
|
||||||
|
@ -187,6 +188,9 @@ to_rate(Str) ->
|
||||||
get_bucket_cfg_path(Type, BucketName) ->
|
get_bucket_cfg_path(Type, BucketName) ->
|
||||||
[limiter, Type, bucket, BucketName].
|
[limiter, Type, bucket, BucketName].
|
||||||
|
|
||||||
|
types() ->
|
||||||
|
[bytes_in, message_in, connection, message_routing, batch].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
config/3,
|
config/3,
|
||||||
|
limiter/3,
|
||||||
config_reset/3,
|
config_reset/3,
|
||||||
configs/3,
|
configs/3,
|
||||||
get_full_config/0,
|
get_full_config/0,
|
||||||
|
@ -71,7 +72,12 @@ api_spec() ->
|
||||||
namespace() -> "configuration".
|
namespace() -> "configuration".
|
||||||
|
|
||||||
paths() ->
|
paths() ->
|
||||||
["/configs", "/configs_reset/:rootname", "/configs/global_zone"] ++
|
[
|
||||||
|
"/configs",
|
||||||
|
"/configs_reset/:rootname",
|
||||||
|
"/configs/global_zone",
|
||||||
|
"/configs/limiter/:limiter_type"
|
||||||
|
] ++
|
||||||
lists:map(fun({Name, _Type}) -> ?PREFIX ++ binary_to_list(Name) end, config_list()).
|
lists:map(fun({Name, _Type}) -> ?PREFIX ++ binary_to_list(Name) end, config_list()).
|
||||||
|
|
||||||
schema("/configs") ->
|
schema("/configs") ->
|
||||||
|
@ -156,6 +162,42 @@ schema("/configs/global_zone") ->
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
schema("/configs/limiter/:limiter_type") ->
|
||||||
|
Schema = hoconsc:ref(emqx_limiter_schema, limiter_opts),
|
||||||
|
Parameters = [
|
||||||
|
{limiter_type,
|
||||||
|
hoconsc:mk(
|
||||||
|
hoconsc:enum(emqx_limiter_schema:types()),
|
||||||
|
#{
|
||||||
|
in => query,
|
||||||
|
required => true,
|
||||||
|
example => <<"bytes_in">>,
|
||||||
|
desc => <<"The limiter type">>
|
||||||
|
}
|
||||||
|
)}
|
||||||
|
],
|
||||||
|
#{
|
||||||
|
'operationId' => limiter,
|
||||||
|
get => #{
|
||||||
|
tags => [conf],
|
||||||
|
description => <<"Get config of this limiter">>,
|
||||||
|
parameters => Parameters,
|
||||||
|
responses => #{
|
||||||
|
200 => Schema,
|
||||||
|
404 => emqx_dashboard_swagger:error_codes(['NOT_FOUND'], <<"config not found">>)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
put => #{
|
||||||
|
tags => [conf],
|
||||||
|
description => <<"Update config of this limiter">>,
|
||||||
|
parameters => Parameters,
|
||||||
|
'requestBody' => Schema,
|
||||||
|
responses => #{
|
||||||
|
200 => Schema,
|
||||||
|
400 => emqx_dashboard_swagger:error_codes(['UPDATE_FAILED'])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
schema(Path) ->
|
schema(Path) ->
|
||||||
{RootKey, {_Root, Schema}} = find_schema(Path),
|
{RootKey, {_Root, Schema}} = find_schema(Path),
|
||||||
#{
|
#{
|
||||||
|
@ -201,18 +243,13 @@ fields(Field) ->
|
||||||
|
|
||||||
%%%==============================================================================================
|
%%%==============================================================================================
|
||||||
%% HTTP API Callbacks
|
%% HTTP API Callbacks
|
||||||
config(get, _Params, Req) ->
|
config(Method, Params, Req) ->
|
||||||
Path = conf_path(Req),
|
Path = conf_path(Req),
|
||||||
{ok, Conf} = emqx_map_lib:deep_find(Path, get_full_config()),
|
do_config(Method, Params, Path).
|
||||||
{200, Conf};
|
|
||||||
config(put, #{body := Body}, Req) ->
|
limiter(Method, #{query_string := QS} = Params, _Req) ->
|
||||||
Path = conf_path(Req),
|
#{<<"limiter_type">> := Type} = QS,
|
||||||
case emqx_conf:update(Path, Body, ?OPTS) of
|
do_config(Method, Params, [<<"limiter">>, erlang:atom_to_binary(Type)]).
|
||||||
{ok, #{raw_config := RawConf}} ->
|
|
||||||
{200, RawConf};
|
|
||||||
{error, Reason} ->
|
|
||||||
{400, #{code => 'UPDATE_FAILED', message => ?ERR_MSG(Reason)}}
|
|
||||||
end.
|
|
||||||
|
|
||||||
global_zone_configs(get, _Params, _Req) ->
|
global_zone_configs(get, _Params, _Req) ->
|
||||||
Paths = global_zone_roots(),
|
Paths = global_zone_roots(),
|
||||||
|
@ -340,3 +377,14 @@ global_zone_roots() ->
|
||||||
global_zone_schema() ->
|
global_zone_schema() ->
|
||||||
Roots = hocon_schema:roots(emqx_zone_schema),
|
Roots = hocon_schema:roots(emqx_zone_schema),
|
||||||
lists:map(fun({RootKey, {_Root, Schema}}) -> {RootKey, Schema} end, Roots).
|
lists:map(fun({RootKey, {_Root, Schema}}) -> {RootKey, Schema} end, Roots).
|
||||||
|
|
||||||
|
do_config(get, _Params, Path) ->
|
||||||
|
{ok, Conf} = emqx_map_lib:deep_find(Path, get_full_config()),
|
||||||
|
{200, Conf};
|
||||||
|
do_config(put, #{body := Body}, Path) ->
|
||||||
|
case emqx_conf:update(Path, Body, ?OPTS) of
|
||||||
|
{ok, #{raw_config := RawConf}} ->
|
||||||
|
{200, RawConf};
|
||||||
|
{error, Reason} ->
|
||||||
|
{400, #{code => 'UPDATE_FAILED', message => ?ERR_MSG(Reason)}}
|
||||||
|
end.
|
||||||
|
|
Loading…
Reference in New Issue