Skip to content

Commit 5179b0d

Browse files
authored
[Connector API] Update status when setting/resetting connector error (#110192)
1 parent 7e81229 commit 5179b0d

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

docs/reference/connector/apis/update-connector-error-api.asciidoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ To get started with Connector APIs, check out the {enterprise-search-ref}/connec
2121
* 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.
2222
* The `connector_id` parameter should reference an existing connector.
2323

24+
[[update-connector-error-api-desc]]
25+
==== {api-description-title}
26+
27+
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`.
28+
2429
[[update-connector-error-api-path-params]]
2530
==== {api-path-parms-title}
2631

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ setup:
2929
connector_id: test-connector
3030

3131
- match: { error: "some error" }
32+
- match: { status: error }
3233

3334

3435
---
@@ -59,6 +60,7 @@ setup:
5960
connector_id: test-connector
6061

6162
- match: { error: null }
63+
- match: { status: connected }
6264

6365
---
6466
"Update Connector Error - 404 when connector doesn't exist":

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,21 +467,26 @@ else if (configurationValues != null) {
467467
}
468468

469469
/**
470-
* Updates the error property of a {@link Connector}.
470+
* Updates the error property of a {@link Connector}. If error is non-null the resulting {@link ConnectorStatus}
471+
* is 'error', otherwise it's 'connected'.
471472
*
472473
* @param connectorId The ID of the {@link Connector} to be updated.
473474
* @param error An instance of error property of {@link Connector}, can be reset to [null].
474475
* @param listener The listener for handling responses, including successful updates or errors.
475476
*/
476477
public void updateConnectorError(String connectorId, String error, ActionListener<UpdateResponse> listener) {
477478
try {
479+
480+
ConnectorStatus connectorStatus = Strings.isNullOrEmpty(error) ? ConnectorStatus.CONNECTED : ConnectorStatus.ERROR;
481+
478482
final UpdateRequest updateRequest = new UpdateRequest(CONNECTOR_INDEX_NAME, connectorId).doc(
479483
new IndexRequest(CONNECTOR_INDEX_NAME).opType(DocWriteRequest.OpType.INDEX)
480484
.id(connectorId)
481485
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
482486
.source(new HashMap<>() {
483487
{
484488
put(Connector.ERROR_FIELD.getPreferredName(), error);
489+
put(Connector.STATUS_FIELD.getPreferredName(), connectorStatus.toString());
485490
}
486491
})
487492
);

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.xpack.application.connector.action.ConnectorCreateActionResponse;
2828
import org.elasticsearch.xpack.application.connector.action.UpdateConnectorApiKeyIdAction;
2929
import org.elasticsearch.xpack.application.connector.action.UpdateConnectorConfigurationAction;
30-
import org.elasticsearch.xpack.application.connector.action.UpdateConnectorErrorAction;
3130
import org.elasticsearch.xpack.application.connector.action.UpdateConnectorIndexNameAction;
3231
import org.elasticsearch.xpack.application.connector.action.UpdateConnectorLastSeenAction;
3332
import org.elasticsearch.xpack.application.connector.action.UpdateConnectorLastSyncStatsAction;
@@ -712,17 +711,14 @@ public void testUpdateConnectorError() throws Exception {
712711
String connectorId = randomUUID();
713712
ConnectorCreateActionResponse resp = awaitCreateConnector(connectorId, connector);
714713
assertThat(resp.status(), anyOf(equalTo(RestStatus.CREATED), equalTo(RestStatus.OK)));
714+
String error = randomAlphaOfLengthBetween(5, 15);
715715

716-
UpdateConnectorErrorAction.Request updateErrorRequest = new UpdateConnectorErrorAction.Request(
717-
connectorId,
718-
randomAlphaOfLengthBetween(5, 15)
719-
);
720-
721-
DocWriteResponse updateResponse = awaitUpdateConnectorError(updateErrorRequest);
716+
DocWriteResponse updateResponse = awaitUpdateConnectorError(connectorId, error);
722717
assertThat(updateResponse.status(), equalTo(RestStatus.OK));
723718

724719
Connector indexedConnector = awaitGetConnector(connectorId);
725-
assertThat(updateErrorRequest.getError(), equalTo(indexedConnector.getError()));
720+
assertThat(indexedConnector.getError(), equalTo(error));
721+
assertThat(indexedConnector.getStatus(), equalTo(ConnectorStatus.ERROR));
726722
}
727723

728724
public void testUpdateConnectorError_resetWithNull() throws Exception {
@@ -731,13 +727,12 @@ public void testUpdateConnectorError_resetWithNull() throws Exception {
731727
ConnectorCreateActionResponse resp = awaitCreateConnector(connectorId, connector);
732728
assertThat(resp.status(), anyOf(equalTo(RestStatus.CREATED), equalTo(RestStatus.OK)));
733729

734-
UpdateConnectorErrorAction.Request updateErrorRequest = new UpdateConnectorErrorAction.Request(connectorId, null);
735-
736-
DocWriteResponse updateResponse = awaitUpdateConnectorError(updateErrorRequest);
730+
DocWriteResponse updateResponse = awaitUpdateConnectorError(connectorId, null);
737731
assertThat(updateResponse.status(), equalTo(RestStatus.OK));
738732

739733
Connector indexedConnector = awaitGetConnector(connectorId);
740-
assertThat(updateErrorRequest.getError(), equalTo(indexedConnector.getError()));
734+
assertNull(indexedConnector.getError());
735+
assertThat(indexedConnector.getStatus(), equalTo(ConnectorStatus.CONNECTED));
741736
}
742737

743738
public void testUpdateConnectorNameOrDescription() throws Exception {
@@ -1347,11 +1342,11 @@ public void onFailure(Exception e) {
13471342
return resp.get();
13481343
}
13491344

1350-
private UpdateResponse awaitUpdateConnectorError(UpdateConnectorErrorAction.Request updatedError) throws Exception {
1345+
private UpdateResponse awaitUpdateConnectorError(String connectorId, String error) throws Exception {
13511346
CountDownLatch latch = new CountDownLatch(1);
13521347
final AtomicReference<UpdateResponse> resp = new AtomicReference<>(null);
13531348
final AtomicReference<Exception> exc = new AtomicReference<>(null);
1354-
connectorIndexService.updateConnectorError(updatedError.getConnectorId(), updatedError.getError(), new ActionListener<>() {
1349+
connectorIndexService.updateConnectorError(connectorId, error, new ActionListener<>() {
13551350
@Override
13561351
public void onResponse(UpdateResponse indexResponse) {
13571352
resp.set(indexResponse);

0 commit comments

Comments
 (0)