From 5ddb122d33b6be8dc0fc0512c3468746c76908c0 Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Tue, 9 Jun 2020 10:48:52 -0400 Subject: [PATCH] address review feedback --- .../lib/component_template_serialization.ts | 59 +++++++++++++------ .../common/types/component_templates.ts | 2 +- .../common/types/templates.ts | 4 +- .../component_template_list/empty_prompt.tsx | 2 +- .../component_template_list/table.tsx | 43 ++++---------- .../component_templates/lib/documentation.ts | 6 +- .../server/client/elasticsearch.ts | 4 +- .../routes/api/component_templates/get.ts | 4 +- 8 files changed, 62 insertions(+), 62 deletions(-) diff --git a/x-pack/plugins/index_management/common/lib/component_template_serialization.ts b/x-pack/plugins/index_management/common/lib/component_template_serialization.ts index ba1e3a9780fe6..0db81bf81d300 100644 --- a/x-pack/plugins/index_management/common/lib/component_template_serialization.ts +++ b/x-pack/plugins/index_management/common/lib/component_template_serialization.ts @@ -4,39 +4,61 @@ * you may not use this file except in compliance with the Elastic License. */ import { - TemplateV2Es, - ComponentTemplateEs, + TemplateFromEs, + ComponentTemplateFromEs, ComponentTemplateDeserialized, ComponentTemplateListItem, } from '../types'; const hasEntries = (data: object = {}) => Object.entries(data).length > 0; -const getAssociatedIndexTemplates = ( - indexTemplates: TemplateV2Es[], - componentTemplateName: string -) => { - return indexTemplates - .filter(({ index_template: indexTemplate }) => { - return indexTemplate.composed_of?.includes(componentTemplateName); - }) - .map(({ name }) => name); +/** + * Normalize a list of component templates to a map where each key + * is a component template name, and the value is an array of index templates name using it + * + * @example + * + { + "comp-1": [ + "template-1", + "template-2" + ], + "comp2": [ + "template-1", + "template-2" + ] + } + * + * @param indexTemplatesEs List of component templates + */ + +const getIndexTemplatesToUsedBy = (indexTemplatesEs: TemplateFromEs[]) => { + return indexTemplatesEs.reduce((acc, item) => { + if (item.index_template.composed_of) { + item.index_template.composed_of.forEach((component) => { + acc[component] = acc[component] ? [...acc[component], item.name] : [item.name]; + }); + } + return acc; + }, {} as { [key: string]: string[] }); }; export function deserializeComponentTemplate( - componentTemplateEs: ComponentTemplateEs, - indexTemplatesEs: TemplateV2Es[] + componentTemplateEs: ComponentTemplateFromEs, + indexTemplatesEs: TemplateFromEs[] ) { const { name, component_template: componentTemplate } = componentTemplateEs; const { template, _meta, version } = componentTemplate; + const indexTemplatesToUsedBy = getIndexTemplatesToUsedBy(indexTemplatesEs); + const deserializedComponentTemplate: ComponentTemplateDeserialized = { name, template, version, _meta, _kbnMeta: { - usedBy: getAssociatedIndexTemplates(indexTemplatesEs, name), + usedBy: indexTemplatesToUsedBy[name] || [], }, }; @@ -44,16 +66,17 @@ export function deserializeComponentTemplate( } export function deserializeComponenTemplateList( - componentTemplateEs: ComponentTemplateEs, - indexTemplatesEs: TemplateV2Es[] + componentTemplateEs: ComponentTemplateFromEs, + indexTemplatesEs: TemplateFromEs[] ) { const { name, component_template: componentTemplate } = componentTemplateEs; const { template } = componentTemplate; - const associatedTemplates = getAssociatedIndexTemplates(indexTemplatesEs, name); + + const indexTemplatesToUsedBy = getIndexTemplatesToUsedBy(indexTemplatesEs); const componentTemplateListItem: ComponentTemplateListItem = { name, - usedBy: associatedTemplates, + usedBy: indexTemplatesToUsedBy[name] || [], hasSettings: hasEntries(template.settings), hasMappings: hasEntries(template.mappings), hasAliases: hasEntries(template.aliases), diff --git a/x-pack/plugins/index_management/common/types/component_templates.ts b/x-pack/plugins/index_management/common/types/component_templates.ts index 30bf328ac0c3d..bc7ebdc2753dd 100644 --- a/x-pack/plugins/index_management/common/types/component_templates.ts +++ b/x-pack/plugins/index_management/common/types/component_templates.ts @@ -25,7 +25,7 @@ export interface ComponentTemplateDeserialized extends ComponentTemplateSerializ }; } -export interface ComponentTemplateEs { +export interface ComponentTemplateFromEs { name: string; component_template: ComponentTemplateSerialized; } diff --git a/x-pack/plugins/index_management/common/types/templates.ts b/x-pack/plugins/index_management/common/types/templates.ts index ddbfce850d321..006a2d9dea8f2 100644 --- a/x-pack/plugins/index_management/common/types/templates.ts +++ b/x-pack/plugins/index_management/common/types/templates.ts @@ -49,9 +49,9 @@ export interface TemplateDeserialized { }; } -export interface TemplateV2Es { +export interface TemplateFromEs { name: string; - index_template: Omit; + index_template: TemplateSerialized; } /** diff --git a/x-pack/plugins/index_management/public/application/components/component_templates/component_template_list/empty_prompt.tsx b/x-pack/plugins/index_management/public/application/components/component_templates/component_template_list/empty_prompt.tsx index e8a4c759ebcf0..edd9f77cbf635 100644 --- a/x-pack/plugins/index_management/public/application/components/component_templates/component_template_list/empty_prompt.tsx +++ b/x-pack/plugins/index_management/public/application/components/component_templates/component_template_list/empty_prompt.tsx @@ -31,7 +31,7 @@ export const EmptyPrompt: FunctionComponent = () => { defaultMessage="For example, you might create a component template that defines index settings that can be reused across index templates." />
- + {i18n.translate('xpack.idxMgmt.home.componentTemplates.emptyPromptDocumentionLink', { defaultMessage: 'Learn more', })} diff --git a/x-pack/plugins/index_management/public/application/components/component_templates/component_template_list/table.tsx b/x-pack/plugins/index_management/public/application/components/component_templates/component_template_list/table.tsx index 0ce7cb8fc0e56..2d9557e64e6e7 100644 --- a/x-pack/plugins/index_management/public/application/components/component_templates/component_template_list/table.tsx +++ b/x-pack/plugins/index_management/public/application/components/component_templates/component_template_list/table.tsx @@ -10,11 +10,10 @@ import { EuiInMemoryTable, EuiButton, EuiInMemoryTableProps, - EuiTableFieldDataColumnType, + EuiTextColor, EuiIcon, } from '@elastic/eui'; -import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; import { ComponentTemplateListItem } from '../types'; export interface Props { @@ -39,24 +38,17 @@ export const ComponentTable: FunctionComponent = ({ onSelectionChange: setSelection, selectable: ({ usedBy }) => usedBy.length === 0, selectableMessage: (selectable) => - !selectable - ? i18n.translate('xpack.idxMgmt.componentTemplatesList.table.disabledSelectionLabel', { - defaultMessage: 'Component template is in use and cannot be deleted', - }) - : i18n.translate('xpack.idxMgmt.componentTemplatesList.table.selectionLabel', { + selectable + ? i18n.translate('xpack.idxMgmt.componentTemplatesList.table.selectionLabel', { defaultMessage: 'Select this component template', + }) + : i18n.translate('xpack.idxMgmt.componentTemplatesList.table.disabledSelectionLabel', { + defaultMessage: 'Component template is in use and cannot be deleted', }), }, rowProps: () => ({ 'data-test-subj': 'componentTemplateTableRow', }), - cellProps: (componentTemplate, column) => { - const { field } = column as EuiTableFieldDataColumnType; - - return { - 'data-test-subj': `componentTemplateTableRow-${field}`, - }; - }, search: { toolsLeft: selection.length > 0 ? ( @@ -132,34 +124,23 @@ export const ComponentTable: FunctionComponent = ({ { field: 'usedBy', name: i18n.translate('xpack.idxMgmt.componentTemplatesList.table.isInUseColumnTitle', { - defaultMessage: 'Used by', + defaultMessage: 'Index templates', }), sortable: true, render: (usedBy: string[]) => { if (usedBy.length) { - return i18n.translate( - 'xpack.idxMgmt.componentTemplatesList.table.inUseCellDescription', - { - defaultMessage: '{count} index {count, plural, one {template} other {templates}}', - values: { - count: usedBy.length, - }, - } - ); + return usedBy.length; } return ( - - - - - + + - - + + ); }, }, diff --git a/x-pack/plugins/index_management/public/application/components/component_templates/lib/documentation.ts b/x-pack/plugins/index_management/public/application/components/component_templates/lib/documentation.ts index 1b5b87f27d5ba..dc27dadf0b807 100644 --- a/x-pack/plugins/index_management/public/application/components/component_templates/lib/documentation.ts +++ b/x-pack/plugins/index_management/public/application/components/component_templates/lib/documentation.ts @@ -10,11 +10,7 @@ export const getDocumentation = ({ ELASTIC_WEBSITE_URL, DOC_LINK_VERSION }: DocL const docsBase = `${ELASTIC_WEBSITE_URL}guide/en`; const esDocsBase = `${docsBase}/elasticsearch/reference/${DOC_LINK_VERSION}`; - function getComponentTemplatesLink() { - return `${esDocsBase}/indices-component-template.html`; - } - return { - getComponentTemplatesLink, + componentTemplates: `${esDocsBase}/indices-component-template.html`, }; }; diff --git a/x-pack/plugins/index_management/server/client/elasticsearch.ts b/x-pack/plugins/index_management/server/client/elasticsearch.ts index 68692cd59b786..b51f7d924dba7 100644 --- a/x-pack/plugins/index_management/server/client/elasticsearch.ts +++ b/x-pack/plugins/index_management/server/client/elasticsearch.ts @@ -10,7 +10,7 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any) Client.prototype.dataManagement = components.clientAction.namespaceFactory(); const dataManagement = Client.prototype.dataManagement.prototype; - // Component template routes + // Component templates dataManagement.getComponentTemplates = ca({ urls: [ { @@ -62,7 +62,7 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any) method: 'DELETE', }); - // Index templates v2 + // Composable index templates dataManagement.getComposableIndexTemplates = ca({ urls: [ { diff --git a/x-pack/plugins/index_management/server/routes/api/component_templates/get.ts b/x-pack/plugins/index_management/server/routes/api/component_templates/get.ts index 951d729ccefd5..f6f8e7d63d370 100644 --- a/x-pack/plugins/index_management/server/routes/api/component_templates/get.ts +++ b/x-pack/plugins/index_management/server/routes/api/component_templates/get.ts @@ -9,7 +9,7 @@ import { deserializeComponentTemplate, deserializeComponenTemplateList, } from '../../../../common/lib'; -import { ComponentTemplateEs } from '../../../../common'; +import { ComponentTemplateFromEs } from '../../../../common'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; @@ -27,7 +27,7 @@ export function registerGetAllRoute({ router, license, lib: { isEsError } }: Rou try { const { component_templates: componentTemplates, - }: { component_templates: ComponentTemplateEs[] } = await callAsCurrentUser( + }: { component_templates: ComponentTemplateFromEs[] } = await callAsCurrentUser( 'dataManagement.getComponentTemplates' );