diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/collect_multinamespaces_references.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/collect_multinamespaces_references.ts new file mode 100644 index 0000000000000..91ccb77223fa1 --- /dev/null +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/collect_multinamespaces_references.ts @@ -0,0 +1,41 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { + SavedObjectsCollectMultiNamespaceReferencesObject, + SavedObjectsCollectMultiNamespaceReferencesOptions, + SavedObjectsCollectMultiNamespaceReferencesResponse, +} from '@kbn/core-saved-objects-api-server'; +import { ApiExecutionContext } from './types'; +import { collectMultiNamespaceReferences } from '../collect_multi_namespace_references'; + +export interface PerformCreateParams { + objects: SavedObjectsCollectMultiNamespaceReferencesObject[]; + options: SavedObjectsCollectMultiNamespaceReferencesOptions; +} + +export const performCollectMultiNamespaceReferences = async ( + { objects, options }: PerformCreateParams, + { registry, helpers, allowedTypes, client, serializer, extensions = {} }: ApiExecutionContext +): Promise => { + const { common: commonHelper } = helpers; + const { securityExtension } = extensions; + + const namespace = commonHelper.getCurrentNamespace(options.namespace); + return collectMultiNamespaceReferences({ + registry, + allowedTypes, + client, + serializer, + getIndexForType: commonHelper.getIndexForType.bind(commonHelper), + createPointInTimeFinder: commonHelper.createPointInTimeFinder.bind(commonHelper), + securityExtension, + objects, + options: { ...options, namespace }, + }); +}; diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/index.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/index.ts index fe490d80ff4d6..3271f1ab25f21 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/index.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/index.ts @@ -23,3 +23,5 @@ export { performOpenPointInTime } from './open_point_in_time'; export { performIncrementCounter } from './increment_counter'; export { performBulkResolve } from './bulk_resolve'; export { performResolve } from './resolve'; +export { performUpdateObjectsSpaces } from './update_objects_spaces'; +export { performCollectMultiNamespaceReferences } from './collect_multinamespaces_references'; diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/types.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/types.ts index da3e19bdf56a7..7d90d72cb549f 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/types.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/types.ts @@ -7,12 +7,12 @@ */ import type { Logger } from '@kbn/logging'; -import { - type ISavedObjectTypeRegistry, - type SavedObjectsExtensions, - type ISavedObjectsSerializer, +import type { + ISavedObjectTypeRegistry, + SavedObjectsExtensions, + ISavedObjectsSerializer, } from '@kbn/core-saved-objects-server'; -import { IKibanaMigrator, IndexMapping } from '@kbn/core-saved-objects-base-server-internal'; +import type { IKibanaMigrator, IndexMapping } from '@kbn/core-saved-objects-base-server-internal'; import type { RepositoryHelpers } from '../helpers'; import type { RepositoryEsClient } from '../repository_es_client'; diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/update_objects_spaces.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/update_objects_spaces.ts new file mode 100644 index 0000000000000..96e18d360a460 --- /dev/null +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/update_objects_spaces.ts @@ -0,0 +1,55 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { + SavedObjectsUpdateObjectsSpacesObject, + SavedObjectsUpdateObjectsSpacesOptions, + SavedObjectsUpdateObjectsSpacesResponse, +} from '@kbn/core-saved-objects-api-server'; +import { ApiExecutionContext } from './types'; +import { updateObjectsSpaces } from '../update_objects_spaces'; + +export interface PerformCreateParams { + objects: SavedObjectsUpdateObjectsSpacesObject[]; + spacesToAdd: string[]; + spacesToRemove: string[]; + options: SavedObjectsUpdateObjectsSpacesOptions; +} + +export const performUpdateObjectsSpaces = async ( + { objects, spacesToAdd, spacesToRemove, options }: PerformCreateParams, + { + registry, + helpers, + allowedTypes, + client, + serializer, + logger, + mappings, + extensions = {}, + }: ApiExecutionContext +): Promise => { + const { common: commonHelper } = helpers; + const { securityExtension } = extensions; + + const namespace = commonHelper.getCurrentNamespace(options.namespace); + return updateObjectsSpaces({ + mappings, + registry, + allowedTypes, + client, + serializer, + logger, + getIndexForType: commonHelper.getIndexForType.bind(commonHelper), + securityExtension, + objects, + spacesToAdd, + spacesToRemove, + options: { ...options, namespace }, + }); +}; diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/collect_multi_namespace_references.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/collect_multi_namespace_references.ts index 8e629fbdb7e50..c8e7423e03f91 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/collect_multi_namespace_references.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/collect_multi_namespace_references.ts @@ -17,14 +17,11 @@ import { type ISavedObjectsSecurityExtension, type ISavedObjectTypeRegistry, type SavedObject, + type ISavedObjectsSerializer, SavedObjectsErrorHelpers, } from '@kbn/core-saved-objects-server'; import { SavedObjectsUtils } from '@kbn/core-saved-objects-utils-server'; -import { - type SavedObjectsSerializer, - getObjectKey, - parseObjectKey, -} from '@kbn/core-saved-objects-base-server-internal'; +import { getObjectKey, parseObjectKey } from '@kbn/core-saved-objects-base-server-internal'; import { findLegacyUrlAliases } from './legacy_url_aliases'; import { getRootFields } from './included_fields'; import { getSavedObjectFromSource, rawDocExistsInNamespace } from './internal_utils'; @@ -55,7 +52,7 @@ export interface CollectMultiNamespaceReferencesParams { registry: ISavedObjectTypeRegistry; allowedTypes: string[]; client: RepositoryEsClient; - serializer: SavedObjectsSerializer; + serializer: ISavedObjectsSerializer; getIndexForType: (type: string) => string; createPointInTimeFinder: CreatePointInTimeFinderFn; securityExtension: ISavedObjectsSecurityExtension | undefined; diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/helpers/common.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/helpers/common.ts index aed103ba1c51b..8ee47e1b20350 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/helpers/common.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/helpers/common.ts @@ -15,6 +15,7 @@ import { getIndexForType } from '@kbn/core-saved-objects-base-server-internal'; import { SavedObjectsUtils } from '@kbn/core-saved-objects-utils-server'; import { SavedObjectsErrorHelpers } from '@kbn/core-saved-objects-server'; import { normalizeNamespace } from '../internal_utils'; +import type { CreatePointInTimeFinderFn } from '../point_in_time_finder'; export class CommonHelper { private registry: ISavedObjectTypeRegistry; @@ -23,8 +24,11 @@ export class CommonHelper { private defaultIndex: string; private kibanaVersion: string; + public readonly createPointInTimeFinder: CreatePointInTimeFinderFn; + constructor({ registry, + createPointInTimeFinder, spaceExtension, encryptionExtension, kibanaVersion, @@ -33,6 +37,7 @@ export class CommonHelper { registry: ISavedObjectTypeRegistry; spaceExtension?: ISavedObjectsSpacesExtension; encryptionExtension?: ISavedObjectsEncryptionExtension; + createPointInTimeFinder: CreatePointInTimeFinderFn; defaultIndex: string; kibanaVersion: string; }) { @@ -41,6 +46,7 @@ export class CommonHelper { this.encryptionExtension = encryptionExtension; this.kibanaVersion = kibanaVersion; this.defaultIndex = defaultIndex; + this.createPointInTimeFinder = createPointInTimeFinder; } /** diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/helpers/preflight_check.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/helpers/preflight_check.ts index 42f7c6daccd39..8834842ec2702 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/helpers/preflight_check.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/helpers/preflight_check.ts @@ -11,7 +11,6 @@ import type { ISavedObjectTypeRegistry, ISavedObjectsSerializer, } from '@kbn/core-saved-objects-server'; -import { ISavedObjectsRepository } from '@kbn/core-saved-objects-api-server'; import { SavedObjectsUtils } from '@kbn/core-saved-objects-utils-server'; import { SavedObjectsErrorHelpers, SavedObjectsRawDocSource } from '@kbn/core-saved-objects-server'; import { isRight, rawDocExistsInNamespaces } from '../internal_utils'; @@ -23,13 +22,14 @@ import { } from '../preflight_check_for_create'; import type { RepositoryEsClient } from '../repository_es_client'; import type { PreflightCheckForBulkDeleteParams } from '../repository_bulk_delete_internal_types'; +import type { CreatePointInTimeFinderFn } from '../point_in_time_finder'; export class PreflightCheckHelper { private registry: ISavedObjectTypeRegistry; private serializer: ISavedObjectsSerializer; private client: RepositoryEsClient; private getIndexForType: (type: string) => string; - private createPointInTimeFinder: ISavedObjectsRepository['createPointInTimeFinder']; + private createPointInTimeFinder: CreatePointInTimeFinderFn; constructor({ registry, @@ -42,7 +42,7 @@ export class PreflightCheckHelper { serializer: ISavedObjectsSerializer; client: RepositoryEsClient; getIndexForType: (type: string) => string; - createPointInTimeFinder: ISavedObjectsRepository['createPointInTimeFinder']; + createPointInTimeFinder: CreatePointInTimeFinderFn; }) { this.registry = registry; this.serializer = serializer; diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/repository.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/repository.ts index 7561687b03482..33bfd26884984 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/repository.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/repository.ts @@ -39,6 +39,7 @@ import type { SavedObjectsResolveResponse, SavedObjectsCollectMultiNamespaceReferencesObject, SavedObjectsUpdateObjectsSpacesObject, + SavedObjectsUpdateObjectsSpacesResponse, SavedObjectsUpdateOptions, SavedObjectsOpenPointInTimeOptions, SavedObjectsClosePointInTimeOptions, @@ -51,10 +52,10 @@ import type { SavedObjectsFindInternalOptions, ISavedObjectsRepository, } from '@kbn/core-saved-objects-api-server'; -import { - type ISavedObjectTypeRegistry, - type SavedObjectsExtensions, - type SavedObject, +import type { + ISavedObjectTypeRegistry, + SavedObjectsExtensions, + SavedObject, } from '@kbn/core-saved-objects-server'; import { SavedObjectsSerializer, @@ -63,8 +64,6 @@ import { } from '@kbn/core-saved-objects-base-server-internal'; import { PointInTimeFinder } from './point_in_time_finder'; import { createRepositoryEsClient, type RepositoryEsClient } from './repository_es_client'; -import { collectMultiNamespaceReferences } from './collect_multi_namespace_references'; -import { updateObjectsSpaces } from './update_objects_spaces'; import { RepositoryHelpers, CommonHelper, @@ -91,6 +90,8 @@ import { performIncrementCounter, performBulkResolve, performResolve, + performUpdateObjectsSpaces, + performCollectMultiNamespaceReferences, } from './apis'; export interface SavedObjectsRepositoryOptions { @@ -145,11 +146,6 @@ export class SavedObjectsRepository implements ISavedObjectsRepository { /** The injectedConstructor is only used for unit testing */ injectedConstructor: any = SavedObjectsRepository ): ISavedObjectsRepository { - const mappings = migrator.getActiveMappings(); - const allTypes = typeRegistry.getAllTypes().map((t) => t.name); - const serializer = new SavedObjectsSerializer(typeRegistry); - const visibleTypes = allTypes.filter((type) => !typeRegistry.isHidden(type)); - const missingTypeMappings = includedHiddenTypes.filter((type) => !allTypes.includes(type)); if (missingTypeMappings.length > 0) { throw new Error( @@ -157,6 +153,10 @@ export class SavedObjectsRepository implements ISavedObjectsRepository { ); } + const mappings = migrator.getActiveMappings(); + const allTypes = typeRegistry.getAllTypes().map((t) => t.name); + const serializer = new SavedObjectsSerializer(typeRegistry); + const visibleTypes = allTypes.filter((type) => !typeRegistry.isHidden(type)); const allowedTypes = [...new Set(visibleTypes.concat(includedHiddenTypes))]; return new injectedConstructor({ @@ -201,6 +201,7 @@ export class SavedObjectsRepository implements ISavedObjectsRepository { const commonHelper = new CommonHelper({ spaceExtension: extensions?.spacesExtension, encryptionExtension: extensions?.encryptionExtension, + createPointInTimeFinder: this.createPointInTimeFinder.bind(this), defaultIndex: index, kibanaVersion: migrator.kibanaVersion, registry: typeRegistry, @@ -216,7 +217,7 @@ export class SavedObjectsRepository implements ISavedObjectsRepository { }); const preflightCheckHelper = new PreflightCheckHelper({ getIndexForType: commonHelper.getIndexForType.bind(commonHelper), - createPointInTimeFinder: this.createPointInTimeFinder.bind(this), + createPointInTimeFinder: commonHelper.createPointInTimeFinder.bind(commonHelper), serializer, registry: typeRegistry, client: this.client, @@ -452,18 +453,13 @@ export class SavedObjectsRepository implements ISavedObjectsRepository { objects: SavedObjectsCollectMultiNamespaceReferencesObject[], options: SavedObjectsCollectMultiNamespaceReferencesOptions = {} ) { - const namespace = this.getCurrentNamespace(options.namespace); - return collectMultiNamespaceReferences({ - registry: this._registry, - allowedTypes: this._allowedTypes, - client: this.client, - serializer: this._serializer, - getIndexForType: this.helpers.common.getIndexForType.bind(this.helpers.common), - createPointInTimeFinder: this.createPointInTimeFinder.bind(this), - securityExtension: this.extensions.securityExtension, - objects, - options: { ...options, namespace }, - }); + return await performCollectMultiNamespaceReferences( + { + objects, + options, + }, + this.apiExecutionContext + ); } /** @@ -474,22 +470,16 @@ export class SavedObjectsRepository implements ISavedObjectsRepository { spacesToAdd: string[], spacesToRemove: string[], options: SavedObjectsUpdateObjectsSpacesOptions = {} - ) { - const namespace = this.getCurrentNamespace(options.namespace); - return updateObjectsSpaces({ - mappings: this._mappings, - registry: this._registry, - allowedTypes: this._allowedTypes, - client: this.client, - serializer: this._serializer, - logger: this._logger, - getIndexForType: this.helpers.common.getIndexForType.bind(this.helpers.common), - securityExtension: this.extensions.securityExtension, - objects, - spacesToAdd, - spacesToRemove, - options: { ...options, namespace }, - }); + ): Promise { + return await performUpdateObjectsSpaces( + { + objects, + spacesToAdd, + spacesToRemove, + options, + }, + this.apiExecutionContext + ); } /** diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/update_objects_spaces.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/update_objects_spaces.ts index f4d6139908ae2..0e24f55bca3f9 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/update_objects_spaces.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/update_objects_spaces.ts @@ -23,14 +23,12 @@ import type { AuthorizeObjectWithExistingSpaces, ISavedObjectsSecurityExtension, ISavedObjectTypeRegistry, + ISavedObjectsSerializer, SavedObjectsRawDocSource, } from '@kbn/core-saved-objects-server'; import { ALL_NAMESPACES_STRING } from '@kbn/core-saved-objects-utils-server'; import { SavedObjectsErrorHelpers, type DecoratedError } from '@kbn/core-saved-objects-server'; -import type { - IndexMapping, - SavedObjectsSerializer, -} from '@kbn/core-saved-objects-base-server-internal'; +import type { IndexMapping } from '@kbn/core-saved-objects-base-server-internal'; import { getBulkOperationError, getExpectedVersionProperties, @@ -54,7 +52,7 @@ export interface UpdateObjectsSpacesParams { registry: ISavedObjectTypeRegistry; allowedTypes: string[]; client: RepositoryEsClient; - serializer: SavedObjectsSerializer; + serializer: ISavedObjectsSerializer; logger: Logger; getIndexForType: (type: string) => string; securityExtension: ISavedObjectsSecurityExtension | undefined;