Explorar el Código

feat: add license setting get/put api

zhongwencool hace 2 años
padre
commit
f03d4d090e

+ 14 - 1
lib-ee/emqx_license/src/emqx_license.erl

@@ -22,7 +22,8 @@
     unload/0,
     unload/0,
     read_license/0,
     read_license/0,
     read_license/1,
     read_license/1,
-    update_key/1
+    update_key/1,
+    update_setting/1
 ]).
 ]).
 
 
 -define(CONF_KEY_PATH, [license]).
 -define(CONF_KEY_PATH, [license]).
@@ -64,6 +65,14 @@ update_key(Value) when is_binary(Value); is_list(Value) ->
     ),
     ),
     handle_config_update_result(Result).
     handle_config_update_result(Result).
 
 
+update_setting(Setting) when is_map(Setting) ->
+    Result = emqx_conf:update(
+        ?CONF_KEY_PATH,
+        {setting, Setting},
+        #{rawconf_with_defaults => true, override_to => cluster}
+    ),
+    handle_config_update_result(Result).
+
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
 %% emqx_hooks
 %% emqx_hooks
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
@@ -96,6 +105,8 @@ check(_ConnInfo, AckProps) ->
 pre_config_update(_, Cmd, Conf) ->
 pre_config_update(_, Cmd, Conf) ->
     {ok, do_update(Cmd, Conf)}.
     {ok, do_update(Cmd, Conf)}.
 
 
+post_config_update(_Path, {setting, _}, NewConf, _Old, _AppEnvs) ->
+    {ok, NewConf};
 post_config_update(_Path, _Cmd, NewConf, _Old, _AppEnvs) ->
 post_config_update(_Path, _Cmd, NewConf, _Old, _AppEnvs) ->
     case read_license(NewConf) of
     case read_license(NewConf) of
         {ok, License} ->
         {ok, License} ->
@@ -122,6 +133,8 @@ do_update({key, Content}, Conf) when is_binary(Content); is_list(Content) ->
         {error, Reason} ->
         {error, Reason} ->
             erlang:throw(Reason)
             erlang:throw(Reason)
     end;
     end;
+do_update({setting, Setting}, Conf) ->
+    maps:merge(Conf, Setting);
 do_update(NewConf, _PrevConf) ->
 do_update(NewConf, _PrevConf) ->
     #{<<"key">> := NewKey} = NewConf,
     #{<<"key">> := NewKey} = NewConf,
     do_update({key, NewKey}, NewConf).
     do_update({key, NewKey}, NewConf).

+ 68 - 9
lib-ee/emqx_license/src/emqx_license_http_api.erl

@@ -13,11 +13,14 @@
     namespace/0,
     namespace/0,
     api_spec/0,
     api_spec/0,
     paths/0,
     paths/0,
-    schema/1
+    schema/1,
+    fields/1
 ]).
 ]).
+-define(LICENSE_TAGS, [<<"License">>]).
 
 
 -export([
 -export([
-    '/license'/2
+    '/license'/2,
+    '/license/setting'/2
 ]).
 ]).
 
 
 -define(BAD_REQUEST, 'BAD_REQUEST').
 -define(BAD_REQUEST, 'BAD_REQUEST').
@@ -29,14 +32,15 @@ api_spec() ->
 
 
 paths() ->
 paths() ->
     [
     [
-        "/license"
+        "/license",
+        "/license/setting"
     ].
     ].
 
 
 schema("/license") ->
 schema("/license") ->
     #{
     #{
         'operationId' => '/license',
         'operationId' => '/license',
         get => #{
         get => #{
-            tags => [<<"license">>],
+            tags => ?LICENSE_TAGS,
             summary => <<"Get license info">>,
             summary => <<"Get license info">>,
             description => ?DESC("desc_license_info_api"),
             description => ?DESC("desc_license_info_api"),
             responses => #{
             responses => #{
@@ -51,18 +55,16 @@ schema("/license") ->
             }
             }
         },
         },
         post => #{
         post => #{
-            tags => [<<"license">>],
+            tags => ?LICENSE_TAGS,
             summary => <<"Update license key">>,
             summary => <<"Update license key">>,
             description => ?DESC("desc_license_key_api"),
             description => ?DESC("desc_license_key_api"),
             'requestBody' => emqx_dashboard_swagger:schema_with_examples(
             'requestBody' => emqx_dashboard_swagger:schema_with_examples(
-                emqx_license_schema:key_license(),
+                hoconsc:ref(?MODULE, key_license),
                 #{
                 #{
                     license_key => #{
                     license_key => #{
                         summary => <<"License key string">>,
                         summary => <<"License key string">>,
                         value => #{
                         value => #{
-                            <<"key">> => <<"xxx">>,
-                            <<"connection_low_watermark">> => "75%",
-                            <<"connection_high_watermark">> => "80%"
+                            <<"key">> => <<"xxx">>
                         }
                         }
                     }
                     }
                 }
                 }
@@ -79,6 +81,28 @@ schema("/license") ->
                 400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad license key">>)
                 400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad license key">>)
             }
             }
         }
         }
