Skip to content

Commit f65a526

Browse files
committed
Use NP elasticsearchclient
1 parent 0ed3d4b commit f65a526

19 files changed

+39
-50
lines changed

x-pack/legacy/plugins/watcher/server/np_ready/lib/call_with_internal_user_factory.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

x-pack/legacy/plugins/watcher/server/np_ready/lib/call_with_request_factory.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { ElasticsearchServiceSetup } from 'src/core/server';
78
import { once } from 'lodash';
89
import { elasticsearchJsPlugin } from './elasticsearch_js_plugin';
910
import { ServerShim } from '../types';
1011

11-
const callWithRequest = once((server: any) => {
12+
const callWithRequest = once((elasticsearchService: ElasticsearchServiceSetup) => {
1213
const config = { plugins: [elasticsearchJsPlugin] };
13-
const cluster = server.plugins.elasticsearch.createCluster('watcher', config);
14-
15-
return cluster.callWithRequest;
14+
return elasticsearchService.createClient('watcher', config);
1615
});
1716

18-
export const callWithRequestFactory = (server: ServerShim, request: any) => {
17+
export const callWithRequestFactory = (
18+
elasticsearchService: ElasticsearchServiceSetup,
19+
request: any
20+
) => {
1921
return (...args: any[]) => {
20-
return callWithRequest(server)(request, ...args);
22+
return callWithRequest(elasticsearchService)
23+
.asScoped(request)
24+
.callAsCurrentUser(...args);
2125
};
2226
};

x-pack/legacy/plugins/watcher/server/np_ready/plugin.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6+
import { first } from 'rxjs/operators';
67
import { Plugin, CoreSetup } from 'src/core/server';
78
import { i18n } from '@kbn/i18n';
89
import { PLUGIN } from '../../common/constants';
@@ -18,9 +19,15 @@ import { registerListFieldsRoute } from './routes/api/register_list_fields_route
1819
import { registerLoadHistoryRoute } from './routes/api/register_load_history_route';
1920

2021
export class WatcherServerPlugin implements Plugin<void, void, any, any> {
21-
async setup({ http }: CoreSetup, { __LEGACY: serverShim }: { __LEGACY: ServerShim }) {
22+
async setup(
23+
{ http, elasticsearch: elasticsearchService }: CoreSetup,
24+
{ __LEGACY: serverShim }: { __LEGACY: ServerShim }
25+
) {
26+
const elasticsearch = await elasticsearchService.adminClient$.pipe(first()).toPromise();
2227
const router = http.createRouter();
2328
const routeDependencies: RouteDependencies = {
29+
elasticsearch,
30+
elasticsearchService,
2431
router,
2532
};
2633
// Register license checker

x-pack/legacy/plugins/watcher/server/np_ready/routes/api/indices/register_get_route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function getIndices(callWithRequest: any, pattern: string, limit = 10) {
6363
export function registerGetRoute(deps: RouteDependencies, legacy: ServerShim) {
6464
const isEsError = isEsErrorFactory(legacy);
6565
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
66-
const callWithRequest = callWithRequestFactory(legacy, request);
66+
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
6767
const { pattern } = request.body;
6868

6969
try {

x-pack/legacy/plugins/watcher/server/np_ready/routes/api/register_list_fields_route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function registerListFieldsRoute(deps: RouteDependencies, legacy: ServerS
2828
const isEsError = isEsErrorFactory(legacy);
2929

3030
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
31-
const callWithRequest = callWithRequestFactory(legacy, request);
31+
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
3232
const { indexes } = request.body;
3333

3434
try {

x-pack/legacy/plugins/watcher/server/np_ready/routes/api/register_load_history_route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function fetchHistoryItem(callWithRequest: any, watchHistoryItemId: string) {
3131
export function registerLoadHistoryRoute(deps: RouteDependencies, legacy: ServerShim) {
3232
const isEsError = isEsErrorFactory(legacy);
3333
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
34-
const callWithRequest = callWithRequestFactory(legacy, request);
34+
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
3535
const id = request.params.id;
3636

3737
try {

x-pack/legacy/plugins/watcher/server/np_ready/routes/api/settings/register_load_route.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { RequestHandler } from 'src/core/server';
8-
import { callWithInternalUserFactory } from '../../../lib/call_with_internal_user_factory';
7+
import { IClusterClient, RequestHandler } from 'src/core/server';
98
import { isEsErrorFactory } from '../../../lib/is_es_error_factory';
109
import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory';
1110
// @ts-ignore
1211
import { Settings } from '../../../models/settings';
1312
import { RouteDependencies, ServerShim } from '../../../types';
1413

15-
function fetchClusterSettings(callWithInternalUser: any) {
16-
return callWithInternalUser('cluster.getSettings', {
14+
function fetchClusterSettings(client: IClusterClient) {
15+
return client.callAsInternalUser('cluster.getSettings', {
1716
includeDefaults: true,
1817
filterPath: '**.xpack.notification',
1918
});
@@ -23,7 +22,7 @@ export function registerLoadRoute(deps: RouteDependencies, legacy: ServerShim) {
2322
const isEsError = isEsErrorFactory(legacy);
2423
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
2524
try {
26-
const settings = await fetchClusterSettings(callWithInternalUser);
25+
const settings = await fetchClusterSettings(deps.elasticsearch);
2726
return response.ok({ body: Settings.fromUpstreamJson(settings).downstreamJson });
2827
} catch (e) {
2928
// Case: Error from Elasticsearch JS client
@@ -35,8 +34,6 @@ export function registerLoadRoute(deps: RouteDependencies, legacy: ServerShim) {
3534
return response.internalError({ body: e });
3635
}
3736
};
38-
const callWithInternalUser = callWithInternalUserFactory(legacy);
39-
4037
deps.router.get(
4138
{
4239
path: '/api/watcher/settings',

x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/action/register_acknowledge_route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function acknowledgeAction(callWithRequest: any, watchId: string, actionId: stri
2424
export function registerAcknowledgeRoute(deps: RouteDependencies, legacy: ServerShim) {
2525
const isEsError = isEsErrorFactory(legacy);
2626
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
27-
const callWithRequest = callWithRequestFactory(legacy, request);
27+
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
2828
const { watchId, actionId } = request.params;
2929

3030
try {

x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_activate_route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function activateWatch(callWithRequest: any, watchId: string) {
2323
export function registerActivateRoute(deps: RouteDependencies, legacy: ServerShim) {
2424
const isEsError = isEsErrorFactory(legacy);
2525
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
26-
const callWithRequest = callWithRequestFactory(legacy, request);
26+
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
2727

2828
const { watchId } = request.params;
2929

x-pack/legacy/plugins/watcher/server/np_ready/routes/api/watch/register_deactivate_route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function deactivateWatch(callWithRequest: any, watchId: string) {
2222
export function registerDeactivateRoute(deps: RouteDependencies, legacy: ServerShim) {
2323
const isEsError = isEsErrorFactory(legacy);
2424
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
25-
const callWithRequest = callWithRequestFactory(legacy, request);
25+
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
2626

2727
const { watchId } = request.params;
2828

0 commit comments

Comments
 (0)