Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ setup:
connector_id: test-connector

- match: { error: "some error" }
- match: { status: error }


---
Expand Down Expand Up @@ -59,6 +60,7 @@ setup:
connector_id: test-connector

- match: { error: null }
- match: { status: connected }

---
"Update Connector Error - 404 when connector doesn't exist":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,21 +467,26 @@ else if (configurationValues != null) {
}

/**
* 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].
* @param listener The listener for handling responses, including successful updates or errors.
*/
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)
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
.source(new HashMap<>() {
{
put(Connector.ERROR_FIELD.getPreferredName(), error);
put(Connector.STATUS_FIELD.getPreferredName(), connectorStatus.toString());
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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;
Expand Down Expand Up @@ -712,17 +711,14 @@ public void testUpdateConnectorError() throws Exception {
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 {
Expand All @@ -731,13 +727,12 @@ public void testUpdateConnectorError_resetWithNull() throws Exception {
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 {
Expand Down Expand Up @@ -1347,11 +1342,11 @@ public void onFailure(Exception e) {
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);
Expand Down
Loading