+    };
+schema("/license/setting") ->
+    #{
+        'operationId' => '/license/setting',
+        get => #{
+            tags => ?LICENSE_TAGS,
+            summary => <<"Get license setting">>,
+            description => ?DESC("desc_license_setting_api"),
+            responses => #{
+                200 => setting()
+            }
+        },
+        post => #{
+            tags => ?LICENSE_TAGS,
+            summary => <<"Update license setting">>,
+            description => ?DESC("desc_license_setting_api"),
+            'requestBody' => setting(),
+            responses => #{
+                200 => setting(),
+                400 => emqx_dashboard_swagger:error_codes([?BAD_REQUEST], <<"Bad setting value">>)
+            }
+        }
     }.
     }.
 
 
 sample_license_info_response() ->
 sample_license_info_response() ->
@@ -117,3 +141,38 @@ error_msg(Code, Msg) ->
     end;
     end;
 '/license'(post, _Params) ->
 '/license'(post, _Params) ->
     {400, error_msg(?BAD_REQUEST, <<"Invalid request params">>)}.
     {400, error_msg(?BAD_REQUEST, <<"Invalid request params">>)}.
+
+'/license/setting'(get, _Params) ->
+    {200, maps:remove(<<"key">>, emqx_config:get_raw([license]))};
+'/license/setting'(post, #{body := Setting}) ->
+    case emqx_license:update_setting(Setting) of
+        {error, Error} ->
+            ?SLOG(error, #{
+                msg => "bad_license_setting",
+                reason => Error
+            }),
+            {400, error_msg(?BAD_REQUEST, <<"Bad license setting">>)};
+        {ok, _} ->
+            ?SLOG(info, #{msg => "updated_license_setting"}),
+            '/license/setting'(get, undefined)
+    end.
+
+fields(key_license) ->
+    Key = lists:keyfind(key, 1, emqx_license_schema:fields(key_license)),
+    [
+        Key,
+        %% FIXME: remove when 5.2.0
+        {connection_low_watermark, #{
+            type => emqx_schema:percent(),
+            deprecated => true,
+            desc => ?DESC(connection_low_watermark_field_deprecated)
+        }},
+        {connection_high_watermark, #{
+            type => emqx_schema:percent(),
+            deprecated => true,
+            desc => ?DESC(connection_high_watermark_field_deprecated)
+        }}
+    ].
+
+setting() ->
+    lists:keydelete(key, 1, emqx_license_schema:fields(key_license)).

+ 2 - 6
lib-ee/emqx_license/src/emqx_license_schema.erl

@@ -16,15 +16,14 @@
 -export([roots/0, fields/1, validations/0, desc/1, tags/0]).
 -export([roots/0, fields/1, validations/0, desc/1, tags/0]).
 
 
 -export([
 -export([
-    default_license/0,
-    key_license/0
+    default_license/0
 ]).
 ]).
 
 
 roots() ->
 roots() ->
     [
     [
         {license,
         {license,
             hoconsc:mk(
             hoconsc:mk(
-                key_license(),
+                hoconsc:ref(?MODULE, key_license),
                 #{
                 #{
                     desc => ?DESC(license_root)
                     desc => ?DESC(license_root)
                 }
                 }
@@ -64,9 +63,6 @@ desc(_) ->
 validations() ->
 validations() ->
     [{check_license_watermark, fun check_license_watermark/1}].
     [{check_license_watermark, fun check_license_watermark/1}].
 
 
-key_license() ->
-    hoconsc:ref(?MODULE, key_license).
-
 check_license_watermark(Conf) ->
 check_license_watermark(Conf) ->
     case hocon_maps:get("license.connection_low_watermark", Conf) of
     case hocon_maps:get("license.connection_low_watermark", Conf) of
         undefined ->
         undefined ->

+ 6 - 0
rel/i18n/emqx_license_http_api.hocon

@@ -12,4 +12,10 @@ desc_license_key_api.desc:
 desc_license_key_api.label:
 desc_license_key_api.label:
 """Update license"""
 """Update license"""
 
 
+desc_license_setting_api.desc:
+"""Update license setting"""
+
+desc_license_setting_api.label:
+"""Update license setting"""
+
 }
 }

+ 14 - 2
rel/i18n/emqx_license_schema.hocon

@@ -12,6 +12,18 @@ connection_low_watermark_field.desc:
 connection_low_watermark_field.label:
 connection_low_watermark_field.label:
 """Connection low watermark"""
 """Connection low watermark"""
 
 
+connection_high_watermark_field_deprecated.desc:
+"""deprecated use /license/setting instead"""
+
+connection_high_watermark_field_deprecated.label:
+"""deprecated use /license/setting instead"""
+
+connection_low_watermark_field_deprecated.desc:
+"""deprecated use /license/setting instead"""
+
+connection_low_watermark_field_deprecated.label:
+"""deprecated use /license/setting instead"""
+
 key_field.desc:
 key_field.desc:
 """License string"""
 """License string"""
 
 
@@ -19,12 +31,12 @@ key_field.label:
 """License string"""
 """License string"""
 
 
 license_root.desc:
 license_root.desc:
-"""Defines the EMQX Enterprise license. 
+"""Defines the EMQX Enterprise license.
 
 
 
 
 The default license has 100 connections limit, it is issued on 2023-01-09 and valid for 5 years (1825 days).
 The default license has 100 connections limit, it is issued on 2023-01-09 and valid for 5 years (1825 days).
 
 
-EMQX comes with a default trial license.  For production use, please 
+EMQX comes with a default trial license.  For production use, please
 visit https://www.emqx.com/apply-licenses/emqx to apply."""
 visit https://www.emqx.com/apply-licenses/emqx to apply."""
 
 
 license_root.label:
 license_root.label: