Browse Source

[Connector API] Update status when setting/resetting connector error (#110192)

Jedr Blaszyk 1 year ago
parent
commit
5179b0db29

+ 5 - 0
docs/reference/connector/apis/update-connector-error-api.asciidoc

@@ -21,6 +21,11 @@ To get started with Connector APIs, check out the {enterprise-search-ref}/connec
 * To sync data using self-managed connectors, you need to deploy the {enterprise-search-ref}/build-connector.html[Elastic connector service] on your own infrastructure. This service runs automatically on Elastic Cloud for native connectors.
 * The `connector_id` parameter should reference an existing connector.
 
+[[update-connector-error-api-desc]]
+==== {api-description-title}
+
+Sets the `error` field for the specified connector. If the `error` provided in the request body is non-null, the connector's status is updated to `error`. Otherwise, if the `error` is reset to null, the connector status is updated to `connected`.
+
 [[update-connector-error-api-path-params]]
 ==== {api-path-parms-title}
 

+ 2 - 0
x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/connector/100_connector_update_error.yml

@@ -29,6 +29,7 @@ setup:
         connector_id: test-connector
 
   - match: { error: "some error" }
+  - match: { status: error }
 
 
 ---
@@ -59,6 +60,7 @@ setup:
         connector_id: test-connector
 
   - match: { error: null }
+  - match: { status: connected }
 
 ---
 "Update Connector Error - 404 when connector doesn't exist":

+ 6 - 1
x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/connector/ConnectorIndexService.java

@@ -467,7 +467,8 @@ public class ConnectorIndexService {
     }
 
     /**
-     * Updates the error property of a {@link Connector}.
+     * Updates the error property of a {@link Connector}. If error is non-null the resulting {@link ConnectorStatus}
+     * is 'error', otherwise it's 'connected'.
      *
      * @param connectorId The ID of the {@link Connector} to be updated.
      * @param error       An instance of error property of {@link Connector}, can be reset to [null].
@@ -475,6 +476,9 @@ public class ConnectorIndexService {
      */
     public void updateConnectorError(String connectorId, String error, ActionListener<UpdateResponse> listener) {
         try {
+
+            ConnectorStatus connectorStatus = Strings.isNullOrEmpty(error) ? ConnectorStatus.CONNECTED : ConnectorStatus.ERROR;
+
             final UpdateRequest updateRequest = new UpdateRequest(CONNECTOR_INDEX_NAME, connectorId).doc(
                 new IndexRequest(CONNECTOR_INDEX_NAME).opType(DocWriteRequest.OpType.INDEX)
                     .id(connectorId)
@@ -482,6 +486,7 @@ public class ConnectorIndexService {
                     .source(new HashMap<>() {
                         {
                             put(Connector.ERROR_FIELD.getPreferredName(), error);
+                            put(Connector.STATUS_FIELD.getPreferredName(), connectorStatus.toString());
                         }
                     })
             );

+ 9 - 14
x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/connector/ConnectorIndexServiceTests.java

@@ -27,7 +27,6 @@ import org.elasticsearch.xcontent.XContentType;
 import org.elasticsearch.xpack.application.connector.action.ConnectorCreateActionResponse;
 import org.elasticsearch.xpack.application.connector.action.UpdateConnectorApiKeyIdAction;
 import org.elasticsearch.xpack.application.connector.action.UpdateConnectorConfigurationAction;
-import org.elasticsearch.xpack.application.connector.action.UpdateConnectorErrorAction;
 import org.elasticsearch.xpack.application.connector.action.UpdateConnectorIndexNameAction;
 import org.elasticsearch.xpack.application.connector.action.UpdateConnectorLastSeenAction;
 import org.elasticsearch.xpack.application.connector.action.UpdateConnectorLastSyncStatsAction;
@@ -712,17 +711,14 @@ public class ConnectorIndexServiceTests extends ESSingleNodeTestCase {
         String connectorId = randomUUID();
         ConnectorCreateActionResponse resp = awaitCreateConnector(connectorId, connector);
         assertThat(resp.status(), anyOf(equalTo(RestStatus.CREATED), equalTo(RestStatus.OK)));
+        String error = randomAlphaOfLengthBetween(5, 15);
 
-        UpdateConnectorErrorAction.Request updateErrorRequest = new UpdateConnectorErrorAction.Request(
-            connectorId,
-            randomAlphaOfLengthBetween(5, 15)
-        );
-
-        DocWriteResponse updateResponse = awaitUpdateConnectorError(updateErrorRequest);
+        DocWriteResponse updateResponse = awaitUpdateConnectorError(connectorId, error);
         assertThat(updateResponse.status(), equalTo(RestStatus.OK));
 
         Connector indexedConnector = awaitGetConnector(connectorId);
-        assertThat(updateErrorRequest.getError(), equalTo(indexedConnector.getError()));
+        assertThat(indexedConnector.getError(), equalTo(error));
+        assertThat(indexedConnector.getStatus(), equalTo(ConnectorStatus.ERROR));
     }
 
     public void testUpdateConnectorError_resetWithNull() throws Exception {
@@ -731,13 +727,12 @@ public class ConnectorIndexServiceTests extends ESSingleNodeTestCase {
         ConnectorCreateActionResponse resp = awaitCreateConnector(connectorId, connector);
         assertThat(resp.status(), anyOf(equalTo(RestStatus.CREATED), equalTo(RestStatus.OK)));
 
-        UpdateConnectorErrorAction.Request updateErrorRequest = new UpdateConnectorErrorAction.Request(connectorId, null);
-
-        DocWriteResponse updateResponse = awaitUpdateConnectorError(updateErrorRequest);
+        DocWriteResponse updateResponse = awaitUpdateConnectorError(connectorId, null);
         assertThat(updateResponse.status(), equalTo(RestStatus.OK));
 
         Connector indexedConnector = awaitGetConnector(connectorId);
-        assertThat(updateErrorRequest.getError(), equalTo(indexedConnector.getError()));
+        assertNull(indexedConnector.getError());
+        assertThat(indexedConnector.getStatus(), equalTo(ConnectorStatus.CONNECTED));
     }
 
     public void testUpdateConnectorNameOrDescription() throws Exception {
@@ -1347,11 +1342,11 @@ public class ConnectorIndexServiceTests extends ESSingleNodeTestCase {
         return resp.get();
     }
 
-    private UpdateResponse awaitUpdateConnectorError(UpdateConnectorErrorAction.Request updatedError) throws Exception {
+    private UpdateResponse awaitUpdateConnectorError(String connectorId, String error) throws Exception {
         CountDownLatch latch = new CountDownLatch(1);
         final AtomicReference<UpdateResponse> resp = new AtomicReference<>(null);
         final AtomicReference<Exception> exc = new AtomicReference<>(null);
-        connectorIndexService.updateConnectorError(updatedError.getConnectorId(), updatedError.getError(), new ActionListener<>() {
+        connectorIndexService.updateConnectorError(connectorId, error, new ActionListener<>() {
             @Override
             public void onResponse(UpdateResponse indexResponse) {
                 resp.set(indexResponse);