fix: refactor handling of operation(s) [ie 'start']

This commit is contained in:
Stefan Strigler 2023-10-18 16:18:46 +02:00 committed by Zaiming (Stone) Shi
parent 676572c65a
commit abcae6b509
1 changed files with 20 additions and 36 deletions

View File

@ -416,20 +416,10 @@ schema("/bridges_v2_probe") ->
}) -> }) ->
?TRY_PARSE_ID( ?TRY_PARSE_ID(
Id, Id,
try is_enabled_bridge(BridgeType, BridgeName) of begin
false -> OperFunc = operation_func(all, Op),
?BRIDGE_NOT_ENABLED; Nodes = mria:running_nodes(),
true -> call_operation_if_enabled(all, OperFunc, [Nodes, BridgeType, BridgeName])
case operation_to_all_func(Op) of
invalid ->
?NOT_FOUND(<<"Invalid operation: ", Op/binary>>);
OperFunc ->
Nodes = mria:running_nodes(),
call_operation(all, OperFunc, [Nodes, BridgeType, BridgeName])
end
catch
throw:not_found ->
?BRIDGE_NOT_FOUND(BridgeType, BridgeName)
end end
). ).
@ -441,22 +431,8 @@ schema("/bridges_v2_probe") ->
Id, Id,
case emqx_utils:safe_to_existing_atom(Node, utf8) of case emqx_utils:safe_to_existing_atom(Node, utf8) of
{ok, TargetNode} -> {ok, TargetNode} ->
try is_enabled_bridge(BridgeType, BridgeName) of OperFunc = operation_func(TargetNode, Op),
false -> call_operation_if_enabled(TargetNode, OperFunc, [TargetNode, BridgeType, BridgeName]);
?BRIDGE_NOT_ENABLED;
true ->
case node_operation_func(Op) of
invalid ->
?NOT_FOUND(<<"Invalid operation: ", Op/binary>>);
OperFunc ->
call_operation(TargetNode, OperFunc, [
TargetNode, BridgeType, BridgeName
])
end
catch
throw:not_found ->
?BRIDGE_NOT_FOUND(BridgeType, BridgeName)
end;
{error, _} -> {error, _} ->
?NOT_FOUND(<<"Invalid node name: ", Node/binary>>) ?NOT_FOUND(<<"Invalid node name: ", Node/binary>>)
end end
@ -555,6 +531,20 @@ lookup_from_all_nodes(BridgeType, BridgeName, SuccCode) ->
?INTERNAL_ERROR(Reason) ?INTERNAL_ERROR(Reason)
end. end.
operation_func(all, start) -> v2_start_bridge_to_all_nodes;
operation_func(_Node, start) -> v2_start_bridge_to_node.
call_operation_if_enabled(NodeOrAll, OperFunc, [Nodes, BridgeType, BridgeName]) ->
try is_enabled_bridge(BridgeType, BridgeName) of
false ->
?BRIDGE_NOT_ENABLED;
true ->
call_operation(NodeOrAll, OperFunc, [Nodes, BridgeType, BridgeName])
catch
throw:not_found ->
?BRIDGE_NOT_FOUND(BridgeType, BridgeName)
end.
is_enabled_bridge(BridgeType, BridgeName) -> is_enabled_bridge(BridgeType, BridgeName) ->
try emqx_bridge_v2:lookup(BridgeType, binary_to_existing_atom(BridgeName)) of try emqx_bridge_v2:lookup(BridgeType, binary_to_existing_atom(BridgeName)) of
{ok, #{raw_config := ConfMap}} -> {ok, #{raw_config := ConfMap}} ->
@ -568,12 +558,6 @@ is_enabled_bridge(BridgeType, BridgeName) ->
throw(not_found) throw(not_found)
end. end.
node_operation_func(<<"start">>) -> v2_start_bridge_to_node;
node_operation_func(_) -> invalid.
operation_to_all_func(<<"start">>) -> v2_start_bridge_to_all_nodes;
operation_to_all_func(_) -> invalid.
call_operation(NodeOrAll, OperFunc, Args = [_Nodes, BridgeType, BridgeName]) -> call_operation(NodeOrAll, OperFunc, Args = [_Nodes, BridgeType, BridgeName]) ->
case is_ok(do_bpapi_call(NodeOrAll, OperFunc, Args)) of case is_ok(do_bpapi_call(NodeOrAll, OperFunc, Args)) of
Ok when Ok =:= ok; is_tuple(Ok), element(1, Ok) =:= ok -> Ok when Ok =:= ok; is_tuple(Ok), element(1, Ok) =:= ok ->