Explorar o código

Merge pull request #12156 from thalesmg/feat-list-disabled-channels-r54-20231212

feat(connector_api): list disabled channels too
Thales Macedo Garitezi %!s(int64=2) %!d(string=hai) anos
pai
achega
6092a824b6

+ 13 - 3
apps/emqx_connector/src/emqx_connector_api.erl

@@ -611,10 +611,11 @@ format_resource(
         type := Type,
         name := ConnectorName,
         raw_config := RawConf0,
-        resource_data := ResourceData
+        resource_data := ResourceData0
     },
     Node
 ) ->
+    ResourceData = lookup_channels(Type, ConnectorName, ResourceData0),
     RawConf = fill_defaults(Type, RawConf0),
     redact(
         maps:merge(
@@ -627,14 +628,23 @@ format_resource(
         )
     ).
 
+lookup_channels(Type, Name, ResourceData0) ->
+    ConnectorResId = emqx_connector_resource:resource_id(Type, Name),
+    case emqx_resource:get_channels(ConnectorResId) of
+        {ok, Channels} ->
+            ResourceData0#{channels => maps:from_list(Channels)};
+        {error, not_found} ->
+            ResourceData0#{channels => #{}}
+    end.
+
 format_resource_data(ResData) ->
-    maps:fold(fun format_resource_data/3, #{}, maps:with([status, error, added_channels], ResData)).
+    maps:fold(fun format_resource_data/3, #{}, maps:with([status, error, channels], ResData)).
 
 format_resource_data(error, undefined, Result) ->
     Result;
 format_resource_data(error, Error, Result) ->
     Result#{status_reason => emqx_utils:readable_error_msg(Error)};
-format_resource_data(added_channels, Channels, Result) ->
+format_resource_data(channels, Channels, Result) ->
     Result#{actions => lists:map(fun format_action/1, maps:keys(Channels))};
 format_resource_data(K, V, Result) ->
     Result#{K => V}.

+ 33 - 0
apps/emqx_connector/test/emqx_connector_api_SUITE.erl

@@ -845,6 +845,39 @@ t_fail_delete_with_action(Config) ->
     ),
     ok.
 
+t_list_disabled_channels(Config) ->
+    ConnectorParams = ?KAFKA_CONNECTOR(?CONNECTOR_NAME),
+    ?assertMatch(
+        {ok, 201, _},
+        request_json(
+            post,
+            uri(["connectors"]),
+            ConnectorParams,
+            Config
+        )
+    ),
+    ActionName = ?BRIDGE_NAME,
+    ActionParams = (?KAFKA_BRIDGE(ActionName))#{<<"enable">> := true},
+    ?assertMatch(
+        {ok, 201, #{<<"enable">> := true}},
+        request_json(
+            post,
+            uri(["actions"]),
+            ActionParams,
+            Config
+        )
+    ),
+    ConnectorID = emqx_connector_resource:connector_id(?CONNECTOR_TYPE, ?CONNECTOR_NAME),
+    ?assertMatch(
+        {ok, 200, #{<<"actions">> := [ActionName]}},
+        request_json(
+            get,
+            uri(["connectors", ConnectorID]),
+            Config
+        )
+    ),
+    ok.
+
 t_raw_config_response_defaults(Config) ->
     Params = maps:without([<<"enable">>, <<"resource_opts">>], ?KAFKA_CONNECTOR(?CONNECTOR_NAME)),
     ?assertMatch(