diff --git a/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/edit_policy/contants.ts b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/edit_policy/contants.ts new file mode 100644 index 00000000000000..a58aad6dc6bc25 --- /dev/null +++ b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/edit_policy/contants.ts @@ -0,0 +1,36 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export const POLICY_NAME = 'my_policy'; + +export const DELETE_PHASE_POLICY = { + version: 1, + modified_date: Date.now(), + policy: { + phases: { + hot: { + min_age: '0ms', + actions: { + rollover: { + max_size: '50gb', + }, + }, + }, + delete: { + min_age: '0ms', + actions: { + wait_for_snapshot: { + policy: 'my_snapshot_policy', + }, + delete: { + delete_searchable_snapshot: true, + }, + }, + }, + }, + }, + name: POLICY_NAME, +}; diff --git a/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/edit_policy/edit_policy.helpers.tsx b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/edit_policy/edit_policy.helpers.tsx new file mode 100644 index 00000000000000..a36cd7e35c36f6 --- /dev/null +++ b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/edit_policy/edit_policy.helpers.tsx @@ -0,0 +1,58 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { act } from 'react-dom/test-utils'; + +import { registerTestBed, TestBed, TestBedConfig } from '../../../../../test_utils'; + +import { POLICY_NAME } from './contants'; +import { TestSubjects } from '../helpers'; + +import { EditPolicy } from '../../../public/application/sections/edit_policy'; +import { indexLifecycleManagementStore } from '../../../public/application/store'; + +const testBedConfig: TestBedConfig = { + store: () => indexLifecycleManagementStore(), + memoryRouter: { + initialEntries: [`/policies/edit/${POLICY_NAME}`], + componentRoutePath: `/policies/edit/:policyName`, + }, +}; + +const initTestBed = registerTestBed(EditPolicy, testBedConfig); + +export interface EditPolicyTestBed extends TestBed { + actions: { + setWaitForSnapshotPolicy: (snapshotPolicyName: string) => void; + savePolicy: () => void; + }; +} + +export const setup = async (): Promise => { + const testBed = await initTestBed(); + + const setWaitForSnapshotPolicy = (snapshotPolicyName: string) => { + const { component, form } = testBed; + form.setInputValue('waitForSnapshotField', snapshotPolicyName, true); + component.update(); + }; + + const savePolicy = async () => { + const { component, find } = testBed; + await act(async () => { + find('savePolicyButton').simulate('click'); + }); + component.update(); + }; + + return { + ...testBed, + actions: { + setWaitForSnapshotPolicy, + savePolicy, + }, + }; +}; diff --git a/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/edit_policy/edit_policy.test.ts b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/edit_policy/edit_policy.test.ts new file mode 100644 index 00000000000000..cc04749af3205f --- /dev/null +++ b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/edit_policy/edit_policy.test.ts @@ -0,0 +1,96 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { act } from 'react-dom/test-utils'; + +import { setupEnvironment } from '../helpers/setup_environment'; + +import { EditPolicyTestBed, setup } from './edit_policy.helpers'; +import { DELETE_PHASE_POLICY } from './contants'; + +import { API_BASE_PATH } from '../../../common/constants'; + +window.scrollTo = jest.fn(); + +describe('', () => { + let testBed: EditPolicyTestBed; + const { server, httpRequestsMockHelpers } = setupEnvironment(); + afterAll(() => { + server.restore(); + }); + + describe('delete phase', () => { + beforeEach(async () => { + httpRequestsMockHelpers.setLoadPolicies([DELETE_PHASE_POLICY]); + + await act(async () => { + testBed = await setup(); + }); + + const { component } = testBed; + component.update(); + }); + + test('wait for snapshot policy field should correctly display snapshot policy name', () => { + expect(testBed.find('waitForSnapshotField').props().value).toEqual( + DELETE_PHASE_POLICY.policy.phases.delete.actions.wait_for_snapshot.policy + ); + }); + + test('wait for snapshot field should correctly update snapshot policy name', async () => { + const { actions } = testBed; + + const newPolicyName = 'my_new_snapshot_policy'; + actions.setWaitForSnapshotPolicy(newPolicyName); + await actions.savePolicy(); + + const expected = { + name: DELETE_PHASE_POLICY.name, + phases: { + ...DELETE_PHASE_POLICY.policy.phases, + delete: { + ...DELETE_PHASE_POLICY.policy.phases.delete, + actions: { + ...DELETE_PHASE_POLICY.policy.phases.delete.actions, + wait_for_snapshot: { + policy: newPolicyName, + }, + }, + }, + }, + }; + + const latestRequest = server.requests[server.requests.length - 1]; + expect(latestRequest.url).toBe(`${API_BASE_PATH}/policies`); + expect(latestRequest.method).toBe('POST'); + expect(JSON.parse(JSON.parse(latestRequest.requestBody).body)).toEqual(expected); + }); + + test('wait for snapshot field should delete action if field is empty', async () => { + const { actions } = testBed; + + actions.setWaitForSnapshotPolicy(''); + await actions.savePolicy(); + + const expected = { + name: DELETE_PHASE_POLICY.name, + phases: { + ...DELETE_PHASE_POLICY.policy.phases, + delete: { + ...DELETE_PHASE_POLICY.policy.phases.delete, + actions: { + ...DELETE_PHASE_POLICY.policy.phases.delete.actions, + }, + }, + }, + }; + delete expected.phases.delete.actions.wait_for_snapshot; + + const latestRequest = server.requests[server.requests.length - 1]; + expect(JSON.parse(JSON.parse(latestRequest.requestBody).body)).toEqual(expected); + }); + }); +}); diff --git a/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/helpers/http_requests.ts b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/helpers/http_requests.ts new file mode 100644 index 00000000000000..f41742fc104ff5 --- /dev/null +++ b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/helpers/http_requests.ts @@ -0,0 +1,33 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { SinonFakeServer, fakeServer } from 'sinon'; +import { API_BASE_PATH } from '../../../common/constants'; + +export const init = () => { + const server = fakeServer.create(); + server.respondImmediately = true; + server.respondWith([200, {}, 'DefaultServerResponse']); + + return { + server, + httpRequestsMockHelpers: registerHttpRequestMockHelpers(server), + }; +}; + +const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { + const setLoadPolicies = (response: any = []) => { + server.respondWith('GET', `${API_BASE_PATH}/policies`, [ + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify(response), + ]); + }; + + return { + setLoadPolicies, + }; +}; diff --git a/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/helpers/index.ts b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/helpers/index.ts new file mode 100644 index 00000000000000..3cff2e3ab050f5 --- /dev/null +++ b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/helpers/index.ts @@ -0,0 +1,7 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export type TestSubjects = 'waitForSnapshotField' | 'savePolicyButton'; diff --git a/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/helpers/setup_environment.ts b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/helpers/setup_environment.ts new file mode 100644 index 00000000000000..b3205a9523c625 --- /dev/null +++ b/x-pack/plugins/index_lifecycle_management/__jest__/client_integration/helpers/setup_environment.ts @@ -0,0 +1,39 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import axios from 'axios'; +import axiosXhrAdapter from 'axios/lib/adapters/xhr'; + +import { init as initHttp } from '../../../public/application/services/http'; +import { init as initHttpRequests } from './http_requests'; +import { init as initUiMetric } from '../../../public/application/services/ui_metric'; +import { init as initNotification } from '../../../public/application/services/notification'; + +import { usageCollectionPluginMock } from '../../../../../../src/plugins/usage_collection/public/mocks'; + +import { + notificationServiceMock, + fatalErrorsServiceMock, +} from '../../../../../../src/core/public/mocks'; + +const mockHttpClient = axios.create({ adapter: axiosXhrAdapter }); + +export const setupEnvironment = () => { + initUiMetric(usageCollectionPluginMock.createSetupContract()); + initNotification( + notificationServiceMock.createSetupContract().toasts, + fatalErrorsServiceMock.createSetupContract() + ); + + mockHttpClient.interceptors.response.use(({ data }) => data); + initHttp(mockHttpClient); + const { server, httpRequestsMockHelpers } = initHttpRequests(); + + return { + server, + httpRequestsMockHelpers, + }; +}; diff --git a/x-pack/plugins/index_lifecycle_management/public/application/constants/index.ts b/x-pack/plugins/index_lifecycle_management/public/application/constants/index.ts index a631a38fbcb7e6..6319fc0d68543f 100644 --- a/x-pack/plugins/index_lifecycle_management/public/application/constants/index.ts +++ b/x-pack/plugins/index_lifecycle_management/public/application/constants/index.ts @@ -37,6 +37,8 @@ export const PHASE_PRIMARY_SHARD_COUNT: string = 'selectedPrimaryShardCount'; export const PHASE_REPLICA_COUNT: string = 'selectedReplicaCount'; export const PHASE_INDEX_PRIORITY: string = 'phaseIndexPriority'; +export const PHASE_WAIT_FOR_SNAPSHOT_POLICY = 'waitForSnapshotPolicy'; + export const PHASE_ATTRIBUTES_THAT_ARE_NUMBERS_VALIDATE: string[] = [ PHASE_ROLLOVER_MINIMUM_AGE, PHASE_FORCE_MERGE_SEGMENTS, diff --git a/x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/components/delete_phase/delete_phase.js b/x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/components/delete_phase/delete_phase.js index 3b3e489d38f7db..299bf28778ab43 100644 --- a/x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/components/delete_phase/delete_phase.js +++ b/x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/components/delete_phase/delete_phase.js @@ -7,10 +7,16 @@ import React, { PureComponent, Fragment } from 'react'; import PropTypes from 'prop-types'; import { FormattedMessage } from '@kbn/i18n/react'; -import { EuiDescribedFormGroup, EuiSwitch } from '@elastic/eui'; +import { + EuiDescribedFormGroup, + EuiSwitch, + EuiFieldText, + EuiTextColor, + EuiFormRow, +} from '@elastic/eui'; -import { PHASE_DELETE, PHASE_ENABLED } from '../../../../constants'; -import { ActiveBadge, PhaseErrorMessage } from '../../../components'; +import { PHASE_DELETE, PHASE_ENABLED, PHASE_WAIT_FOR_SNAPSHOT_POLICY } from '../../../../constants'; +import { ActiveBadge, LearnMoreLink, OptionalLabel, PhaseErrorMessage } from '../../../components'; import { MinAgeInput } from '../min_age_input'; export class DeletePhase extends PureComponent { @@ -85,6 +91,48 @@ export class DeletePhase extends PureComponent {
)} + {phaseData[PHASE_ENABLED] ? ( + + + + } + description={ + + {' '} + + + } + titleSize="xs" + fullWidth + > + + + + + } + > + setPhaseData(PHASE_WAIT_FOR_SNAPSHOT_POLICY, e.target.value)} + /> + + + ) : null}
); } diff --git a/x-pack/plugins/index_lifecycle_management/public/application/store/defaults/delete_phase.js b/x-pack/plugins/index_lifecycle_management/public/application/store/defaults/delete_phase.js index b5296cd83fabdc..8534893e7e3b38 100644 --- a/x-pack/plugins/index_lifecycle_management/public/application/store/defaults/delete_phase.js +++ b/x-pack/plugins/index_lifecycle_management/public/application/store/defaults/delete_phase.js @@ -9,6 +9,7 @@ import { PHASE_ROLLOVER_MINIMUM_AGE, PHASE_ROLLOVER_MINIMUM_AGE_UNITS, PHASE_ROLLOVER_ALIAS, + PHASE_WAIT_FOR_SNAPSHOT_POLICY, } from '../../constants'; export const defaultDeletePhase = { @@ -17,5 +18,6 @@ export const defaultDeletePhase = { [PHASE_ROLLOVER_ALIAS]: '', [PHASE_ROLLOVER_MINIMUM_AGE]: 0, [PHASE_ROLLOVER_MINIMUM_AGE_UNITS]: 'd', + [PHASE_WAIT_FOR_SNAPSHOT_POLICY]: '', }; export const defaultEmptyDeletePhase = defaultDeletePhase; diff --git a/x-pack/plugins/index_lifecycle_management/public/application/store/defaults/index.d.ts b/x-pack/plugins/index_lifecycle_management/public/application/store/defaults/index.d.ts index 889e038f9e2c49..abf6db416c7f4a 100644 --- a/x-pack/plugins/index_lifecycle_management/public/application/store/defaults/index.d.ts +++ b/x-pack/plugins/index_lifecycle_management/public/application/store/defaults/index.d.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +export declare const defaultDeletePhase: any; export declare const defaultColdPhase: any; export declare const defaultWarmPhase: any; export declare const defaultHotPhase: any; diff --git a/x-pack/plugins/index_lifecycle_management/public/application/store/selectors/policies.js b/x-pack/plugins/index_lifecycle_management/public/application/store/selectors/policies.js index a3aef8679817d1..32c6d93383c227 100644 --- a/x-pack/plugins/index_lifecycle_management/public/application/store/selectors/policies.js +++ b/x-pack/plugins/index_lifecycle_management/public/application/store/selectors/policies.js @@ -31,6 +31,7 @@ import { PHASE_FREEZE_ENABLED, PHASE_INDEX_PRIORITY, PHASE_ROLLOVER_MAX_DOCUMENTS, + PHASE_WAIT_FOR_SNAPSHOT_POLICY, } from '../../constants'; import { filterItems, sortTable } from '../../services'; @@ -194,6 +195,9 @@ const phaseFromES = (phase, phaseName, defaultEmptyPolicy) => { if (actions.set_priority) { policy[PHASE_INDEX_PRIORITY] = actions.set_priority.priority; } + if (actions.wait_for_snapshot) { + policy[PHASE_WAIT_FOR_SNAPSHOT_POLICY] = actions.wait_for_snapshot.policy; + } } return policy; }; @@ -308,5 +312,13 @@ export const phaseToES = (phase, originalEsPhase) => { priority: phase[PHASE_INDEX_PRIORITY], }; } + + if (phase[PHASE_WAIT_FOR_SNAPSHOT_POLICY]) { + esPhase.actions.wait_for_snapshot = { + policy: phase[PHASE_WAIT_FOR_SNAPSHOT_POLICY], + }; + } else { + delete esPhase.actions.wait_for_snapshot; + } return esPhase; };