-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Connectors] Use Connector API to create a connector #183398
Changes from 4 commits
c838c08
163c482
2a31346
c24a4d0
de8757f
2e8b139
67a4ea4
74fd9b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { errors } from '@elastic/elasticsearch'; | ||
|
||
import { ElasticsearchClient } from '@kbn/core/server'; | ||
import { FeatureName } from '../types'; | ||
|
||
import { createConnector } from './create_connector'; | ||
|
||
const notFoundError = new errors.ResponseError({ | ||
statusCode: 404, | ||
body: { | ||
error: { | ||
type: `document_missing_exception`, | ||
}, | ||
}, | ||
} as any); | ||
|
||
describe('createConnector lib', () => { | ||
const mockClient = { | ||
transport: { | ||
request: jest.fn(), | ||
}, | ||
}; | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should create connector with _connector API endpoint', async () => { | ||
const connectorId = 'connectorId'; | ||
const mockConnector = { | ||
id: connectorId, | ||
index_name: 'indexName', | ||
language: 'en', | ||
is_native: true, | ||
}; | ||
mockClient.transport.request | ||
.mockResolvedValueOnce({ id: connectorId }) | ||
.mockResolvedValueOnce(mockConnector); | ||
|
||
await expect( | ||
createConnector(mockClient as unknown as ElasticsearchClient, { | ||
isNative: true, | ||
indexName: mockConnector.index_name, | ||
language: mockConnector.language, | ||
}) | ||
).resolves.toEqual(mockConnector); | ||
expect(mockClient.transport.request).toHaveBeenCalledWith({ | ||
method: 'POST', | ||
path: `/_connector`, | ||
body: { | ||
index_name: 'indexName', | ||
language: 'en', | ||
is_native: true, | ||
name: '', | ||
}, | ||
}); | ||
expect(mockClient.transport.request).toHaveBeenCalledWith({ | ||
method: 'GET', | ||
path: `/_connector/${connectorId}`, | ||
}); | ||
}); | ||
|
||
it('should update pipeline params if provided', async () => { | ||
const connectorId = 'connectorId'; | ||
const mockConnector = { | ||
id: connectorId, | ||
index_name: 'indexName', | ||
language: 'en', | ||
is_native: true, | ||
}; | ||
|
||
const mockPipeline = { | ||
extract_binary_content: true, | ||
name: 'test', | ||
reduce_whitespace: true, | ||
run_ml_inference: true, | ||
}; | ||
|
||
mockClient.transport.request | ||
.mockResolvedValueOnce({ id: connectorId }) | ||
.mockResolvedValueOnce({ result: 'updated' }) | ||
.mockResolvedValueOnce(mockConnector); | ||
|
||
await expect( | ||
createConnector(mockClient as unknown as ElasticsearchClient, { | ||
isNative: true, | ||
indexName: 'indexName', | ||
language: 'en', | ||
pipeline: mockPipeline, | ||
}) | ||
).resolves.toEqual(mockConnector); | ||
|
||
expect(mockClient.transport.request).toHaveBeenCalledWith({ | ||
method: 'POST', | ||
path: `/_connector`, | ||
body: { | ||
index_name: 'indexName', | ||
language: 'en', | ||
is_native: true, | ||
name: '', | ||
}, | ||
}); | ||
expect(mockClient.transport.request).toHaveBeenCalledWith({ | ||
method: 'PUT', | ||
path: `/_connector/${connectorId}/_pipeline`, | ||
body: { pipeline: mockPipeline }, | ||
}); | ||
expect(mockClient.transport.request).toHaveBeenCalledWith({ | ||
method: 'GET', | ||
path: `/_connector/${connectorId}`, | ||
}); | ||
}); | ||
|
||
it('should update connector features if provided', async () => { | ||
const connectorId = 'connectorId'; | ||
const mockConnector = { | ||
id: connectorId, | ||
index_name: 'indexName', | ||
language: 'en', | ||
is_native: true, | ||
}; | ||
|
||
const mockFeatures = { | ||
[FeatureName.FILTERING_ADVANCED_CONFIG]: true, | ||
[FeatureName.FILTERING_RULES]: true, | ||
[FeatureName.SYNC_RULES]: { | ||
advanced: { enabled: true }, | ||
basic: { enabled: true }, | ||
}, | ||
}; | ||
|
||
mockClient.transport.request | ||
.mockResolvedValueOnce({ id: connectorId }) | ||
.mockResolvedValueOnce({ result: 'updated' }) | ||
.mockResolvedValueOnce(mockConnector); | ||
|
||
await expect( | ||
createConnector(mockClient as unknown as ElasticsearchClient, { | ||
isNative: true, | ||
indexName: 'indexName', | ||
language: 'en', | ||
features: mockFeatures, | ||
}) | ||
).resolves.toEqual(mockConnector); | ||
|
||
expect(mockClient.transport.request).toHaveBeenCalledWith({ | ||
method: 'POST', | ||
path: `/_connector`, | ||
body: { | ||
index_name: 'indexName', | ||
language: 'en', | ||
is_native: true, | ||
name: '', | ||
}, | ||
}); | ||
expect(mockClient.transport.request).toHaveBeenCalledWith({ | ||
method: 'PUT', | ||
path: `/_connector/${connectorId}/_features`, | ||
body: mockFeatures, | ||
}); | ||
expect(mockClient.transport.request).toHaveBeenCalledWith({ | ||
method: 'GET', | ||
path: `/_connector/${connectorId}`, | ||
}); | ||
}); | ||
|
||
it('should throw an error if connector doc is not found', async () => { | ||
mockClient.transport.request | ||
.mockResolvedValueOnce({ id: 'connectorId' }) | ||
.mockRejectedValueOnce(notFoundError); | ||
|
||
await expect( | ||
createConnector(mockClient as unknown as ElasticsearchClient, { | ||
isNative: true, | ||
indexName: 'some-index', | ||
language: 'somelang', | ||
}) | ||
).rejects.toEqual(new Error('Could not retrieve the created connector')); | ||
|
||
expect(mockClient.transport.request).toHaveBeenCalledWith({ | ||
method: 'POST', | ||
path: `/_connector`, | ||
body: { | ||
index_name: 'some-index', | ||
is_native: true, | ||
language: 'somelang', | ||
name: '', | ||
}, | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,9 +44,10 @@ export function connectorStatusToText(connector: Connector): string { | |
); | ||
} | ||
if ( | ||
connector.error === SyncStatus.ERROR || | ||
connector.last_sync_error !== null || | ||
connector.last_access_control_sync_error !== null | ||
connector.last_sync_status === SyncStatus.ERROR || | ||
connector.last_access_control_sync_status === SyncStatus.ERROR || | ||
connector.last_sync_error != null || | ||
connector.last_access_control_sync_error != null | ||
) { | ||
return i18n.translate( | ||
'xpack.enterpriseSearch.content.searchIndices.connectorStatus.syncFailure.label', | ||
|
@@ -87,9 +88,10 @@ export function connectorStatusToColor(connector: Connector): 'warning' | 'dange | |
if ( | ||
isLastSeenOld(connector) || | ||
connectorStatus === ConnectorStatus.ERROR || | ||
connector.error === SyncStatus.ERROR || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous check didn't make sense (error can be arbitrary string), I adapted to check for last sync status |
||
connector.last_sync_error !== null || | ||
connector.last_access_control_sync_error !== null | ||
connector.last_sync_status === SyncStatus.ERROR || | ||
connector.last_access_control_sync_status === SyncStatus.ERROR || | ||
connector.last_sync_error != null || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
connector.last_access_control_sync_error != null | ||
) { | ||
return 'danger'; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have
immediate
refresh policy by default: https://github.com/elastic/elasticsearch/blob/main/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/connector/ConnectorIndexService.java#L133