Skip to content

Commit

Permalink
[ILM] TS conversion of Index Management plugin extension (#76517) (#7…
Browse files Browse the repository at this point in the history
…6757)

* [ILM] TS conversion of Index Management plugin extension

* [ILM] Implement review suggestions

* [ILM] Fix jest test

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
yuliacech and elasticmachine authored Sep 4, 2020
1 parent e66f75a commit 285e8e4
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 129 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import moment from 'moment-timezone';
import axios from 'axios';
import axiosXhrAdapter from 'axios/lib/adapters/xhr';

import { mountWithIntl } from '../../../test_utils/enzyme_helpers';
import { mountWithIntl } from 'test_utils/enzyme_helpers';
import { usageCollectionPluginMock } from '../../../../src/plugins/usage_collection/public/mocks';
import {
retryLifecycleActionExtension,
removeLifecyclePolicyActionExtension,
Expand All @@ -19,19 +20,22 @@ import {
} from '../public/extend_index_management';
import { init as initHttp } from '../public/application/services/http';
import { init as initUiMetric } from '../public/application/services/ui_metric';
import { Index } from '../public/application/services/policies/types';

// We need to init the http with a mock for any tests that depend upon the http service.
// For example, add_lifecycle_confirm_modal makes an API request in its componentDidMount
// lifecycle method. If we don't mock this, CI will fail with "Call retries were exceeded".
initHttp(axios.create({ adapter: axiosXhrAdapter }), (path) => path);
initUiMetric({ reportUiStats: () => {} });
// This expects HttpSetup but we're giving it AxiosInstance.
// @ts-ignore
initHttp(axios.create({ adapter: axiosXhrAdapter }));
initUiMetric(usageCollectionPluginMock.createSetupContract());

jest.mock('../../../plugins/index_management/public', async () => {
const { indexManagementMock } = await import('../../../plugins/index_management/public/mocks.ts');
const { indexManagementMock } = await import('../../../plugins/index_management/public/mocks');
return indexManagementMock.createSetup();
});

const indexWithoutLifecyclePolicy = {
const indexWithoutLifecyclePolicy: Index = {
health: 'yellow',
status: 'open',
name: 'noPolicy',
Expand All @@ -43,13 +47,14 @@ const indexWithoutLifecyclePolicy = {
size: '3.4kb',
primary_size: '3.4kb',
aliases: 'none',
isFrozen: false,
ilm: {
index: 'testy1',
managed: false,
},
};

const indexWithLifecyclePolicy = {
const indexWithLifecyclePolicy: Index = {
health: 'yellow',
status: 'open',
name: 'testy3',
Expand All @@ -61,6 +66,7 @@ const indexWithLifecyclePolicy = {
size: '6.5kb',
primary_size: '6.5kb',
aliases: 'none',
isFrozen: false,
ilm: {
index: 'testy3',
managed: true,
Expand All @@ -87,6 +93,7 @@ const indexWithLifecycleError = {
size: '6.5kb',
primary_size: '6.5kb',
aliases: 'none',
isFrozen: false,
ilm: {
index: 'testy3',
managed: true,
Expand Down Expand Up @@ -115,10 +122,12 @@ const indexWithLifecycleError = {

moment.tz.setDefault('utc');

const getUrlForApp = (appId, options) => {
const getUrlForApp = (appId: string, options: any) => {
return appId + '/' + (options ? options.path : '');
};

const reloadIndices = () => {};

describe('extend index management', () => {
describe('retry lifecycle action extension', () => {
test('should return null when no indices have index lifecycle policy', () => {
Expand Down Expand Up @@ -153,20 +162,23 @@ describe('extend index management', () => {
test('should return null when no indices have index lifecycle policy', () => {
const extension = removeLifecyclePolicyActionExtension({
indices: [indexWithoutLifecyclePolicy],
reloadIndices,
});
expect(extension).toBeNull();
});

test('should return null when some indices have index lifecycle policy', () => {
const extension = removeLifecyclePolicyActionExtension({
indices: [indexWithoutLifecyclePolicy, indexWithLifecyclePolicy],
reloadIndices,
});
expect(extension).toBeNull();
});

test('should return extension when all indices have lifecycle policy', () => {
const extension = removeLifecyclePolicyActionExtension({
indices: [indexWithLifecycleError, indexWithLifecycleError],
reloadIndices,
});
expect(extension).toBeDefined();
expect(extension).toMatchSnapshot();
Expand All @@ -175,16 +187,18 @@ describe('extend index management', () => {

describe('add lifecycle policy action extension', () => {
test('should return null when index has index lifecycle policy', () => {
const extension = addLifecyclePolicyActionExtension(
{ indices: [indexWithLifecyclePolicy] },
getUrlForApp
);
const extension = addLifecyclePolicyActionExtension({
indices: [indexWithLifecyclePolicy],
reloadIndices,
getUrlForApp,
});
expect(extension).toBeNull();
});

test('should return null when more than one index is passed', () => {
const extension = addLifecyclePolicyActionExtension({
indices: [indexWithoutLifecyclePolicy, indexWithoutLifecyclePolicy],
reloadIndices,
getUrlForApp,
});
expect(extension).toBeNull();
Expand All @@ -193,10 +207,11 @@ describe('extend index management', () => {
test('should return extension when one index is passed and it does not have lifecycle policy', () => {
const extension = addLifecyclePolicyActionExtension({
indices: [indexWithoutLifecyclePolicy],
reloadIndices,
getUrlForApp,
});
expect(extension.renderConfirmModal).toBeDefined;
const component = extension.renderConfirmModal(jest.fn());
expect(extension?.renderConfirmModal).toBeDefined();
const component = extension!.renderConfirmModal(jest.fn());
const rendered = mountWithIntl(component);
expect(rendered.exists('.euiModal--confirmation'));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
UIM_POLICY_ATTACH_INDEX_TEMPLATE,
UIM_POLICY_DETACH_INDEX,
UIM_INDEX_RETRY_STEP,
} from '../constants/ui_metric';
} from '../constants';

import { trackUiMetric } from './ui_metric';
import { sendGet, sendPost, sendDelete, useRequest } from './http';
Expand Down Expand Up @@ -78,7 +78,11 @@ export const removeLifecycleForIndex = async (indexNames: string[]) => {
return response;
};

export const addLifecyclePolicyToIndex = async (body: GenericObject) => {
export const addLifecyclePolicyToIndex = async (body: {
indexName: string;
policyName: string;
alias: string;
}) => {
const response = await sendPost(`index/add`, body);
// Only track successful actions.
trackUiMetric(METRIC_TYPE.COUNT, UIM_POLICY_ATTACH_INDEX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Index as IndexInterface } from '../../../../../index_management/public';

export interface SerializedPolicy {
name: string;
phases: Phases;
Expand Down Expand Up @@ -169,3 +171,36 @@ export interface FrozenPhase
export interface DeletePhase extends CommonPhaseSettings, PhaseWithMinAge {
waitForSnapshotPolicy: string;
}

export interface IndexLifecyclePolicy {
index: string;
managed: boolean;
action?: string;
action_time_millis?: number;
age?: string;
failed_step?: string;
failed_step_retry_count?: number;
is_auto_retryable_error?: boolean;
lifecycle_date_millis?: number;
phase?: string;
phase_execution?: {
policy: string;
modified_date_in_millis: number;
version: number;
phase_definition: SerializedPhase;
};
phase_time_millis?: number;
policy?: string;
step?: string;
step_info?: {
reason?: string;
stack_trace?: string;
type?: string;
message?: string;
};
step_time_millis?: number;
}

export interface Index extends IndexInterface {
ilm: IndexLifecyclePolicy;
}
Loading

0 comments on commit 285e8e4

Please sign in to comment.