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

[ILM] Fix rollover config validation #87423

Closed
Show file tree
Hide file tree
Changes from all 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 @@ -111,6 +111,13 @@ export const setup = async (arg?: { appServicesContext: Partial<AppServicesConte
component.update();
};

const toggleRequestFlyout = async () => {
await act(async () => {
find('requestButton').simulate('click');
});
component.update();
};

const toggleDefaultRollover = createFormToggleAction('useDefaultRolloverSwitch');

const toggleRollover = createFormToggleAction('rolloverSwitch');
Expand Down Expand Up @@ -236,6 +243,7 @@ export const setup = async (arg?: { appServicesContext: Partial<AppServicesConte
actions: {
setWaitForSnapshotPolicy,
savePolicy,
toggleRequestFlyout,
hot: {
setMaxSize,
setMaxDocs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,63 @@ describe('<EditPolicy />', () => {
expect(actions.hot.searchableSnapshotsExists()).toBeTruthy();
});
});

describe('validation', () => {
test('should validate rollover configuration', async () => {
// Load a policy with no existing rollover configuration
httpRequestsMockHelpers.setLoadPolicies([
{
version: 1,
modified_date: Date.now().toString(),
policy: {
name: 'my_policy',
phases: {
hot: {
min_age: '0ms',
actions: {},
},
},
},
name: 'my_policy',
},
]);
httpRequestsMockHelpers.setListNodes({
nodesByRoles: {},
nodesByAttributes: { test: ['123'] },
isUsingDeprecatedDataRoleConfig: false,
});
httpRequestsMockHelpers.setLoadSnapshotPolicies([]);

await act(async () => {
testBed = await setup();
});

const { component, exists } = testBed;
component.update();

const { actions } = testBed;

// Enable rollover
await actions.hot.toggleRollover();
// Open the request flyout to trigger form validation without submitting the form
await actions.toggleRequestFlyout();

// No rollover configuration provided, so validation should fail and error messages should render
expect(exists('rolloverSettingsRequired')).toBe(true);
expect(exists('invalidPolicyJsonCallout')).toBe(true);

// Close request flyout
await actions.toggleRequestFlyout();
// Enable default configuration
await actions.hot.toggleDefaultRollover();
// Open the request flyout to trigger form validation without submitting the form
await actions.toggleRequestFlyout();

// Validation should pass, so no error messages should render
expect(exists('rolloverSettingsRequired')).toBe(false);
expect(exists('invalidPolicyJsonCallout')).toBe(false);
});
});
});

describe('warm phase', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { FunctionComponent, useState } from 'react';
import React, { FunctionComponent, useState, useEffect } from 'react';
import { get } from 'lodash';
import { FormattedMessage } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';
Expand All @@ -22,7 +22,13 @@ import {

import { Phases } from '../../../../../../../common/types';

import { useFormData, UseField, SelectField, NumericField } from '../../../../../../shared_imports';
import {
useFormData,
UseField,
SelectField,
NumericField,
useFormContext,
} from '../../../../../../shared_imports';

import { i18nTexts } from '../../../i18n_texts';

Expand All @@ -48,13 +54,23 @@ const hotProperty: keyof Phases = 'hot';

export const HotPhase: FunctionComponent = () => {
const { license } = useEditPolicyContext();
const { getFields } = useFormContext();
const [formData] = useFormData({
watch: isUsingDefaultRolloverPath,
});
const { isUsingRollover } = useConfigurationIssues();
const isUsingDefaultRollover = get(formData, isUsingDefaultRolloverPath);
const [showEmptyRolloverFieldsError, setShowEmptyRolloverFieldsError] = useState(false);

useEffect(() => {
if (isUsingDefaultRollover === true) {
const fields = getFields();
fields[ROLLOVER_FORM_PATHS.maxAge].reset({ resetValue: false });
fields[ROLLOVER_FORM_PATHS.maxDocs].reset({ resetValue: false });
fields[ROLLOVER_FORM_PATHS.maxSize].reset({ resetValue: false });
}
}, [getFields, isUsingDefaultRollover]);

return (
<>
<EuiDescribedFormGroup
Expand Down Expand Up @@ -165,7 +181,7 @@ export const HotPhase: FunctionComponent = () => {
content={
<FormattedMessage
id="xpack.indexLifecycleMgmt.editPolicy.hotPhase.enableRolloverTipContent"
defaultMessage="Roll over to a new index when the
defaultMessage="Roll over to a new index when the
current index meets one of the defined conditions."
/>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const PolicyJsonFlyout: React.FunctionComponent<Props> = ({ policyName, c
'xpack.indexLifecycleMgmt.policyJsonFlyout.validationErrorCallout.title',
{ defaultMessage: 'Invalid policy' }
)}
data-test-subj="invalidPolicyJsonCallout"
>
{i18n.translate('xpack.indexLifecycleMgmt.policyJsonFlyout.validationErrorCallout.body', {
defaultMessage: 'To view the JSON for this policy address all validation errors.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { fieldValidators, ValidationFunc, ValidationConfig } from '../../../../shared_imports';

import { ROLLOVER_FORM_PATHS } from '../constants';
import { ROLLOVER_FORM_PATHS, isUsingDefaultRolloverPath, useRolloverPath } from '../constants';

import { i18nTexts } from '../i18n_texts';
import { PolicyFromES } from '../../../../../common/types';
Expand Down Expand Up @@ -59,6 +59,8 @@ export const ROLLOVER_EMPTY_VALIDATION = 'ROLLOVER_EMPTY_VALIDATION';
export const rolloverThresholdsValidator: ValidationFunc = ({ form, path }) => {
const fields = form.getFields();
if (
fields[useRolloverPath]?.value === true &&
!fields[isUsingDefaultRolloverPath]?.value &&
!(
fields[ROLLOVER_FORM_PATHS.maxAge]?.value ||
fields[ROLLOVER_FORM_PATHS.maxDocs]?.value ||
Expand Down