Skip to content

Commit

Permalink
[Index management] Update TemplateDeserialized interface (#78913)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
sebelga and kibanamachine authored Oct 5, 2020
1 parent f960e89 commit a8b1b9e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('Index Templates tab', () => {
const indexTemplate = templates[i];
const { name, indexPatterns, ilmPolicy, composedOf, template } = indexTemplate;

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

Expand All @@ -152,7 +152,7 @@ describe('Index Templates tab', () => {
const legacyIndexTemplate = legacyTemplates[i];
const { name, indexPatterns, ilmPolicy, template } = legacyIndexTemplate;

const hasContent = !!template.settings || !!template.mappings || !!template.aliases;
const hasContent = !!template?.settings || !!template?.mappings || !!template?.aliases;
const ilmPolicyName = ilmPolicy && ilmPolicy.name ? ilmPolicy.name : '';

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('<TemplateClone />', () => {
name: `${templateToClone.name}-copy`,
indexPatterns: DEFAULT_INDEX_PATTERNS,
};
// @ts-expect-error

delete expected.template; // As no settings, mappings or aliases have been defined, no "template" param is sent

expect(JSON.parse(JSON.parse(latestRequest.requestBody).body)).toEqual(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function deserializeTemplateList(
): TemplateListItem[] {
return indexTemplates.map(({ name, index_template: templateSerialized }) => {
const {
template: { mappings, settings, aliases },
template: { mappings, settings, aliases } = {},
...deserializedTemplate
} = deserializeTemplate({ name, ...templateSerialized }, cloudManagedTemplatePrefix);

Expand Down Expand Up @@ -149,7 +149,7 @@ export function deserializeLegacyTemplateList(
): TemplateListItem[] {
return Object.entries(indexTemplatesByName).map(([name, templateSerialized]) => {
const {
template: { mappings, settings, aliases },
template: { mappings, settings, aliases } = {},
...deserializedTemplate
} = deserializeLegacyTemplate({ name, ...templateSerialized }, cloudManagedTemplatePrefix);

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 @@ -13,7 +13,7 @@ import { Mappings } from './mappings';
*/
export interface TemplateSerialized {
index_patterns: string[];
template: {
template?: {
settings?: IndexSettings;
aliases?: Aliases;
mappings?: Mappings;
Expand All @@ -33,7 +33,7 @@ export interface TemplateSerialized {
export interface TemplateDeserialized {
name: string;
indexPatterns: string[];
template: {
template?: {
settings?: IndexSettings;
aliases?: Aliases;
mappings?: Mappings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const TemplateForm = ({
};

const {
template: { settings, mappings, aliases },
template: { settings, mappings, aliases } = {},
composedOf,
_kbnMeta,
...logistics
Expand Down Expand Up @@ -170,18 +170,19 @@ export const TemplateForm = ({
const cleanupTemplateObject = (template: TemplateDeserialized) => {
const outputTemplate = { ...template };

if (outputTemplate.template.settings === undefined) {
delete outputTemplate.template.settings;
}
if (outputTemplate.template.mappings === undefined) {
delete outputTemplate.template.mappings;
}
if (outputTemplate.template.aliases === undefined) {
delete outputTemplate.template.aliases;
}
if (Object.keys(outputTemplate.template).length === 0) {
// @ts-expect-error
delete outputTemplate.template;
if (outputTemplate.template) {
if (outputTemplate.template.settings === undefined) {
delete outputTemplate.template.settings;
}
if (outputTemplate.template.mappings === undefined) {
delete outputTemplate.template.mappings;
}
if (outputTemplate.template.aliases === undefined) {
delete outputTemplate.template.aliases;
}
if (Object.keys(outputTemplate.template).length === 0) {
delete outputTemplate.template;
}
}

return outputTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ export const TemplateDetailsContent = ({
}

if (templateDetails) {
const {
template: { settings, mappings, aliases },
} = templateDetails;
const { template: { settings, mappings, aliases } = {} } = templateDetails;

const tabToComponentMap: Record<string, React.ReactNode> = {
[SUMMARY_TAB_ID]: <TabSummary templateDetails={templateDetails} />,
Expand Down

0 comments on commit a8b1b9e

Please sign in to comment.