[Connector] Use api to fetch connector index names (#183308)

This commit is contained in:
Jedr Blaszyk 2024-05-14 14:25:20 +02:00 committed by GitHub
parent ba08761ba8
commit 6f59810d3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 120 additions and 8 deletions

View file

@ -0,0 +1,117 @@
/*
* 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 { fetchConnectorIndexNames } from './fetch_connector_index_names';
const otherError = {
meta: {
body: {
error: {
type: 'other_error',
},
},
},
};
const indexNotFoundError = new errors.ResponseError({
statusCode: 404,
body: {
error: {
type: `index_not_found_exception`,
},
},
} as any);
describe('fetchConnectorIndexNames lib function', () => {
const mockClient = {
transport: {
request: jest.fn(),
},
};
beforeEach(() => {
jest.clearAllMocks();
});
it('should fetch connector index names', async () => {
const mockResult = {
count: 2,
results: [
{ id: 'connectorId1', index_name: 'indexName1' },
{ id: 'connectorId2', index_name: 'indexName2' },
],
};
mockClient.transport.request.mockResolvedValue(mockResult);
await expect(
fetchConnectorIndexNames(mockClient as unknown as ElasticsearchClient)
).resolves.toEqual(['indexName1', 'indexName2']);
expect(mockClient.transport.request).toHaveBeenCalledWith({
method: 'GET',
path: `/_connector`,
querystring: {
from: 0,
size: 1000,
},
});
});
it('should return [] if no connectors are found', async () => {
const mockResult = {
count: 0,
results: [],
};
mockClient.transport.request.mockResolvedValue(mockResult);
await expect(
fetchConnectorIndexNames(mockClient as unknown as ElasticsearchClient)
).resolves.toEqual([]);
expect(mockClient.transport.request).toHaveBeenCalledWith({
method: 'GET',
path: `/_connector`,
querystring: {
from: 0,
size: 1000,
},
});
});
it('should return [] if connector index is missing', async () => {
mockClient.transport.request.mockImplementationOnce(() => Promise.reject(indexNotFoundError));
await expect(
fetchConnectorIndexNames(mockClient as unknown as ElasticsearchClient)
).resolves.toEqual([]);
expect(mockClient.transport.request).toHaveBeenCalledWith({
method: 'GET',
path: `/_connector`,
querystring: {
from: 0,
size: 1000,
},
});
});
it('should throw on other errors', async () => {
mockClient.transport.request.mockImplementationOnce(() => Promise.reject(otherError));
await expect(fetchConnectorIndexNames(mockClient as any)).rejects.toEqual(otherError);
expect(mockClient.transport.request).toHaveBeenCalledWith({
method: 'GET',
path: `/_connector`,
querystring: {
from: 0,
size: 1000,
},
});
});
});

View file

@ -8,18 +8,13 @@
import { ElasticsearchClient } from '@kbn/core/server';
import { CONNECTORS_INDEX } from '..';
import { fetchConnectors } from './fetch_connectors';
import { isIndexNotFoundException } from '../utils/identify_exceptions';
export async function fetchConnectorIndexNames(client: ElasticsearchClient): Promise<string[]> {
try {
const result = await client.search({
_source: false,
fields: [{ field: 'index_name' }],
index: CONNECTORS_INDEX,
size: 10000,
});
return (result?.hits.hits ?? []).map((field) => field.fields?.index_name[0] ?? '');
const allConnectors = await fetchConnectors(client);
return (allConnectors ?? []).map((connector) => connector.index_name ?? '');
} catch (error) {
if (isIndexNotFoundException(error)) {
return [];