From c947f83b3a1cf05026b241d58fdffb4baac120fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Loix?= Date: Thu, 2 Apr 2020 20:14:46 +0200 Subject: [PATCH] [Index management] Prepare support Index template V2 format (#61588) # Conflicts: # x-pack/plugins/index_management/public/application/components/mappings_editor/mappings_state.tsx # x-pack/plugins/index_management/public/application/services/api.ts # x-pack/plugins/index_management/server/routes/api/templates/register_create_route.ts # x-pack/plugins/index_management/server/routes/api/templates/register_get_routes.ts # x-pack/plugins/index_management/server/routes/api/templates/register_update_route.ts --- .../public/request/np_ready_request.ts | 2 +- .../components/form_data_provider.ts | 5 +- .../helpers/home.helpers.ts | 13 +- .../helpers/template_form.helpers.ts | 36 +++-- .../__jest__/client_integration/home.test.ts | 56 ++++---- .../template_clone.test.tsx | 12 +- .../template_create.test.tsx | 46 ++++--- .../client_integration/template_edit.test.tsx | 83 +++++++----- .../common/constants/index.ts | 1 + .../common/constants/index_templates.ts | 12 ++ .../plugins/index_management/common/index.ts | 6 +- .../index_management/common/lib/index.ts | 6 +- .../common/lib/template_serialization.ts | 128 +++++++++++------- .../index_management/common/lib/utils.test.ts | 35 +++++ .../index_management/common/lib/utils.ts | 29 ++++ .../index_management/common/types/aliases.ts | 9 ++ .../index_management/common/types/index.ts | 6 + .../index_management/common/types/indices.ts | 43 ++++++ .../index_management/common/types/mappings.ts | 11 ++ .../common/types/templates.ts | 79 +++++++---- .../load_mappings_provider.test.tsx | 14 +- .../mappings_editor/mappings_state.tsx | 3 +- .../components/mappings_editor/reducer.ts | 16 ++- .../components/template_delete_modal.tsx | 21 ++- .../template_form/steps/step_aliases.tsx | 2 +- .../template_form/steps/step_mappings.tsx | 19 ++- .../template_form/steps/step_review.tsx | 30 ++-- .../template_form/steps/step_settings.tsx | 2 +- .../template_form/steps/use_json_step.ts | 10 +- .../template_form/template_form.tsx | 51 +++++-- .../components/template_form/types.ts | 13 +- .../public/application/lib/index_templates.ts | 17 +++ .../template_details/tabs/tab_aliases.tsx | 8 +- .../template_details/tabs/tab_mappings.tsx | 8 +- .../template_details/tabs/tab_settings.tsx | 8 +- .../template_details/tabs/tab_summary.tsx | 11 +- .../template_details/template_details.tsx | 31 +++-- .../home/template_list/template_list.tsx | 30 ++-- .../template_table/template_table.tsx | 39 +++--- .../template_clone/template_clone.tsx | 18 ++- .../template_create/template_create.tsx | 7 +- .../sections/template_edit/template_edit.tsx | 17 ++- .../public/application/services/api.ts | 37 +++-- .../public/application/services/routing.ts | 23 +++- .../application/services/use_request.ts | 4 +- .../api/templates/register_create_route.ts | 18 ++- .../api/templates/register_delete_route.ts | 32 +++-- .../api/templates/register_get_routes.ts | 25 +++- .../api/templates/register_update_route.ts | 16 ++- .../routes/api/templates/validate_schemas.ts | 13 +- .../test/fixtures/template.ts | 24 ++-- .../index_management/templates.helpers.js | 57 ++++---- .../management/index_management/templates.js | 31 +++-- x-pack/test_utils/testbed/testbed.ts | 39 ++++++ x-pack/test_utils/testbed/types.ts | 6 + 55 files changed, 916 insertions(+), 402 deletions(-) create mode 100644 x-pack/plugins/index_management/common/constants/index_templates.ts create mode 100644 x-pack/plugins/index_management/common/lib/utils.test.ts create mode 100644 x-pack/plugins/index_management/common/lib/utils.ts create mode 100644 x-pack/plugins/index_management/common/types/aliases.ts create mode 100644 x-pack/plugins/index_management/common/types/indices.ts create mode 100644 x-pack/plugins/index_management/common/types/mappings.ts create mode 100644 x-pack/plugins/index_management/public/application/lib/index_templates.ts diff --git a/src/plugins/es_ui_shared/public/request/np_ready_request.ts b/src/plugins/es_ui_shared/public/request/np_ready_request.ts index 6771abd64df7e0..06af698f2ce023 100644 --- a/src/plugins/es_ui_shared/public/request/np_ready_request.ts +++ b/src/plugins/es_ui_shared/public/request/np_ready_request.ts @@ -43,7 +43,7 @@ export interface UseRequestResponse { isInitialRequest: boolean; isLoading: boolean; error: E | null; - data: D | null; + data?: D | null; sendRequest: (...args: any[]) => Promise>; } diff --git a/src/plugins/es_ui_shared/static/forms/hook_form_lib/components/form_data_provider.ts b/src/plugins/es_ui_shared/static/forms/hook_form_lib/components/form_data_provider.ts index a8d24984cec7cf..0509b8081c35be 100644 --- a/src/plugins/es_ui_shared/static/forms/hook_form_lib/components/form_data_provider.ts +++ b/src/plugins/es_ui_shared/static/forms/hook_form_lib/components/form_data_provider.ts @@ -28,9 +28,9 @@ interface Props { } export const FormDataProvider = React.memo(({ children, pathsToWatch }: Props) => { - const [formData, setFormData] = useState({}); - const previousRawData = useRef({}); const form = useFormContext(); + const previousRawData = useRef(form.__formData$.current.value); + const [formData, setFormData] = useState(previousRawData.current); useEffect(() => { const subscription = form.subscribe(({ data: { raw } }) => { @@ -41,6 +41,7 @@ export const FormDataProvider = React.memo(({ children, pathsToWatch }: Props) = const valuesToWatchArray = Array.isArray(pathsToWatch) ? (pathsToWatch as string[]) : ([pathsToWatch] as string[]); + if (valuesToWatchArray.some(value => previousRawData.current[value] !== raw[value])) { previousRawData.current = raw; setFormData(raw); 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 7e3e1fba9c44a6..397a78354f4707 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 c8b1322b6100e9..e565432e07fe1e 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 @@ -5,7 +5,7 @@ */ import { TestBed, SetupFunc, UnwrapPromise } from '../../../../../test_utils'; -import { Template } from '../../../common/types'; +import { TemplateDeserialized } from '../../../common'; import { nextTick } from './index'; interface MappingField { @@ -63,8 +63,8 @@ export const formSetup = async (initTestBed: SetupFunc) => { indexPatterns, order, version, - }: Partial