Skip to content

Commit

Permalink
implement remaining missing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed May 10, 2023
1 parent ba8563a commit f640226
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -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<T = unknown> {
objects: SavedObjectsCollectMultiNamespaceReferencesObject[];
options: SavedObjectsCollectMultiNamespaceReferencesOptions;
}

export const performCollectMultiNamespaceReferences = async <T>(
{ objects, options }: PerformCreateParams<T>,
{ registry, helpers, allowedTypes, client, serializer, extensions = {} }: ApiExecutionContext
): Promise<SavedObjectsCollectMultiNamespaceReferencesResponse> => {
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 },
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
@@ -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<T = unknown> {
objects: SavedObjectsUpdateObjectsSpacesObject[];
spacesToAdd: string[];
spacesToRemove: string[];
options: SavedObjectsUpdateObjectsSpacesOptions;
}

export const performUpdateObjectsSpaces = async <T>(
{ objects, spacesToAdd, spacesToRemove, options }: PerformCreateParams<T>,
{
registry,
helpers,
allowedTypes,
client,
serializer,
logger,
mappings,
extensions = {},
}: ApiExecutionContext
): Promise<SavedObjectsUpdateObjectsSpacesResponse> => {
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 },
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,8 +24,11 @@ export class CommonHelper {
private defaultIndex: string;
private kibanaVersion: string;

public readonly createPointInTimeFinder: CreatePointInTimeFinderFn;

constructor({
registry,
createPointInTimeFinder,
spaceExtension,
encryptionExtension,
kibanaVersion,
Expand All @@ -33,6 +37,7 @@ export class CommonHelper {
registry: ISavedObjectTypeRegistry;
spaceExtension?: ISavedObjectsSpacesExtension;
encryptionExtension?: ISavedObjectsEncryptionExtension;
createPointInTimeFinder: CreatePointInTimeFinderFn;
defaultIndex: string;
kibanaVersion: string;
}) {
Expand All @@ -41,6 +46,7 @@ export class CommonHelper {
this.encryptionExtension = encryptionExtension;
this.kibanaVersion = kibanaVersion;
this.defaultIndex = defaultIndex;
this.createPointInTimeFinder = createPointInTimeFinder;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import type {
SavedObjectsResolveResponse,
SavedObjectsCollectMultiNamespaceReferencesObject,
SavedObjectsUpdateObjectsSpacesObject,
SavedObjectsUpdateObjectsSpacesResponse,
SavedObjectsUpdateOptions,
SavedObjectsOpenPointInTimeOptions,
SavedObjectsClosePointInTimeOptions,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -91,6 +90,8 @@ import {
performIncrementCounter,
performBulkResolve,
performResolve,
performUpdateObjectsSpaces,
performCollectMultiNamespaceReferences,
} from './apis';

export interface SavedObjectsRepositoryOptions {
Expand Down Expand Up @@ -145,18 +146,17 @@ 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(
`Missing mappings for saved objects types: '${missingTypeMappings.join(', ')}'`
);
}

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({
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
);
}

/**
Expand All @@ -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<SavedObjectsUpdateObjectsSpacesResponse> {
return await performUpdateObjectsSpaces(
{
objects,
spacesToAdd,
spacesToRemove,
options,
},
this.apiExecutionContext
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit f640226

Please sign in to comment.