Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Composable template] Demo and PR review fixes #71065

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,9 @@ export const FormRow = ({
titleWrapped = title;
}

if (!children && !field) {
throw new Error('You need to provide either children or a field to the FormRow');
}

return (
<EuiDescribedFormGroup title={titleWrapped} description={description} fullWidth>
{children ? children : <Field field={field!} {...rest} />}
{children ? children : field ? <Field field={field!} {...rest} /> : null}
</EuiDescribedFormGroup>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ const createActions = (testBed: TestBed<TestSubjects>) => {
find('closeDetailsButton').simulate('click');
};

const toggleViewItem = (view: 'composable' | 'system') => {
const toggleViewItem = (view: 'managed' | 'cloudManaged' | 'system') => {
const { find, component } = testBed;
const views = ['composable', 'system'];
const views = ['managed', 'cloudManaged', 'system'];

// First open the pop over
act(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ describe('Index Templates tab', () => {
},
},
});
(template1 as any).hasSettings = true;

const template2 = fixtures.getTemplate({
name: `b${getRandomString()}`,
Expand All @@ -73,6 +72,7 @@ describe('Index Templates tab', () => {
const template3 = fixtures.getTemplate({
name: `.c${getRandomString()}`, // mock system template
indexPatterns: ['template3Pattern1*', 'template3Pattern2', 'template3Pattern3'],
type: 'system',
});

const template4 = fixtures.getTemplate({
Expand Down Expand Up @@ -101,6 +101,7 @@ describe('Index Templates tab', () => {
name: `.c${getRandomString()}`, // mock system template
indexPatterns: ['template6Pattern1*', 'template6Pattern2', 'template6Pattern3'],
isLegacy: true,
type: 'system',
});

const templates = [template1, template2, template3];
Expand All @@ -124,44 +125,49 @@ describe('Index Templates tab', () => {
// Test composable table content
tableCellsValues.forEach((row, i) => {
const indexTemplate = templates[i];
const { name, indexPatterns, priority, ilmPolicy, composedOf, template } = indexTemplate;
const { name, indexPatterns, ilmPolicy, composedOf, template } = indexTemplate;

const hasContent = !!template.settings || !!template.mappings || !!template.aliases;
const ilmPolicyName = ilmPolicy && ilmPolicy.name ? ilmPolicy.name : '';
const composedOfString = composedOf ? composedOf.join(',') : '';
const priorityFormatted = priority ? priority.toString() : '';

expect(removeWhiteSpaceOnArrayValues(row)).toEqual([
'', // Checkbox to select row
name,
indexPatterns.join(', '),
ilmPolicyName,
composedOfString,
priorityFormatted,
hasContent ? 'M S A' : 'None', // M S A -> Mappings Settings Aliases badges
'', // Column of actions
]);

try {
expect(removeWhiteSpaceOnArrayValues(row)).toEqual([
'', // Checkbox to select row
name,
indexPatterns.join(', '),
ilmPolicyName,
composedOfString,
hasContent ? 'M S A' : 'None', // M S A -> Mappings Settings Aliases badges
'', // Column of actions
]);
} catch (e) {
console.error(`Error in index template at row ${i}`); // eslint-disable-line no-console
throw e;
}
});

// Test legacy table content
legacyTableCellsValues.forEach((row, i) => {
const template = legacyTemplates[i];
const { name, indexPatterns, order, ilmPolicy } = template;
const legacyIndexTemplate = legacyTemplates[i];
const { name, indexPatterns, ilmPolicy, template } = legacyIndexTemplate;

const hasContent = !!template.settings || !!template.mappings || !!template.aliases;
const ilmPolicyName = ilmPolicy && ilmPolicy.name ? ilmPolicy.name : '';
const orderFormatted = order ? order.toString() : order;

expect(removeWhiteSpaceOnArrayValues(row)).toEqual([
'',
name,
indexPatterns.join(', '),
ilmPolicyName,
orderFormatted,
'',
'',
'',
'',
]);

try {
expect(removeWhiteSpaceOnArrayValues(row)).toEqual([
'',
name,
indexPatterns.join(', '),
ilmPolicyName,
hasContent ? 'M S A' : 'None', // M S A -> Mappings Settings Aliases badges
'', // Column of actions
]);
} catch (e) {
console.error(`Error in legacy template at row ${i}`); // eslint-disable-line no-console
throw e;
}
});
});

Expand Down Expand Up @@ -211,7 +217,7 @@ describe('Index Templates tab', () => {
await actions.clickTemplateAt(0);
expect(exists('templateList')).toBe(true);
expect(exists('templateDetails')).toBe(true);
expect(find('templateDetails.title').text()).toBe(templates[0].name);
expect(find('templateDetails.title').text().trim()).toBe(templates[0].name);

// Close flyout
await act(async () => {
Expand All @@ -223,7 +229,7 @@ describe('Index Templates tab', () => {

expect(exists('templateList')).toBe(true);
expect(exists('templateDetails')).toBe(true);
expect(find('templateDetails.title').text()).toBe(legacyTemplates[0].name);
expect(find('templateDetails.title').text().trim()).toBe(legacyTemplates[0].name);
});

describe('table row actions', () => {
Expand Down Expand Up @@ -460,7 +466,7 @@ describe('Index Templates tab', () => {
const { find } = testBed;
const [{ name }] = templates;

expect(find('templateDetails.title').text()).toEqual(name);
expect(find('templateDetails.title').text().trim()).toEqual(name);
});

it('should have a close button and be able to close flyout', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ describe.skip('<TemplateCreate />', () => {
aliases: ALIASES,
},
_kbnMeta: {
type: 'default',
isLegacy: false,
isManaged: false,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe.skip('<TemplateEdit />', () => {
aliases: ALIASES,
},
_kbnMeta: {
isManaged: false,
type: 'default',
isLegacy: templateToEdit._kbnMeta.isLegacy,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@ import {
LegacyTemplateSerialized,
TemplateSerialized,
TemplateListItem,
TemplateType,
} from '../types';

const hasEntries = (data: object = {}) => Object.entries(data).length > 0;

export function serializeTemplate(templateDeserialized: TemplateDeserialized): TemplateSerialized {
const { version, priority, indexPatterns, template, composedOf, _meta } = templateDeserialized;
const {
version,
priority,
indexPatterns,
template,
composedOf,
dataStream,
_meta,
} = templateDeserialized;

return {
version,
priority,
template,
index_patterns: indexPatterns,
data_stream: dataStream,
composed_of: composedOf,
_meta,
};
Expand All @@ -41,6 +51,15 @@ export function deserializeTemplate(
} = templateEs;
const { settings } = template;

let type: TemplateType = 'default';
if (Boolean(cloudManagedTemplatePrefix && name.startsWith(cloudManagedTemplatePrefix))) {
type = 'cloudManaged';
} else if (name.startsWith('.')) {
type = 'system';
} else if (Boolean(_meta?.managed === true)) {
type = 'managed';
}

const deserializedTemplate: TemplateDeserialized = {
name,
version,
Expand All @@ -52,10 +71,7 @@ export function deserializeTemplate(
dataStream,
_meta,
_kbnMeta: {
isManaged: Boolean(_meta?.managed === true),
isCloudManaged: Boolean(
cloudManagedTemplatePrefix && name.startsWith(cloudManagedTemplatePrefix)
),
type,
hasDatastream: Boolean(dataStream),
},
};
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/index_management/common/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export const getTemplateParameter = (
) => {
return isLegacyTemplate(template)
? (template as LegacyTemplateSerialized)[setting]
: (template as TemplateSerialized).template[setting];
: (template as TemplateSerialized).template?.[setting];
};
18 changes: 9 additions & 9 deletions x-pack/plugins/index_management/common/types/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,24 @@ export interface TemplateDeserialized {
aliases?: Aliases;
mappings?: Mappings;
};
composedOf?: string[]; // Used on composable index template
composedOf?: string[]; // Composable template only
version?: number;
priority?: number;
order?: number; // Used on legacy index template
priority?: number; // Composable template only
order?: number; // Legacy template only
ilmPolicy?: {
name: string;
};
_meta?: { [key: string]: any };
dataStream?: { timestamp_field: string };
_meta?: { [key: string]: any }; // Composable template only
dataStream?: { timestamp_field: string }; // Composable template only
_kbnMeta: {
isManaged: boolean;
isCloudManaged: boolean;
type: TemplateType;
hasDatastream: boolean;
isLegacy?: boolean;
};
}

export type TemplateType = 'default' | 'managed' | 'cloudManaged' | 'system';

export interface TemplateFromEs {
name: string;
index_template: TemplateSerialized;
Expand All @@ -78,8 +79,7 @@ export interface TemplateListItem {
name: string;
};
_kbnMeta: {
isManaged: boolean;
isCloudManaged: boolean;
type: TemplateType;
hasDatastream: boolean;
isLegacy?: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const i18nTexts = {
searchBoxPlaceholder: i18n.translate(
'xpack.idxMgmt.componentTemplatesSelector.searchBox.placeholder',
{
defaultMessage: 'Search components',
defaultMessage: 'Search component templates',
}
),
};
Expand Down Expand Up @@ -78,24 +78,33 @@ export const ComponentTemplates = ({ isLoading, components, listItemProps }: Pro
return [];
}

return components.filter((component) => {
if (filters.settings.checked === 'on' && !component.hasSettings) {
return false;
}
if (filters.mappings.checked === 'on' && !component.hasMappings) {
return false;
}
if (filters.aliases.checked === 'on' && !component.hasAliases) {
return false;
}

if (searchValue.trim() === '') {
return true;
}

const match = fuzzyMatch(searchValue, component.name);
return match;
});
return components
.filter((component) => {
if (filters.settings.checked === 'on' && !component.hasSettings) {
return false;
}
if (filters.mappings.checked === 'on' && !component.hasMappings) {
return false;
}
if (filters.aliases.checked === 'on' && !component.hasAliases) {
return false;
}

if (searchValue.trim() === '') {
return true;
}

const match = fuzzyMatch(searchValue, component.name);
return match;
})
.sort((a, b) => {
if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
return 1;
}
return 0;
});
}, [isLoading, components, searchValue, filters]);

const isSearchResultEmpty = filteredComponents.length === 0 && components.length > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const ComponentTemplatesSelector = ({
<SectionLoading>
<FormattedMessage
id="xpack.idxMgmt.componentTemplatesSelector.loadingComponentsDescription"
defaultMessage="Loading components…"
defaultMessage="Loading component templates…"
/>
</SectionLoading>
);
Expand Down Expand Up @@ -163,7 +163,7 @@ export const ComponentTemplatesSelector = ({
<div>
<FormattedMessage
id="xpack.idxMgmt.componentTemplatesSelector.noComponentSelectedLabel"
defaultMessage="No component selected."
defaultMessage="No component template selected."
/>
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export const StepLogistics: React.FunctionComponent<Props> = React.memo(
</>
}
>
{isMetaVisible ? (
{isMetaVisible && (
<UseField
path="_meta"
component={JsonEditorField}
Expand All @@ -211,16 +211,12 @@ export const StepLogistics: React.FunctionComponent<Props> = React.memo(
'aria-label': i18n.translate(
'xpack.idxMgmt.componentTemplateForm.stepLogistics.metaAriaLabel',
{
defaultMessage: 'Metadata JSON editor',
defaultMessage: '_meta field data editor',
}
),
},
}}
/>
) : (
// <FormRow/> requires children or a field
// For now, we return an empty <div> if the editor is not visible
<div />
)}
</FormRow>
</Form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const logisticsFormSchema: FormSchema = {
},
_meta: {
label: i18n.translate('xpack.idxMgmt.componentTemplateForm.stepLogistics.metaFieldLabel', {
defaultMessage: 'Metadata (optional)',
defaultMessage: '_meta field data (optional)',
}),
helpText: (
<FormattedMessage
Expand Down
Loading