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

[ML] Add optional ability to delete target index and index pattern when deleting DFA job #66934

Merged
merged 26 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
df67c40
[ML] Add additional arg to deleteDFA to allow delete of destination i…
qn895 May 15, 2020
23ee850
[ML] Add option to delete target idx & idx pattern
qn895 May 18, 2020
f880eac
[ML] Modify styling of delete DFA job to use EUI
qn895 May 18, 2020
d14fafa
[ML] Refactor delete analytics endpoint
qn895 May 21, 2020
4b8d140
[ML] Refactor toggleDeleteIndex & toggleDeleteIndexPattern before render
qn895 May 21, 2020
01b4956
Merge branch 'master' into add-opt-del-index-when-del-job
elasticmachine May 21, 2020
e5e618f
[ML] Clarify delete index & ip with names in modal
qn895 May 21, 2020
ba82079
[ML] Update test for DFA action_delete, rename deleteDataFrameAnalyti…
qn895 May 25, 2020
98e90b0
Merge branch 'master' into add-opt-del-index-when-del-job
elasticmachine May 25, 2020
b867ef2
[ML] Update to match new eslint arrow-paren rule & clean up try/catch
qn895 May 25, 2020
d9342f6
Revert "[ML] Modify styling of delete DFA job to use EUI"
qn895 May 26, 2020
17b5fdf
[ML] Re-add xpack.ml.dataframe.analyticsList.deleteModalBody
qn895 May 26, 2020
950f077
[ML] Reformat error handling & stylistic changes
qn895 May 26, 2020
ccbd251
[ML] Add API integration test for delete DFA endpoint
qn895 May 27, 2020
ff8f9af
[ML] Changed to test groups for better set up & tear down
qn895 May 27, 2020
2894714
[ML] Add unit test with default behavior for action_delete.tsx
qn895 May 27, 2020
6642aae
[ML] Update status check to be cleaner
qn895 May 27, 2020
8071964
[ML] Fix i18n duplicate id
qn895 May 27, 2020
5bd97e9
[ML] Change names to match dest schema, replace JSON.stringify(e), ha…
qn895 May 28, 2020
d906bff
[ML] Update test for delete dfa endpoint
qn895 May 28, 2020
1144296
Merge remote-tracking branch 'upstream/master' into add-opt-del-index…
qn895 May 28, 2020
f67115d
[ML] Update common imports for delete dfa test
qn895 May 28, 2020
725d56d
[ML] Update dfa delete tests to re-use code
qn895 May 29, 2020
9e51430
[ML] Remove securityDisabled check on server side
qn895 May 29, 2020
e9e1d93
[ML] Refactor common use types and reformat jest test block
qn895 May 29, 2020
c118898
Merge branch 'master' into add-opt-del-index-when-del-job
elasticmachine Jun 1, 2020
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
11 changes: 11 additions & 0 deletions x-pack/plugins/ml/common/types/data_frame_analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 { CustomHttpResponseOptions, ResponseError } from 'kibana/server';
export interface DeleteDataFrameAnalyticsWithIndexStatus {
success: boolean;
error?: CustomHttpResponseOptions<ResponseError>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,41 @@
*/

import React from 'react';
import { render } from '@testing-library/react';

import { fireEvent, render } from '@testing-library/react';
import * as CheckPrivilige from '../../../../../capabilities/check_capabilities';

import { DeleteAction } from './action_delete';

import mockAnalyticsListItem from './__mocks__/analytics_list_item.json';
import { DeleteAction } from './action_delete';
import { I18nProvider } from '@kbn/i18n/react';
import {
coreMock as mockCoreServices,
i18nServiceMock,
} from '../../../../../../../../../../src/core/public/mocks';

jest.mock('../../../../../capabilities/check_capabilities', () => ({
checkPermission: jest.fn(() => false),
createPermissionFailureMessage: jest.fn(),
}));

jest.mock('../../../../../../application/util/dependency_cache', () => ({
getToastNotifications: () => ({ addSuccess: jest.fn(), addDanger: jest.fn() }),
}));

jest.mock('../../../../../contexts/kibana', () => ({
useMlKibana: () => ({
services: mockCoreServices.createStart(),
}),
}));
export const MockI18nService = i18nServiceMock.create();
export const I18nServiceConstructor = jest.fn().mockImplementation(() => MockI18nService);
jest.doMock('@kbn/i18n', () => ({
I18nService: I18nServiceConstructor,
}));

describe('DeleteAction', () => {
darnautov marked this conversation as resolved.
Show resolved Hide resolved
afterEach(() => {
jest.clearAllMocks();
});

test('When canDeleteDataFrameAnalytics permission is false, button should be disabled.', () => {
const { getByTestId } = render(<DeleteAction item={mockAnalyticsListItem} />);
expect(getByTestId('mlAnalyticsJobDeleteButton')).toHaveAttribute('disabled');
Expand Down Expand Up @@ -46,4 +67,24 @@ describe('DeleteAction', () => {

expect(getByTestId('mlAnalyticsJobDeleteButton')).toHaveAttribute('disabled');
});

describe('When delete model is open', () => {
test('should allow to delete target index by default.', () => {
const mock = jest.spyOn(CheckPrivilige, 'checkPermission');
mock.mockImplementation((p) => p === 'canDeleteDataFrameAnalytics');
const { getByTestId, queryByTestId } = render(
<I18nProvider>
<DeleteAction item={mockAnalyticsListItem} />
</I18nProvider>
);
const deleteButton = getByTestId('mlAnalyticsJobDeleteButton');
fireEvent.click(deleteButton);
expect(getByTestId('mlAnalyticsJobDeleteModal')).toBeInTheDocument();
expect(getByTestId('mlAnalyticsJobDeleteIndexSwitch')).toBeInTheDocument();
const mlAnalyticsJobDeleteIndexSwitch = getByTestId('mlAnalyticsJobDeleteIndexSwitch');
expect(mlAnalyticsJobDeleteIndexSwitch).toHaveAttribute('aria-checked', 'true');
expect(queryByTestId('mlAnalyticsJobDeleteIndexPatternSwitch')).toBeNull();
mock.mockRestore();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,132 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { Fragment, FC, useState } from 'react';
import React, { Fragment, FC, useState, useEffect } from 'react';
import { i18n } from '@kbn/i18n';
import {
EuiButtonEmpty,
EuiConfirmModal,
EuiOverlayMask,
EuiToolTip,
EuiSwitch,
EuiFlexGroup,
EuiFlexItem,
EUI_MODAL_CONFIRM_BUTTON,
} from '@elastic/eui';

import { deleteAnalytics } from '../../services/analytics_service';

import { IIndexPattern } from 'src/plugins/data/common';
import { FormattedMessage } from '@kbn/i18n/react';
import {
deleteAnalytics,
deleteAnalyticsAndDestIndex,
canDeleteIndex,
} from '../../services/analytics_service';
import {
checkPermission,
createPermissionFailureMessage,
} from '../../../../../capabilities/check_capabilities';

import { useMlKibana } from '../../../../../contexts/kibana';
import { isDataFrameAnalyticsRunning, DataFrameAnalyticsListRow } from './common';
import { extractErrorMessage } from '../../../../../util/error_utils';

interface DeleteActionProps {
item: DataFrameAnalyticsListRow;
}

export const DeleteAction: FC<DeleteActionProps> = ({ item }) => {
const disabled = isDataFrameAnalyticsRunning(item.stats.state);

const canDeleteDataFrameAnalytics: boolean = checkPermission('canDeleteDataFrameAnalytics');

const [isModalVisible, setModalVisible] = useState(false);
const [deleteTargetIndex, setDeleteTargetIndex] = useState<boolean>(true);
const [deleteIndexPattern, setDeleteIndexPattern] = useState<boolean>(true);
const [userCanDeleteIndex, setUserCanDeleteIndex] = useState<boolean>(false);
const [indexPatternExists, setIndexPatternExists] = useState<boolean>(false);

const { savedObjects, notifications } = useMlKibana().services;
const savedObjectsClient = savedObjects.client;

const indexName = item.config.dest.index;

const checkIndexPatternExists = async () => {
try {
const response = await savedObjectsClient.find<IIndexPattern>({
type: 'index-pattern',
perPage: 10,
search: `"${indexName}"`,
searchFields: ['title'],
fields: ['title'],
});
const ip = response.savedObjects.find(
(obj) => obj.attributes.title.toLowerCase() === indexName.toLowerCase()
);
if (ip !== undefined) {
setIndexPatternExists(true);
}
} catch (e) {
const { toasts } = notifications;
const error = extractErrorMessage(e);

toasts.addDanger(
i18n.translate(
'xpack.ml.dataframe.analyticsList.errorWithCheckingIfIndexPatternExistsNotificationErrorMessage',
{
defaultMessage:
'An error occurred checking if index pattern {indexPattern} exists: {error}',
values: { indexPattern: indexName, error },
}
)
);
}
};
const checkUserIndexPermission = () => {
try {
const userCanDelete = canDeleteIndex(indexName);
if (userCanDelete) {
setUserCanDeleteIndex(true);
}
} catch (e) {
const { toasts } = notifications;
const error = extractErrorMessage(e);

toasts.addDanger(
i18n.translate(
'xpack.ml.dataframe.analyticsList.errorWithCheckingIfUserCanDeleteIndexNotificationErrorMessage',
{
defaultMessage:
'An error occurred checking if user can delete {destinationIndex}: {error}',
values: { destinationIndex: indexName, error },
}
)
);
}
};

useEffect(() => {
// Check if an index pattern exists corresponding to current DFA job
// if pattern does exist, show it to user
checkIndexPatternExists();

// Check if an user has permission to delete the index & index pattern
checkUserIndexPermission();
}, []);

const closeModal = () => setModalVisible(false);
const deleteAndCloseModal = () => {
setModalVisible(false);
deleteAnalytics(item);

if ((userCanDeleteIndex && deleteTargetIndex) || (userCanDeleteIndex && deleteIndexPattern)) {
deleteAnalyticsAndDestIndex(
item,
deleteTargetIndex,
indexPatternExists && deleteIndexPattern
);
} else {
deleteAnalytics(item);
}
};
const openModal = () => setModalVisible(true);
const toggleDeleteIndex = () => setDeleteTargetIndex(!deleteTargetIndex);
const toggleDeleteIndexPattern = () => setDeleteIndexPattern(!deleteIndexPattern);

const buttonDeleteText = i18n.translate('xpack.ml.dataframe.analyticsList.deleteActionName', {
defaultMessage: 'Delete',
Expand Down Expand Up @@ -84,8 +174,9 @@ export const DeleteAction: FC<DeleteActionProps> = ({ item }) => {
<Fragment>
{deleteButton}
{isModalVisible && (
<EuiOverlayMask>
<EuiOverlayMask data-test-subj="mlAnalyticsJobDeleteOverlay">
<EuiConfirmModal
data-test-subj="mlAnalyticsJobDeleteModal"
title={i18n.translate('xpack.ml.dataframe.analyticsList.deleteModalTitle', {
defaultMessage: 'Delete {analyticsId}',
values: { analyticsId: item.config.id },
Expand All @@ -108,10 +199,47 @@ export const DeleteAction: FC<DeleteActionProps> = ({ item }) => {
buttonColor="danger"
>
<p>
{i18n.translate('xpack.ml.dataframe.analyticsList.deleteModalBody', {
defaultMessage: `Are you sure you want to delete this analytics job? The analytics job's destination index and optional Kibana index pattern will not be deleted.`,
})}
<FormattedMessage
id="xpack.ml.dataframe.analyticsList.deleteModalBody"
defaultMessage="Are you sure you want to delete this analytics job?"
/>
</p>

<EuiFlexGroup direction="column" gutterSize="none">
<EuiFlexItem>
{userCanDeleteIndex && (
<EuiSwitch
data-test-subj="mlAnalyticsJobDeleteIndexSwitch"
style={{ paddingBottom: 10 }}
label={i18n.translate(
'xpack.ml.dataframe.analyticsList.deleteDestinationIndexTitle',
{
defaultMessage: 'Delete destination index {indexName}',
values: { indexName },
}
)}
checked={deleteTargetIndex}
onChange={toggleDeleteIndex}
/>
)}
</EuiFlexItem>
<EuiFlexItem>
{userCanDeleteIndex && indexPatternExists && (
<EuiSwitch
data-test-subj="mlAnalyticsJobDeleteIndexPatternSwitch"
label={i18n.translate(
'xpack.ml.dataframe.analyticsList.deleteTargetIndexPatternTitle',
{
defaultMessage: 'Delete index pattern {indexPattern}',
values: { indexPattern: indexName },
}
)}
checked={deleteIndexPattern}
onChange={toggleDeleteIndexPattern}
/>
)}
</EuiFlexItem>
</EuiFlexGroup>
</EuiConfirmModal>
</EuiOverlayMask>
)}
Expand Down
Loading