Skip to content

Commit

Permalink
address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Jun 9, 2020
1 parent 906789e commit 5ddb122
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,79 @@
* 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] || [],
},
};

return deserializedComponentTemplate;
}

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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface ComponentTemplateDeserialized extends ComponentTemplateSerializ
};
}

export interface ComponentTemplateEs {
export interface ComponentTemplateFromEs {
name: string;
component_template: ComponentTemplateSerialized;
}
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/index_management/common/types/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export interface TemplateDeserialized {
};
}

export interface TemplateV2Es {
export interface TemplateFromEs {
name: string;
index_template: Omit<TemplateSerialized, 'name'>;
index_template: TemplateSerialized;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
/>
<br />
<EuiLink href={documentation.getComponentTemplatesLink()} target="_blank" external>
<EuiLink href={documentation.componentTemplates} target="_blank" external>
{i18n.translate('xpack.idxMgmt.home.componentTemplates.emptyPromptDocumentionLink', {
defaultMessage: 'Learn more',
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -39,24 +38,17 @@ export const ComponentTable: FunctionComponent<Props> = ({
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<ComponentTemplateListItem>;

return {
'data-test-subj': `componentTemplateTableRow-${field}`,
};
},
search: {
toolsLeft:
selection.length > 0 ? (
Expand Down Expand Up @@ -132,34 +124,23 @@ export const ComponentTable: FunctionComponent<Props> = ({
{
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 (
<EuiFlexGroup gutterSize="xs" alignItems="center">
<EuiFlexItem grow={false}>
<EuiIcon type="cross" color="danger" />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiTextColor color="subdued">
<i>
<FormattedMessage
id="xpack.idxMgmt.componentTemplatesList.table.notInUseCellDescription"
defaultMessage="Not in use"
/>
</EuiFlexItem>
</EuiFlexGroup>
</i>
</EuiTextColor>
);
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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'
);

Expand Down

0 comments on commit 5ddb122

Please sign in to comment.