Skip to content

Commit

Permalink
Fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaStoeva committed Nov 13, 2024
1 parent efa8406 commit 364b5b0
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,6 @@ describe('<TemplateCreate />', () => {
testBed = await setup(httpSetup);
});
testBed.component.update();

const { actions } = testBed;
});

it('setting index pattern to logs-*-* should set the index mode to logsdb', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ export type TestSubjects =
| 'priorityField.input'
| 'dataStreamField.input'
| 'indexModeField'
| 'indexModeCallout'
| 'dataRetentionToggle.input'
| 'allowAutoCreateField.input'
| 'pageTitle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import { deserializeTemplate, serializeTemplate } from './template_serialization';
import { TemplateDeserialized, TemplateSerialized } from '../types';
import { TemplateDeserialized, TemplateSerialized, IndexMode } from '../types';
import { STANDARD_INDEX_MODE, LOGSDB_INDEX_MODE, TIME_SERIES_MODE } from '../constants';

const defaultSerializedTemplate: TemplateSerialized = {
template: {},
Expand All @@ -17,6 +18,7 @@ const defaultSerializedTemplate: TemplateSerialized = {
const defaultDeserializedTemplate: TemplateDeserialized = {
name: 'my_template',
indexPatterns: ['test'],
indexMode: STANDARD_INDEX_MODE,
_kbnMeta: {
type: 'default',
hasDatastream: true,
Expand All @@ -26,12 +28,13 @@ const defaultDeserializedTemplate: TemplateDeserialized = {

const allowAutoCreateRadioOptions = ['NO_OVERWRITE', 'TRUE', 'FALSE'];
const allowAutoCreateSerializedValues = [undefined, true, false];
const indexModeValues = [STANDARD_INDEX_MODE, LOGSDB_INDEX_MODE, TIME_SERIES_MODE, undefined];

describe('Template serialization', () => {
describe('serialization of allow_auto_create parameter', () => {
describe('deserializeTemplate()', () => {
allowAutoCreateSerializedValues.forEach((value, index) => {
test(`correctly deserializes ${value} value`, () => {
test(`correctly deserializes ${value} allow_auto_create value`, () => {
expect(
deserializeTemplate({
...defaultSerializedTemplate,
Expand All @@ -41,11 +44,29 @@ describe('Template serialization', () => {
).toHaveProperty('allowAutoCreate', allowAutoCreateRadioOptions[index]);
});
});

indexModeValues.forEach((value) => {
test(`correctly deserializes ${value} index mode settings value`, () => {
expect(
deserializeTemplate({
...defaultSerializedTemplate,
name: 'my_template',
template: {
settings: {
index: {
mode: value,
},
},
},
})
).toHaveProperty('indexMode', value ?? STANDARD_INDEX_MODE);
});
});
});

describe('serializeTemplate()', () => {
allowAutoCreateRadioOptions.forEach((option, index) => {
test(`correctly serializes ${option} radio option`, () => {
test(`correctly serializes ${option} allowAutoCreate radio option`, () => {
expect(
serializeTemplate({
...defaultDeserializedTemplate,
Expand All @@ -54,6 +75,18 @@ describe('Template serialization', () => {
).toHaveProperty('allow_auto_create', allowAutoCreateSerializedValues[index]);
});
});

// Only use the first three values (omit undefined)
indexModeValues.slice(0, 3).forEach((value) => {
test(`correctly serializes ${value} indexMode option`, () => {
expect(
serializeTemplate({
...defaultDeserializedTemplate,
indexMode: value as IndexMode,
})
).toHaveProperty('template.settings.index.mode', value);
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TemplateSerialized,
TemplateListItem,
TemplateType,
IndexMode,
} from '../types';
import { deserializeESLifecycle } from './data_stream_utils';
import {
Expand Down Expand Up @@ -90,11 +91,10 @@ export function deserializeTemplate(

const ilmPolicyName = settings?.index?.lifecycle?.name;

const indexMode =
settings?.index?.mode ??
const indexMode = (settings?.index?.mode ??
(indexPatterns.some((pattern) => pattern === 'logs-*-*')
? LOGSDB_INDEX_MODE
: STANDARD_INDEX_MODE);
: STANDARD_INDEX_MODE)) as IndexMode;

const deserializedTemplate: TemplateDeserialized = {
name,
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/index_management/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type {
DataStream,
DataStreamIndex,
DataRetention,
IndexMode,
} from './data_streams';

export * from './component_templates';
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 @@ -5,7 +5,7 @@
* 2.0.
*/

import { DataRetention, DataStream } from './data_streams';
import { DataRetention, DataStream, IndexMode } from './data_streams';
import { IndexSettings } from './indices';
import { Aliases } from './aliases';
import { Mappings } from './mappings';
Expand Down Expand Up @@ -51,7 +51,7 @@ export interface TemplateDeserialized {
priority?: number; // Composable template only
allowAutoCreate: string;
order?: number; // Legacy template only
indexMode: string;
indexMode: IndexMode;
ilmPolicy?: {
name: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,7 @@ export const ComponentTemplateForm = ({
</FormWizardStep>

<FormWizardStep id={wizardSections.settings.id} label={wizardSections.settings.label}>
<StepSettingsContainer
esDocsBase={documentation.esDocsBase}
getTemplateData={buildComponentTemplateObject(defaultValue)}
/>
<StepSettingsContainer esDocsBase={documentation.esDocsBase} />
</FormWizardStep>

<FormWizardStep id={wizardSections.mappings.id} label={wizardSections.mappings.label}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Forms } from '../../../../../shared_imports';
import { useJsonStep } from './use_json_step';
import { documentationService } from '../../../mappings_editor/shared_imports';
import { indexModeLabels } from '../../../../lib/index_mode_labels';
import { IndexMode } from '../../../../../../common/types/data_streams';
import { IndexMode } from '../../../../../../common/types';

interface Props {
onChange: (content: Forms.Content) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import React from 'react';

import { Forms } from '../../../../../shared_imports';
import { TemplateDeserialized } from '../../../../../../common';
import { WizardContent } from '../../../template_form/template_form';
import { CommonWizardSteps } from './types';
import { StepSettings } from './step_settings';

interface Props {
esDocsBase: string;
getTemplateData: (wizardContent: WizardContent) => TemplateDeserialized;
getTemplateData?: (wizardContent: WizardContent) => TemplateDeserialized;
}

export const StepSettingsContainer = React.memo(({ esDocsBase, getTemplateData }: Props) => {
Expand All @@ -22,16 +24,20 @@ export const StepSettingsContainer = React.memo(({ esDocsBase, getTemplateData }
);
const { getData } = Forms.useMultiContentContext<WizardContent>();

const wizardContent = getData();
// Build the current template object, providing the wizard content data
const template = getTemplateData(wizardContent);
let indexMode;
if (getTemplateData) {
const wizardContent = getData();
// Build the current template object, providing the wizard content data
const template = getTemplateData(wizardContent);
indexMode = template.indexMode;
}

return (
<StepSettings
defaultValue={defaultValue}
onChange={updateContent}
esDocsBase={esDocsBase}
indexMode={template?.indexMode}
indexMode={indexMode}
/>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FormattedMessage } from '@kbn/i18n-react';
import { EuiSpacer, EuiButton, EuiPageHeader } from '@elastic/eui';
import { ScopedHistory } from '@kbn/core/public';

import { allowAutoCreateRadioIds } from '../../../../common/constants';
import { allowAutoCreateRadioIds, STANDARD_INDEX_MODE } from '../../../../common/constants';
import { TemplateDeserialized } from '../../../../common';
import { serializers, Forms, GlobalFlyout } from '../../../shared_imports';
import {
Expand Down Expand Up @@ -118,6 +118,7 @@ export const TemplateForm = ({
name: '',
indexPatterns: [],
dataStream: {},
indexMode: STANDARD_INDEX_MODE,
template: {},
_kbnMeta: {
type: 'default',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ import { getLifecycleValue } from '../../../../../lib/data_streams';
import { TemplateDeserialized } from '../../../../../../../common';
import { ILM_PAGES_POLICY_EDIT } from '../../../../../constants';
import { useIlmLocator } from '../../../../../services/use_ilm_locator';
import {
allowAutoCreateRadioIds,
STANDARD_INDEX_MODE,
} from '../../../../../../../common/constants';
import { allowAutoCreateRadioIds } from '../../../../../../../common/constants';
import { indexModeLabels } from '../../../../../lib/index_mode_labels';

interface Props {
Expand Down Expand Up @@ -62,7 +59,6 @@ export const TabSummary: React.FunctionComponent<Props> = ({ templateDetails })
_meta,
_kbnMeta: { isLegacy, hasDatastream },
allowAutoCreate,
template,
} = templateDetails;

const numIndexPatterns = indexPatterns.length;
Expand Down Expand Up @@ -235,7 +231,7 @@ export const TabSummary: React.FunctionComponent<Props> = ({ templateDetails })
/>
</EuiDescriptionListTitle>
<EuiDescriptionListDescription>
{indexModeLabels[template?.settings?.index?.mode ?? STANDARD_INDEX_MODE]}
{indexModeLabels[indexMode]}
</EuiDescriptionListDescription>

{/* Allow auto create */}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/index_management/test/fixtures/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const getTemplate = ({
order = getRandomNumber(),
indexPatterns = [],
template: { settings, aliases, mappings } = {},
indexMode,
indexMode = 'standard',
dataStream,
composedOf,
ignoreMissingComponentTemplates,
Expand Down

0 comments on commit 364b5b0

Please sign in to comment.