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

[Console] Add Kibana APIs Support #128562

Merged
merged 16 commits into from
Apr 21, 2022
1 change: 1 addition & 0 deletions src/plugins/console/common/constants/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
*/

export const API_BASE_PATH = '/api/console';
export const KIBANA_API_KEYWORD = 'kbn:';
mibragimov marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion src/plugins/console/common/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/

export { MAJOR_VERSION } from './plugin';
export { API_BASE_PATH } from './api';
export { API_BASE_PATH, KIBANA_API_KEYWORD } from './api';
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ jest.mock('../../../../models/sense_editor', () => {
};
});

jest.mock('../../../../hooks/use_send_current_request_to_es/send_request_to_es', () => ({
sendRequestToES: jest.fn(),
jest.mock('../../../../hooks/use_send_current_request/send_request', () => ({
sendRequest: jest.fn(),
}));
jest.mock('../../../../../lib/autocomplete/get_endpoint_from_position', () => ({
getEndpointFromPosition: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '../../../../contexts';

// Mocked functions
import { sendRequestToES } from '../../../../hooks/use_send_current_request_to_es/send_request_to_es';
import { sendRequest } from '../../../../hooks/use_send_current_request/send_request';
import { getEndpointFromPosition } from '../../../../../lib/autocomplete/get_endpoint_from_position';
import type { DevToolsSettings } from '../../../../../services';
import * as consoleMenuActions from '../console_menu_actions';
Expand Down Expand Up @@ -58,15 +58,15 @@ describe('Legacy (Ace) Console Editor Component Smoke Test', () => {
sandbox.restore();
});

it('calls send current request to ES', async () => {
it('calls send current request', async () => {
(getEndpointFromPosition as jest.Mock).mockReturnValue({ patterns: [] });
(sendRequestToES as jest.Mock).mockRejectedValue({});
(sendRequest as jest.Mock).mockRejectedValue({});
const editor = doMount();
act(() => {
editor.find('button[data-test-subj~="sendRequestButton"]').simulate('click');
});
await nextTick();
expect(sendRequestToES).toBeCalledTimes(1);
expect(sendRequest).toBeCalledTimes(1);
});

it('opens docs', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { ConsoleMenu } from '../../../../components';
import { useEditorReadContext, useServicesContext } from '../../../../contexts';
import {
useSaveCurrentTextObject,
useSendCurrentRequestToES,
useSendCurrentRequest,
useSetInputEditor,
} from '../../../../hooks';
import * as senseEditor from '../../../../models/sense_editor';
Expand Down Expand Up @@ -72,7 +72,7 @@ function EditorUI({ initialTextValue, setEditorInstance }: EditorProps) {

const { settings } = useEditorReadContext();
const setInputEditor = useSetInputEditor();
const sendCurrentRequestToES = useSendCurrentRequestToES();
const sendCurrentRequest = useSendCurrentRequest();
const saveCurrentTextObject = useSaveCurrentTextObject();

const editorRef = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -231,11 +231,11 @@ function EditorUI({ initialTextValue, setEditorInstance }: EditorProps) {
if (!isKeyboardShortcutsDisabled) {
registerCommands({
senseEditor: editorInstanceRef.current!,
sendCurrentRequestToES,
sendCurrentRequest,
openDocumentation,
});
}
}, [sendCurrentRequestToES, openDocumentation, settings]);
}, [openDocumentation, settings, sendCurrentRequest]);

useEffect(() => {
const { current: editor } = editorInstanceRef;
Expand All @@ -262,7 +262,7 @@ function EditorUI({ initialTextValue, setEditorInstance }: EditorProps) {
>
<EuiLink
color="success"
onClick={sendCurrentRequestToES}
onClick={sendCurrentRequest}
data-test-subj="sendRequestButton"
aria-label={i18n.translate('console.sendRequestButtonTooltip', {
defaultMessage: 'Click to send request',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SenseEditor } from '../../../../models/sense_editor';

interface Actions {
senseEditor: SenseEditor;
sendCurrentRequestToES: () => void;
sendCurrentRequest: () => void;
openDocumentation: () => void;
}

Expand All @@ -24,11 +24,7 @@ const COMMANDS = {
GO_TO_LINE: 'gotoline',
};

export function registerCommands({
senseEditor,
sendCurrentRequestToES,
openDocumentation,
}: Actions) {
export function registerCommands({ senseEditor, sendCurrentRequest, openDocumentation }: Actions) {
const throttledAutoIndent = throttle(() => senseEditor.autoIndent(), 500, {
leading: true,
trailing: true,
Expand All @@ -39,7 +35,7 @@ export function registerCommands({
keys: { win: 'Ctrl-Enter', mac: 'Command-Enter' },
name: COMMANDS.SEND_TO_ELASTICSEARCH,
fn: () => {
sendCurrentRequestToES();
sendCurrentRequest();
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/console/public/application/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

export { useSetInputEditor } from './use_set_input_editor';
export { useRestoreRequestFromHistory } from './use_restore_request_from_history';
export { useSendCurrentRequestToES } from './use_send_current_request_to_es';
export { useSendCurrentRequest } from './use_send_current_request';
export { useSaveCurrentTextObject } from './use_save_current_text_object';
export { useDataInit } from './use_data_init';
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
* Side Public License, v 1.
*/

export { useSendCurrentRequestToES } from './use_send_current_request_to_es';
export { useSendCurrentRequest } from './use_send_current_request';
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import type { ContextValue } from '../../contexts';

jest.mock('./send_request_to_es', () => ({ sendRequestToES: jest.fn(() => Promise.resolve()) }));
jest.mock('./send_request', () => ({ sendRequest: jest.fn(() => Promise.resolve()) }));

import { sendRequestToES } from './send_request_to_es';
import { sendRequest } from './send_request';
import { serviceContextMock } from '../../contexts/services_context.mock';

const mockedSendRequestToES = sendRequestToES as jest.Mock;
const mockedSendRequest = sendRequest as jest.Mock;

describe('sendRequestToES', () => {
describe('sendRequest', () => {
let mockContextValue: ContextValue;

beforeEach(() => {
Expand All @@ -26,8 +26,8 @@ describe('sendRequestToES', () => {
jest.resetAllMocks();
});

it('should send request to ES', async () => {
mockedSendRequestToES.mockResolvedValue([
it('should send request', async () => {
mockedSendRequest.mockResolvedValue([
{
response: {
statusCode: 200,
Expand All @@ -40,17 +40,17 @@ describe('sendRequestToES', () => {
http: mockContextValue.services.http,
requests: [{ method: 'PUT', url: 'test', data: [] }],
};
const results = await sendRequestToES(args);
const results = await sendRequest(args);

const [request] = results;
expect(request.response.statusCode).toEqual(200);
expect(request.response.value).toContain('"acknowledged": true');
expect(mockedSendRequestToES).toHaveBeenCalledWith(args);
expect(mockedSendRequestToES).toHaveBeenCalledTimes(1);
expect(mockedSendRequest).toHaveBeenCalledWith(args);
expect(mockedSendRequest).toHaveBeenCalledTimes(1);
});

it('should send multiple requests to ES', async () => {
mockedSendRequestToES.mockResolvedValue([
it('should send multiple requests', async () => {
mockedSendRequest.mockResolvedValue([
{
response: {
statusCode: 200,
Expand All @@ -70,63 +70,64 @@ describe('sendRequestToES', () => {
{ method: 'GET', url: 'test-2', data: [] },
],
};
const results = await sendRequestToES(args);
const results = await sendRequest(args);

const [firstRequest, secondRequest] = results;
expect(firstRequest.response.statusCode).toEqual(200);
expect(secondRequest.response.statusCode).toEqual(200);
expect(mockedSendRequestToES).toHaveBeenCalledWith(args);
expect(mockedSendRequestToES).toHaveBeenCalledTimes(1);
expect(mockedSendRequest).toHaveBeenCalledWith(args);
expect(mockedSendRequest).toHaveBeenCalledTimes(1);
});

it('should handle errors', async () => {
mockedSendRequestToES.mockRejectedValue({
mockedSendRequest.mockRejectedValue({
response: {
statusCode: 500,
statusText: 'error',
},
});

try {
await sendRequestToES({
await sendRequest({
http: mockContextValue.services.http,
requests: [{ method: 'GET', url: 'test', data: [] }],
});
} catch (error) {
expect(error.response.statusCode).toEqual(500);
expect(error.response.statusText).toEqual('error');
expect(mockedSendRequestToES).toHaveBeenCalledTimes(1);
expect(mockedSendRequest).toHaveBeenCalledTimes(1);
}
});

describe('successful response value', () => {
describe('with text', () => {
it('should return value with lines separated', async () => {
mockedSendRequestToES.mockResolvedValue('\ntest_index-1 [] \ntest_index-2 []\n');
const response = await sendRequestToES({
mockedSendRequest.mockResolvedValue('\ntest_index-1 []\ntest_index-2 []\n');
const response = await sendRequest({
http: mockContextValue.services.http,
requests: [{ method: 'GET', url: 'test-1', data: [] }],
});

expect(response).toMatchInlineSnapshot(`
"
test_index-1 []
test_index-1 []
test_index-2 []
"
`);
expect(mockedSendRequestToES).toHaveBeenCalledTimes(1);
expect(mockedSendRequest).toHaveBeenCalledTimes(1);
});
});

describe('with parsed json', () => {
it('should stringify value', async () => {
mockedSendRequestToES.mockResolvedValue(JSON.stringify({ test: 'some value' }));
const response = await sendRequestToES({
mockedSendRequest.mockResolvedValue(JSON.stringify({ test: 'some value' }));
const response = await sendRequest({
http: mockContextValue.services.http,
requests: [{ method: 'GET', url: 'test-2', data: [] }],
});

expect(typeof response).toBe('string');
expect(mockedSendRequestToES).toHaveBeenCalledTimes(1);
expect(mockedSendRequest).toHaveBeenCalledTimes(1);
});
});
});
Expand Down
Loading