kibana/x-pack/legacy/plugins/spaces/index.ts
Larry Gregory 9c2fa892ca
[7.x] Spaces - Client NP Migration, Phase 1 (#40856) (#53113)
* Spaces - Client NP Migration, Phase 1 (#40856)

* shimming NP for spaces client-side plugin

* refresh active space in nav control when updated

* fix advanced settings screen

* allow npStart from unauthed routes

* use NP for deriving space management url

* remove security's usage of SpacesManager

* remove usages of ui/capabilities

* fix tests

* implement NP plugin interface

* remove hack in favor of convention in migration guide

* shim feature catalogue registration

* streamline nav control, and handle async loading more gracefully

* adding opaqueId

* fixes from merge

* fix merge from master

* fixing merge from master

* move _active_space route to NP

* moving to the NP feature catalogue registry

* moving setup to setup phase

* optimizing active space retrieval

* reverting test isolation change

* Apply suggestions from code review

Co-Authored-By: Aleh Zasypkin <aleh.zasypkin@gmail.com>

* removing unnecessary PluginInitializerContext

* updating advanced settings subtitle

* using NP anonymousPaths service

* additional nav_control_popover cleanup

* additional cleanup

* testing out onActiveSpaceChange$ property

* make the linter happy

* make the type checker happy

* fixing types

* fix merge from master

* spaces LP init should run on all pages, not just the kibana app

* address nits

* fix infra/logs, and the spaces disabled scenario

* fix typescript errors

* revert changes to infra plugin

* reintroducing activeSpace injected var for legacy plugins

* fixing react deprecation warning and unhandled promise rejection

* restore activeSpace default var

* spaces does not need to check its own enabled status

* fix from merge

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

* fix backport merge
2019-12-16 12:13:05 -05:00

157 lines
5.3 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { resolve } from 'path';
import KbnServer, { Server } from 'src/legacy/server/kbn_server';
import { Legacy } from 'kibana';
import { KibanaRequest } from '../../../../src/core/server';
import { SpacesServiceSetup } from '../../../plugins/spaces/server/spaces_service/spaces_service';
import { SpacesPluginSetup } from '../../../plugins/spaces/server';
// @ts-ignore
import { AuditLogger } from '../../server/lib/audit_logger';
import mappings from './mappings.json';
import { wrapError } from './server/lib/errors';
import { migrateToKibana660 } from './server/lib/migrations';
// @ts-ignore
import { watchStatusAndLicenseToInitialize } from '../../server/lib/watch_status_and_license_to_initialize';
import { initSpaceSelectorView, initEnterSpaceView } from './server/routes/views';
export interface LegacySpacesPlugin {
getSpaceId: (request: Legacy.Request) => ReturnType<SpacesServiceSetup['getSpaceId']>;
getActiveSpace: (request: Legacy.Request) => ReturnType<SpacesServiceSetup['getActiveSpace']>;
spaceIdToNamespace: SpacesServiceSetup['spaceIdToNamespace'];
namespaceToSpaceId: SpacesServiceSetup['namespaceToSpaceId'];
getBasePath: SpacesServiceSetup['getBasePath'];
}
export const spaces = (kibana: Record<string, any>) =>
new kibana.Plugin({
id: 'spaces',
configPrefix: 'xpack.spaces',
publicDir: resolve(__dirname, 'public'),
require: ['kibana', 'elasticsearch', 'xpack_main'],
uiCapabilities() {
return {
spaces: {
manage: true,
},
management: {
kibana: {
spaces: true,
},
},
};
},
uiExports: {
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
managementSections: ['plugins/spaces/views/management'],
apps: [
{
id: 'space_selector',
title: 'Spaces',
main: 'plugins/spaces/views/space_selector',
url: 'space_selector',
hidden: true,
},
],
hacks: ['plugins/spaces/legacy'],
mappings,
migrations: {
space: {
'6.6.0': migrateToKibana660,
},
},
savedObjectSchemas: {
space: {
isNamespaceAgnostic: true,
hidden: true,
},
},
home: [],
injectDefaultVars(server: Server) {
return {
serverBasePath: server.config().get('server.basePath'),
activeSpace: null,
};
},
async replaceInjectedVars(
vars: Record<string, any>,
request: Legacy.Request,
server: Server
) {
// NOTICE: use of `activeSpace` is deprecated and will not be made available in the New Platform.
// Known usages:
// - x-pack/legacy/plugins/infra/public/utils/use_kibana_space_id.ts
const spacesPlugin = server.newPlatform.setup.plugins.spaces as SpacesPluginSetup;
if (!spacesPlugin) {
throw new Error('New Platform XPack Spaces plugin is not available.');
}
const kibanaRequest = KibanaRequest.from(request);
const spaceId = spacesPlugin.spacesService.getSpaceId(kibanaRequest);
const spacesClient = await spacesPlugin.spacesService.scopedClient(kibanaRequest);
try {
vars.activeSpace = {
valid: true,
space: await spacesClient.get(spaceId),
};
} catch (e) {
vars.activeSpace = {
valid: false,
error: wrapError(e).output.payload,
};
}
return vars;
},
},
async init(server: Server) {
const kbnServer = (server as unknown) as KbnServer;
const spacesPlugin = kbnServer.newPlatform.setup.plugins.spaces as SpacesPluginSetup;
if (!spacesPlugin) {
throw new Error('New Platform XPack Spaces plugin is not available.');
}
const config = server.config();
const { registerLegacyAPI, createDefaultSpace } = spacesPlugin.__legacyCompat;
registerLegacyAPI({
legacyConfig: {
kibanaIndex: config.get('kibana.index'),
},
savedObjects: server.savedObjects,
tutorial: {
addScopedTutorialContextFactory: server.addScopedTutorialContextFactory,
},
auditLogger: {
create: (pluginId: string) =>
new AuditLogger(server, pluginId, server.config(), server.plugins.xpack_main.info),
},
xpackMain: server.plugins.xpack_main,
});
initEnterSpaceView(server);
initSpaceSelectorView(server);
watchStatusAndLicenseToInitialize(server.plugins.xpack_main, this, async () => {
await createDefaultSpace();
});
server.expose('getSpaceId', (request: Legacy.Request) =>
spacesPlugin.spacesService.getSpaceId(request)
);
server.expose('getActiveSpace', (request: Legacy.Request) =>
spacesPlugin.spacesService.getActiveSpace(request)
);
server.expose('spaceIdToNamespace', spacesPlugin.spacesService.spaceIdToNamespace);
server.expose('namespaceToSpaceId', spacesPlugin.spacesService.namespaceToSpaceId);
server.expose('getBasePath', spacesPlugin.spacesService.getBasePath);
},
});