Skip to content

Commit

Permalink
[Connector API] Assert required service_type of connector before star…
Browse files Browse the repository at this point in the history
…ting a sync (#108769)
  • Loading branch information
jedrazb authored May 17, 2024
1 parent 83e8fb1 commit 458e147
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ setup:
is_native: false
service_type: super-connector

- do:
connector.put:
connector_id: test-connector-no-service-type
body:
index_name: search-test-2
name: my-connector
language: de
is_native: false

---
'Create connector sync job':
- do:
Expand Down Expand Up @@ -307,6 +316,16 @@ setup:
trigger_method: full
catch: bad_request

---
'Create connector sync job with no service type':
- do:
connector.sync_job_post:
body:
id: test-connector-no-service-type
job_type: full
trigger_method: full
catch: bad_request

---
"Create connector sync job fails for unprivileged user":
- skip:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public void createConnectorSyncJob(
String connectorId = request.getId();
try {
getSyncJobConnectorInfo(connectorId, listener.delegateFailure((l, connector) -> {

if (Strings.isNullOrEmpty(connector.getIndexName())) {
l.onFailure(
new ElasticsearchStatusException(
Expand All @@ -112,6 +111,19 @@ public void createConnectorSyncJob(
return;
}

if (Strings.isNullOrEmpty(connector.getServiceType())) {
l.onFailure(
new ElasticsearchStatusException(
"Cannot start a sync for connector ["
+ connectorId
+ "] with [service_type] not defined. Set the service type of your connector "
+ "before starting the sync.",
RestStatus.BAD_REQUEST
)
);
return;
}

Instant now = Instant.now();
ConnectorSyncJobType jobType = Objects.requireNonNullElse(request.getJobType(), ConnectorSyncJob.DEFAULT_JOB_TYPE);
ConnectorSyncJobTriggerMethod triggerMethod = Objects.requireNonNullElse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ private static Connector.Builder getRandomConnectorBuilder() {
.setSyncInfo(getRandomConnectorSyncInfo())
.setName(randomFrom(new String[] { null, randomAlphaOfLength(10) }))
.setPipeline(randomBoolean() ? getRandomConnectorIngestPipeline() : null)
.setServiceType(randomAlphaOfLengthBetween(5, 10))
.setScheduling(getRandomConnectorScheduling())
.setStatus(getRandomConnectorInitialStatus())
.setSyncCursor(randomBoolean() ? Map.of(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10)) : null)
Expand All @@ -344,6 +345,10 @@ public static Connector getRandomConnectorWithDetachedIndex() {
return getRandomConnectorBuilder().setIndexName(null).build();
}

public static Connector getRandomConnectorWithServiceTypeNotDefined() {
return getRandomConnectorBuilder().setServiceType(null).build();
}

private static BytesReference convertConnectorToBytesReference(Connector connector) {
try {
return XContentHelper.toXContent((builder, params) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class ConnectorSyncJobIndexServiceTests extends ESSingleNodeTestCase {
private String connectorOneId;
private String connectorTwoId;
private String connectorThreeId;
private String connectorFourId;

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
Expand All @@ -94,6 +95,7 @@ public void setup() throws Exception {
connectorOneId = createConnector(ConnectorTestUtils.getRandomConnector());
connectorTwoId = createConnector(ConnectorTestUtils.getRandomConnector());
connectorThreeId = createConnector(ConnectorTestUtils.getRandomConnectorWithDetachedIndex());
connectorFourId = createConnector(ConnectorTestUtils.getRandomConnectorWithServiceTypeNotDefined());

this.connectorSyncJobIndexService = new ConnectorSyncJobIndexService(client());
}
Expand Down Expand Up @@ -176,6 +178,15 @@ public void testDeleteConnectorSyncJob_WithDetachedConnectorIndex_ExpectExceptio
expectThrows(ElasticsearchStatusException.class, () -> awaitPutConnectorSyncJob(syncJobRequest));
}

public void testDeleteConnectorSyncJob_WithServiceTypeNotDefined_ExpectException() {
PostConnectorSyncJobAction.Request syncJobRequest = new PostConnectorSyncJobAction.Request(
connectorFourId,
ConnectorSyncJobType.FULL,
ConnectorSyncJobTriggerMethod.ON_DEMAND
);
expectThrows(ElasticsearchStatusException.class, () -> awaitPutConnectorSyncJob(syncJobRequest));
}

public void testDeleteConnectorSyncJob_WithNonExistentConnectorId_ExpectException() {
PostConnectorSyncJobAction.Request syncJobRequest = new PostConnectorSyncJobAction.Request(
"non-existent-connector-id",
Expand Down

0 comments on commit 458e147

Please sign in to comment.