Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed Nov 29, 2020
1 parent 8f06356 commit 2256c32
Show file tree
Hide file tree
Showing 24 changed files with 1,001 additions and 35 deletions.
23 changes: 23 additions & 0 deletions src/core/server/saved_objects/object_types/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* @internal
*/
export const LEGACY_URL_ALIAS_TYPE = 'legacy-url-alias';
22 changes: 22 additions & 0 deletions src/core/server/saved_objects/object_types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { LEGACY_URL_ALIAS_TYPE } from './constants';
export { LegacyUrlAlias } from './types';
export { registerCoreObjectTypes } from './registration';
36 changes: 36 additions & 0 deletions src/core/server/saved_objects/object_types/registration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { typeRegistryMock } from '../saved_objects_type_registry.mock';
import { LEGACY_URL_ALIAS_TYPE } from './constants';
import { registerCoreObjectTypes } from './registration';

describe('Core saved object types registration', () => {
describe('#registerCoreObjectTypes', () => {
it('registers all expected types', () => {
const typeRegistry = typeRegistryMock.create();
registerCoreObjectTypes(typeRegistry);

expect(typeRegistry.registerType).toHaveBeenCalledTimes(1);
expect(typeRegistry.registerType).toHaveBeenCalledWith(
expect.objectContaining({ name: LEGACY_URL_ALIAS_TYPE })
);
});
});
});
46 changes: 46 additions & 0 deletions src/core/server/saved_objects/object_types/registration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { LEGACY_URL_ALIAS_TYPE } from './constants';
import { ISavedObjectTypeRegistry, SavedObjectTypeRegistry } from '..';

const legacyUrlAliasMappings = {
properties: {
targetNamespace: { type: 'keyword' },
targetType: { type: 'keyword' },
targetId: { type: 'keyword' },
lastResolved: { type: 'date' },
resolveCounter: { type: 'integer' },
disabled: { type: 'boolean' },
},
};

/**
* @internal
*/
export function registerCoreObjectTypes(
typeRegistry: ISavedObjectTypeRegistry & Pick<SavedObjectTypeRegistry, 'registerType'>
) {
typeRegistry.registerType({
name: LEGACY_URL_ALIAS_TYPE,
namespaceType: 'agnostic',
mappings: legacyUrlAliasMappings,
hidden: true,
});
}
30 changes: 30 additions & 0 deletions src/core/server/saved_objects/object_types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* @internal
*/
export interface LegacyUrlAlias {
targetNamespace: string;
targetType: string;
targetId: string;
lastResolved?: string;
resolveCounter?: number;
disabled?: boolean;
}
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Logger } from '../../logging';
import { SavedObjectConfig } from '../saved_objects_config';
import { IKibanaMigrator } from '../migrations';
import { registerGetRoute } from './get';
import { registerResolveRoute } from './resolve';
import { registerCreateRoute } from './create';
import { registerDeleteRoute } from './delete';
import { registerFindRoute } from './find';
Expand Down Expand Up @@ -49,6 +50,7 @@ export function registerRoutes({
const router = http.createRouter('/api/saved_objects/');

registerGetRoute(router);
registerResolveRoute(router);
registerCreateRoute(router);
registerDeleteRoute(router);
registerFindRoute(router);
Expand Down
40 changes: 40 additions & 0 deletions src/core/server/saved_objects/routes/resolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { schema } from '@kbn/config-schema';
import { IRouter } from '../../http';

export const registerResolveRoute = (router: IRouter) => {
router.get(
{
path: '/resolve/{type}/{id}',
validate: {
params: schema.object({
type: schema.string(),
id: schema.string(),
}),
},
},
router.handleLegacyErrors(async (context, req, res) => {
const { type, id } = req.params;
const result = await context.core.savedObjects.client.resolve(type, id);
return res.ok({ body: result });
})
);
};
3 changes: 3 additions & 0 deletions src/core/server/saved_objects/saved_objects_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { registerRoutes } from './routes';
import { ServiceStatus } from '../status';
import { calculateStatus$ } from './status';
import { createMigrationEsClient } from './migrations/core/';
import { registerCoreObjectTypes } from './object_types';
/**
* Saved Objects is Kibana's data persistence mechanism allowing plugins to
* use Elasticsearch for storing and querying state. The SavedObjectsServiceSetup API exposes methods
Expand Down Expand Up @@ -306,6 +307,8 @@ export class SavedObjectsService
migratorPromise: this.migrator$.pipe(first()).toPromise(),
});

registerCoreObjectTypes(this.typeRegistry);

return {
status$: calculateStatus$(
this.migrator$.pipe(switchMap((migrator) => migrator.getStatus$())),
Expand Down
22 changes: 22 additions & 0 deletions src/core/server/saved_objects/serialization/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { SavedObjectsSerializer } from './serializer';
import { SavedObjectsRawDoc } from './types';
import { typeRegistryMock } from '../saved_objects_type_registry.mock';
import { encodeVersion } from '../version';
import { LEGACY_URL_ALIAS_TYPE } from '../object_types';

let typeRegistry = typeRegistryMock.create();
typeRegistry.isNamespaceAgnostic.mockReturnValue(true);
Expand Down Expand Up @@ -1269,3 +1270,24 @@ describe('#generateRawId', () => {
});
});
});

describe('#generateRawLegacyUrlAliasId', () => {
describe(`returns expected value`, () => {
const expected = `${LEGACY_URL_ALIAS_TYPE}:foo:bar:baz`;

test(`for single-namespace types`, () => {
const id = singleNamespaceSerializer.generateRawLegacyUrlAliasId('foo', 'bar', 'baz');
expect(id).toEqual(expected);
});

test(`for multi-namespace types`, () => {
const id = multiNamespaceSerializer.generateRawLegacyUrlAliasId('foo', 'bar', 'baz');
expect(id).toEqual(expected);
});

test(`for namespace-agnostic types`, () => {
const id = namespaceAgnosticSerializer.generateRawLegacyUrlAliasId('foo', 'bar', 'baz');
expect(id).toEqual(expected);
});
});
});
12 changes: 12 additions & 0 deletions src/core/server/saved_objects/serialization/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import uuid from 'uuid';
import { LEGACY_URL_ALIAS_TYPE } from '../object_types';
import { decodeVersion, encodeVersion } from '../version';
import { ISavedObjectTypeRegistry } from '../saved_objects_type_registry';
import { SavedObjectsRawDoc, SavedObjectSanitizedDoc, RawDocParseOptions } from './types';
Expand Down Expand Up @@ -150,6 +151,17 @@ export class SavedObjectsSerializer {
return `${namespacePrefix}${type}:${id || uuid.v1()}`;
}

/**
* Given a saved object type and id, generates the compound id that is stored in the raw document for its legacy URL alias.
*
* @param {string} namespace - The namespace of the saved object
* @param {string} type - The saved object type
* @param {string} id - The id of the saved object
*/
public generateRawLegacyUrlAliasId(namespace: string, type: string, id: string) {
return `${LEGACY_URL_ALIAS_TYPE}:${namespace}:${type}:${id}`;
}

private trimIdPrefix(
namespace: string | undefined,
type: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const create = (): jest.Mocked<ISavedObjectsRepository> => ({
bulkGet: jest.fn(),
find: jest.fn(),
get: jest.fn(),
resolve: jest.fn(),
update: jest.fn(),
addToNamespaces: jest.fn(),
deleteFromNamespaces: jest.fn(),
Expand Down
Loading

0 comments on commit 2256c32

Please sign in to comment.