fix: bpapi spec type

This commit is contained in:
JimMoen 2024-05-22 03:05:31 +08:00
parent 5abd23af5a
commit 14f2a68799
No known key found for this signature in database
3 changed files with 35 additions and 23 deletions

View File

@ -180,6 +180,9 @@ schema("/plugins/:name/config") ->
responses => #{ responses => #{
%% avro data, json encoded %% avro data, json encoded
200 => hoconsc:mk(binary()), 200 => hoconsc:mk(binary()),
400 => emqx_dashboard_swagger:error_codes(
['BAD_CONFIG'], <<"Plugin Config Not Found">>
),
404 => emqx_dashboard_swagger:error_codes(['NOT_FOUND'], <<"Plugin Not Found">>) 404 => emqx_dashboard_swagger:error_codes(['NOT_FOUND'], <<"Plugin Not Found">>)
} }
}, },
@ -490,13 +493,13 @@ update_plugin(put, #{bindings := #{name := Name, action := Action}}) ->
plugin_config(get, #{bindings := #{name := NameVsn}}) -> plugin_config(get, #{bindings := #{name := NameVsn}}) ->
case emqx_plugins:describe(NameVsn) of case emqx_plugins:describe(NameVsn) of
{ok, _} -> {ok, _} ->
case emqx_plugins:get_config(NameVsn) of case emqx_plugins:get_config(NameVsn, ?CONFIG_FORMAT_MAP, ?plugin_conf_not_found) of
{ok, AvroJson} -> {ok, AvroJson} when is_map(AvroJson) ->
{200, #{<<"content-type">> => <<"'application/json'">>}, AvroJson}; {200, #{<<"content-type">> => <<"'application/json'">>}, AvroJson};
{error, _} -> {ok, ?plugin_conf_not_found} ->
{400, #{ {400, #{
code => 'BAD_CONFIG', code => 'BAD_CONFIG',
message => <<"Failed to get plugin config">> message => <<"Plugin Config Not Found">>
}} }}
end; end;
_ -> _ ->

View File

@ -305,30 +305,36 @@ ensure_stopped(NameVsn) ->
end end
). ).
get_config(Name, Vsn, Options, Default) -> get_config(Name, Vsn, Opt, Default) ->
get_config(make_name_vsn_string(Name, Vsn), Options, Default). get_config(make_name_vsn_string(Name, Vsn), Opt, Default).
-spec get_config(name_vsn()) -> -spec get_config(name_vsn()) ->
{ok, plugin_config()} {ok, plugin_config() | any()}
| {error, term()}. | {error, term()}.
get_config(NameVsn) -> get_config(NameVsn) ->
get_config(NameVsn, #{format => ?CONFIG_FORMAT_MAP}). get_config(NameVsn, ?CONFIG_FORMAT_MAP, #{}).
-spec get_config(name_vsn(), Options :: map()) -> -spec get_config(name_vsn(), ?CONFIG_FORMAT_MAP | ?CONFIG_FORMAT_AVRO) ->
{ok, avro_binary() | plugin_config()} {ok, avro_binary() | plugin_config() | any()}
| {error, term()}. | {error, term()}.
get_config(NameVsn, #{format := ?CONFIG_FORMAT_AVRO}) -> get_config(NameVsn, ?CONFIG_FORMAT_MAP) ->
get_config(NameVsn, ?CONFIG_FORMAT_MAP, #{});
get_config(NameVsn, ?CONFIG_FORMAT_AVRO) ->
get_config_bin(NameVsn).
%% Present default config value only in map format.
-spec get_config(name_vsn(), ?CONFIG_FORMAT_MAP, any()) ->
{ok, plugin_config() | any()}
| {error, term()}.
get_config(NameVsn, ?CONFIG_FORMAT_MAP, Default) ->
{ok, persistent_term:get(?PLUGIN_PERSIS_CONFIG_KEY(bin(NameVsn)), Default)}.
get_config_bin(NameVsn) ->
%% no default value when get raw binary config %% no default value when get raw binary config
case read_plugin_avro(NameVsn) of case read_plugin_avro(NameVsn) of
{ok, _AvroJson} = Res -> Res; {ok, _AvroJson} = Res -> Res;
{error, _Reason} = Err -> Err {error, _Reason} = Err -> Err
end; end.
get_config(NameVsn, #{format := ?CONFIG_FORMAT_MAP} = Options) ->
get_config(NameVsn, Options, #{}).
%% Present default config value only in map format.
get_config(NameVsn, #{format := ?CONFIG_FORMAT_MAP}, Default) ->
{ok, persistent_term:get(?PLUGIN_PERSIS_CONFIG_KEY(bin(NameVsn)), Default)}.
%% @doc Update plugin's config. %% @doc Update plugin's config.
%% RPC call from Management API or CLI. %% RPC call from Management API or CLI.
@ -770,7 +776,7 @@ get_avro_config_from_any_node([], _NameVsn, Errors) ->
get_avro_config_from_any_node([Node | T], NameVsn, Errors) -> get_avro_config_from_any_node([Node | T], NameVsn, Errors) ->
case case
emqx_plugins_proto_v2:get_config( emqx_plugins_proto_v2:get_config(
Node, NameVsn, #{format => ?CONFIG_FORMAT_MAP}, ?plugin_conf_not_found, 5_000 Node, NameVsn, ?CONFIG_FORMAT_MAP, ?plugin_conf_not_found, 5_000
) )
of of
{ok, _} = Res -> {ok, _} = Res ->

View File

@ -24,6 +24,7 @@
get_config/5 get_config/5
]). ]).
-include("emqx_plugins.hrl").
-include_lib("emqx/include/bpapi.hrl"). -include_lib("emqx/include/bpapi.hrl").
-type name_vsn() :: binary() | string(). -type name_vsn() :: binary() | string().
@ -31,10 +32,12 @@
introduced_in() -> introduced_in() ->
"5.7.0". "5.7.0".
-spec get_tar(node(), name_vsn(), timeout()) -> {ok, binary()} | {error, any}. -spec get_tar(node(), name_vsn(), timeout()) -> {ok, binary()} | {error, any()}.
get_tar(Node, NameVsn, Timeout) -> get_tar(Node, NameVsn, Timeout) ->
rpc:call(Node, emqx_plugins, get_tar, [NameVsn], Timeout). rpc:call(Node, emqx_plugins, get_tar, [NameVsn], Timeout).
-spec get_config(node(), name_vsn(), map(), any(), timeout()) -> {ok, any()} | {error, any()}. -spec get_config(
get_config(Node, NameVsn, Opts, Default, Timeout) -> node(), name_vsn(), ?CONFIG_FORMAT_MAP, any(), timeout()
rpc:call(Node, emqx_plugins, get_config, [NameVsn, Opts, Default], Timeout). ) -> {ok, map() | any()} | {error, any()}.
get_config(Node, NameVsn, Opt, Default, Timeout) ->
rpc:call(Node, emqx_plugins, get_config, [NameVsn, Opt, Default], Timeout).