Skip to content

Commit 7376e4c

Browse files
[Console] Get ES Config from core (#75406)
* Server side changes - removed console_legacy plugin! - added new es_config endpoint that returns server side es config at the moment this is just the first value in hosts - Slight refactor to how routes are registered to bring them more in line with other ES UI plugins * Client side update - Updated the client to not get es host from injected metadata. Instead use the new endpoint created server side that returns this value - Added a small README.md regarding the hooks lib and need to refactor use of jQuery in console - Write code to init the es host value on the client once at start up in a non-blocking way. If this fails we just use the default value of http://localhost:9200 as this powers non-essential console functionality (i.e., copy as cURL). * fix type issue and jest tests * fix another type issue * simplify proxy assignment in proxy handler mock Co-authored-by: Elastic Machine <[email protected]>
1 parent 506bf6c commit 7376e4c

File tree

25 files changed

+451
-164
lines changed

25 files changed

+451
-164
lines changed

src/legacy/core_plugins/console_legacy/index.ts

-49
This file was deleted.

src/legacy/core_plugins/console_legacy/package.json

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
export interface EsConfigApiResponse {
21+
/**
22+
* This is the first host in the hosts array that Kibana is configured to use
23+
* to communicate with ES.
24+
*
25+
* At the moment this is used to power the copy as cURL functionality in Console
26+
* to complete the host portion of the URL.
27+
*/
28+
host?: string;
29+
}

src/plugins/console/public/application/containers/editor/legacy/console_editor/editor.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ const inputId = 'ConAppInputTextarea';
6767

6868
function EditorUI({ initialTextValue }: EditorProps) {
6969
const {
70-
services: { history, notifications, settings: settingsService },
70+
services: { history, notifications, settings: settingsService, esHostService },
7171
docLinkVersion,
72-
elasticsearchUrl,
7372
} = useServicesContext();
7473

7574
const { settings } = useEditorReadContext();
@@ -232,7 +231,7 @@ function EditorUI({ initialTextValue }: EditorProps) {
232231
<EuiFlexItem>
233232
<ConsoleMenu
234233
getCurl={() => {
235-
return editorInstanceRef.current!.getRequestsAsCURL(elasticsearchUrl);
234+
return editorInstanceRef.current!.getRequestsAsCURL(esHostService.getHost());
236235
}}
237236
getDocumentation={() => {
238237
return getDocumentation(editorInstanceRef.current!, docLinkVersion);

src/plugins/console/public/application/contexts/services_context.mock.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,27 @@
1717
* under the License.
1818
*/
1919
import { notificationServiceMock } from '../../../../../core/public/mocks';
20+
import { httpServiceMock } from '../../../../../core/public/mocks';
21+
2022
import { HistoryMock } from '../../services/history.mock';
2123
import { SettingsMock } from '../../services/settings.mock';
2224
import { StorageMock } from '../../services/storage.mock';
25+
import { createApi, createEsHostService } from '../lib';
2326

2427
import { ContextValue } from './services_context';
2528

2629
export const serviceContextMock = {
2730
create: (): ContextValue => {
2831
const storage = new StorageMock({} as any, 'test');
32+
const http = httpServiceMock.createSetupContract();
33+
const api = createApi({ http });
34+
const esHostService = createEsHostService({ api });
2935
(storage.keys as jest.Mock).mockImplementation(() => []);
3036
return {
31-
elasticsearchUrl: 'test',
3237
services: {
3338
trackUiMetric: { count: () => {}, load: () => {} },
3439
storage,
40+
esHostService,
3541
settings: new SettingsMock(storage),
3642
history: new HistoryMock(storage),
3743
notifications: notificationServiceMock.createSetupContract(),

src/plugins/console/public/application/contexts/services_context.tsx

+19-11
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,25 @@
1717
* under the License.
1818
*/
1919

20-
import React, { createContext, useContext } from 'react';
20+
import React, { createContext, useContext, useEffect } from 'react';
2121
import { NotificationsSetup } from 'kibana/public';
22-
import { History, Storage, Settings } from '../../services';
22+
import { History, Settings, Storage } from '../../services';
2323
import { ObjectStorageClient } from '../../../common/types';
2424
import { MetricsTracker } from '../../types';
25+
import { EsHostService } from '../lib';
26+
27+
interface ContextServices {
28+
history: History;
29+
storage: Storage;
30+
settings: Settings;
31+
notifications: NotificationsSetup;
32+
objectStorageClient: ObjectStorageClient;
33+
trackUiMetric: MetricsTracker;
34+
esHostService: EsHostService;
35+
}
2536

2637
export interface ContextValue {
27-
services: {
28-
history: History;
29-
storage: Storage;
30-
settings: Settings;
31-
notifications: NotificationsSetup;
32-
objectStorageClient: ObjectStorageClient;
33-
trackUiMetric: MetricsTracker;
34-
};
35-
elasticsearchUrl: string;
38+
services: ContextServices;
3639
docLinkVersion: string;
3740
}
3841

@@ -44,6 +47,11 @@ interface ContextProps {
4447
const ServicesContext = createContext<ContextValue>(null as any);
4548

4649
export function ServicesContextProvider({ children, value }: ContextProps) {
50+
useEffect(() => {
51+
// Fire and forget, we attempt to init the host service once.
52+
value.services.esHostService.init();
53+
}, [value.services.esHostService]);
54+
4755
return <ServicesContext.Provider value={value}>{children}</ServicesContext.Provider>;
4856
}
4957

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Notes
2+
3+
* Do not add any code directly to this directory. This code should be moved to the neighbouring `lib` directory to be in line with future ES UI plugin patterns.
4+
5+
* The `es.send` method uses $.ajax under the hood and needs to be refactored to use the new platform-provided http client.

src/plugins/console/public/application/index.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@
1919

2020
import React from 'react';
2121
import { render, unmountComponentAtNode } from 'react-dom';
22-
import { NotificationsSetup } from 'src/core/public';
22+
import { HttpSetup, NotificationsSetup } from 'src/core/public';
2323
import { ServicesContextProvider, EditorContextProvider, RequestContextProvider } from './contexts';
2424
import { Main } from './containers';
2525
import { createStorage, createHistory, createSettings } from '../services';
2626
import * as localStorageObjectClient from '../lib/local_storage_object_client';
2727
import { createUsageTracker } from '../services/tracker';
2828
import { UsageCollectionSetup } from '../../../usage_collection/public';
29+
import { createApi, createEsHostService } from './lib';
2930

3031
export interface BootDependencies {
32+
http: HttpSetup;
3133
docLinkVersion: string;
3234
I18nContext: any;
3335
notifications: NotificationsSetup;
34-
elasticsearchUrl: string;
3536
usageCollection?: UsageCollectionSetup;
3637
element: HTMLElement;
3738
}
@@ -40,9 +41,9 @@ export function renderApp({
4041
I18nContext,
4142
notifications,
4243
docLinkVersion,
43-
elasticsearchUrl,
4444
usageCollection,
4545
element,
46+
http,
4647
}: BootDependencies) {
4748
const trackUiMetric = createUsageTracker(usageCollection);
4849
trackUiMetric.load('opened_app');
@@ -54,14 +55,16 @@ export function renderApp({
5455
const history = createHistory({ storage });
5556
const settings = createSettings({ storage });
5657
const objectStorageClient = localStorageObjectClient.create(storage);
58+
const api = createApi({ http });
59+
const esHostService = createEsHostService({ api });
5760

5861
render(
5962
<I18nContext>
6063
<ServicesContextProvider
6164
value={{
62-
elasticsearchUrl,
6365
docLinkVersion,
6466
services: {
67+
esHostService,
6568
storage,
6669
history,
6770
settings,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { HttpSetup } from 'kibana/public';
21+
import { EsConfigApiResponse } from '../../../common/types/api_responses';
22+
import { sendRequest } from '../../shared_imports';
23+
24+
interface Dependencies {
25+
http: HttpSetup;
26+
}
27+
28+
export type Api = ReturnType<typeof createApi>;
29+
30+
export const createApi = ({ http }: Dependencies) => {
31+
return {
32+
getEsConfig: () => {
33+
return sendRequest<EsConfigApiResponse>(http, {
34+
path: '/api/console/es_config',
35+
method: 'get',
36+
});
37+
},
38+
};
39+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { Api } from './api';
21+
22+
/**
23+
* Very simple state for holding the current ES host.
24+
*
25+
* This is used to power the copy as cURL functionality.
26+
*/
27+
export class EsHostService {
28+
private host = 'http://localhost:9200';
29+
30+
constructor(private readonly api: Api) {}
31+
32+
private setHost(host: string): void {
33+
this.host = host;
34+
}
35+
36+
/**
37+
* Initialize the host value based on the value set on the server.
38+
*
39+
* This call is necessary because this value can only be retrieved at
40+
* runtime.
41+
*/
42+
public async init() {
43+
const { data } = await this.api.getEsConfig();
44+
if (data && data.host) {
45+
this.setHost(data.host);
46+
}
47+
}
48+
49+
public getHost(): string {
50+
return this.host;
51+
}
52+
}
53+
54+
export const createEsHostService = ({ api }: { api: Api }) => new EsHostService(api);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
export { createApi, Api } from './api';
21+
export { createEsHostService, EsHostService } from './es_host_service';

src/plugins/console/public/plugin.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { AppSetupUIPluginDependencies } from './types';
2525

2626
export class ConsoleUIPlugin implements Plugin<void, void, AppSetupUIPluginDependencies> {
2727
public setup(
28-
{ notifications, getStartServices }: CoreSetup,
28+
{ notifications, getStartServices, http }: CoreSetup,
2929
{ devTools, home, usageCollection }: AppSetupUIPluginDependencies
3030
) {
3131
home.featureCatalogue.register({
@@ -53,23 +53,17 @@ export class ConsoleUIPlugin implements Plugin<void, void, AppSetupUIPluginDepen
5353
const [core] = await getStartServices();
5454

5555
const {
56-
injectedMetadata,
5756
i18n: { Context: I18nContext },
5857
docLinks: { DOC_LINK_VERSION },
5958
} = core;
6059

6160
const { renderApp } = await import('./application');
6261

63-
const elasticsearchUrl = injectedMetadata.getInjectedVar(
64-
'elasticsearchUrl',
65-
'http://localhost:9200'
66-
) as string;
67-
6862
return renderApp({
63+
http,
6964
docLinkVersion: DOC_LINK_VERSION,
7065
I18nContext,
7166
notifications,
72-
elasticsearchUrl,
7367
usageCollection,
7468
element,
7569
});

0 commit comments

Comments
 (0)