Skip to content

Commit

Permalink
Spaces - Client NP Migration, Phase 1 (#40856)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
legrego and elasticmachine authored Dec 16, 2019
1 parent 408139b commit fcdaed9
Show file tree
Hide file tree
Showing 57 changed files with 792 additions and 667 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'plugins/security/services/shield_user';
import 'plugins/security/services/shield_role';
import 'plugins/security/services/shield_indices';
import { xpackInfo } from 'plugins/xpack_main/services/xpack_info';
import { SpacesManager } from '../../../../../spaces/public/lib';
import { ROLES_PATH, CLONE_ROLES_PATH, EDIT_ROLES_PATH } from '../management_urls';
import { getEditRoleBreadcrumbs, getCreateRoleBreadcrumbs } from '../breadcrumbs';

Expand Down Expand Up @@ -79,7 +78,7 @@ const routeDefinition = action => ({
},
spaces(spacesEnabled) {
if (spacesEnabled) {
return new SpacesManager().getSpaces();
return kfetch({ method: 'get', pathname: '/api/spaces/space' });
}
return [];
},
Expand Down
15 changes: 8 additions & 7 deletions x-pack/legacy/plugins/spaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const spaces = (kibana: Record<string, any>) =>
},

uiExports: {
chromeNavControls: ['plugins/spaces/views/nav_control'],
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
managementSections: ['plugins/spaces/views/management'],
apps: [
Expand All @@ -60,7 +59,7 @@ export const spaces = (kibana: Record<string, any>) =>
hidden: true,
},
],
hacks: [],
hacks: ['plugins/spaces/legacy'],
mappings,
migrations: {
space: {
Expand All @@ -73,19 +72,21 @@ export const spaces = (kibana: Record<string, any>) =>
hidden: true,
},
},
home: ['plugins/spaces/register_feature'],
injectDefaultVars(server: any) {
home: [],
injectDefaultVars(server: Server) {
return {
spaces: [],
activeSpace: null,
serverBasePath: server.config().get('server.basePath'),
activeSpace: null,
};
},
async replaceInjectedVars(
vars: Record<string, any>,
request: Legacy.Request,
server: Record<string, any>
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.');
Expand Down
24 changes: 0 additions & 24 deletions x-pack/legacy/plugins/spaces/public/__mocks__/ui_capabilities.ts

This file was deleted.

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

import { setMockCapabilities } from '../__mocks__/ui_capabilities';
import React from 'react';
import { shallowWithIntl } from 'test_utils/enzyme_helpers';
import { ManageSpacesButton } from './manage_spaces_button';

describe('ManageSpacesButton', () => {
it('renders as expected', () => {
setMockCapabilities({
navLinks: {},
management: {},
catalogue: {},
spaces: {
manage: true,
},
});

const component = <ManageSpacesButton />;
const component = (
<ManageSpacesButton
capabilities={{
navLinks: {},
management: {},
catalogue: {},
spaces: {
manage: true,
},
}}
/>
);
expect(shallowWithIntl(component)).toMatchSnapshot();
});

it(`doesn't render if user profile forbids managing spaces`, () => {
setMockCapabilities({
navLinks: {},
management: {},
catalogue: {},
spaces: {
manage: false,
},
});

const component = <ManageSpacesButton />;
const component = (
<ManageSpacesButton
capabilities={{
navLinks: {},
management: {},
catalogue: {},
spaces: {
manage: false,
},
}}
/>
);
expect(shallowWithIntl(component)).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@
import { EuiButton } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import React, { Component, CSSProperties } from 'react';
import { capabilities } from 'ui/capabilities';
import { MANAGE_SPACES_URL } from '../lib/constants';
import { Capabilities } from 'src/core/public';
import { getManageSpacesUrl } from '../lib/constants';

interface Props {
isDisabled?: boolean;
className?: string;
size?: 's' | 'm';
style?: CSSProperties;
onClick?: () => void;
capabilities: Capabilities;
}

export class ManageSpacesButton extends Component<Props, {}> {
public render() {
if (!capabilities.get().spaces.manage) {
if (!this.props.capabilities.spaces.manage) {
return null;
}

Expand All @@ -44,6 +45,6 @@ export class ManageSpacesButton extends Component<Props, {}> {
if (this.props.onClick) {
this.props.onClick();
}
window.location.replace(MANAGE_SPACES_URL);
window.location.replace(getManageSpacesUrl());
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

import { i18n } from '@kbn/i18n';
import {
FeatureCatalogueEntry,
FeatureCatalogueCategory,
FeatureCatalogueRegistryProvider,
// @ts-ignore
} from 'ui/registry/feature_catalogue';
} from '../../../../../src/plugins/home/public';
import { getSpacesFeatureDescription } from './lib/constants';

FeatureCatalogueRegistryProvider.register(() => {
export const createSpacesFeatureCatalogueEntry = (): FeatureCatalogueEntry => {
return {
id: 'spaces',
title: i18n.translate('xpack.spaces.spacesTitle', {
Expand All @@ -24,4 +23,4 @@ FeatureCatalogueRegistryProvider.register(() => {
showOnHomePage: true,
category: FeatureCatalogueCategory.ADMIN,
};
});
};
10 changes: 10 additions & 0 deletions x-pack/legacy/plugins/spaces/public/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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 { SpacesPlugin } from './plugin';

export const plugin = () => {
return new SpacesPlugin();
};
18 changes: 18 additions & 0 deletions x-pack/legacy/plugins/spaces/public/legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 { npSetup, npStart } from 'ui/new_platform';
import { plugin } from '.';
import { SpacesPlugin, PluginsSetup } from './plugin';

const spacesPlugin: SpacesPlugin = plugin();

const plugins: PluginsSetup = {
home: npSetup.plugins.home,
};

export const setup = spacesPlugin.setup(npSetup.core, plugins);
export const start = spacesPlugin.start(npStart.core);
5 changes: 3 additions & 2 deletions x-pack/legacy/plugins/spaces/public/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { i18n } from '@kbn/i18n';
import chrome from 'ui/chrome';
import { npSetup } from 'ui/new_platform';

let spacesFeatureDescription: string;

Expand All @@ -20,4 +20,5 @@ export const getSpacesFeatureDescription = () => {
return spacesFeatureDescription;
};

export const MANAGE_SPACES_URL = chrome.addBasePath(`/app/kibana#/management/spaces/list`);
export const getManageSpacesUrl = () =>
npSetup.core.http.basePath.prepend(`/app/kibana#/management/spaces/list`);
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
SavedObjectsManagementRecord,
} from '../../../../../../../src/legacy/core_plugins/management/public';
import { CopySavedObjectsToSpaceFlyout } from '../../views/management/components/copy_saved_objects_to_space';
import { Space } from '../../../common/model/space';
import { SpacesManager } from '../spaces_manager';

export class CopyToSpaceSavedObjectsManagementAction extends SavedObjectsManagementAction {
Expand All @@ -31,7 +30,7 @@ export class CopyToSpaceSavedObjectsManagementAction extends SavedObjectsManagem
},
};

constructor(private readonly spacesManager: SpacesManager, private readonly activeSpace: Space) {
constructor(private readonly spacesManager: SpacesManager) {
super();
}

Expand All @@ -44,7 +43,6 @@ export class CopyToSpaceSavedObjectsManagementAction extends SavedObjectsManagem
onClose={this.onClose}
savedObject={this.record}
spacesManager={this.spacesManager}
activeSpace={this.activeSpace}
toastNotifications={toastNotifications}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { of, Observable } from 'rxjs';
import { Space } from '../../common/model/space';

function createSpacesManagerMock() {
return {
onActiveSpaceChange$: (of(undefined) as unknown) as Observable<Space>,
getSpaces: jest.fn().mockResolvedValue([]),
getSpace: jest.fn().mockResolvedValue(undefined),
getActiveSpace: jest.fn().mockResolvedValue(undefined),
createSpace: jest.fn().mockResolvedValue(undefined),
updateSpace: jest.fn().mockResolvedValue(undefined),
deleteSpace: jest.fn().mockResolvedValue(undefined),
copySavedObjects: jest.fn().mockResolvedValue(undefined),
resolveCopySavedObjectsErrors: jest.fn().mockResolvedValue(undefined),
redirectToSpaceSelector: jest.fn().mockResolvedValue(undefined),
requestRefresh: jest.fn(),
on: jest.fn(),
};
}

Expand Down
Loading

0 comments on commit fcdaed9

Please sign in to comment.