From d69332ae14753725f989339c44ef17d35e1abba8 Mon Sep 17 00:00:00 2001 From: Larry Gregory Date: Thu, 27 Feb 2020 07:01:31 -0500 Subject: [PATCH] use NP saved objects service for type and wrapper registration --- .../core/server/kibana-plugin-server.md | 1 + ...plugin-server.savedobjectunsanitizeddoc.md | 13 ++++ src/core/server/index.ts | 1 + src/core/server/server.api.md | 5 +- x-pack/legacy/plugins/spaces/index.ts | 14 ----- x-pack/legacy/plugins/spaces/mappings.json | 34 ----------- .../server/lib/migrations/migrate_6x.ts | 12 ---- .../server/lib/migrations/migrate_6x.test.ts | 40 ------------ .../server/lib/migrations/migrate_6x.ts | 12 ---- x-pack/plugins/spaces/server/plugin.ts | 19 ++---- .../spaces_saved_objects_client.test.ts.snap | 0 .../migrations => saved_objects}/index.ts | 2 +- .../spaces/server/saved_objects/mappings.ts | 38 ++++++++++++ .../server/saved_objects}/migrations/index.ts | 0 .../migrations/migrate_6x.test.ts | 4 ++ .../saved_objects/migrations/migrate_6x.ts | 15 +++++ .../saved_objects_client_wrapper_factory.ts | 18 +++--- .../saved_objects/saved_objects_service.ts | 36 +++++++++++ .../spaces_saved_objects_client.test.ts | 61 +++++++++++++------ .../spaces_saved_objects_client.ts | 20 +++--- 20 files changed, 179 insertions(+), 166 deletions(-) create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectunsanitizeddoc.md delete mode 100644 x-pack/legacy/plugins/spaces/mappings.json delete mode 100644 x-pack/legacy/plugins/spaces/server/lib/migrations/migrate_6x.ts delete mode 100644 x-pack/plugins/spaces/server/lib/migrations/migrate_6x.test.ts delete mode 100644 x-pack/plugins/spaces/server/lib/migrations/migrate_6x.ts rename x-pack/plugins/spaces/server/{lib/saved_objects_client => saved_objects}/__snapshots__/spaces_saved_objects_client.test.ts.snap (100%) rename x-pack/plugins/spaces/server/{lib/migrations => saved_objects}/index.ts (77%) create mode 100644 x-pack/plugins/spaces/server/saved_objects/mappings.ts rename x-pack/{legacy/plugins/spaces/server/lib => plugins/spaces/server/saved_objects}/migrations/index.ts (100%) rename x-pack/{legacy/plugins/spaces/server/lib => plugins/spaces/server/saved_objects}/migrations/migrate_6x.test.ts (92%) create mode 100644 x-pack/plugins/spaces/server/saved_objects/migrations/migrate_6x.ts rename x-pack/plugins/spaces/server/{lib/saved_objects_client => saved_objects}/saved_objects_client_wrapper_factory.ts (55%) create mode 100644 x-pack/plugins/spaces/server/saved_objects/saved_objects_service.ts rename x-pack/plugins/spaces/server/{lib/saved_objects_client => saved_objects}/spaces_saved_objects_client.test.ts (92%) rename x-pack/plugins/spaces/server/{lib/saved_objects_client => saved_objects}/spaces_saved_objects_client.ts (92%) diff --git a/docs/development/core/server/kibana-plugin-server.md b/docs/development/core/server/kibana-plugin-server.md index 15a1fd050625685..1be03d0fe7f0f2c 100644 --- a/docs/development/core/server/kibana-plugin-server.md +++ b/docs/development/core/server/kibana-plugin-server.md @@ -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 | diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectunsanitizeddoc.md b/docs/development/core/server/kibana-plugin-server.savedobjectunsanitizeddoc.md new file mode 100644 index 000000000000000..aeadfe366f306f2 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectunsanitizeddoc.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [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). + +Signature: + +```typescript +export declare type SavedObjectUnsanitizedDoc = SavedObjectDoc & Partial; +``` diff --git a/src/core/server/index.ts b/src/core/server/index.ts index e45d4f28edcc37c..88a9e8fde22029c 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -204,6 +204,7 @@ export { SavedObjectMigrationContext, SavedObjectsMigrationLogger, SavedObjectsRawDoc, + SavedObjectUnsanitizedDoc, SavedObjectSanitizedDoc, SavedObjectsRepositoryFactory, SavedObjectsResolveImportErrorsOptions, diff --git a/src/core/server/server.api.md b/src/core/server/server.api.md index 8f4feb7169651ed..18d942f8382bca2 100644 --- a/src/core/server/server.api.md +++ b/src/core/server/server.api.md @@ -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; @@ -2087,6 +2085,9 @@ export class SavedObjectTypeRegistry { registerType(type: SavedObjectsType): void; } +// @public +export type SavedObjectUnsanitizedDoc = SavedObjectDoc & Partial; + // @public export type ScopeableRequest = KibanaRequest | LegacyRequest | FakeRequest; diff --git a/x-pack/legacy/plugins/spaces/index.ts b/x-pack/legacy/plugins/spaces/index.ts index ab3388ae9647597..c7cb0890484e6e0 100644 --- a/x-pack/legacy/plugins/spaces/index.ts +++ b/x-pack/legacy/plugins/spaces/index.ts @@ -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'; @@ -52,18 +50,6 @@ export const spaces = (kibana: Record) => managementSections: [], apps: [], hacks: ['plugins/spaces/legacy'], - mappings, - migrations: { - space: { - '6.6.0': migrateToKibana660, - }, - }, - savedObjectSchemas: { - space: { - isNamespaceAgnostic: true, - hidden: true, - }, - }, home: [], injectDefaultVars(server: Server) { return { diff --git a/x-pack/legacy/plugins/spaces/mappings.json b/x-pack/legacy/plugins/spaces/mappings.json deleted file mode 100644 index dc73dc287188540..000000000000000 --- a/x-pack/legacy/plugins/spaces/mappings.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "space": { - "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" - } - } - } -} diff --git a/x-pack/legacy/plugins/spaces/server/lib/migrations/migrate_6x.ts b/x-pack/legacy/plugins/spaces/server/lib/migrations/migrate_6x.ts deleted file mode 100644 index 0c080a8dabb0a76..000000000000000 --- a/x-pack/legacy/plugins/spaces/server/lib/migrations/migrate_6x.ts +++ /dev/null @@ -1,12 +0,0 @@ -/* - * 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 function migrateToKibana660(doc: Record) { - if (!doc.attributes.hasOwnProperty('disabledFeatures')) { - doc.attributes.disabledFeatures = []; - } - return doc; -} diff --git a/x-pack/plugins/spaces/server/lib/migrations/migrate_6x.test.ts b/x-pack/plugins/spaces/server/lib/migrations/migrate_6x.test.ts deleted file mode 100644 index 964eb8137685f69..000000000000000 --- a/x-pack/plugins/spaces/server/lib/migrations/migrate_6x.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 { migrateToKibana660 } from './migrate_6x'; - -describe('migrateTo660', () => { - it('adds a "disabledFeatures" attribute initialized as an empty array', () => { - expect( - migrateToKibana660({ - id: 'space:foo', - attributes: {}, - }) - ).toEqual({ - id: 'space:foo', - attributes: { - disabledFeatures: [], - }, - }); - }); - - it('does not initialize "disabledFeatures" if the property already exists', () => { - // This scenario shouldn't happen organically. Protecting against defects in the migration. - expect( - migrateToKibana660({ - id: 'space:foo', - attributes: { - disabledFeatures: ['foo', 'bar', 'baz'], - }, - }) - ).toEqual({ - id: 'space:foo', - attributes: { - disabledFeatures: ['foo', 'bar', 'baz'], - }, - }); - }); -}); diff --git a/x-pack/plugins/spaces/server/lib/migrations/migrate_6x.ts b/x-pack/plugins/spaces/server/lib/migrations/migrate_6x.ts deleted file mode 100644 index 0c080a8dabb0a76..000000000000000 --- a/x-pack/plugins/spaces/server/lib/migrations/migrate_6x.ts +++ /dev/null @@ -1,12 +0,0 @@ -/* - * 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 function migrateToKibana660(doc: Record) { - if (!doc.attributes.hasOwnProperty('disabledFeatures')) { - doc.attributes.disabledFeatures = []; - } - return doc; -} diff --git a/x-pack/plugins/spaces/server/plugin.ts b/x-pack/plugins/spaces/server/plugin.ts index 90c2da6e69df8c8..9b003f651a08e86 100644 --- a/x-pack/plugins/spaces/server/plugin.ts +++ b/x-pack/plugins/spaces/server/plugin.ts @@ -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'; @@ -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 @@ -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, @@ -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({ @@ -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, diff --git a/x-pack/plugins/spaces/server/lib/saved_objects_client/__snapshots__/spaces_saved_objects_client.test.ts.snap b/x-pack/plugins/spaces/server/saved_objects/__snapshots__/spaces_saved_objects_client.test.ts.snap similarity index 100% rename from x-pack/plugins/spaces/server/lib/saved_objects_client/__snapshots__/spaces_saved_objects_client.test.ts.snap rename to x-pack/plugins/spaces/server/saved_objects/__snapshots__/spaces_saved_objects_client.test.ts.snap diff --git a/x-pack/plugins/spaces/server/lib/migrations/index.ts b/x-pack/plugins/spaces/server/saved_objects/index.ts similarity index 77% rename from x-pack/plugins/spaces/server/lib/migrations/index.ts rename to x-pack/plugins/spaces/server/saved_objects/index.ts index b303a8489ffb098..fb02c7cb7245ab3 100644 --- a/x-pack/plugins/spaces/server/lib/migrations/index.ts +++ b/x-pack/plugins/spaces/server/saved_objects/index.ts @@ -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'; diff --git a/x-pack/plugins/spaces/server/saved_objects/mappings.ts b/x-pack/plugins/spaces/server/saved_objects/mappings.ts new file mode 100644 index 000000000000000..a8d57d420f63c32 --- /dev/null +++ b/x-pack/plugins/spaces/server/saved_objects/mappings.ts @@ -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', + }, + }, +}); diff --git a/x-pack/legacy/plugins/spaces/server/lib/migrations/index.ts b/x-pack/plugins/spaces/server/saved_objects/migrations/index.ts similarity index 100% rename from x-pack/legacy/plugins/spaces/server/lib/migrations/index.ts rename to x-pack/plugins/spaces/server/saved_objects/migrations/index.ts diff --git a/x-pack/legacy/plugins/spaces/server/lib/migrations/migrate_6x.test.ts b/x-pack/plugins/spaces/server/saved_objects/migrations/migrate_6x.test.ts similarity index 92% rename from x-pack/legacy/plugins/spaces/server/lib/migrations/migrate_6x.test.ts rename to x-pack/plugins/spaces/server/saved_objects/migrations/migrate_6x.test.ts index 964eb8137685f69..7de594d84fb1adf 100644 --- a/x-pack/legacy/plugins/spaces/server/lib/migrations/migrate_6x.test.ts +++ b/x-pack/plugins/spaces/server/saved_objects/migrations/migrate_6x.test.ts @@ -11,10 +11,12 @@ describe('migrateTo660', () => { expect( migrateToKibana660({ id: 'space:foo', + type: 'space', attributes: {}, }) ).toEqual({ id: 'space:foo', + type: 'space', attributes: { disabledFeatures: [], }, @@ -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'], }, diff --git a/x-pack/plugins/spaces/server/saved_objects/migrations/migrate_6x.ts b/x-pack/plugins/spaces/server/saved_objects/migrations/migrate_6x.ts new file mode 100644 index 000000000000000..5f86a09f26c9892 --- /dev/null +++ b/x-pack/plugins/spaces/server/saved_objects/migrations/migrate_6x.ts @@ -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; + if (!attributes.hasOwnProperty('disabledFeatures')) { + attributes.disabledFeatures = []; + } + return doc; +} diff --git a/x-pack/plugins/spaces/server/lib/saved_objects_client/saved_objects_client_wrapper_factory.ts b/x-pack/plugins/spaces/server/saved_objects/saved_objects_client_wrapper_factory.ts similarity index 55% rename from x-pack/plugins/spaces/server/lib/saved_objects_client/saved_objects_client_wrapper_factory.ts rename to x-pack/plugins/spaces/server/saved_objects/saved_objects_client_wrapper_factory.ts index aa61af07c268ecd..e545cccfeadd74f 100644 --- a/x-pack/plugins/spaces/server/lib/saved_objects_client/saved_objects_client_wrapper_factory.ts +++ b/x-pack/plugins/spaces/server/saved_objects/saved_objects_client_wrapper_factory.ts @@ -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, }); } diff --git a/x-pack/plugins/spaces/server/saved_objects/saved_objects_service.ts b/x-pack/plugins/spaces/server/saved_objects/saved_objects_service.ts new file mode 100644 index 000000000000000..40ea49573e3c178 --- /dev/null +++ b/x-pack/plugins/spaces/server/saved_objects/saved_objects_service.ts @@ -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; + 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) + ); + } +} diff --git a/x-pack/plugins/spaces/server/lib/saved_objects_client/spaces_saved_objects_client.test.ts b/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.test.ts similarity index 92% rename from x-pack/plugins/spaces/server/lib/saved_objects_client/spaces_saved_objects_client.test.ts rename to x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.test.ts index c2bc534f742a871..2d6fe36792c403a 100644 --- a/x-pack/plugins/spaces/server/lib/saved_objects_client/spaces_saved_objects_client.test.ts +++ b/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.test.ts @@ -4,12 +4,33 @@ * you may not use this file except in compliance with the Elastic License. */ -import { DEFAULT_SPACE_ID } from '../../../common/constants'; +import { DEFAULT_SPACE_ID } from '../../common/constants'; import { SpacesSavedObjectsClient } from './spaces_saved_objects_client'; -import { spacesServiceMock } from '../../spaces_service/spaces_service.mock'; -import { savedObjectsClientMock } from '../../../../../../src/core/server/mocks'; +import { spacesServiceMock } from '../spaces_service/spaces_service.mock'; +import { savedObjectsClientMock } from '../../../../../src/core/server/mocks'; +import { SavedObjectTypeRegistry } from 'src/core/server'; + +const typeRegistry = new SavedObjectTypeRegistry(); +typeRegistry.registerType({ + name: 'foo', + namespaceAgnostic: false, + hidden: false, + mappings: { properties: {} }, +}); + +typeRegistry.registerType({ + name: 'bar', + namespaceAgnostic: false, + hidden: false, + mappings: { properties: {} }, +}); -const types = ['foo', 'bar', 'space']; +typeRegistry.registerType({ + name: 'space', + namespaceAgnostic: true, + hidden: true, + mappings: { properties: {} }, +}); const createMockRequest = () => ({}); @@ -44,7 +65,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); await expect( @@ -63,7 +84,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); const type = Symbol(); const id = Symbol(); @@ -89,7 +110,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); await expect( @@ -110,7 +131,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); const objects = [{ type: 'foo' }]; @@ -136,7 +157,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); await expect( @@ -160,7 +181,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); const options = Object.freeze({ type: 'foo' }); @@ -189,7 +210,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); const options = Object.freeze({ type: ['foo', 'bar'] }); @@ -213,7 +234,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); await expect( @@ -232,7 +253,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); const type = Symbol(); @@ -259,7 +280,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); await expect( @@ -280,7 +301,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); const objects = [{ type: 'foo' }]; @@ -306,7 +327,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); await expect( @@ -326,7 +347,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); const type = Symbol(); @@ -358,7 +379,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); const actualReturnValue = await client.bulkUpdate([ @@ -390,7 +411,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); await expect( @@ -410,7 +431,7 @@ const createMockResponse = () => ({ request, baseClient, spacesService, - types, + typeRegistry, }); const type = Symbol(); diff --git a/x-pack/plugins/spaces/server/lib/saved_objects_client/spaces_saved_objects_client.ts b/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.ts similarity index 92% rename from x-pack/plugins/spaces/server/lib/saved_objects_client/spaces_saved_objects_client.ts rename to x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.ts index 534d79712394011..d7792a05b9e487d 100644 --- a/x-pack/plugins/spaces/server/lib/saved_objects_client/spaces_saved_objects_client.ts +++ b/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.ts @@ -13,15 +13,16 @@ import { SavedObjectsCreateOptions, SavedObjectsFindOptions, SavedObjectsUpdateOptions, + ISavedObjectTypeRegistry, } from 'src/core/server'; -import { SpacesServiceSetup } from '../../spaces_service/spaces_service'; -import { spaceIdToNamespace } from '../utils/namespace'; +import { SpacesServiceSetup } from '../spaces_service/spaces_service'; +import { spaceIdToNamespace } from '../lib/utils/namespace'; interface SpacesSavedObjectsClientOptions { baseClient: SavedObjectsClientContract; request: any; spacesService: SpacesServiceSetup; - types: string[]; + typeRegistry: ISavedObjectTypeRegistry; } const coerceToArray = (param: string | string[]) => { @@ -41,15 +42,15 @@ const throwErrorIfNamespaceSpecified = (options: any) => { export class SpacesSavedObjectsClient implements SavedObjectsClientContract { private readonly client: SavedObjectsClientContract; private readonly spaceId: string; - private readonly types: string[]; + private readonly typeRegistry: ISavedObjectTypeRegistry; public readonly errors: SavedObjectsClientContract['errors']; constructor(options: SpacesSavedObjectsClientOptions) { - const { baseClient, request, spacesService, types } = options; + const { baseClient, request, spacesService, typeRegistry } = options; this.client = baseClient; this.spaceId = spacesService.getSpaceId(request); - this.types = types; + this.typeRegistry = typeRegistry; this.errors = baseClient.errors; } @@ -137,9 +138,10 @@ export class SpacesSavedObjectsClient implements SavedObjectsClientContract { return await this.client.find({ ...options, - type: (options.type ? coerceToArray(options.type) : this.types).filter( - type => type !== 'space' - ), + type: (options.type + ? coerceToArray(options.type) + : this.typeRegistry.getAllTypes().map(t => t.name) + ).filter(type => type !== 'space'), namespace: spaceIdToNamespace(this.spaceId), }); }