Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [SavedObjectsSchema](./kibana-plugin-server.savedobjectsschema.md) &gt; [getConvertToAliasScript](./kibana-plugin-server.savedobjectsschema.getconverttoaliasscript.md)

## SavedObjectsSchema.getConvertToAliasScript() method

<b>Signature:</b>

```typescript
getConvertToAliasScript(type: string): string | undefined;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| type | <code>string</code> | |

<b>Returns:</b>

`string | undefined`

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<b>Signature:</b>

```typescript
getIndexForType(type: string): string | undefined;
getIndexForType(config: Config, type: string): string | undefined;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| config | <code>Config</code> | |
| type | <code>string</code> | |

<b>Returns:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export declare class SavedObjectsSchema

| Method | Modifiers | Description |
| --- | --- | --- |
| [getIndexForType(type)](./kibana-plugin-server.savedobjectsschema.getindexfortype.md) | | |
| [getConvertToAliasScript(type)](./kibana-plugin-server.savedobjectsschema.getconverttoaliasscript.md) | | |
| [getIndexForType(config, type)](./kibana-plugin-server.savedobjectsschema.getindexfortype.md) | | |
| [isHiddenType(type)](./kibana-plugin-server.savedobjectsschema.ishiddentype.md) | | |
| [isNamespaceAgnostic(type)](./kibana-plugin-server.savedobjectsschema.isnamespaceagnostic.md) | | |

Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,28 @@
*/

import { createIndexMap } from './build_index_map';
import { ObjectToConfigAdapter } from '../../../config';
import { SavedObjectsSchema } from '../../schema';

