refactor(emqx_authz): use module name builder functions
This commit is contained in:
parent
ce1772c2b5
commit
84ed368d41
|
@ -210,23 +210,14 @@ gen_id(Type) ->
|
||||||
create_resource(#{type := DB,
|
create_resource(#{type := DB,
|
||||||
config := Config,
|
config := Config,
|
||||||
annotations := #{id := ResourceID}}) ->
|
annotations := #{id := ResourceID}}) ->
|
||||||
case emqx_resource:update(
|
case emqx_resource:update(ResourceID, connector_module(DB), Config, []) of
|
||||||
ResourceID,
|
|
||||||
list_to_existing_atom(io_lib:format("~s_~s",[emqx_connector, DB])),
|
|
||||||
Config,
|
|
||||||
[])
|
|
||||||
of
|
|
||||||
{ok, _} -> ResourceID;
|
{ok, _} -> ResourceID;
|
||||||
{error, Reason} -> {error, Reason}
|
{error, Reason} -> {error, Reason}
|
||||||
end;
|
end;
|
||||||
create_resource(#{type := DB,
|
create_resource(#{type := DB,
|
||||||
config := Config}) ->
|
config := Config}) ->
|
||||||
ResourceID = gen_id(DB),
|
ResourceID = gen_id(DB),
|
||||||
case emqx_resource:create(
|
case emqx_resource:create(ResourceID, connector_module(DB), Config) of
|
||||||
ResourceID,
|
|
||||||
list_to_existing_atom(io_lib:format("~s_~s",[emqx_connector, DB])),
|
|
||||||
Config)
|
|
||||||
of
|
|
||||||
{ok, already_created} -> ResourceID;
|
{ok, already_created} -> ResourceID;
|
||||||
{ok, _} -> ResourceID;
|
{ok, _} -> ResourceID;
|
||||||
{error, Reason} -> {error, Reason}
|
{error, Reason} -> {error, Reason}
|
||||||
|
@ -279,7 +270,7 @@ init_source(#{enable := true,
|
||||||
sql := SQL
|
sql := SQL
|
||||||
} = Source) when DB =:= mysql;
|
} = Source) when DB =:= mysql;
|
||||||
DB =:= pgsql ->
|
DB =:= pgsql ->
|
||||||
Mod = list_to_existing_atom(io_lib:format("~s_~s",[?APP, DB])),
|
Mod = authz_module(DB),
|
||||||
case create_resource(Source) of
|
case create_resource(Source) of
|
||||||
{error, Reason} -> error({load_config_error, Reason});
|
{error, Reason} -> error({load_config_error, Reason});
|
||||||
Id -> Source#{annotations =>
|
Id -> Source#{annotations =>
|
||||||
|
@ -326,8 +317,14 @@ do_authorize(Client, PubSub, Topic, [#{type := file} = F | Tail]) ->
|
||||||
end;
|
end;
|
||||||
do_authorize(Client, PubSub, Topic,
|
do_authorize(Client, PubSub, Topic,
|
||||||
[Connector = #{type := Type} | Tail] ) ->
|
[Connector = #{type := Type} | Tail] ) ->
|
||||||
Mod = list_to_existing_atom(io_lib:format("emqx_authz_~s",[Type])),
|
Mod = authz_module(Type),
|
||||||
case Mod:authorize(Client, PubSub, Topic, Connector) of
|
case Mod:authorize(Client, PubSub, Topic, Connector) of
|
||||||
nomatch -> do_authorize(Client, PubSub, Topic, Tail);
|
nomatch -> do_authorize(Client, PubSub, Topic, Tail);
|
||||||
Matched -> Matched
|
Matched -> Matched
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
authz_module(Type) ->
|
||||||
|
list_to_existing_atom("emqx_authz_" ++ atom_to_list(Type)).
|
||||||
|
|
||||||
|
connector_module(Type) ->
|
||||||
|
list_to_existing_atom("emqx_connector_" ++ atom_to_list(Type)).
|
||||||
|
|
Loading…
Reference in New Issue