Skip to content

Commit

Permalink
[Endpoint] Can delete policies (elastic#68567)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlog authored Jun 12, 2020
1 parent 1b85b40 commit ccf8def
Show file tree
Hide file tree
Showing 13 changed files with 496 additions and 55 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 @@ -21,13 +21,14 @@ import {
import React, { memo, MouseEventHandler, ReactNode, useMemo } from 'react';
import styled from 'styled-components';
import { EuiTabProps } from '@elastic/eui/src/components/tabs/tab';
import { gutterTimeline } from '../../lib/helpers';

const StyledEuiPage = styled(EuiPage)`
&.endpoint--isListView {
padding: 0;
padding: 0 ${gutterTimeline} 0 ${(props) => props.theme.eui.euiSizeL};
.endpoint-header {
padding: ${(props) => props.theme.eui.euiSizeL};
padding: ${(props) => props.theme.eui.euiSizeL} 0;
margin-bottom: 0;
}
.endpoint-page-content {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { PolicyData } from '../../../../../../common/endpoint/types';
import { ServerApiError } from '../../../../../common/types';
import { GetAgentStatusResponse } from '../../../../../../../ingest_manager/common/types/rest_spec';

interface ServerReturnedPolicyListData {
type: 'serverReturnedPolicyListData';
Expand All @@ -22,4 +23,42 @@ interface ServerFailedToReturnPolicyListData {
payload: ServerApiError;
}

export type PolicyListAction = ServerReturnedPolicyListData | ServerFailedToReturnPolicyListData;
interface UserClickedPolicyListDeleteButton {
type: 'userClickedPolicyListDeleteButton';
payload: { policyId: string };
}

interface UserOpenedPolicyListDeleteModal {
type: 'userOpenedPolicyListDeleteModal';
payload: { agentConfigId: string };
}

interface ServerDeletedPolicyFailure {
type: 'serverDeletedPolicyFailure';
payload: ServerApiError;
}

interface ServerDeletedPolicy {
type: 'serverDeletedPolicy';
payload: { id: string; success: boolean };
}

interface ServerReturnedPolicyAgentsSummaryForDeleteFailure {
type: 'serverReturnedPolicyAgentsSummaryForDeleteFailure';
payload: ServerApiError;
}

interface ServerReturnedPolicyAgentsSummaryForDelete {
type: 'serverReturnedPolicyAgentsSummaryForDelete';
payload: { agentStatusSummary: GetAgentStatusResponse['results'] };
}

export type PolicyListAction =
| ServerReturnedPolicyListData
| ServerFailedToReturnPolicyListData
| UserClickedPolicyListDeleteButton
| ServerDeletedPolicyFailure
| ServerDeletedPolicy
| UserOpenedPolicyListDeleteModal
| ServerReturnedPolicyAgentsSummaryForDeleteFailure
| ServerReturnedPolicyAgentsSummaryForDelete;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import { DATASOURCE_SAVED_OBJECT_TYPE } from '../../../../../../../ingest_manage
import { policyListReducer } from './reducer';
import { policyListMiddlewareFactory } from './middleware';

import { isOnPolicyListPage, selectIsLoading, urlSearchParams } from './selectors';
import {
isOnPolicyListPage,
selectIsLoading,
urlSearchParams,
selectIsDeleting,
} from './selectors';
import { DepsStartMock, depsStartMock } from '../../../../../common/mock/endpoint';
import { setPolicyListApiMockImplementation } from './test_mock_utils';
import { INGEST_API_DATASOURCES } from './services/ingest';
Expand Down Expand Up @@ -85,6 +90,33 @@ describe('policy list store concerns', () => {
expect(selectIsLoading(store.getState())).toBe(false);
});

it('it sets `isDeleting` when `userClickedPolicyListDeleteButton`', async () => {
expect(selectIsDeleting(store.getState())).toBe(false);
store.dispatch({
type: 'userClickedPolicyListDeleteButton',
payload: {
policyId: '123',
},
});
expect(selectIsDeleting(store.getState())).toBe(true);
await waitForAction('serverDeletedPolicy');
expect(selectIsDeleting(store.getState())).toBe(false);
});

it('it sets refreshes policy data when `serverDeletedPolicy`', async () => {
expect(selectIsLoading(store.getState())).toBe(false);
store.dispatch({
type: 'serverDeletedPolicy',
payload: {
policyId: '',
success: true,
},
});
expect(selectIsLoading(store.getState())).toBe(true);
await waitForAction('serverReturnedPolicyListData');
expect(selectIsLoading(store.getState())).toBe(false);
});

it('it resets state on `userChangedUrl` and pathname is NOT `/policy`', async () => {
store.dispatch({
type: 'userChangedUrl',
Expand All @@ -108,9 +140,18 @@ describe('policy list store concerns', () => {
location: undefined,
policyItems: [],
isLoading: false,
isDeleting: false,
deleteStatus: undefined,
pageIndex: 0,
pageSize: 10,
total: 0,
agentStatusSummary: {
error: 0,
events: 0,
offline: 0,
online: 0,
total: 0,
},
});
});
it('uses default pagination params when not included in url', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
*/

import { GetPolicyListResponse, PolicyListState } from '../../types';
import { sendGetEndpointSpecificDatasources } from './services/ingest';
import {
sendGetEndpointSpecificDatasources,
sendDeleteDatasource,
sendGetFleetAgentStatusForConfig,
} from './services/ingest';
import { isOnPolicyListPage, urlSearchParams } from './selectors';
import { ImmutableMiddlewareFactory } from '../../../../../common/store';
import { initialPolicyListState } from './reducer';
import {
DeleteDatasourcesResponse,
DeleteDatasourcesRequest,
GetAgentStatusResponse,
} from '../../../../../../../ingest_manager/common';

export const policyListMiddlewareFactory: ImmutableMiddlewareFactory<PolicyListState> = (
coreStart
Expand All @@ -19,7 +28,10 @@ export const policyListMiddlewareFactory: ImmutableMiddlewareFactory<PolicyListS
next(action);

const state = getState();
if (action.type === 'userChangedUrl' && isOnPolicyListPage(state)) {
if (
(action.type === 'userChangedUrl' && isOnPolicyListPage(state)) ||
action.type === 'serverDeletedPolicy'
) {
const { page_index: pageIndex, page_size: pageSize } = urlSearchParams(state);
let response: GetPolicyListResponse;

Expand Down Expand Up @@ -47,6 +59,45 @@ export const policyListMiddlewareFactory: ImmutableMiddlewareFactory<PolicyListS
total: response ? response.total : initialPolicyListState().total,
},
});
} else if (action.type === 'userClickedPolicyListDeleteButton') {
const { policyId } = action.payload;
const datasourceIds: DeleteDatasourcesRequest['body']['datasourceIds'] = [policyId];
let apiResponse: DeleteDatasourcesResponse;
try {
apiResponse = await sendDeleteDatasource(http, { body: { datasourceIds } });
} catch (err) {
dispatch({
type: 'serverDeletedPolicyFailure',
payload: err.body ?? err,
});
return;
}

dispatch({
type: 'serverDeletedPolicy',
payload: {
id: apiResponse ? apiResponse[0].id : '',
success: true,
},
});
} else if (action.type === 'userOpenedPolicyListDeleteModal') {
const { agentConfigId } = action.payload;
let apiResponse: GetAgentStatusResponse;
try {
apiResponse = await sendGetFleetAgentStatusForConfig(http, agentConfigId);
} catch (err) {
dispatch({
type: 'serverReturnedPolicyAgentsSummaryForDeleteFailure',
payload: err.body ?? err,
});
return;
}
dispatch({
type: 'serverReturnedPolicyAgentsSummaryForDelete',
payload: {
agentStatusSummary: apiResponse.results,
},
});
}
};
};
Loading

0 comments on commit ccf8def

Please sign in to comment.