Skip to content
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

[Search] Integrate putUpdateNative with Connectors API #175536

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/kbn-search-connectors/lib/update_native.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 { putUpdateNative } from './update_native';

describe('putUpdateNative lib function', () => {
const mockClient = {
transport: {
request: jest.fn(),
},
};

beforeEach(() => {
jest.clearAllMocks();
});

it('should update connector is_native flag', async () => {
mockClient.transport.request.mockImplementation(() => ({ result: 'updated' }));

await expect(
putUpdateNative(mockClient as unknown as ElasticsearchClient, 'connectorId', true)
).resolves.toEqual({ result: 'updated' });
expect(mockClient.transport.request).toHaveBeenCalledWith({
body: {
is_native: true,
},
method: 'PUT',
path: '/_connector/connectorId/_native',
});
});

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(
putUpdateNative(mockClient as unknown as ElasticsearchClient, 'connectorId', true)
).rejects.toEqual(
new errors.ResponseError({
statusCode: 404,
body: {
error: {
type: `document_missing_exception`,
},
},
} as any)
);
});
});
15 changes: 6 additions & 9 deletions packages/kbn-search-connectors/lib/update_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@
* Side Public License, v 1.
*/

import { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import { Result } from '@elastic/elasticsearch/lib/api/types';

import { CONNECTORS_INDEX } from '..';
import { Connector, ConnectorStatus } from '../types/connectors';
import { ElasticsearchClient } from '@kbn/core-elasticsearch-server';

export const putUpdateNative = async (
client: ElasticsearchClient,
connectorId: string,
isNative: boolean
) => {
const result = await client.update<Connector>({
doc: {
const result = await client.transport.request<Result>({
method: 'PUT',
path: `/_connector/${connectorId}/_native`,
body: {
is_native: isNative,
status: ConnectorStatus.CONFIGURED,
},
id: connectorId,
index: CONNECTORS_INDEX,
});

return result;
};