diff --git a/apps/emqx_management/src/emqx_mgmt_api_plugins.erl b/apps/emqx_management/src/emqx_mgmt_api_plugins.erl index 0a575befd..d308cb42c 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_plugins.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_plugins.erl @@ -200,7 +200,7 @@ schema("/plugins/:name/config") -> responses => #{ 204 => <<"Config updated successfully">>, 400 => emqx_dashboard_swagger:error_codes( - ['UNEXPECTED_ERROR'], <<"Update plugin config failed">> + ['BAD_CONFIG', 'UNEXPECTED_ERROR'], <<"Update plugin config failed">> ), 404 => emqx_dashboard_swagger:error_codes(['NOT_FOUND'], <<"Plugin Not Found">>) } @@ -485,31 +485,41 @@ update_plugin(put, #{bindings := #{name := Name, action := Action}}) -> Res = emqx_mgmt_api_plugins_proto_v2:ensure_action(Name, Action), return(204, Res). -plugin_config(get, #{bindings := #{name := Name}}) -> - case emqx_plugins:get_plugin_config(Name, #{format => ?CONFIG_FORMAT_MAP}) of - {ok, AvroJson} -> - {200, #{<<"content-type">> => <<"'application/json'">>}, AvroJson}; - {error, _} -> - {400, #{ - code => 'BAD_CONFIG', - message => <<"Failed to get plugin config">> - }} +plugin_config(get, #{bindings := #{name := NameVsn}}) -> + case emqx_plugins:describe(NameVsn) of + {ok, _} -> + case emqx_plugins:get_plugin_config(NameVsn, #{format => ?CONFIG_FORMAT_MAP}) of + {ok, AvroJson} -> + {200, #{<<"content-type">> => <<"'application/json'">>}, AvroJson}; + {error, _} -> + {400, #{ + code => 'BAD_CONFIG', + message => <<"Failed to get plugin config">> + }} + end; + _ -> + {404, plugin_not_found_msg()} end; -plugin_config(put, #{bindings := #{name := Name}, body := AvroJsonMap}) -> - AvroJsonBin = emqx_utils_json:encode(AvroJsonMap), - case emqx_plugins:decode_plugin_avro_config(Name, AvroJsonBin) of - {ok, AvroValueConfig} -> - Nodes = emqx:running_nodes(), - %% cluster call with config in map (binary key-value) - _Res = emqx_mgmt_api_plugins_proto_v3:update_plugin_config( - Nodes, Name, AvroJsonMap, AvroValueConfig - ), - {204}; - {error, Reason} -> - {400, #{ - code => 'BAD_CONFIG', - message => readable_error_msg(Reason) - }} +plugin_config(put, #{bindings := #{name := NameVsn}, body := AvroJsonMap}) -> + case emqx_plugins:describe(NameVsn) of + {ok, _} -> + AvroJsonBin = emqx_utils_json:encode(AvroJsonMap), + case emqx_plugins:decode_plugin_avro_config(NameVsn, AvroJsonBin) of + {ok, AvroValueConfig} -> + Nodes = emqx:running_nodes(), + %% cluster call with config in map (binary key-value) + _Res = emqx_mgmt_api_plugins_proto_v3:update_plugin_config( + Nodes, NameVsn, AvroJsonMap, AvroValueConfig + ), + {204}; + {error, Reason} -> + {400, #{ + code => 'BAD_CONFIG', + message => readable_error_msg(Reason) + }} + end; + _ -> + {404, plugin_not_found_msg()} end. plugin_schema(get, #{bindings := #{name := NameVsn}}) -> @@ -517,10 +527,7 @@ plugin_schema(get, #{bindings := #{name := NameVsn}}) -> {ok, _Plugin} -> {200, format_plugin_avsc_and_i18n(NameVsn)}; _ -> - {404, #{ - code => 'NOT_FOUND', - message => <<"Plugin Not Found">> - }} + {404, plugin_not_found_msg()} end. update_boot_order(post, #{bindings := #{name := Name}, body := Body}) -> @@ -610,6 +617,12 @@ return(_, {error, #{error_msg := "bad_avro_config_file", reason := {enoent, _} = return(_, {error, Reason}) -> {400, #{code => 'PARAM_ERROR', message => readable_error_msg(Reason)}}. +plugin_not_found_msg() -> + #{ + code => 'NOT_FOUND', + message => <<"Plugin Not Found">> + }. + readable_error_msg(Msg) -> emqx_utils:readable_error_msg(Msg). diff --git a/apps/emqx_management/src/proto/emqx_mgmt_api_plugins_proto_v3.erl b/apps/emqx_management/src/proto/emqx_mgmt_api_plugins_proto_v3.erl index b428a38cc..641d35f70 100644 --- a/apps/emqx_management/src/proto/emqx_mgmt_api_plugins_proto_v3.erl +++ b/apps/emqx_management/src/proto/emqx_mgmt_api_plugins_proto_v3.erl @@ -59,11 +59,11 @@ ensure_action(Name, Action) -> map() ) -> emqx_rpc:multicall_result(). -update_plugin_config(Nodes, Name, AvroJsonMap, PluginConfig) -> +update_plugin_config(Nodes, NameVsn, AvroJsonMap, PluginConfig) -> rpc:multicall( Nodes, emqx_mgmt_api_plugins, do_update_plugin_config, - [Name, AvroJsonMap, PluginConfig], + [NameVsn, AvroJsonMap, PluginConfig], 10000 ).