Skip to content

Commit

Permalink
use NP saved objects service for type and wrapper registration
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Feb 27, 2020
1 parent 0764380 commit d69332a
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 166 deletions.
1 change: 1 addition & 0 deletions docs/development/core/server/kibana-plugin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [SavedObjectsClientFactoryProvider](./kibana-plugin-server.savedobjectsclientfactoryprovider.md) | Provider to invoke to retrieve a [SavedObjectsClientFactory](./kibana-plugin-server.savedobjectsclientfactory.md)<!-- -->. |
| [SavedObjectsClientWrapperFactory](./kibana-plugin-server.savedobjectsclientwrapperfactory.md) | Describes the factory used to create instances of Saved Objects Client Wrappers. |
| [SavedObjectsFieldMapping](./kibana-plugin-server.savedobjectsfieldmapping.md) | Describe a [saved object type mapping](./kibana-plugin-server.savedobjectstypemappingdefinition.md) field.<!-- -->Please refer to [elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html) For the mapping documentation |
| [SavedObjectUnsanitizedDoc](./kibana-plugin-server.savedobjectunsanitizeddoc.md) | We want to have two types, one that guarantees a "references" attribute will exist and one that allows it to be null. Since we're not migrating all the saved objects to have a "references" array, we need to support the scenarios where it may be missing (ex migrations). |
| [ScopeableRequest](./kibana-plugin-server.scopeablerequest.md) | A user credentials container. It accommodates the necessary auth credentials to impersonate the current user.<!-- -->See [KibanaRequest](./kibana-plugin-server.kibanarequest.md)<!-- -->. |
| [SharedGlobalConfig](./kibana-plugin-server.sharedglobalconfig.md) | |
| [StringValidation](./kibana-plugin-server.stringvalidation.md) | Allows regex objects or a regex string |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [SavedObjectUnsanitizedDoc](./kibana-plugin-server.savedobjectunsanitizeddoc.md)

## SavedObjectUnsanitizedDoc type

We want to have two types, one that guarantees a "references" attribute will exist and one that allows it to be null. Since we're not migrating all the saved objects to have a "references" array, we need to support the scenarios where it may be missing (ex migrations).

<b>Signature:</b>

