Skip to content

Commit

Permalink
[Search] Use Connector API for update_service_type (elastic#176306)
Browse files Browse the repository at this point in the history
## Summary

Use connectors API for update service type operation
  • Loading branch information
jedrazb authored Feb 7, 2024
1 parent 0721933 commit cc00868
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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)
);
});
});
49 changes: 18 additions & 31 deletions packages/kbn-search-connectors/lib/update_connector_service_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConnectorDocument>({
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<Result>({
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<Result>({
method: 'PUT',
path: `/_connector/${connectorId}/_service_type`,
body: {
service_type: serviceType,
},
});
};

0 comments on commit cc00868

Please sign in to comment.