Skip to content

Commit

Permalink
Use NP elasticsearchclient
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Dec 9, 2019
1 parent 0ed3d4b commit f65a526
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 50 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { ElasticsearchServiceSetup } from 'src/core/server';
import { once } from 'lodash';
import { elasticsearchJsPlugin } from './elasticsearch_js_plugin';
import { ServerShim } from '../types';

const callWithRequest = once((server: any) => {
const callWithRequest = once((elasticsearchService: ElasticsearchServiceSetup) => {
const config = { plugins: [elasticsearchJsPlugin] };
const cluster = server.plugins.elasticsearch.createCluster('watcher', config);

return cluster.callWithRequest;
return elasticsearchService.createClient('watcher', config);
});

export const callWithRequestFactory = (server: ServerShim, request: any) => {
export const callWithRequestFactory = (
elasticsearchService: ElasticsearchServiceSetup,
request: any
) => {
return (...args: any[]) => {
return callWithRequest(server)(request, ...args);
return callWithRequest(elasticsearchService)
.asScoped(request)
.callAsCurrentUser(...args);
};
};
9 changes: 8 additions & 1 deletion x-pack/legacy/plugins/watcher/server/np_ready/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { first } from 'rxjs/operators';
import { Plugin, CoreSetup } from 'src/core/server';
import { i18n } from '@kbn/i18n';
import { PLUGIN } from '../../common/constants';
Expand All @@ -18,9 +19,15 @@ import { registerListFieldsRoute } from './routes/api/register_list_fields_route
import { registerLoadHistoryRoute } from './routes/api/register_load_history_route';

export class WatcherServerPlugin implements Plugin<void, void, any, any> {
async setup({ http }: CoreSetup, { __LEGACY: serverShim }: { __LEGACY: ServerShim }) {
async setup(
{ http, elasticsearch: elasticsearchService }: CoreSetup,
{ __LEGACY: serverShim }: { __LEGACY: ServerShim }
) {
const elasticsearch = await elasticsearchService.adminClient$.pipe(first()).toPromise();
const router = http.createRouter();
const routeDependencies: RouteDependencies = {
elasticsearch,
elasticsearchService,
router,
};
// Register license checker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function getIndices(callWithRequest: any, pattern: string, limit = 10) {
export function registerGetRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
const { pattern } = request.body;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function registerListFieldsRoute(deps: RouteDependencies, legacy: ServerS
const isEsError = isEsErrorFactory(legacy);

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

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function fetchHistoryItem(callWithRequest: any, watchHistoryItemId: string) {
export function registerLoadHistoryRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
const id = request.params.id;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { RequestHandler } from 'src/core/server';
import { callWithInternalUserFactory } from '../../../lib/call_with_internal_user_factory';
import { IClusterClient, RequestHandler } from 'src/core/server';
import { isEsErrorFactory } from '../../../lib/is_es_error_factory';
import { licensePreRoutingFactory } from '../../../lib/license_pre_routing_factory';
// @ts-ignore
import { Settings } from '../../../models/settings';
import { RouteDependencies, ServerShim } from '../../../types';

function fetchClusterSettings(callWithInternalUser: any) {
return callWithInternalUser('cluster.getSettings', {
function fetchClusterSettings(client: IClusterClient) {
return client.callAsInternalUser('cluster.getSettings', {
includeDefaults: true,
filterPath: '**.xpack.notification',
});
Expand All @@ -23,7 +22,7 @@ export function registerLoadRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
try {
const settings = await fetchClusterSettings(callWithInternalUser);
const settings = await fetchClusterSettings(deps.elasticsearch);
return response.ok({ body: Settings.fromUpstreamJson(settings).downstreamJson });
} catch (e) {
// Case: Error from Elasticsearch JS client
Expand All @@ -35,8 +34,6 @@ export function registerLoadRoute(deps: RouteDependencies, legacy: ServerShim) {
return response.internalError({ body: e });
}
};
const callWithInternalUser = callWithInternalUserFactory(legacy);

deps.router.get(
{
path: '/api/watcher/settings',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function acknowledgeAction(callWithRequest: any, watchId: string, actionId: stri
export function registerAcknowledgeRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
const { watchId, actionId } = request.params;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function activateWatch(callWithRequest: any, watchId: string) {
export function registerActivateRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);

const { watchId } = request.params;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function deactivateWatch(callWithRequest: any, watchId: string) {
export function registerDeactivateRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);

const { watchId } = request.params;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function deleteWatch(callWithRequest: any, watchId: string) {
export function registerDeleteRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);

const { watchId } = request.params;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function executeWatch(callWithRequest: any, executeDetails: any, watchJson: any)
export function registerExecuteRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
const executeDetails = ExecuteDetails.fromDownstreamJson(request.body.executeDetails);
const watch = Watch.fromDownstreamJson(request.body.watch);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function fetchHistoryItems(callWithRequest: any, watchId: any, startTime: any) {
export function registerHistoryRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
const { watchId } = request.params;
const { startTime } = request.query;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function fetchWatch(callWithRequest: any, watchId: string) {
export function registerLoadRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);

const id = request.params.id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function saveWatch(callWithRequest: any, id: string, body: any) {
export function registerSaveRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
const { id } = request.params;
const { type, isNew, ...watchConfig } = request.body;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function fetchVisualizeData(callWithRequest: any, index: any, body: any) {
export function registerVisualizeRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);
const watch = Watch.fromDownstreamJson(request.body.watch);
const options = VisualizeOptions.fromDownstreamJson(request.body.options);
const body = watch.getVisualizeQuery(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function deleteWatches(callWithRequest: any, watchIds: string[]) {

export function registerDeleteRoute(deps: RouteDependencies, legacy: ServerShim) {
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);

try {
const results = await deleteWatches(callWithRequest, request.body.watchIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function fetchWatches(callWithRequest: any) {
export function registerListRoute(deps: RouteDependencies, legacy: ServerShim) {
const isEsError = isEsErrorFactory(legacy);
const handler: RequestHandler<any, any, any> = async (ctx, request, response) => {
const callWithRequest = callWithRequestFactory(legacy, request);
const callWithRequest = callWithRequestFactory(deps.elasticsearchService, request);

try {
const hits = await fetchWatches(callWithRequest);
Expand Down
6 changes: 3 additions & 3 deletions x-pack/legacy/plugins/watcher/server/np_ready/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { IRouter } from 'src/core/server';
import { IRouter, ElasticsearchServiceSetup, IClusterClient } from 'src/core/server';
import { XPackMainPlugin } from '../../../xpack_main/xpack_main';
import { ElasticsearchPlugin } from '../../../../../../src/legacy/core_plugins/elasticsearch';

export interface ServerShim {
route: any;
plugins: {
xpack_main: XPackMainPlugin;
watcher: any;
elasticsearch: ElasticsearchPlugin;
};
}

export interface RouteDependencies {
router: IRouter;
elasticsearchService: ElasticsearchServiceSetup;
elasticsearch: IClusterClient;
}

0 comments on commit f65a526

Please sign in to comment.