Skip to content

Commit

Permalink
[Search] Integrate putUpdateNative with Connectors API (elastic#175536)
Browse files Browse the repository at this point in the history
## Summary
- Use Connectors API endpoint `_connector/{id}/_native` to update
connector `is_native` flag
  • Loading branch information
jedrazb authored Feb 6, 2024
1 parent d69108c commit 083c17b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
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;
};

0 comments on commit 083c17b

Please sign in to comment.