Merge pull request #12184 from sstrigler/EMQX-11587-use-common-functions-to-create-api-schemata-and-examples-in-action-implementations
refactor(emqx_bridge): common api_fields fn for actions
This commit is contained in:
commit
a5978aa39a
|
@ -31,8 +31,9 @@
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
common_bridge_fields/0,
|
common_bridge_fields/0,
|
||||||
|
metrics_fields/0,
|
||||||
status_fields/0,
|
status_fields/0,
|
||||||
metrics_fields/0
|
type_and_name_fields/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
%% for testing only
|
%% for testing only
|
||||||
|
@ -156,6 +157,12 @@ metrics_fields() ->
|
||||||
)}
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
|
type_and_name_fields(ConnectorType) ->
|
||||||
|
[
|
||||||
|
{type, mk(ConnectorType, #{required => true, desc => ?DESC("desc_type")})},
|
||||||
|
{name, mk(binary(), #{required => true, desc => ?DESC("desc_name")})}
|
||||||
|
].
|
||||||
|
|
||||||
%%======================================================================================
|
%%======================================================================================
|
||||||
%% For config files
|
%% For config files
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,10 @@
|
||||||
-export([types/0, types_sc/0]).
|
-export([types/0, types_sc/0]).
|
||||||
-export([resource_opts_fields/0, resource_opts_fields/1]).
|
-export([resource_opts_fields/0, resource_opts_fields/1]).
|
||||||
|
|
||||||
|
-export([
|
||||||
|
api_fields/3
|
||||||
|
]).
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
make_producer_action_schema/1,
|
make_producer_action_schema/1,
|
||||||
make_consumer_action_schema/1,
|
make_consumer_action_schema/1,
|
||||||
|
@ -153,6 +157,24 @@ method_values(get, Type) ->
|
||||||
method_values(put, _Type) ->
|
method_values(put, _Type) ->
|
||||||
#{}.
|
#{}.
|
||||||
|
|
||||||
|
api_fields("get_bridge_v2", Type, Fields) ->
|
||||||
|
lists:append(
|
||||||
|
[
|
||||||
|
emqx_bridge_schema:type_and_name_fields(Type),
|
||||||
|
emqx_bridge_schema:status_fields(),
|
||||||
|
Fields
|
||||||
|
]
|
||||||
|
);
|
||||||
|
api_fields("post_bridge_v2", Type, Fields) ->
|
||||||
|
lists:append(
|
||||||
|
[
|
||||||
|
emqx_bridge_schema:type_and_name_fields(Type),
|
||||||
|
Fields
|
||||||
|
]
|
||||||
|
);
|
||||||
|
api_fields("put_bridge_v2", _Type, Fields) ->
|
||||||
|
Fields.
|
||||||
|
|
||||||
%%======================================================================================
|
%%======================================================================================
|
||||||
%% HOCON Schema Callbacks
|
%% HOCON Schema Callbacks
|
||||||
%%======================================================================================
|
%%======================================================================================
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-define(CONNECTOR_TYPE, mongodb).
|
-define(CONNECTOR_TYPE, mongodb).
|
||||||
|
-define(ACTION_TYPE, mongodb).
|
||||||
|
|
||||||
%%=================================================================================================
|
%%=================================================================================================
|
||||||
%% hocon_schema API
|
%% hocon_schema API
|
||||||
|
@ -111,20 +112,18 @@ fields(Field) when
|
||||||
fields("connection_fields") ++
|
fields("connection_fields") ++
|
||||||
emqx_connector_schema:resource_opts_ref(?MODULE, connector_resource_opts),
|
emqx_connector_schema:resource_opts_ref(?MODULE, connector_resource_opts),
|
||||||
emqx_connector_schema:api_fields(Field, ?CONNECTOR_TYPE, Fields);
|
emqx_connector_schema:api_fields(Field, ?CONNECTOR_TYPE, Fields);
|
||||||
fields("get_bridge_v2") ->
|
fields(Field) when
|
||||||
emqx_bridge_schema:status_fields() ++
|
Field == "get_bridge_v2";
|
||||||
fields("post_bridge_v2");
|
Field == "post_bridge_v2";
|
||||||
fields("post_bridge_v2") ->
|
Field == "put_bridge_v2"
|
||||||
type_and_name_fields(mongodb) ++
|
->
|
||||||
fields(mongodb_action);
|
emqx_bridge_v2_schema:api_fields(Field, ?ACTION_TYPE, fields(mongodb_action));
|
||||||
fields("put_bridge_v2") ->
|
|
||||||
fields(mongodb_action);
|
|
||||||
fields("post_rs") ->
|
fields("post_rs") ->
|
||||||
fields(mongodb_rs) ++ type_and_name_fields(mongodb_rs);
|
fields(mongodb_rs) ++ emqx_bridge_schema:type_and_name_fields(mongodb_rs);
|
||||||
fields("post_sharded") ->
|
fields("post_sharded") ->
|
||||||
fields(mongodb_sharded) ++ type_and_name_fields(mongodb_sharded);
|
fields(mongodb_sharded) ++ emqx_bridge_schema:type_and_name_fields(mongodb_sharded);
|
||||||
fields("post_single") ->
|
fields("post_single") ->
|
||||||
fields(mongodb_single) ++ type_and_name_fields(mongodb_single);
|
fields(mongodb_single) ++ emqx_bridge_schema:type_and_name_fields(mongodb_single);
|
||||||
fields("put_rs") ->
|
fields("put_rs") ->
|
||||||
fields(mongodb_rs);
|
fields(mongodb_rs);
|
||||||
fields("put_sharded") ->
|
fields("put_sharded") ->
|
||||||
|
@ -134,22 +133,24 @@ fields("put_single") ->
|
||||||
fields("get_rs") ->
|
fields("get_rs") ->
|
||||||
emqx_bridge_schema:status_fields() ++
|
emqx_bridge_schema:status_fields() ++
|
||||||
fields(mongodb_rs) ++
|
fields(mongodb_rs) ++
|
||||||
type_and_name_fields(mongodb_rs);
|
emqx_bridge_schema:type_and_name_fields(mongodb_rs);
|
||||||
fields("get_sharded") ->
|
fields("get_sharded") ->
|
||||||
emqx_bridge_schema:status_fields() ++
|
emqx_bridge_schema:status_fields() ++
|
||||||
fields(mongodb_sharded) ++
|
fields(mongodb_sharded) ++
|
||||||
type_and_name_fields(mongodb_sharded);
|
emqx_bridge_schema:type_and_name_fields(mongodb_sharded);
|
||||||
fields("get_single") ->
|
fields("get_single") ->
|
||||||
emqx_bridge_schema:status_fields() ++
|
emqx_bridge_schema:status_fields() ++
|
||||||
fields(mongodb_single) ++
|
fields(mongodb_single) ++
|
||||||
type_and_name_fields(mongodb_single).
|
emqx_bridge_schema:type_and_name_fields(mongodb_single).
|
||||||
|
|
||||||
bridge_v2_examples(Method) ->
|
bridge_v2_examples(Method) ->
|
||||||
[
|
[
|
||||||
#{
|
#{
|
||||||
<<"mongodb">> => #{
|
<<"mongodb">> => #{
|
||||||
summary => <<"MongoDB Action">>,
|
summary => <<"MongoDB Action">>,
|
||||||
value => action_values(Method)
|
value => emqx_bridge_v2_schema:action_values(
|
||||||
|
Method, mongodb, mongodb, #{parameters => #{collection => <<"mycol">>}}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
].
|
].
|
||||||
|
@ -181,19 +182,25 @@ connector_examples(Method) ->
|
||||||
#{
|
#{
|
||||||
<<"mongodb_rs">> => #{
|
<<"mongodb_rs">> => #{
|
||||||
summary => <<"MongoDB Replica Set Connector">>,
|
summary => <<"MongoDB Replica Set Connector">>,
|
||||||
value => connector_values(mongodb_rs, Method)
|
value => emqx_connector_schema:connector_values(
|
||||||
|
Method, mongodb_rs, #{parameters => connector_values()}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
#{
|
#{
|
||||||
<<"mongodb_sharded">> => #{
|
<<"mongodb_sharded">> => #{
|
||||||
summary => <<"MongoDB Sharded Connector">>,
|
summary => <<"MongoDB Sharded Connector">>,
|
||||||
value => connector_values(mongodb_sharded, Method)
|
value => emqx_connector_schema:connector_values(
|
||||||
|
Method, mongodb_sharded, #{parameters => connector_values()}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
#{
|
#{
|
||||||
<<"mongodb_single">> => #{
|
<<"mongodb_single">> => #{
|
||||||
summary => <<"MongoDB Standalone Connector">>,
|
summary => <<"MongoDB Standalone Connector">>,
|
||||||
value => connector_values(mongodb_single, Method)
|
value => emqx_connector_schema:connector_values(
|
||||||
|
Method, mongodb_single, #{parameters => connector_values()}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
].
|
].
|
||||||
|
@ -227,40 +234,6 @@ desc(_) ->
|
||||||
%% Internal fns
|
%% Internal fns
|
||||||
%%=================================================================================================
|
%%=================================================================================================
|
||||||
|
|
||||||
type_and_name_fields(MongoType) ->
|
|
||||||
[
|
|
||||||
{type, mk(MongoType, #{required => true, desc => ?DESC("desc_type")})},
|
|
||||||
{name, mk(binary(), #{required => true, desc => ?DESC("desc_name")})}
|
|
||||||
].
|
|
||||||
|
|
||||||
connector_values(Type, Method) ->
|
|
||||||
lists:foldl(
|
|
||||||
fun(M1, M2) ->
|
|
||||||
maps:merge(M1, M2)
|
|
||||||
end,
|
|
||||||
#{
|
|
||||||
description => <<"My example connector">>,
|
|
||||||
parameters => mongo_type_opts(Type)
|
|
||||||
},
|
|
||||||
[
|
|
||||||
common_values(),
|
|
||||||
method_values(mongodb, Method)
|
|
||||||
]
|
|
||||||
).
|
|
||||||
|
|
||||||
action_values(Method) ->
|
|
||||||
maps:merge(
|
|
||||||
method_values(mongodb, Method),
|
|
||||||
#{
|
|
||||||
description => <<"My example action">>,
|
|
||||||
enable => true,
|
|
||||||
connector => <<"my_mongodb_connector">>,
|
|
||||||
parameters => #{
|
|
||||||
collection => <<"mycol">>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
).
|
|
||||||
|
|
||||||
values(MongoType, Method) ->
|
values(MongoType, Method) ->
|
||||||
maps:merge(
|
maps:merge(
|
||||||
mongo_type_opts(MongoType),
|
mongo_type_opts(MongoType),
|
||||||
|
@ -298,10 +271,10 @@ bridge_values(Type, _Method) ->
|
||||||
type => TypeBin,
|
type => TypeBin,
|
||||||
collection => <<"mycol">>
|
collection => <<"mycol">>
|
||||||
},
|
},
|
||||||
common_values()
|
connector_values()
|
||||||
).
|
).
|
||||||
|
|
||||||
common_values() ->
|
connector_values() ->
|
||||||
#{
|
#{
|
||||||
enable => true,
|
enable => true,
|
||||||
database => <<"mqtt">>,
|
database => <<"mqtt">>,
|
||||||
|
@ -310,26 +283,3 @@ common_values() ->
|
||||||
username => <<"myuser">>,
|
username => <<"myuser">>,
|
||||||
password => <<"******">>
|
password => <<"******">>
|
||||||
}.
|
}.
|
||||||
|
|
||||||
method_values(Type, post) ->
|
|
||||||
TypeBin = atom_to_binary(Type),
|
|
||||||
#{
|
|
||||||
name => <<TypeBin/binary, "_demo">>,
|
|
||||||
type => TypeBin
|
|
||||||
};
|
|
||||||
method_values(Type, get) ->
|
|
||||||
maps:merge(
|
|
||||||
method_values(Type, post),
|
|
||||||
#{
|
|
||||||
status => <<"connected">>,
|
|
||||||
node_status => [
|
|
||||||
#{
|
|
||||||
node => <<"emqx@localhost">>,
|
|
||||||
status => <<"connected">>
|
|
||||||
}
|
|
||||||
],
|
|
||||||
actions => [<<"my_action">>]
|
|
||||||
}
|
|
||||||
);
|
|
||||||
method_values(_Type, put) ->
|
|
||||||
#{}.
|
|
||||||
|
|
|
@ -160,12 +160,12 @@ fields("put") ->
|
||||||
fields("config");
|
fields("config");
|
||||||
fields("get") ->
|
fields("get") ->
|
||||||
emqx_bridge_schema:status_fields() ++ fields("post");
|
emqx_bridge_schema:status_fields() ++ fields("post");
|
||||||
fields("get_bridge_v2") ->
|
fields(Field) when
|
||||||
emqx_bridge_schema:status_fields() ++ fields("post_bridge_v2");
|
Field == "get_bridge_v2";
|
||||||
fields("post_bridge_v2") ->
|
Field == "post_bridge_v2";
|
||||||
[type_field(), name_field() | fields(mysql_action)];
|
Field == "put_bridge_v2"
|
||||||
fields("put_bridge_v2") ->
|
->
|
||||||
fields(mysql_action);
|
emqx_bridge_v2_schema:api_fields(Field, ?ACTION_TYPE, fields(mysql_action));
|
||||||
fields(Field) when
|
fields(Field) when
|
||||||
Field == "get_connector";
|
Field == "get_connector";
|
||||||
Field == "put_connector";
|
Field == "put_connector";
|
||||||
|
|
Loading…
Reference in New Issue