Skip to content

Commit

Permalink
create management service registry and adapt existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Mar 4, 2020
1 parent db4469a commit 3f82724
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,45 @@
* under the License.
*/

import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { npStart } from 'ui/new_platform';
import { SavedObjectLoader } from '../../../../../plugins/saved_objects/public';
import { npSetup, npStart } from 'ui/new_platform';
import { createSavedDashboardLoader } from '../dashboard';
import { TypesService, createSavedVisLoader } from '../../../visualizations/public';
import { createSavedSearchesLoader } from '../../../../../plugins/discover/public';

/**
* This registry is used for the editing mode of Saved Searches, Visualizations,
* Dashboard and Time Lion saved objects.
*/
interface SavedObjectRegistryEntry {
id: string;
service: SavedObjectLoader;
title: string;
const registry = npSetup.plugins.savedObjectsManagement?.serviceRegistry;

// TODO: should no longer be needed after migration
export const savedObjectManagementRegistry = registry!;

if (registry) {
const services = {
savedObjectsClient: npStart.core.savedObjects.client,
indexPatterns: npStart.plugins.data.indexPatterns,
chrome: npStart.core.chrome,
overlays: npStart.core.overlays,
};

savedObjectManagementRegistry.register({
id: 'savedVisualizations',
service: createSavedVisLoader({
...services,
...{ visualizationTypes: new TypesService().start() },
}),
title: 'visualizations',
});

savedObjectManagementRegistry.register({
id: 'savedDashboards',
service: createSavedDashboardLoader(services),
title: i18n.translate('kbn.dashboard.savedDashboardsTitle', {
defaultMessage: 'dashboards',
}),
});

savedObjectManagementRegistry.register({
id: 'savedSearches',
service: createSavedSearchesLoader(services),
title: 'searches',
});
}

const registry: SavedObjectRegistryEntry[] = [];

export const savedObjectManagementRegistry = {
register: (service: SavedObjectRegistryEntry) => {
registry.push(service);
},
all: () => {
return registry;
},
get: (id: string) => {
return _.find(registry, { id });
},
};

const services = {
savedObjectsClient: npStart.core.savedObjects.client,
indexPatterns: npStart.plugins.data.indexPatterns,
chrome: npStart.core.chrome,
overlays: npStart.core.overlays,
};

savedObjectManagementRegistry.register({
id: 'savedVisualizations',
service: createSavedVisLoader({
...services,
...{ visualizationTypes: new TypesService().start() },
}),
title: 'visualizations',
});

savedObjectManagementRegistry.register({
id: 'savedDashboards',
service: createSavedDashboardLoader(services),
title: i18n.translate('kbn.dashboard.savedDashboardsTitle', {
defaultMessage: 'dashboards',
}),
});

savedObjectManagementRegistry.register({
id: 'savedSearches',
service: createSavedSearchesLoader(services),
title: 'searches',
});
2 changes: 2 additions & 0 deletions src/legacy/ui/public/new_platform/new_platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
NavigationPublicPluginStart,
} from '../../../../plugins/navigation/public';
import { VisTypeVegaSetup } from '../../../../plugins/vis_type_vega/public';
import { SavedObjectsManagementPluginSetup } from '../../../../plugins/so_management/public';

