From ec1bd97422fbde94b07cc8d9dee4102a2ef76f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien=20Loix?= Date: Wed, 25 Mar 2020 15:12:35 +0100 Subject: [PATCH 01/31] Add helper method to get template format version (1 or 2) --- .../plugins/index_management/common/index.ts | 2 ++ .../common/types/templates.ts | 21 +++++++++--- .../index_management/server/lib/utils.test.ts | 33 +++++++++++++++++++ .../index_management/server/lib/utils.ts | 16 +++++++++ 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 x-pack/plugins/index_management/server/lib/utils.test.ts create mode 100644 x-pack/plugins/index_management/server/lib/utils.ts diff --git a/x-pack/plugins/index_management/common/index.ts b/x-pack/plugins/index_management/common/index.ts index 0cc4ba79711ce..ac9db87c6a72b 100644 --- a/x-pack/plugins/index_management/common/index.ts +++ b/x-pack/plugins/index_management/common/index.ts @@ -5,3 +5,5 @@ */ export { PLUGIN, API_BASE_PATH } from './constants'; + +export * from './types'; diff --git a/x-pack/plugins/index_management/common/types/templates.ts b/x-pack/plugins/index_management/common/types/templates.ts index e31c10a775156..cdeaa2429296c 100644 --- a/x-pack/plugins/index_management/common/types/templates.ts +++ b/x-pack/plugins/index_management/common/types/templates.ts @@ -17,20 +17,33 @@ export interface TemplateListItem { }; isManaged: boolean; } -export interface Template { +interface TemplateBase { name: string; indexPatterns: string[]; version?: number; order?: number; - settings?: object; - aliases?: object; - mappings?: object; ilmPolicy?: { name: string; }; isManaged: boolean; } +export interface TemplateV1 extends TemplateBase { + settings?: object; + aliases?: object; + mappings?: object; +} + +export interface TemplateV2 extends TemplateBase { + template: { + settings?: object; + aliases?: object; + mappings?: object; + }; +} + +export type Template = TemplateV1 | TemplateV2; + export interface TemplateEs { name: string; index_patterns: string[]; diff --git a/x-pack/plugins/index_management/server/lib/utils.test.ts b/x-pack/plugins/index_management/server/lib/utils.test.ts new file mode 100644 index 0000000000000..7ec3313a68a51 --- /dev/null +++ b/x-pack/plugins/index_management/server/lib/utils.test.ts @@ -0,0 +1,33 @@ +/* + * 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 { TemplateV1, TemplateV2 } from '../../common'; +import { getTemplateVersion } from './utils'; + +describe('utils', () => { + describe('getTemplateVersion', () => { + test('should detect v1 template', () => { + const template = { + indexPatterns: ['logs*'], + mappings: { + properties: {}, + }, + }; + expect(getTemplateVersion(template as TemplateV1)).toBe(1); + }); + + test('should detect v2 template', () => { + const template = { + indexPatterns: ['logs*'], + template: { + mappings: { + properties: {}, + }, + }, + }; + expect(getTemplateVersion(template as TemplateV2)).toBe(2); + }); + }); +}); diff --git a/x-pack/plugins/index_management/server/lib/utils.ts b/x-pack/plugins/index_management/server/lib/utils.ts new file mode 100644 index 0000000000000..3fff95b4ce34f --- /dev/null +++ b/x-pack/plugins/index_management/server/lib/utils.ts @@ -0,0 +1,16 @@ +/* + * 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 { Template } from '../../common'; + +/** + * Helper to get the format version of an index template. + * v1 will be supported up until 9.x but marked as deprecated from 7.8 + * v2 will be supported from 7.8 + */ +export const getTemplateVersion = (template: Template): 1 | 2 => { + return {}.hasOwnProperty.call(template, 'template') ? 2 : 1; +}; From 1a91c0071aebea32ed5698160b5ff73d6bdb2a9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien=20Loix?= Date: Wed, 25 Mar 2020 15:56:29 +0100 Subject: [PATCH 02/31] Rename interface to make explicit deserialized template vs serialized one --- .../client_integration/helpers/home.helpers.ts | 13 ++++++++----- .../helpers/template_form.helpers.ts | 4 ++-- .../common/lib/template_serialization.ts | 12 ++++++------ .../index_management/common/types/templates.ts | 8 ++++++-- .../components/mappings_editor/reducer.ts | 4 ++-- .../components/template_delete_modal.tsx | 4 ++-- .../template_form/steps/step_review.tsx | 4 ++-- .../components/template_form/template_form.tsx | 13 ++++++++----- .../components/template_form/types.ts | 4 ++-- .../template_details/tabs/tab_aliases.tsx | 4 ++-- .../template_details/tabs/tab_mappings.tsx | 4 ++-- .../template_details/tabs/tab_settings.tsx | 4 ++-- .../template_details/tabs/tab_summary.tsx | 4 ++-- .../template_details/template_details.tsx | 12 ++++++------ .../home/template_list/template_list.tsx | 12 +++++++----- .../template_table/template_table.tsx | 18 +++++++++--------- .../sections/template_clone/template_clone.tsx | 6 +++--- .../template_create/template_create.tsx | 4 ++-- .../sections/template_edit/template_edit.tsx | 4 ++-- .../public/application/services/api.ts | 10 +++++----- .../index_management/server/lib/utils.ts | 4 ++-- .../api/templates/register_create_route.ts | 6 +++--- .../api/templates/register_delete_route.ts | 4 ++-- .../api/templates/register_update_route.ts | 6 +++--- .../index_management/test/fixtures/template.ts | 4 ++-- 25 files changed, 92 insertions(+), 80 deletions(-) diff --git a/x-pack/plugins/index_management/__jest__/client_integration/helpers/home.helpers.ts b/x-pack/plugins/index_management/__jest__/client_integration/helpers/home.helpers.ts index 7e3e1fba9c44a..397a78354f470 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/helpers/home.helpers.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/helpers/home.helpers.ts @@ -16,7 +16,7 @@ import { import { IndexManagementHome } from '../../../public/application/sections/home'; // eslint-disable-line @kbn/eslint/no-restricted-paths import { BASE_PATH } from '../../../common/constants'; import { indexManagementStore } from '../../../public/application/store'; // eslint-disable-line @kbn/eslint/no-restricted-paths -import { Template } from '../../../common/types'; +import { TemplateDeserialized } from '../../../common'; import { WithAppDependencies, services } from './setup_environment'; const testBedConfig: TestBedConfig = { @@ -36,10 +36,13 @@ export interface IdxMgmtHomeTestBed extends TestBed { selectHomeTab: (tab: 'indicesTab' | 'templatesTab') => void; selectDetailsTab: (tab: 'summary' | 'settings' | 'mappings' | 'aliases') => void; clickReloadButton: () => void; - clickTemplateAction: (name: Template['name'], action: 'edit' | 'clone' | 'delete') => void; + clickTemplateAction: ( + name: TemplateDeserialized['name'], + action: 'edit' | 'clone' | 'delete' + ) => void; clickTemplateAt: (index: number) => void; clickCloseDetailsButton: () => void; - clickActionMenu: (name: Template['name']) => void; + clickActionMenu: (name: TemplateDeserialized['name']) => void; }; } @@ -78,7 +81,7 @@ export const setup = async (): Promise => { find('reloadButton').simulate('click'); }; - const clickActionMenu = async (templateName: Template['name']) => { + const clickActionMenu = async (templateName: TemplateDeserialized['name']) => { const { component } = testBed; // When a table has > 2 actions, EUI displays an overflow menu with an id "-actions" @@ -87,7 +90,7 @@ export const setup = async (): Promise => { }; const clickTemplateAction = ( - templateName: Template['name'], + templateName: TemplateDeserialized['name'], action: 'edit' | 'clone' | 'delete' ) => { const actions = ['edit', 'clone', 'delete']; diff --git a/x-pack/plugins/index_management/__jest__/client_integration/helpers/template_form.helpers.ts b/x-pack/plugins/index_management/__jest__/client_integration/helpers/template_form.helpers.ts index 9d4eb631a1c40..9d62ed6bdbe5f 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/helpers/template_form.helpers.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/helpers/template_form.helpers.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import { TestBed, SetupFunc, UnwrapPromise } from '../../../../../test_utils'; -import { Template } from '../../../common/types'; +import { TemplateDeserialized } from '../../../common'; import { nextTick } from './index'; interface MappingField { @@ -62,7 +62,7 @@ export const formSetup = async (initTestBed: SetupFunc) => { indexPatterns, order, version, - }: Partial