From 10a70c7c59f03ea97792a5d2f716656426d3996d Mon Sep 17 00:00:00 2001 From: Corey Robertson Date: Fri, 28 Feb 2020 10:58:20 -0500 Subject: [PATCH 1/2] Move saved object mappings and migratins to kibana platform --- x-pack/legacy/plugins/canvas/index.js | 4 -- .../legacy/plugins/canvas/migrations.test.js | 37 ------------------ x-pack/plugins/canvas/server/plugin.ts | 4 ++ .../server/saved_objects/custom_element.ts} | 28 +++++-------- .../canvas/server/saved_objects/index.ts} | 14 ++----- .../migrations/remove_attributes_id.test.ts | 39 +++++++++++++++++++ .../migrations/remove_attributes_id.ts | 14 +++++++ .../canvas/server/saved_objects/workpad.ts | 34 ++++++++++++++++ 8 files changed, 103 insertions(+), 71 deletions(-) delete mode 100644 x-pack/legacy/plugins/canvas/migrations.test.js rename x-pack/{legacy/plugins/canvas/server/mappings.ts => plugins/canvas/server/saved_objects/custom_element.ts} (56%) rename x-pack/{legacy/plugins/canvas/migrations.js => plugins/canvas/server/saved_objects/index.ts} (53%) create mode 100644 x-pack/plugins/canvas/server/saved_objects/migrations/remove_attributes_id.test.ts create mode 100644 x-pack/plugins/canvas/server/saved_objects/migrations/remove_attributes_id.ts create mode 100644 x-pack/plugins/canvas/server/saved_objects/workpad.ts diff --git a/x-pack/legacy/plugins/canvas/index.js b/x-pack/legacy/plugins/canvas/index.js index 489b9600f200..a1d4b35826b0 100644 --- a/x-pack/legacy/plugins/canvas/index.js +++ b/x-pack/legacy/plugins/canvas/index.js @@ -7,9 +7,7 @@ import { resolve } from 'path'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/utils'; import { init } from './init'; -import { mappings } from './server/mappings'; import { CANVAS_APP, CANVAS_TYPE, CUSTOM_ELEMENT_TYPE } from './common/lib'; -import { migrations } from './migrations'; export function canvas(kibana) { return new kibana.Plugin({ @@ -33,8 +31,6 @@ export function canvas(kibana) { 'plugins/canvas/lib/window_error_handler.js', ], home: ['plugins/canvas/legacy_register_feature'], - mappings, - migrations, savedObjectsManagement: { [CANVAS_TYPE]: { icon: 'canvasApp', diff --git a/x-pack/legacy/plugins/canvas/migrations.test.js b/x-pack/legacy/plugins/canvas/migrations.test.js deleted file mode 100644 index 182ef3b18cce..000000000000 --- a/x-pack/legacy/plugins/canvas/migrations.test.js +++ /dev/null @@ -1,37 +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 { migrations } from './migrations'; -import { CANVAS_TYPE } from './common/lib'; - -describe(`${CANVAS_TYPE}`, () => { - describe('7.0.0', () => { - const migrate = doc => migrations[CANVAS_TYPE]['7.0.0'](doc); - - it('does not throw error on empty object', () => { - const migratedDoc = migrate({}); - expect(migratedDoc).toMatchInlineSnapshot(`Object {}`); - }); - - it('removes id from "attributes"', () => { - const migratedDoc = migrate({ - foo: true, - attributes: { - id: '123', - bar: true, - }, - }); - expect(migratedDoc).toMatchInlineSnapshot(` -Object { - "attributes": Object { - "bar": true, - }, - "foo": true, -} -`); - }); - }); -}); diff --git a/x-pack/plugins/canvas/server/plugin.ts b/x-pack/plugins/canvas/server/plugin.ts index bfda7ef5885b..a0d77bfe2627 100644 --- a/x-pack/plugins/canvas/server/plugin.ts +++ b/x-pack/plugins/canvas/server/plugin.ts @@ -14,6 +14,7 @@ import { initRoutes } from './routes'; import { registerCanvasUsageCollector } from './collectors'; import { loadSampleData } from './sample_data'; import { setupInterpreter } from './setup_interpreter'; +import { customElementType, workpadType } from './saved_objects'; interface PluginsSetup { expressions: ExpressionsServerSetup; @@ -29,6 +30,9 @@ export class CanvasPlugin implements Plugin { } public async setup(coreSetup: CoreSetup, plugins: PluginsSetup) { + coreSetup.savedObjects.registerType(customElementType); + coreSetup.savedObjects.registerType(workpadType); + plugins.features.registerFeature({ id: 'canvas', name: 'Canvas', diff --git a/x-pack/legacy/plugins/canvas/server/mappings.ts b/x-pack/plugins/canvas/server/saved_objects/custom_element.ts similarity index 56% rename from x-pack/legacy/plugins/canvas/server/mappings.ts rename to x-pack/plugins/canvas/server/saved_objects/custom_element.ts index bf2be51882b1..1d4ed89f6a82 100644 --- a/x-pack/legacy/plugins/canvas/server/mappings.ts +++ b/x-pack/plugins/canvas/server/saved_objects/custom_element.ts @@ -4,26 +4,15 @@ * you may not use this file except in compliance with the Elastic License. */ -// @ts-ignore converting /libs/constants to TS breaks CI -import { CANVAS_TYPE, CUSTOM_ELEMENT_TYPE } from '../common/lib/constants'; +import { SavedObjectsType } from 'src/core/server'; +import { CUSTOM_ELEMENT_TYPE } from '../../../../legacy/plugins/canvas/common/lib/constants'; -export const mappings = { - [CANVAS_TYPE]: { - dynamic: false, - properties: { - name: { - type: 'text', - fields: { - keyword: { - type: 'keyword', - }, - }, - }, - '@timestamp': { type: 'date' }, - '@created': { type: 'date' }, - }, - }, - [CUSTOM_ELEMENT_TYPE]: { +export const customElementType: SavedObjectsType = { + name: CUSTOM_ELEMENT_TYPE, + hidden: false, + namespaceAgnostic: false, + mappings: { + // @ts-ignore dynamic: false, properties: { name: { @@ -41,4 +30,5 @@ export const mappings = { '@created': { type: 'date' }, }, }, + migrations: {}, }; diff --git a/x-pack/legacy/plugins/canvas/migrations.js b/x-pack/plugins/canvas/server/saved_objects/index.ts similarity index 53% rename from x-pack/legacy/plugins/canvas/migrations.js rename to x-pack/plugins/canvas/server/saved_objects/index.ts index d5b3d3fb1ce2..dd7e74b87e2f 100644 --- a/x-pack/legacy/plugins/canvas/migrations.js +++ b/x-pack/plugins/canvas/server/saved_objects/index.ts @@ -4,15 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { CANVAS_TYPE } from './common/lib'; +import { workpadType } from './workpad'; +import { customElementType } from './custom_element'; -export const migrations = { - [CANVAS_TYPE]: { - '7.0.0': doc => { - if (doc.attributes) { - delete doc.attributes.id; - } - return doc; - }, - }, -}; +export { customElementType, workpadType }; diff --git a/x-pack/plugins/canvas/server/saved_objects/migrations/remove_attributes_id.test.ts b/x-pack/plugins/canvas/server/saved_objects/migrations/remove_attributes_id.test.ts new file mode 100644 index 000000000000..a7112504e998 --- /dev/null +++ b/x-pack/plugins/canvas/server/saved_objects/migrations/remove_attributes_id.test.ts @@ -0,0 +1,39 @@ +/* + * 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 { removeAttributesId } from './remove_attributes_id'; + +const context: any = { + log: jest.fn(), +}; + +describe(`removeAttributesId`, () => { + it('does not throw error on empty object', () => { + const migratedDoc = removeAttributesId({} as any, context); + expect(migratedDoc).toMatchInlineSnapshot(`Object {}`); + }); + + it('removes id from "attributes"', () => { + const migratedDoc = removeAttributesId( + { + foo: true, + attributes: { + id: '123', + bar: true, + }, + } as any, + context + ); + expect(migratedDoc).toMatchInlineSnapshot(` +Object { + "attributes": Object { + "bar": true, + }, + "foo": true, +} +`); + }); +}); diff --git a/x-pack/plugins/canvas/server/saved_objects/migrations/remove_attributes_id.ts b/x-pack/plugins/canvas/server/saved_objects/migrations/remove_attributes_id.ts new file mode 100644 index 000000000000..893a73d7b591 --- /dev/null +++ b/x-pack/plugins/canvas/server/saved_objects/migrations/remove_attributes_id.ts @@ -0,0 +1,14 @@ +/* + * 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 { SavedObjectMigrationFn } from 'src/core/server'; + +export const removeAttributesId: SavedObjectMigrationFn = doc => { + if (typeof doc.attributes === 'object' && doc.attributes !== null) { + delete (doc.attributes as any).id; + } + return doc; +}; diff --git a/x-pack/plugins/canvas/server/saved_objects/workpad.ts b/x-pack/plugins/canvas/server/saved_objects/workpad.ts new file mode 100644 index 000000000000..27a938485056 --- /dev/null +++ b/x-pack/plugins/canvas/server/saved_objects/workpad.ts @@ -0,0 +1,34 @@ +/* + * 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 { SavedObjectsType } from 'src/core/server'; +import { CANVAS_TYPE } from '../../../../legacy/plugins/canvas/common/lib/constants'; +import { removeAttributesId } from './migrations/remove_attributes_id'; + +export const workpadType: SavedObjectsType = { + name: CANVAS_TYPE, + hidden: false, + namespaceAgnostic: false, + mappings: { + // @ts-ignore + dynamic: false, + properties: { + name: { + type: 'text', + fields: { + keyword: { + type: 'keyword', + }, + }, + }, + '@timestamp': { type: 'date' }, + '@created': { type: 'date' }, + }, + }, + migrations: { + '7.0.0': removeAttributesId, + }, +}; From 46c9cc86ad51e07c2afe865ab56607a376678a62 Mon Sep 17 00:00:00 2001 From: Corey Robertson Date: Fri, 28 Feb 2020 11:00:22 -0500 Subject: [PATCH 2/2] Remove ts-ignore --- x-pack/plugins/canvas/server/saved_objects/custom_element.ts | 1 - x-pack/plugins/canvas/server/saved_objects/workpad.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/x-pack/plugins/canvas/server/saved_objects/custom_element.ts b/x-pack/plugins/canvas/server/saved_objects/custom_element.ts index 1d4ed89f6a82..dadead0263be 100644 --- a/x-pack/plugins/canvas/server/saved_objects/custom_element.ts +++ b/x-pack/plugins/canvas/server/saved_objects/custom_element.ts @@ -12,7 +12,6 @@ export const customElementType: SavedObjectsType = { hidden: false, namespaceAgnostic: false, mappings: { - // @ts-ignore dynamic: false, properties: { name: { diff --git a/x-pack/plugins/canvas/server/saved_objects/workpad.ts b/x-pack/plugins/canvas/server/saved_objects/workpad.ts index 27a938485056..e83ba9720b43 100644 --- a/x-pack/plugins/canvas/server/saved_objects/workpad.ts +++ b/x-pack/plugins/canvas/server/saved_objects/workpad.ts @@ -13,7 +13,6 @@ export const workpadType: SavedObjectsType = { hidden: false, namespaceAgnostic: false, mappings: { - // @ts-ignore dynamic: false, properties: { name: {