Skip to content

Commit 2903882

Browse files
committed
[ILM] TS conversion of Index Management plugin extension
1 parent d932830 commit 2903882

7 files changed

Lines changed: 107 additions & 60 deletions

File tree

x-pack/plugins/index_lifecycle_management/__jest__/__snapshots__/extend_index_management.test.js.snap renamed to x-pack/plugins/index_lifecycle_management/__jest__/__snapshots__/extend_index_management.test.tsx.snap

File renamed without changes.

x-pack/plugins/index_lifecycle_management/__jest__/extend_index_management.test.js renamed to x-pack/plugins/index_lifecycle_management/__jest__/extend_index_management.test.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import moment from 'moment-timezone';
88
import axios from 'axios';
99
import axiosXhrAdapter from 'axios/lib/adapters/xhr';
1010

11-
import { mountWithIntl } from '../../../test_utils/enzyme_helpers';
11+
import { mountWithIntl } from 'test_utils/enzyme_helpers';
12+
import { usageCollectionPluginMock } from '../../../../src/plugins/usage_collection/public/mocks';
1213
import {
1314
retryLifecycleActionExtension,
1415
removeLifecyclePolicyActionExtension,
@@ -23,11 +24,13 @@ import { init as initUiMetric } from '../public/application/services/ui_metric';
2324
// We need to init the http with a mock for any tests that depend upon the http service.
2425
// For example, add_lifecycle_confirm_modal makes an API request in its componentDidMount
2526
// lifecycle method. If we don't mock this, CI will fail with "Call retries were exceeded".
26-
initHttp(axios.create({ adapter: axiosXhrAdapter }), (path) => path);
27-
initUiMetric({ reportUiStats: () => {} });
27+
// This expects HttpSetup but we're giving it AxiosInstance.
28+
// @ts-ignore
29+
initHttp(axios.create({ adapter: axiosXhrAdapter }));
30+
initUiMetric(usageCollectionPluginMock.createSetupContract());
2831

2932
jest.mock('../../../plugins/index_management/public', async () => {
30-
const { indexManagementMock } = await import('../../../plugins/index_management/public/mocks.ts');
33+
const { indexManagementMock } = await import('../../../plugins/index_management/public/mocks');
3134
return indexManagementMock.createSetup();
3235
});
3336

@@ -115,7 +118,7 @@ const indexWithLifecycleError = {
115118

116119
moment.tz.setDefault('utc');
117120

118-
const getUrlForApp = (appId, options) => {
121+
const getUrlForApp = (appId: string, options: any) => {
119122
return appId + '/' + (options ? options.path : '');
120123
};
121124

@@ -175,10 +178,10 @@ describe('extend index management', () => {
175178

176179
describe('add lifecycle policy action extension', () => {
177180
test('should return null when index has index lifecycle policy', () => {
178-
const extension = addLifecyclePolicyActionExtension(
179-
{ indices: [indexWithLifecyclePolicy] },
180-
getUrlForApp
181-
);
181+
const extension = addLifecyclePolicyActionExtension({
182+
indices: [indexWithLifecyclePolicy],
183+
getUrlForApp,
184+
});
182185
expect(extension).toBeNull();
183186
});
184187

@@ -195,8 +198,8 @@ describe('extend index management', () => {
195198
indices: [indexWithoutLifecyclePolicy],
196199
getUrlForApp,
197200
});
198-
expect(extension.renderConfirmModal).toBeDefined;
199-
const component = extension.renderConfirmModal(jest.fn());
201+
expect(extension?.renderConfirmModal).toBeDefined();
202+
const component = extension!.renderConfirmModal(jest.fn());
200203
const rendered = mountWithIntl(component);
201204
expect(rendered.exists('.euiModal--confirmation'));
202205
});

x-pack/plugins/index_lifecycle_management/public/application/services/api.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
UIM_POLICY_ATTACH_INDEX_TEMPLATE,
1313
UIM_POLICY_DETACH_INDEX,
1414
UIM_INDEX_RETRY_STEP,
15-
} from '../constants/ui_metric';
15+
} from '../constants';
1616

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

81-
export const addLifecyclePolicyToIndex = async (body: GenericObject) => {
81+
export const addLifecyclePolicyToIndex = async (body: {
82+
indexName: string;
83+
policyName: string;
84+
alias: string;
85+
}) => {
8286
const response = await sendPost(`index/add`, body);
8387
// Only track successful actions.
8488
trackUiMetric(METRIC_TYPE.COUNT, UIM_POLICY_ATTACH_INDEX);

x-pack/plugins/index_lifecycle_management/public/extend_index_management/components/add_lifecycle_confirm_modal.js renamed to x-pack/plugins/index_lifecycle_management/public/extend_index_management/components/add_lifecycle_confirm_modal.tsx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import React, { Component, Fragment } from 'react';
88
import { get } from 'lodash';
99
import { i18n } from '@kbn/i18n';
1010
import { FormattedMessage } from '@kbn/i18n/react';
11+
import { ApplicationStart } from 'kibana/public';
12+
1113
import {
1214
EuiLink,
1315
EuiSelect,
@@ -26,9 +28,25 @@ import {
2628
import { loadPolicies, addLifecyclePolicyToIndex } from '../../application/services/api';
2729
import { showApiError } from '../../application/services/api_errors';
2830
import { toasts } from '../../application/services/notification';
31+
import { PolicyFromES } from '../../application/services/policies/types';
32+
33+
interface Props {
34+
indexName: string;
35+
closeModal: () => void;
36+
index: any;
37+
reloadIndices: () => void;
38+
getUrlForApp: ApplicationStart['getUrlForApp'];
39+
}
40+
41+
interface State {
42+
selectedPolicyName: string;
43+
selectedAlias: string;
44+
policies: PolicyFromES[];
45+
policyError?: string;
46+
}
2947

30-
export class AddLifecyclePolicyConfirmModal extends Component {
31-
constructor(props) {
48+
export class AddLifecyclePolicyConfirmModal extends Component<Props, State> {
49+
constructor(props: Props) {
3250
super(props);
3351
this.state = {
3452
policies: [],
@@ -81,7 +99,7 @@ export class AddLifecyclePolicyConfirmModal extends Component {
8199
);
82100
}
83101
};
84-
renderAliasFormElement = (selectedPolicy) => {
102+
renderAliasFormElement = (selectedPolicy?: PolicyFromES) => {
85103
const { selectedAlias } = this.state;
86104
const { index } = this.props;
87105
const showAliasSelect =
@@ -109,15 +127,15 @@ export class AddLifecyclePolicyConfirmModal extends Component {
109127
defaultMessage="Policy {policyName} is configured for rollover,
110128
but index {indexName} does not have an alias, which is required for rollover."
111129
values={{
112-
policyName: selectedPolicy.name,
130+
policyName: selectedPolicy?.name,
113131
indexName: index.name,
114132
}}
115133
/>
116134
</EuiCallOut>
117135
</Fragment>
118136
);
119137
}
120-
const aliasOptions = aliases.map((alias) => {
138+
const aliasOptions = aliases.map((alias: string) => {
121139
return {
122140
text: alias,
123141
value: alias,
@@ -155,7 +173,7 @@ export class AddLifecyclePolicyConfirmModal extends Component {
155173
const { policies, selectedPolicyName, policyError } = this.state;
156174
const selectedPolicy = selectedPolicyName
157175
? policies.find((policy) => policy.name === selectedPolicyName)
158-
: null;
176+
: undefined;
159177

160178
const options = policies.map(({ name }) => {
161179
return {
@@ -188,7 +206,7 @@ export class AddLifecyclePolicyConfirmModal extends Component {
188206
options={options}
189207
value={selectedPolicyName}
190208
onChange={(e) => {
191-
this.setState({ policyError: null, selectedPolicyName: e.target.value });
209+
this.setState({ policyError: undefined, selectedPolicyName: e.target.value });
192210
}}
193211
/>
194212
</EuiFormRow>
@@ -198,7 +216,7 @@ export class AddLifecyclePolicyConfirmModal extends Component {
198216
}
199217
async componentDidMount() {
200218
try {
201-
const policies = await loadPolicies(false, this.props.httpClient);
219+
const policies = await loadPolicies(false);
202220
this.setState({ policies });
203221
} catch (err) {
204222
showApiError(

x-pack/plugins/index_lifecycle_management/public/extend_index_management/components/index_lifecycle_summary.js renamed to x-pack/plugins/index_lifecycle_management/public/extend_index_management/components/index_lifecycle_summary.tsx

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ import {
2424
EuiPopoverTitle,
2525
} from '@elastic/eui';
2626

27+
import { ApplicationStart } from 'kibana/public';
2728
import { getPolicyPath } from '../../application/services/navigation';
2829

29-
const getHeaders = () => {
30+
interface Headers {
31+
policy: string;
32+
phase: string;
33+
action: string;
34+
action_time_millis: string;
35+
failed_step: string;
36+
}
37+
const getHeaders = (): Headers => {
3038
return {
3139
policy: i18n.translate(
3240
'xpack.indexLifecycleMgmt.indexLifecycleMgmtSummary.headers.lifecyclePolicyHeader',
@@ -60,8 +68,18 @@ const getHeaders = () => {
6068
),
6169
};
6270
};
63-
export class IndexLifecycleSummary extends Component {
64-
constructor(props) {
71+
72+
interface Props {
73+
index: any;
74+
getUrlForApp: ApplicationStart['getUrlForApp'];
75+
}
76+
interface State {
77+
showStackPopover: boolean;
78+
showPhaseExecutionPopover: boolean;
79+
}
80+
81+
export class IndexLifecycleSummary extends Component<Props, State> {
82+
constructor(props: Props) {
6583
super(props);
6684
this.state = {
6785
showStackPopover: false,
@@ -80,7 +98,7 @@ export class IndexLifecycleSummary extends Component {
8098
closePhaseExecutionPopover = () => {
8199
this.setState({ showPhaseExecutionPopover: false });
82100
};
83-
renderStackPopoverButton(ilm) {
101+
renderStackPopoverButton(ilm: any) {
84102
if (!ilm.stack_trace) {
85103
return null;
86104
}
@@ -105,10 +123,7 @@ export class IndexLifecycleSummary extends Component {
105123
</EuiPopover>
106124
);
107125
}
108-
renderPhaseExecutionPopoverButton(ilm) {
109-
if (!ilm.phase_execution) {
110-
return null;
111-
}
126+
renderPhaseExecutionPopoverButton(ilm: any) {
112127
const button = (
113128
<EuiLink onClick={this.togglePhaseExecutionPopover}>
114129
<FormattedMessage
@@ -153,11 +168,14 @@ export class IndexLifecycleSummary extends Component {
153168
index: { ilm = {} },
154169
} = this.props;
155170
const headers = getHeaders();
156-
const rows = {
171+
const rows: {
172+
left: JSX.Element[];
173+
right: JSX.Element[];
174+
} = {
157175
left: [],
158176
right: [],
159177
};
160-
Object.keys(headers).forEach((fieldName, arrayIndex) => {
178+
Object.entries(headers).forEach(([fieldName, label], arrayIndex) => {
161179
const value = ilm[fieldName];
162180
let content;
163181
if (fieldName === 'action_time_millis') {
@@ -176,21 +194,25 @@ export class IndexLifecycleSummary extends Component {
176194
content = value;
177195
}
178196
content = content || '-';
179-
const cell = [
180-
<EuiDescriptionListTitle key={fieldName}>
181-
<strong>{headers[fieldName]}</strong>
182-
</EuiDescriptionListTitle>,
183-
<EuiDescriptionListDescription key={fieldName + '_desc'}>
184-
{content}
185-
</EuiDescriptionListDescription>,
186-
];
197+
const cell = (
198+
<Fragment>
199+
<EuiDescriptionListTitle key={fieldName}>
200+
<strong>{label}</strong>
201+
</EuiDescriptionListTitle>
202+
<EuiDescriptionListDescription key={fieldName + '_desc'}>
203+
{content}
204+
</EuiDescriptionListDescription>
205+
</Fragment>
206+
);
187207
if (arrayIndex % 2 === 0) {
188208
rows.left.push(cell);
189209
} else {
190210
rows.right.push(cell);
191211
}
192212
});
193-
rows.right.push(this.renderPhaseExecutionPopoverButton(ilm));
213+
if (ilm.phase_execution) {
214+
rows.right.push(this.renderPhaseExecutionPopoverButton(ilm));
215+
}
194216
return rows;
195217
}
196218

x-pack/plugins/index_lifecycle_management/public/extend_index_management/index.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)