Skip to content

Commit

Permalink
reintroducing activeSpace injected var for legacy plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Dec 10, 2019
1 parent d771522 commit 0186d5f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as rt from 'io-ts';
import { useKibanaInjectedVar } from './use_kibana_injected_var';

export const useKibanaSpaceId = (): string => {
// NOTICE: use of `activeSpace` is deprecated and will not be made available in the New Platform.
const activeSpace = useKibanaInjectedVar('activeSpace');

return pipe(
Expand Down
31 changes: 31 additions & 0 deletions x-pack/legacy/plugins/spaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
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';
Expand Down Expand Up @@ -76,6 +78,35 @@ export const spaces = (kibana: Record<string, any>) =>
serverBasePath: server.config().get('server.basePath'),
};
},
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) {
Expand Down

0 comments on commit 0186d5f

Please sign in to comment.