```typescript
export declare type SavedObjectUnsanitizedDoc = SavedObjectDoc & Partial<Referencable>;
```
1 change: 1 addition & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export {
SavedObjectMigrationContext,
SavedObjectsMigrationLogger,
SavedObjectsRawDoc,
SavedObjectUnsanitizedDoc,
SavedObjectSanitizedDoc,
SavedObjectsRepositoryFactory,
SavedObjectsResolveImportErrorsOptions,
Expand Down
5 changes: 3 additions & 2 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1497,8 +1497,6 @@ export interface SavedObjectMigrationContext {
log: SavedObjectsMigrationLogger;
}

// Warning: (ae-forgotten-export) The symbol "SavedObjectUnsanitizedDoc" needs to be exported by the entry point index.d.ts
//
// @public
export type SavedObjectMigrationFn = (doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc;

Expand Down Expand Up @@ -2087,6 +2085,9 @@ export class SavedObjectTypeRegistry {
registerType(type: SavedObjectsType): void;
}

// @public
export type SavedObjectUnsanitizedDoc = SavedObjectDoc & Partial<Referencable>;

// @public
export type ScopeableRequest = KibanaRequest | LegacyRequest | FakeRequest;

Expand Down
14 changes: 0 additions & 14 deletions x-pack/legacy/plugins/spaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import { SpacesServiceSetup } from '../../../plugins/spaces/server';
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 { initEnterSpaceView } from './server/routes/views';
Expand Down Expand Up @@ -52,18 +50,6 @@ export const spaces = (kibana: Record<string, any>) =>
managementSections: [],
apps: [],
hacks: ['plugins/spaces/legacy'],
mappings,
migrations: {
space: {
'6.6.0': migrateToKibana660,
},
},
savedObjectSchemas: {
space: {
isNamespaceAgnostic: true,
hidden: true,
},
},
home: [],
injectDefaultVars(server: Server) {
return {
Expand Down
34 changes: 0 additions & 34 deletions x-pack/legacy/plugins/spaces/mappings.json

This file was deleted.

12 changes: 0 additions & 12 deletions x-pack/legacy/plugins/spaces/server/lib/migrations/migrate_6x.ts

This file was deleted.

40 changes: 0 additions & 40 deletions x-pack/plugins/spaces/server/lib/migrations/migrate_6x.test.ts

This file was deleted.

12 changes: 0 additions & 12 deletions x-pack/plugins/spaces/server/lib/migrations/migrate_6x.ts

This file was deleted.

19 changes: 5 additions & 14 deletions x-pack/plugins/spaces/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { LicensingPluginSetup } from '../../licensing/server';
import { createDefaultSpace } from './lib/create_default_space';
// @ts-ignore
import { AuditLogger } from '../../../../server/lib/audit_logger';
import { spacesSavedObjectsClientWrapperFactory } from './lib/saved_objects_client/saved_objects_client_wrapper_factory';
import { SpacesAuditLogger } from './lib/audit_logger';
import { createSpacesTutorialContextFactory } from './lib/spaces_tutorial_context_factory';
import { registerSpacesUsageCollector } from './lib/spaces_usage_collector';
Expand All @@ -31,6 +30,7 @@ import { initSpacesRequestInterceptors } from './lib/request_interceptors';
import { initExternalSpacesApi } from './routes/api/external';
import { initInternalSpacesApi } from './routes/api/internal';
import { initSpacesViewsRoutes } from './routes/views';
import { SpacesSavedObjectsService } from './saved_objects';

/**
* Describes a set of APIs that is available in the legacy platform only and required by this plugin
Expand Down Expand Up @@ -108,6 +108,9 @@ export class Plugin {
config$: this.config$,
});

const savedObjectsService = new SpacesSavedObjectsService();
savedObjectsService.setup({ core, spacesService });

const viewRouter = core.http.createRouter();
initSpacesViewsRoutes({
viewRouter,
Expand Down Expand Up @@ -161,12 +164,7 @@ export class Plugin {
__legacyCompat: {
registerLegacyAPI: (legacyAPI: LegacyAPI) => {
this.legacyAPI = legacyAPI;
this.setupLegacyComponents(
spacesService,
plugins.features,
plugins.licensing,
plugins.usageCollection
);
this.setupLegacyComponents(plugins.features, plugins.licensing, plugins.usageCollection);
},
createDefaultSpace: async () => {
return await createDefaultSpace({
Expand All @@ -181,18 +179,11 @@ export class Plugin {
public stop() {}

private setupLegacyComponents(
spacesService: SpacesServiceSetup,
featuresSetup: FeaturesPluginSetup,
licensingSetup: LicensingPluginSetup,
usageCollectionSetup?: UsageCollectionSetup
) {
const legacyAPI = this.getLegacyAPI();
const { addScopedSavedObjectsClientWrapperFactory, types } = legacyAPI.savedObjects;
addScopedSavedObjectsClientWrapperFactory(
Number.MIN_SAFE_INTEGER,
'spaces',
spacesSavedObjectsClientWrapperFactory(spacesService, types)
);
// Register a function with server to manage the collection of usage stats
registerSpacesUsageCollector(usageCollectionSetup, {
kibanaIndex: legacyAPI.legacyConfig.kibanaIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { migrateToKibana660 } from './migrate_6x';
export { SpacesSavedObjectsService } from './saved_objects_service';
38 changes: 38 additions & 0 deletions x-pack/plugins/spaces/server/saved_objects/mappings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.
*/

export const SpacesSavedObjectMappings = Object.freeze({
properties: {
name: {
type: 'text',
fields: {
keyword: {
type: 'keyword',
ignore_above: 2048,
},
},
},
description: {
type: 'text',
},
initials: {
type: 'keyword',
},
color: {
type: 'keyword',
},
disabledFeatures: {
type: 'keyword',
},
imageUrl: {
type: 'text',
index: false,
},
_reserved: {
type: 'boolean',
},
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ describe('migrateTo660', () => {
expect(
migrateToKibana660({
id: 'space:foo',
type: 'space',
attributes: {},
})
).toEqual({
id: 'space:foo',
type: 'space',
attributes: {
disabledFeatures: [],
},
Expand All @@ -26,12 +28,14 @@ describe('migrateTo660', () => {
expect(
migrateToKibana660({
id: 'space:foo',
type: 'space',
attributes: {
disabledFeatures: ['foo', 'bar', 'baz'],
},
})
).toEqual({
id: 'space:foo',
type: 'space',
attributes: {
disabledFeatures: ['foo', 'bar', 'baz'],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 { SavedObjectUnsanitizedDoc } from 'src/core/server';

export function migrateToKibana660(doc: SavedObjectUnsanitizedDoc) {
const attributes = doc.attributes as Record<string, any>;
if (!attributes.hasOwnProperty('disabledFeatures')) {
attributes.disabledFeatures = [];
}
return doc;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsClientWrapperFactory } from 'src/core/server';
import {
SavedObjectsClientWrapperFactory,
SavedObjectsClientWrapperOptions,
} from 'src/core/server';
import { SpacesSavedObjectsClient } from './spaces_saved_objects_client';
import { SpacesServiceSetup } from '../../spaces_service/spaces_service';
import { SpacesServiceSetup } from '../spaces_service/spaces_service';

export function spacesSavedObjectsClientWrapperFactory(
spacesService: SpacesServiceSetup,
types: string[]
spacesService: SpacesServiceSetup
): SavedObjectsClientWrapperFactory {
return ({ client, request }) =>
return (options: SavedObjectsClientWrapperOptions) =>
new SpacesSavedObjectsClient({
baseClient: client,
request,
baseClient: options.client,
request: options.request,
spacesService,
types,
typeRegistry: options.typeRegistry,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { CoreSetup } from 'src/core/server';
import { SpacesSavedObjectMappings } from './mappings';
import { migrateToKibana660 } from './migrations';
import { spacesSavedObjectsClientWrapperFactory } from './saved_objects_client_wrapper_factory';
import { SpacesServiceSetup } from '../spaces_service';

interface SetupDeps {
core: Pick<CoreSetup, 'savedObjects' | 'getStartServices'>;
spacesService: SpacesServiceSetup;
}

export class SpacesSavedObjectsService {
public setup({ core, spacesService }: SetupDeps) {
core.savedObjects.registerType({
name: 'space',
hidden: true,
namespaceAgnostic: true,
mappings: SpacesSavedObjectMappings,
migrations: {
'6.6.0': migrateToKibana660,
},
});

core.savedObjects.addClientWrapper(
Number.MIN_SAFE_INTEGER,
'spaces',
spacesSavedObjectsClientWrapperFactory(spacesService)
);
}
}
Loading

0 comments on commit d69332a

Please sign in to comment.