test('mappings without index pattern goes to default index', () => {
const result = createIndexMap(
'.kibana',
{
const result = createIndexMap({
config: new ObjectToConfigAdapter({}),
kibanaIndexName: '.kibana',
schema: new SavedObjectsSchema({
type1: {
isNamespaceAgnostic: false,
},
},
{
}),
indexMap: {
type1: {
properties: {
field1: {
type: 'string',
},
},
},
}
);
},
});
expect(result).toEqual({
'.kibana': {
typeMappings: {
Expand All @@ -53,24 +56,25 @@ test('mappings without index pattern goes to default index', () => {
});

test(`mappings with custom index pattern doesn't go to default index`, () => {
const result = createIndexMap(
'.kibana',
{
const result = createIndexMap({
config: new ObjectToConfigAdapter({}),
kibanaIndexName: '.kibana',
schema: new SavedObjectsSchema({
type1: {
isNamespaceAgnostic: false,
indexPattern: '.other_kibana',
},
},
{
}),
indexMap: {
type1: {
properties: {
field1: {
type: 'string',
},
},
},
}
);
},
});
expect(result).toEqual({
'.other_kibana': {
typeMappings: {
Expand All @@ -87,25 +91,26 @@ test(`mappings with custom index pattern doesn't go to default index`, () => {
});

test('creating a script gets added to the index pattern', () => {
const result = createIndexMap(
'.kibana',
{
const result = createIndexMap({
config: new ObjectToConfigAdapter({}),
kibanaIndexName: '.kibana',
schema: new SavedObjectsSchema({
type1: {
isNamespaceAgnostic: false,
indexPattern: '.other_kibana',
convertToAliasScript: `ctx._id = ctx._source.type + ':' + ctx._id`,
},
},
{
}),
indexMap: {
type1: {
properties: {
field1: {
type: 'string',
},
},
},
}
);
},
});
expect(result).toEqual({
'.other_kibana': {
script: `ctx._id = ctx._source.type + ':' + ctx._id`,
Expand All @@ -124,7 +129,7 @@ test('creating a script gets added to the index pattern', () => {

test('throws when two scripts are defined for an index pattern', () => {
const defaultIndex = '.kibana';
const savedObjectSchemas = {
const schema = new SavedObjectsSchema({
type1: {
isNamespaceAgnostic: false,
convertToAliasScript: `ctx._id = ctx._source.type + ':' + ctx._id`,
Expand All @@ -133,7 +138,7 @@ test('throws when two scripts are defined for an index pattern', () => {
isNamespaceAgnostic: false,
convertToAliasScript: `ctx._id = ctx._source.type + ':' + ctx._id`,
},
};
});
const indexMap = {
type1: {
properties: {
Expand All @@ -151,7 +156,12 @@ test('throws when two scripts are defined for an index pattern', () => {
},
};
expect(() =>
createIndexMap(defaultIndex, savedObjectSchemas, indexMap)
createIndexMap({
config: new ObjectToConfigAdapter({}),
kibanaIndexName: defaultIndex,
schema,
indexMap,
})
).toThrowErrorMatchingInlineSnapshot(
`"convertToAliasScript has been defined more than once for index pattern \\".kibana\\""`
);
Expand Down
27 changes: 18 additions & 9 deletions src/core/server/saved_objects/migrations/core/build_index_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@
* under the License.
*/

import { Config } from '../../../config';
import { MappingProperties } from '../../mappings';
import { SavedObjectsSchemaDefinition } from '../../schema';
import { SavedObjectsSchema } from '../../schema';

export interface CreateIndexMapOptions {
config: Config;
kibanaIndexName: string;
schema: SavedObjectsSchema;
indexMap: MappingProperties;
}

export interface IndexMap {
[index: string]: {
Expand All @@ -30,16 +38,17 @@ export interface IndexMap {
/*
* This file contains logic to convert savedObjectSchemas into a dictonary of indexes and documents
*/
export function createIndexMap(
defaultIndex: string,
savedObjectSchemas: SavedObjectsSchemaDefinition,
indexMap: MappingProperties
) {
export function createIndexMap({
config,
kibanaIndexName,
schema,
indexMap,
}: CreateIndexMapOptions) {
const map: IndexMap = {};
Object.keys(indexMap).forEach(type => {
const schema = savedObjectSchemas[type] || {};
const script = schema.convertToAliasScript;
const indexPattern = schema.indexPattern || defaultIndex;
const script = schema.getConvertToAliasScript(type);
// Defaults to kibanaIndexName if indexPattern isn't defined
const indexPattern = schema.getIndexForType(config, type) || kibanaIndexName;
if (!map.hasOwnProperty(indexPattern as string)) {
map[indexPattern] = { typeMappings: {} };
}
Expand Down
18 changes: 10 additions & 8 deletions src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { docValidator } from '../../validation';
import { buildActiveMappings, CallCluster, IndexMigrator, LogFn } from '../core';
import { DocumentMigrator, VersionedTransformer } from '../core/document_migrator';
import { createIndexMap } from '../core/build_index_map';
import { Config } from '../../../config';
export interface KbnServer {
server: Server;
version: string;
Expand Down Expand Up @@ -92,13 +93,14 @@ export class KibanaMigrator {
// Wait until elasticsearch is green...
await server.plugins.elasticsearch.waitUntilReady();

const config = server.config();
const config = server.config() as Config;
const kibanaIndexName = config.get('kibana.index');
const indexMap = createIndexMap(
const indexMap = createIndexMap({
config,
kibanaIndexName,
this.kbnServer.uiExports.savedObjectSchemas,
this.mappingProperties
);
indexMap: this.mappingProperties,
schema: this.schema,
});

const migrators = Object.keys(indexMap).map(index => {
return new IndexMigrator({
Expand Down Expand Up @@ -130,6 +132,7 @@ export class KibanaMigrator {
private mappingProperties: MappingProperties;
private log: LogFn;
private serializer: SavedObjectsSerializer;
private readonly schema: SavedObjectsSchema;

/**
* Creates an instance of KibanaMigrator.
Expand All @@ -141,9 +144,8 @@ export class KibanaMigrator {
constructor({ kbnServer }: { kbnServer: KbnServer }) {
this.kbnServer = kbnServer;

this.serializer = new SavedObjectsSerializer(
new SavedObjectsSchema(kbnServer.uiExports.savedObjectSchemas)
);
this.schema = new SavedObjectsSchema(kbnServer.uiExports.savedObjectSchemas);
this.serializer = new SavedObjectsSerializer(this.schema);

this.mappingProperties = mergeProperties(kbnServer.uiExports.savedObjectMappings || []);

Expand Down
1 change: 1 addition & 0 deletions src/core/server/saved_objects/schema/schema.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const createSchemaMock = () => {
getIndexForType: jest.fn().mockReturnValue('.kibana-test'),
isHiddenType: jest.fn().mockReturnValue(false),
isNamespaceAgnostic: jest.fn((type: string) => type === 'global'),
getConvertToAliasScript: jest.fn().mockReturnValue(undefined),
};
return mocked;
};
Expand Down
16 changes: 13 additions & 3 deletions src/core/server/saved_objects/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/

import { Config } from '../../config';

interface SavedObjectsSchemaTypeDefinition {
isNamespaceAgnostic: boolean;
hidden?: boolean;
indexPattern?: string;
indexPattern?: ((config: Config) => string) | string;
convertToAliasScript?: string;
}

Expand All @@ -41,14 +44,21 @@ export class SavedObjectsSchema {
return false;
}

public getIndexForType(type: string): string | undefined {
public getIndexForType(config: Config, type: string): string | undefined {
if (this.definition != null && this.definition.hasOwnProperty(type)) {
return this.definition[type].indexPattern;
const { indexPattern } = this.definition[type];
return typeof indexPattern === 'function' ? indexPattern(config) : indexPattern;
} else {
return undefined;
}
}

public getConvertToAliasScript(type: string): string | undefined {
if (this.definition != null && this.definition.hasOwnProperty(type)) {
return this.definition[type].convertToAliasScript;
}
}

public isNamespaceAgnostic(type: string) {
// if no plugins have registered a uiExports.savedObjectSchemas,
// this.schema will be undefined, and no types are namespace agnostic
Expand Down
9 changes: 7 additions & 2 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { SavedObjectsErrorHelpers } from './errors';
import { decodeRequestVersion, encodeVersion, encodeHitVersion } from '../../version';
import { SavedObjectsSchema } from '../../schema';
import { KibanaMigrator } from '../../migrations';
import { Config } from '../../../config';
import { SavedObjectsSerializer, SanitizedSavedObjectDoc, RawDoc } from '../../serialization';
import {
SavedObject,
Expand Down Expand Up @@ -64,6 +65,7 @@ const isLeft = <L, R>(either: Either<L, R>): either is Left<L> => {

export interface SavedObjectsRepositoryOptions {
index: string;
config: Config;
mappings: IndexMapping;
callCluster: CallCluster;
schema: SavedObjectsSchema;
Expand All @@ -80,6 +82,7 @@ export interface IncrementCounterOptions extends SavedObjectsBaseOptions {
export class SavedObjectsRepository {
private _migrator: KibanaMigrator;
private _index: string;
private _config: Config;
private _mappings: IndexMapping;
private _schema: SavedObjectsSchema;
private _allowedTypes: string[];
Expand All @@ -90,6 +93,7 @@ export class SavedObjectsRepository {
constructor(options: SavedObjectsRepositoryOptions) {
const {
index,
config,
mappings,
callCluster,
schema,
Expand All @@ -108,6 +112,7 @@ export class SavedObjectsRepository {
// to returning them.
this._migrator = migrator;
this._index = index;
this._config = config;
this._mappings = mappings;
this._schema = schema;
if (allowedTypes.length === 0) {
Expand Down Expand Up @@ -741,7 +746,7 @@ export class SavedObjectsRepository {
* @param type - the type
*/
private getIndexForType(type: string) {
return this._schema.getIndexForType(type) || this._index;
return this._schema.getIndexForType(this._config, type) || this._index;
}

/**
Expand All @@ -753,7 +758,7 @@ export class SavedObjectsRepository {
*/
private getIndicesForTypes(types: string[]) {
const unique = (array: string[]) => [...new Set(array)];
return unique(types.map(t => this._schema.getIndexForType(t) || this._index));
return unique(types.map(t => this._schema.getIndexForType(this._config, t) || this._index));
}

private _getCurrentTime() {
Expand Down
4 changes: 3 additions & 1 deletion src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,9 @@ export class SavedObjectsSchema {
// Warning: (ae-forgotten-export) The symbol "SavedObjectsSchemaDefinition" needs to be exported by the entry point index.d.ts
constructor(schemaDefinition?: SavedObjectsSchemaDefinition);
// (undocumented)
getIndexForType(type: string): string | undefined;
getConvertToAliasScript(type: string): string | undefined;
// (undocumented)
getIndexForType(config: Config, type: string): string | undefined;
// (undocumented)
isHiddenType(type: string): boolean;
// (undocumented)
Expand Down
1 change: 1 addition & 0 deletions src/es_archiver/lib/indices/kibana_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export async function migrateKibanaIndex({ client, log, kibanaPluginIds }) {
'migrations.scrollDuration': '5m',
'migrations.batchSize': 100,
'migrations.pollInterval': 100,
'xpack.task_manager.index': '.kibana_task_manager',
};
const ready = async () => undefined;
const elasticsearch = {
Expand Down
Loading