mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[Search] Use Connector API for update_service_type (#176306)
## Summary Use connectors API for update service type operation
This commit is contained in:
parent
0721933085
commit
cc00868225
2 changed files with 104 additions and 31 deletions
|
@ -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)
|
||||
);
|
||||
});
|
||||
});
|
|
@ -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,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue