diff --git a/apps/emqx_management/src/emqx_mgmt_api_plugins.erl b/apps/emqx_management/src/emqx_mgmt_api_plugins.erl index fdfbd864a..ca0d3ac10 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_plugins.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_plugins.erl @@ -180,6 +180,9 @@ schema("/plugins/:name/config") -> responses => #{ %% avro data, json encoded 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">>) } }, @@ -490,13 +493,13 @@ update_plugin(put, #{bindings := #{name := Name, action := Action}}) -> plugin_config(get, #{bindings := #{name := NameVsn}}) -> case emqx_plugins:describe(NameVsn) of {ok, _} -> - case emqx_plugins:get_config(NameVsn) of - {ok, AvroJson} -> + case emqx_plugins:get_config(NameVsn, ?CONFIG_FORMAT_MAP, ?plugin_conf_not_found) of + {ok, AvroJson} when is_map(AvroJson) -> {200, #{<<"content-type">> => <<"'application/json'">>}, AvroJson}; - {error, _} -> + {ok, ?plugin_conf_not_found} -> {400, #{ code => 'BAD_CONFIG', - message => <<"Failed to get plugin config">> + message => <<"Plugin Config Not Found">> }} end; _ -> diff --git a/apps/emqx_plugins/src/emqx_plugins.erl b/apps/emqx_plugins/src/emqx_plugins.erl index da5bca312..11ef113b0 100644 --- a/apps/emqx_plugins/src/emqx_plugins.erl +++ b/apps/emqx_plugins/src/emqx_plugins.erl @@ -305,30 +305,36 @@ ensure_stopped(NameVsn) -> end ). -get_config(Name, Vsn, Options, Default) -> - get_config(make_name_vsn_string(Name, Vsn), Options, Default). +get_config(Name, Vsn, Opt, Default) -> + get_config(make_name_vsn_string(Name, Vsn), Opt, Default). -spec get_config(name_vsn()) -> - {ok, plugin_config()} + {ok, plugin_config() | any()} | {error, term()}. get_config(NameVsn) -> - get_config(NameVsn, #{format => ?CONFIG_FORMAT_MAP}). + get_config(NameVsn, ?CONFIG_FORMAT_MAP, #{}). --spec get_config(name_vsn(), Options :: map()) -> - {ok, avro_binary() | plugin_config()} +-spec get_config(name_vsn(), ?CONFIG_FORMAT_MAP | ?CONFIG_FORMAT_AVRO) -> + {ok, avro_binary() | plugin_config() | any()} | {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 case read_plugin_avro(NameVsn) of {ok, _AvroJson} = Res -> Res; {error, _Reason} = Err -> Err - 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)}. + end. %% @doc Update plugin's config. %% 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) -> case 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 {ok, _} = Res -> diff --git a/apps/emqx_plugins/src/proto/emqx_plugins_proto_v2.erl b/apps/emqx_plugins/src/proto/emqx_plugins_proto_v2.erl index 68b8090a3..0e933d351 100644 --- a/apps/emqx_plugins/src/proto/emqx_plugins_proto_v2.erl +++ b/apps/emqx_plugins/src/proto/emqx_plugins_proto_v2.erl @@ -24,6 +24,7 @@ get_config/5 ]). +-include("emqx_plugins.hrl"). -include_lib("emqx/include/bpapi.hrl"). -type name_vsn() :: binary() | string(). @@ -31,10 +32,12 @@ introduced_in() -> "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) -> rpc:call(Node, emqx_plugins, get_tar, [NameVsn], Timeout). --spec get_config(node(), name_vsn(), map(), any(), timeout()) -> {ok, any()} | {error, any()}. -get_config(Node, NameVsn, Opts, Default, Timeout) -> - rpc:call(Node, emqx_plugins, get_config, [NameVsn, Opts, Default], Timeout). +-spec get_config( + node(), name_vsn(), ?CONFIG_FORMAT_MAP, any(), timeout() +) -> {ok, map() | any()} | {error, any()}. +get_config(Node, NameVsn, Opt, Default, Timeout) -> + rpc:call(Node, emqx_plugins, get_config, [NameVsn, Opt, Default], Timeout).