diff --git a/packages/kbn-search-connectors/lib/update_connector_service_type.test.ts b/packages/kbn-search-connectors/lib/update_connector_service_type.test.ts new file mode 100644 index 0000000000000..9a452e982430d --- /dev/null +++ b/packages/kbn-search-connectors/lib/update_connector_service_type.test.ts @@ -0,0 +1,86 @@ +/* + * 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 { ElasticsearchClient } from '@kbn/core/server'; + +import { errors } from '@elastic/elasticsearch'; + +import { updateConnectorServiceType } from './update_connector_service_type'; + +describe('updateConnectorServiceType lib function', () => { + const mockClient = { + transport: { + request: jest.fn(), + }, + }; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should update connector service type', async () => { + mockClient.transport.request.mockImplementation(() => ({ result: 'updated' })); + + const connectorId = 'connectorId'; + const serviceType = 'new-service-type'; + + const result = await updateConnectorServiceType( + mockClient as unknown as ElasticsearchClient, + connectorId, + serviceType + ); + expect(result).toEqual({ result: 'updated' }); + + expect(mockClient.transport.request).toHaveBeenNthCalledWith(1, { + method: 'PUT', + path: `/_connector/${connectorId}/_configuration`, + body: { + configuration: {}, + }, + }); + expect(mockClient.transport.request).toHaveBeenNthCalledWith(2, { + method: 'PUT', + path: `/_connector/${connectorId}/_service_type`, + body: { + service_type: serviceType, + }, + }); + expect(mockClient.transport.request).toHaveBeenCalledTimes(2); + }); + + it('should not index document if there is no connector', async () => { + mockClient.transport.request.mockImplementationOnce(() => { + return Promise.reject( + new errors.ResponseError({ + statusCode: 404, + body: { + error: { + type: `document_missing_exception`, + }, + }, + } as any) + ); + }); + await expect( + updateConnectorServiceType( + mockClient as unknown as ElasticsearchClient, + 'connectorId', + 'new-service-type' + ) + ).rejects.toEqual( + new errors.ResponseError({ + statusCode: 404, + body: { + error: { + type: `document_missing_exception`, + }, + }, + } as any) + ); + }); +}); diff --git a/packages/kbn-search-connectors/lib/update_connector_service_type.ts b/packages/kbn-search-connectors/lib/update_connector_service_type.ts index 3040918a75b95..70d1105e69c3c 100644 --- a/packages/kbn-search-connectors/lib/update_connector_service_type.ts +++ b/packages/kbn-search-connectors/lib/update_connector_service_type.ts @@ -6,42 +6,29 @@ * Side Public License, v 1. */ +import { Result } from '@elastic/elasticsearch/lib/api/types'; import { ElasticsearchClient } from '@kbn/core/server'; -import { i18n } from '@kbn/i18n'; - -import { CONNECTORS_INDEX, fetchConnectorById } from '..'; - -import { ConnectorDocument, ConnectorStatus } from '../types/connectors'; export const updateConnectorServiceType = async ( client: ElasticsearchClient, connectorId: string, serviceType: string ) => { - const connectorResult = await fetchConnectorById(client, connectorId); - - if (connectorResult?.value) { - const result = await client.index({ - document: { - ...connectorResult.value, - configuration: {}, - service_type: serviceType, - status: - connectorResult.value.status === ConnectorStatus.CREATED - ? ConnectorStatus.CREATED - : ConnectorStatus.NEEDS_CONFIGURATION, - }, - id: connectorId, - index: CONNECTORS_INDEX, - if_seq_no: connectorResult.seqNo, - if_primary_term: connectorResult.primaryTerm, - }); - return result; - } else { - throw new Error( - i18n.translate('searchConnectors.server.connectors.serviceType.error', { - defaultMessage: 'Could not find document', - }) - ); - } + // First clear connector configuration + await client.transport.request({ + method: 'PUT', + path: `/_connector/${connectorId}/_configuration`, + body: { + configuration: {}, + }, + }); + // Then update service type, on startup connector framework + // will populate missing config fields + return await client.transport.request({ + method: 'PUT', + path: `/_connector/${connectorId}/_service_type`, + body: { + service_type: serviceType, + }, + }); };