refactor(authz_api): authz sources move api style
This commit is contained in:
parent
910a0b9a77
commit
68c473c7cc
|
@ -34,10 +34,10 @@
|
||||||
-define(CMD_APPEND, append).
|
-define(CMD_APPEND, append).
|
||||||
-define(CMD_MOVE, move).
|
-define(CMD_MOVE, move).
|
||||||
|
|
||||||
-define(CMD_MOVE_TOP, <<"top">>).
|
-define(CMD_MOVE_TOP, top).
|
||||||
-define(CMD_MOVE_BOTTOM, <<"bottom">>).
|
-define(CMD_MOVE_BOTTOM, bottom).
|
||||||
-define(CMD_MOVE_BEFORE(Before), {<<"before">>, Before}).
|
-define(CMD_MOVE_BEFORE(Before), {before, Before}).
|
||||||
-define(CMD_MOVE_AFTER(After), {<<"after">>, After}).
|
-define(CMD_MOVE_AFTER(After), {'after', After}).
|
||||||
|
|
||||||
-define(CONF_KEY_PATH, [authorization, sources]).
|
-define(CONF_KEY_PATH, [authorization, sources]).
|
||||||
|
|
||||||
|
|
|
@ -110,10 +110,10 @@ lookup(Type) ->
|
||||||
{Source, _Front, _Rear} = take(Type),
|
{Source, _Front, _Rear} = take(Type),
|
||||||
Source.
|
Source.
|
||||||
|
|
||||||
move(Type, #{<<"before">> := Before}) ->
|
move(Type, {before, Before}) ->
|
||||||
emqx_authz_utils:update_config(
|
emqx_authz_utils:update_config(
|
||||||
?CONF_KEY_PATH, {?CMD_MOVE, type(Type), ?CMD_MOVE_BEFORE(type(Before))});
|
?CONF_KEY_PATH, {?CMD_MOVE, type(Type), ?CMD_MOVE_BEFORE(type(Before))});
|
||||||
move(Type, #{<<"after">> := After}) ->
|
move(Type, {'after', After}) ->
|
||||||
emqx_authz_utils:update_config(
|
emqx_authz_utils:update_config(
|
||||||
?CONF_KEY_PATH, {?CMD_MOVE, type(Type), ?CMD_MOVE_AFTER(type(After))});
|
?CONF_KEY_PATH, {?CMD_MOVE, type(Type), ?CMD_MOVE_AFTER(type(After))});
|
||||||
move(Type, Position) ->
|
move(Type, Position) ->
|
||||||
|
@ -334,7 +334,7 @@ take(Type, Sources) ->
|
||||||
{Front, Rear} = lists:splitwith(fun(T) -> type(T) =/= type(Type) end, Sources),
|
{Front, Rear} = lists:splitwith(fun(T) -> type(T) =/= type(Type) end, Sources),
|
||||||
case Rear =:= [] of
|
case Rear =:= [] of
|
||||||
true ->
|
true ->
|
||||||
error({authz_source_of_type_not_found, Type});
|
error({not_found_source, Type});
|
||||||
_ ->
|
_ ->
|
||||||
{hd(Rear), Front, tl(Rear)}
|
{hd(Rear), Front, tl(Rear)}
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -74,11 +74,10 @@ fields(file) ->
|
||||||
, example => <<"acl.conf">>}}];
|
, example => <<"acl.conf">>}}];
|
||||||
fields(position) ->
|
fields(position) ->
|
||||||
[ { position
|
[ { position
|
||||||
, mk( hoconsc:union([binary(), map()])
|
, mk( string()
|
||||||
, #{ desc => <<"Where to place the source">>
|
, #{ desc => <<"Where to place the source">>
|
||||||
, required => true
|
, required => true
|
||||||
, in => body
|
, in => body})}].
|
||||||
, example => #{<<"before">> => <<"file">>}})}].
|
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% http type funcs
|
%% http type funcs
|
||||||
|
|
|
@ -262,9 +262,11 @@ move_source(Method, #{bindings := #{type := Type} = Bindings } = Req)
|
||||||
when is_atom(Type) ->
|
when is_atom(Type) ->
|
||||||
move_source(Method, Req#{bindings => Bindings#{type => atom_to_binary(Type, utf8)}});
|
move_source(Method, Req#{bindings => Bindings#{type => atom_to_binary(Type, utf8)}});
|
||||||
move_source(post, #{bindings := #{type := Type}, body := #{<<"position">> := Position}}) ->
|
move_source(post, #{bindings := #{type := Type}, body := #{<<"position">> := Position}}) ->
|
||||||
case emqx_authz:move(Type, Position) of
|
case parse_position(Position) of
|
||||||
|
{ok, NPosition} ->
|
||||||
|
try emqx_authz:move(Type, NPosition) of
|
||||||
{ok, _} -> {204};
|
{ok, _} -> {204};
|
||||||
{error, not_found_source} ->
|
{error, {not_found_source, _Type}} ->
|
||||||
{404, #{code => <<"NOT_FOUND">>,
|
{404, #{code => <<"NOT_FOUND">>,
|
||||||
message => <<"source ", Type/binary, " not found">>}};
|
message => <<"source ", Type/binary, " not found">>}};
|
||||||
{error, {emqx_conf_schema, _}} ->
|
{error, {emqx_conf_schema, _}} ->
|
||||||
|
@ -273,6 +275,15 @@ move_source(post, #{bindings := #{type := Type}, body := #{<<"position">> := Pos
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{400, #{code => <<"BAD_REQUEST">>,
|
{400, #{code => <<"BAD_REQUEST">>,
|
||||||
message => bin(Reason)}}
|
message => bin(Reason)}}
|
||||||
|
catch
|
||||||
|
error : {unknown_authz_source_type, Unknown} ->
|
||||||
|
NUnknown = bin(Unknown),
|
||||||
|
{400, #{code => <<"BAD_REQUEST">>,
|
||||||
|
message => <<"Unknown authz Source Type: ", NUnknown/binary>>}}
|
||||||
|
end;
|
||||||
|
{error, Reason} ->
|
||||||
|
{400, #{code => <<"BAD_REQUEST">>,
|
||||||
|
message => bin(Reason)}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
@ -457,8 +468,6 @@ do_write_file(Filename, Bytes) ->
|
||||||
error(Reason)
|
error(Reason)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
bin(Term) -> erlang:iolist_to_binary(io_lib:format("~p", [Term])).
|
|
||||||
|
|
||||||
acl_conf_file() ->
|
acl_conf_file() ->
|
||||||
emqx_authz:acl_conf_file().
|
emqx_authz:acl_conf_file().
|
||||||
|
|
||||||
|
@ -468,8 +477,34 @@ parameters_field() ->
|
||||||
}
|
}
|
||||||
].
|
].
|
||||||
|
|
||||||
|
parse_position(<<"top">>) ->
|
||||||
|
{ok, ?CMD_MOVE_TOP};
|
||||||
|
parse_position(<<"bottom">>) ->
|
||||||
|
{ok, ?CMD_MOVE_BOTTOM};
|
||||||
|
parse_position(<<"before:", Before/binary>>) ->
|
||||||
|
{ok, ?CMD_MOVE_BEFORE(Before)};
|
||||||
|
parse_position(<<"after:", After/binary>>) ->
|
||||||
|
{ok, ?CMD_MOVE_AFTER(After)};
|
||||||
|
parse_position(<<"before:">>) ->
|
||||||
|
{error, {invalid_parameter, position}};
|
||||||
|
parse_position(<<"after:">>) ->
|
||||||
|
{error, {invalid_parameter, position}};
|
||||||
|
parse_position(_) ->
|
||||||
|
{error, {invalid_parameter, position}}.
|
||||||
|
|
||||||
position_example() ->
|
position_example() ->
|
||||||
#{<<"position">> => #{<<"before">> => <<"file">>}}.
|
#{ top =>
|
||||||
|
#{ summary => <<"top example">>
|
||||||
|
, value => #{<<"position">> => <<"top">>}}
|
||||||
|
, bottom =>
|
||||||
|
#{ summary => <<"bottom example">>
|
||||||
|
, value => #{<<"position">> => <<"bottom">>}}
|
||||||
|
, relative =>
|
||||||
|
#{ summary => <<"relative example">>
|
||||||
|
, value => #{<<"position">> => <<"before:file">>}}
|
||||||
|
}.
|
||||||
|
|
||||||
authz_sources_types(Type) ->
|
authz_sources_types(Type) ->
|
||||||
emqx_authz_api_schema:authz_sources_types(Type).
|
emqx_authz_api_schema:authz_sources_types(Type).
|
||||||
|
|
||||||
|
bin(Term) -> erlang:iolist_to_binary(io_lib:format("~p", [Term])).
|
||||||
|
|
Loading…
Reference in New Issue