From 3a41dd8e917a31142e32a3f7fe01c82de4cdf9f4 Mon Sep 17 00:00:00 2001 From: Jason Rhodes Date: Tue, 3 Dec 2019 17:16:39 -0500 Subject: [PATCH] Converts getApmIndices function to accept saved object client, implements usage in infra --- .../plugins/apm/server/lib/helpers/setup_request.ts | 4 ++-- .../lib/settings/apm_indices/get_apm_indices.ts | 4 ++-- .../plugins/apm/server/routes/settings/apm_indices.ts | 5 ++++- x-pack/legacy/plugins/infra/index.ts | 11 +++-------- .../server/lib/adapters/framework/adapter_types.ts | 5 ++--- .../infra/server/routes/metadata/lib/has_apm_data.ts | 2 +- x-pack/plugins/apm/server/plugin.ts | 11 ++++++++--- 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/x-pack/legacy/plugins/apm/server/lib/helpers/setup_request.ts b/x-pack/legacy/plugins/apm/server/lib/helpers/setup_request.ts index 2578b3d2e7deb..6fd1456068433 100644 --- a/x-pack/legacy/plugins/apm/server/lib/helpers/setup_request.ts +++ b/x-pack/legacy/plugins/apm/server/lib/helpers/setup_request.ts @@ -70,10 +70,10 @@ export async function setupRequest( context: APMRequestHandlerContext, request: KibanaRequest ): Promise> { - const { config } = context; + const { config, core } = context; const { query } = context.params; - const indices = await getApmIndices(context.core, context.config); + const indices = await getApmIndices(core.savedObjects.client, config); const dynamicIndexPattern = await getDynamicIndexPattern({ context, diff --git a/x-pack/legacy/plugins/apm/server/lib/settings/apm_indices/get_apm_indices.ts b/x-pack/legacy/plugins/apm/server/lib/settings/apm_indices/get_apm_indices.ts index ddb1a60a4c85b..2918c794ed68a 100644 --- a/x-pack/legacy/plugins/apm/server/lib/settings/apm_indices/get_apm_indices.ts +++ b/x-pack/legacy/plugins/apm/server/lib/settings/apm_indices/get_apm_indices.ts @@ -59,12 +59,12 @@ export function getApmIndicesConfig(config: APMConfig): ApmIndicesConfig { // } export async function getApmIndices( - core: APMRequestHandlerContext['core'], + savedObjectsClient: SavedObjectsClientContract, config: APMRequestHandlerContext['config'] ) { try { const apmIndicesSavedObject = await getApmIndicesSavedObject( - core.savedObjects.client + savedObjectsClient ); const apmIndicesConfig = getApmIndicesConfig(config); return merge({}, apmIndicesConfig, apmIndicesSavedObject); diff --git a/x-pack/legacy/plugins/apm/server/routes/settings/apm_indices.ts b/x-pack/legacy/plugins/apm/server/routes/settings/apm_indices.ts index 040cd1f135e9c..d41e32edc50c0 100644 --- a/x-pack/legacy/plugins/apm/server/routes/settings/apm_indices.ts +++ b/x-pack/legacy/plugins/apm/server/routes/settings/apm_indices.ts @@ -26,7 +26,10 @@ export const apmIndicesRoute = createRoute(() => ({ method: 'GET', path: '/api/apm/settings/apm-indices', handler: async ({ context }) => { - return await getApmIndices(context.core, context.config); + return await getApmIndices( + context.core.savedObjects.client, + context.config + ); } })); diff --git a/x-pack/legacy/plugins/infra/index.ts b/x-pack/legacy/plugins/infra/index.ts index 1906e1700d3dc..8e9756c7249eb 100644 --- a/x-pack/legacy/plugins/infra/index.ts +++ b/x-pack/legacy/plugins/infra/index.ts @@ -7,14 +7,14 @@ import { i18n } from '@kbn/i18n'; import JoiNamespace from 'joi'; import { resolve } from 'path'; -import { PluginInitializerContext } from 'src/core/server'; +import { PluginInitializerContext, SavedObjectsClientContract } from 'src/core/server'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import KbnServer from 'src/legacy/server/kbn_server'; import { getConfigSchema } from './server/kibana.index'; import { savedObjectMappings } from './server/saved_objects'; import { plugin, InfraServerPluginDeps } from './server/new_platform_index'; import { InfraSetup } from '../../../plugins/infra/server'; -import { getApmIndices } from '../apm/server/lib/settings/apm_indices/get_apm_indices'; +import { APMPluginContract } from '../../../plugins/apm/server/plugin'; const APP_ID = 'infra'; const logsSampleDataLinkLabel = i18n.translate('xpack.infra.sampleDataLinkLabel', { @@ -103,12 +103,7 @@ export function infra(kibana: any) { __internals: legacyServer.newPlatform.__internals, }, }, - apm: { - // NP_NOTE: This needs migrating once APM's getApmIndices() has been ported to NP. - // On our side we're using the NP savedObjectsClient, but legacyServer.config() needs to go. - getIndices: savedObjectsClient => - getApmIndices({ savedObjectsClient, config: legacyServer.config() }), - }, + apm: plugins.apm as APMPluginContract, }; const infraPluginInstance = plugin(initContext); diff --git a/x-pack/legacy/plugins/infra/server/lib/adapters/framework/adapter_types.ts b/x-pack/legacy/plugins/infra/server/lib/adapters/framework/adapter_types.ts index 318f2c010bb52..b26c3ba265ba0 100644 --- a/x-pack/legacy/plugins/infra/server/lib/adapters/framework/adapter_types.ts +++ b/x-pack/legacy/plugins/infra/server/lib/adapters/framework/adapter_types.ts @@ -9,6 +9,7 @@ import { Lifecycle } from 'hapi'; import { ObjectType } from '@kbn/config-schema'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { RouteMethod, RouteConfig } from '../../../../../../../../src/core/server'; +import { APMPluginContract } from '../../../../../../../plugins/apm/server/plugin'; interface ApmIndices { 'apm_oss.transactionIndices': string | undefined; @@ -25,9 +26,7 @@ export interface InfraServerPluginDeps { indexPatternsServiceFactory: any; }; features: any; - apm: { - getIndices: (savedObjectsClient: any) => Promise; - }; + apm: APMPluginContract; ___legacy: any; } diff --git a/x-pack/legacy/plugins/infra/server/routes/metadata/lib/has_apm_data.ts b/x-pack/legacy/plugins/infra/server/routes/metadata/lib/has_apm_data.ts index a3aee59e799d2..ab242804173c0 100644 --- a/x-pack/legacy/plugins/infra/server/routes/metadata/lib/has_apm_data.ts +++ b/x-pack/legacy/plugins/infra/server/routes/metadata/lib/has_apm_data.ts @@ -17,7 +17,7 @@ export const hasAPMData = async ( nodeId: string, nodeType: 'host' | 'pod' | 'container' ) => { - const apmIndices = await framework.plugins.apm.getIndices( + const apmIndices = await framework.plugins.apm.getApmIndices( requestContext.core.savedObjects.client ); const apmIndex = apmIndices['apm_oss.transactionIndices'] || 'apm-*'; diff --git a/x-pack/plugins/apm/server/plugin.ts b/x-pack/plugins/apm/server/plugin.ts index 4788525e60001..a6bc56c8d77e5 100644 --- a/x-pack/plugins/apm/server/plugin.ts +++ b/x-pack/plugins/apm/server/plugin.ts @@ -8,6 +8,7 @@ import { Plugin, CoreSetup, RequestHandlerContext, + SavedObjectsClientContract, } from 'src/core/server'; import { Observable, combineLatest, AsyncSubject } from 'rxjs'; import { map } from 'rxjs/operators'; @@ -26,14 +27,18 @@ export interface LegacySetup { export interface APMPluginContract { config$: Observable; registerLegacyAPI: (__LEGACY: LegacySetup) => void; + getApmIndices: ( + savedObjectsClient: SavedObjectsClientContract + ) => ReturnType; } export class APMPlugin implements Plugin { legacySetup$: AsyncSubject; - currentConfig?: APMConfig; + currentConfig: APMConfig; constructor(private readonly initContext: PluginInitializerContext) { this.initContext = initContext; this.legacySetup$ = new AsyncSubject(); + this.currentConfig = {} as APMConfig; } public setup( @@ -69,8 +74,8 @@ export class APMPlugin implements Plugin { this.legacySetup$.next(__LEGACY); this.legacySetup$.complete(); }), - getApmIndices: async (requestContext: RequestHandlerContext) => { - return getApmIndices(requestContext.core, this.currentConfig as APMConfig); + getApmIndices: async (savedObjectsClient: SavedObjectsClientContract) => { + return getApmIndices(savedObjectsClient, this.currentConfig); }, }; }