export interface PluginsSetup {
bfetch: BfetchPublicSetup;
Expand All @@ -71,6 +72,7 @@ export interface PluginsSetup {
management: ManagementSetup;
visTypeVega: VisTypeVegaSetup;
telemetry?: TelemetryPluginSetup;
savedObjectsManagement?: SavedObjectsManagementPluginSetup;
}

export interface PluginsStart {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/so_management/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import { PluginInitializerContext } from 'kibana/public';
import { SavedObjectsManagementPlugin } from './plugin';

export { SavedObjectsManagementPluginSetup } from './types';
export { ISavedObjectsManagementRegistry } from './management_registry';

export function plugin(initializerContext: PluginInitializerContext) {
return new SavedObjectsManagementPlugin();
}
8 changes: 7 additions & 1 deletion src/plugins/so_management/public/management/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@ import { CoreSetup, CoreStart } from 'src/core/public';
import { ManagementSetup } from '../../../management/public';
import { DataPublicPluginStart } from '../../../data/public';
import { StartDependencies } from '../plugin';
import { ISavedObjectsManagementRegistry } from '../management_registry';
import { SavedObjectsTable } from './saved_objects_table';
import { getAllowedTypes } from './lib';

interface RegisterOptions {
core: CoreSetup<StartDependencies>;
sections: ManagementSetup['sections'];
serviceRegistry: ISavedObjectsManagementRegistry;
}

const title = i18n.translate('kbn.management.objects.savedObjectsSectionLabel', {
defaultMessage: 'Saved Objects XXX',
});

export const registerManagementSection = ({ core, sections }: RegisterOptions) => {
export const registerManagementSection = ({ core, sections, serviceRegistry }: RegisterOptions) => {
const kibanaSection = sections.getSection('kibana');
if (!kibanaSection) {
throw new Error('`kibana` management section not found.');
Expand All @@ -60,6 +62,7 @@ export const registerManagementSection = ({ core, sections }: RegisterOptions) =
<SavedObjectsTablePage
coreStart={coreStart}
dataStart={data}
serviceRegistry={serviceRegistry}
allowedTypes={allowedTypes}
/>
</Route>
Expand All @@ -80,16 +83,19 @@ const SavedObjectsTablePage = ({
coreStart,
dataStart,
allowedTypes,
serviceRegistry,
}: {
coreStart: CoreStart;
dataStart: DataPublicPluginStart;
allowedTypes: string[];
serviceRegistry: ISavedObjectsManagementRegistry;
}) => {
const capabilities = coreStart.application.capabilities;
const itemsPerPage = coreStart.uiSettings.get<number>('savedObjects:perPage', 50);
return (
<SavedObjectsTable
allowedTypes={allowedTypes}
serviceRegistry={serviceRegistry}
savedObjectsClient={coreStart.savedObjects.client}
indexPatterns={dataStart.indexPatterns}
http={coreStart.http}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
EuiSwitch,
EuiToolTip,
EuiPageContent,
// @ts-ignore
Query,
} from '@elastic/eui';
import {
Expand All @@ -57,6 +58,7 @@ import {
} from 'src/core/public';
import { IndexPatternsContract } from '../../../data/public';
import { SavedObjectWithMetadata } from '../types';
import { ISavedObjectsManagementRegistry } from '../management_registry';
import {
parseQuery,
getSavedObjectCounts,
Expand All @@ -72,6 +74,7 @@ import { Table, Header, Relationships } from './components';

interface SavedObjectsTableProps {
allowedTypes: string[];
serviceRegistry: ISavedObjectsManagementRegistry;
savedObjectsClient: SavedObjectsClientContract;
indexPatterns: IndexPatternsContract;
http: HttpStart;
Expand Down
47 changes: 47 additions & 0 deletions src/plugins/so_management/public/management_registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { SavedObjectLoader } from '../../saved_objects/public';

export interface SavedObjectsManagementRegistryEntry {
id: string;
service: SavedObjectLoader;
title: string;
}

export type ISavedObjectsManagementRegistry = PublicMethodsOf<SavedObjectsManagementRegistry>;

export class SavedObjectsManagementRegistry {
private readonly registry = new Map<string, SavedObjectsManagementRegistryEntry>();

public register(service: SavedObjectsManagementRegistryEntry) {
if (this.registry.has(service.id)) {
throw new Error('');
}
this.registry.set(service.id, service);
}

public all(): SavedObjectsManagementRegistryEntry[] {
return Object.values(this.registry);
}

public get(id: string): SavedObjectsManagementRegistryEntry | undefined {
return this.registry.get(id);
}
}
16 changes: 13 additions & 3 deletions src/plugins/so_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { CoreSetup, CoreStart, Plugin } from 'src/core/public';
import { ManagementSetup } from '../../management/public';
import { DataPublicPluginStart } from '../../data/public';
import { registerManagementSection } from './management';
import { SavedObjectsManagementPluginSetup } from './types';
import { SavedObjectsManagementRegistry } from './management_registry';

export interface SetupDependencies {
management: ManagementSetup;
Expand All @@ -31,14 +33,22 @@ export interface StartDependencies {
}

export class SavedObjectsManagementPlugin
implements Plugin<{}, {}, SetupDependencies, StartDependencies> {
public setup(core: CoreSetup<StartDependencies>, { management }: SetupDependencies) {
implements Plugin<SavedObjectsManagementPluginSetup, {}, SetupDependencies, StartDependencies> {
private readonly serviceRegistry = new SavedObjectsManagementRegistry();

public setup(
core: CoreSetup<StartDependencies>,
{ management }: SetupDependencies
): SavedObjectsManagementPluginSetup {
registerManagementSection({
core,
serviceRegistry: this.serviceRegistry,
sections: management.sections,
});

return {};
return {
serviceRegistry: this.serviceRegistry,
};
}

public start(core: CoreStart) {
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/so_management/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
*/

import { SavedObject } from 'src/core/public';
import { ISavedObjectsManagementRegistry } from './management_registry';

export interface SavedObjectsManagementPluginSetup {
serviceRegistry: ISavedObjectsManagementRegistry;
}

export type SavedObjectWithMetadata<T = unknown> = SavedObject<T> & {
meta: {
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/so_management/server/services/management.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ import { SavedObjectsManagement } from './management';
type Management = PublicMethodsOf<SavedObjectsManagement>;
const createManagementMock = () => {
const mocked: jest.Mocked<Management> = {
isImportAndExportable: jest.fn().mockReturnValue(true),
getImportableAndExportableTypes: jest.fn(),
isImportAndExportable: jest.fn(),
getDefaultSearchField: jest.fn(),
getIcon: jest.fn(),
getTitle: jest.fn(),
getEditUrl: jest.fn(),
getInAppUrl: jest.fn(),
};

mocked.getImportableAndExportableTypes.mockReturnValue([]);
mocked.isImportAndExportable.mockReturnValue(true);

return mocked;
};

Expand Down

0 comments on commit 3f82724

Please sign in to comment.