Skip to content

Commit

Permalink
Change data streams privileges request
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliacech committed Nov 20, 2020
1 parent 1549295 commit b9d34f7
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,6 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
]);
};

const setLoadDataStreamsPrivilegesResponse = (response?: HttpResponse, error?: any) => {
const status = error ? error.status || 400 : 200;
const body = error ? error.body : response;

server.respondWith('POST', `${API_BASE_PATH}/data_streams_privileges`, [
status,
{ 'Content-Type': 'application/json' },
JSON.stringify(body),
]);
};

const setDeleteTemplateResponse = (response: HttpResponse = []) => {
server.respondWith('POST', `${API_BASE_PATH}/delete_index_templates`, [
200,
Expand Down Expand Up @@ -131,7 +120,6 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
setLoadDataStreamsResponse,
setLoadDataStreamResponse,
setDeleteDataStreamResponse,
setLoadDataStreamsPrivilegesResponse,
setDeleteTemplateResponse,
setLoadTemplateResponse,
setCreateTemplateResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ export const createDataStreamPayload = (dataStream: Partial<DataStream>): DataSt
indexTemplateName: 'indexTemplate',
storageSize: '1b',
maxTimeStamp: 420,
privileges: {
delete_index: true,
},
...dataStream,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ describe('Data Streams tab', () => {
setLoadDataStreamResponse,
setLoadTemplateResponse,
setLoadTemplatesResponse,
setLoadDataStreamsPrivilegesResponse,
} = httpRequestsMockHelpers;

setLoadIndicesResponse([
Expand All @@ -111,11 +110,6 @@ describe('Data Streams tab', () => {
setLoadTemplatesResponse({ templates: [indexTemplate], legacyTemplates: [] });
setLoadTemplateResponse(indexTemplate);

setLoadDataStreamsPrivilegesResponse({
dataStream1: { delete_index: true },
dataStream2: { delete_index: true },
});

testBed = await setup({ history: createMemoryHistory() });
await act(async () => {
testBed.actions.goToDataStreamsList();
Expand Down Expand Up @@ -286,7 +280,6 @@ describe('Data Streams tab', () => {
setLoadIndicesResponse,
setLoadDataStreamsResponse,
setLoadDataStreamResponse,
setLoadDataStreamsPrivilegesResponse,
} = httpRequestsMockHelpers;

setLoadIndicesResponse([
Expand All @@ -298,10 +291,6 @@ describe('Data Streams tab', () => {
setLoadDataStreamsResponse([dataStreamPercentSign]);
setLoadDataStreamResponse(dataStreamPercentSign);

setLoadDataStreamsPrivilegesResponse({
'%dataStream': { delete_index: true },
});

testBed = await setup({
history: createMemoryHistory(),
});
Expand Down Expand Up @@ -462,23 +451,20 @@ describe('Data Streams tab', () => {
});

describe('delete data stream privileges', () => {
const {
setLoadDataStreamsResponse,
setLoadDataStreamResponse,
setLoadDataStreamsPrivilegesResponse,
} = httpRequestsMockHelpers;
const { setLoadDataStreamsResponse, setLoadDataStreamResponse } = httpRequestsMockHelpers;

const dataStreamWithDelete = createDataStreamPayload({ name: 'dataStreamWithDelete' });
const dataStreamNoDelete = createDataStreamPayload({ name: 'dataStreamNoDelete' });
const dataStreamWithDelete = createDataStreamPayload({
name: 'dataStreamWithDelete',
privileges: { delete_index: true },
});
const dataStreamNoDelete = createDataStreamPayload({
name: 'dataStreamNoDelete',
privileges: { delete_index: false },
});

beforeEach(async () => {
setLoadDataStreamsResponse([dataStreamWithDelete, dataStreamNoDelete]);

setLoadDataStreamsPrivilegesResponse({
dataStreamWithDelete: { delete_index: true },
dataStreamNoDelete: { delete_index: false },
});

testBed = await setup({ history: createMemoryHistory() });
await act(async () => {
testBed.actions.goToDataStreamsList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function deserializeDataStream(dataStreamFromEs: DataStreamFromEs): DataS
store_size: storageSize,
maximum_timestamp: maxTimeStamp,
_meta,
privileges,
} = dataStreamFromEs;

return {
Expand All @@ -37,6 +38,7 @@ export function deserializeDataStream(dataStreamFromEs: DataStreamFromEs): DataS
storageSize,
maxTimeStamp,
_meta,
privileges,
};
}

Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/index_management/common/types/data_streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ interface MetaFieldFromEs {
managed: boolean;
}

interface PrivilegesFieldFromEs {
delete_index: boolean;
}

type PrivilegesField = PrivilegesFieldFromEs;

type MetaField = MetaFieldFromEs;

export type HealthFromEs = 'GREEN' | 'YELLOW' | 'RED';
Expand All @@ -31,6 +37,7 @@ export interface DataStreamFromEs {
ilm_policy?: string;
store_size?: string;
maximum_timestamp?: number;
privileges: PrivilegesFieldFromEs;
}

export interface DataStreamIndexFromEs {
Expand All @@ -51,6 +58,7 @@ export interface DataStream {
storageSize?: string;
maxTimeStamp?: number;
_meta?: MetaField;
privileges: PrivilegesField;
}

export interface DataStreamIndex {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ const DetailsList: React.FunctionComponent<DetailsListProps> = ({ details }) =>
interface Props {
dataStreamName: string;
onClose: (shouldReload?: boolean) => void;
canDelete: boolean;
}

export const DataStreamDetailPanel: React.FunctionComponent<Props> = ({
dataStreamName,
onClose,
canDelete,
}) => {
const { error, data: dataStream, isLoading } = useLoadDataStream(dataStreamName);

Expand Down Expand Up @@ -292,7 +290,7 @@ export const DataStreamDetailPanel: React.FunctionComponent<Props> = ({
</EuiButtonEmpty>
</EuiFlexItem>

{!isLoading && !error && canDelete ? (
{!isLoading && !error && dataStream?.privileges.delete_index ? (
<EuiFlexItem grow={false}>
<EuiButton
color="danger"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ import {
} from '../../../../shared_imports';
import { useAppContext } from '../../../app_context';
import { SectionError, SectionLoading, Error } from '../../../components';
import { useLoadDataStreams, useLoadDataStreamsPrivileges } from '../../../services/api';
import { useLoadDataStreams } from '../../../services/api';
import { documentationService } from '../../../services/documentation';
import { Section } from '../home';
import { DataStreamTable } from './data_stream_table';
import { DataStreamDetailPanel } from './data_stream_detail_panel';
import { filterDataStreams } from '../../../lib/data_streams';
import { DataStream } from '../../../../../common/types';
import { DELETE_INDEX_PRIVILEGE } from '../../../constants';

interface MatchParams {
dataStreamName?: string;
Expand All @@ -48,6 +46,7 @@ export const DataStreamList: React.FunctionComponent<RouteComponentProps<MatchPa
history,
}) => {
const { isDeepLink } = extractQueryParams(search);
const decodedDataStreamName = attemptToURIDecode(dataStreamName);

const {
core: { getUrlForApp },
Expand All @@ -60,11 +59,6 @@ export const DataStreamList: React.FunctionComponent<RouteComponentProps<MatchPa
includeStats: isIncludeStatsChecked,
});

const { data: deletePrivileges } = useLoadDataStreamsPrivileges<typeof DELETE_INDEX_PRIVILEGE>({
names: dataStreams ? dataStreams!.map((dataStream: DataStream) => dataStream.name) : [],
privileges: [DELETE_INDEX_PRIVILEGE],
});

let content;

if (isLoading) {
Expand Down Expand Up @@ -248,15 +242,14 @@ export const DataStreamList: React.FunctionComponent<RouteComponentProps<MatchPa

<DataStreamTable
filters={
isDeepLink && dataStreamName !== undefined
? `name="${attemptToURIDecode(dataStreamName)}"`
isDeepLink && decodedDataStreamName !== undefined
? `name="${decodedDataStreamName}"`
: ''
}
dataStreams={filteredDataStreams}
reload={reload}
history={history as ScopedHistory}
includeStats={isIncludeStatsChecked}
deletePrivileges={deletePrivileges!}
/>
</>
);
Expand All @@ -270,9 +263,9 @@ export const DataStreamList: React.FunctionComponent<RouteComponentProps<MatchPa
If the user has been deep-linked, they'll expect to see the detail panel because it reflects
the URL state, even if there are no data streams or if there was an error loading them.
*/}
{dataStreamName && (
{decodedDataStreamName && (
<DataStreamDetailPanel
dataStreamName={attemptToURIDecode(dataStreamName)!}
dataStreamName={decodedDataStreamName}
onClose={(shouldReload?: boolean) => {
history.push(`/${Section.DataStreams}`);

Expand All @@ -281,9 +274,6 @@ export const DataStreamList: React.FunctionComponent<RouteComponentProps<MatchPa
reload();
}
}}
canDelete={
!deletePrivileges || deletePrivileges[attemptToURIDecode(dataStreamName)!]?.delete_index
}
/>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ import {
} from '@elastic/eui';
import { ScopedHistory } from 'kibana/public';

import { DataStream, DataStreamPrivileges } from '../../../../../../common/types';
import { DataStream } from '../../../../../../common/types';
import { UseRequestResponse, reactRouterNavigate } from '../../../../../shared_imports';
import { getDataStreamDetailsLink, getIndexListUri } from '../../../../services/routing';
import { isManagedByIngestManager } from '../../../../lib/data_streams';
import { DataHealth } from '../../../../components';
import { DeleteDataStreamConfirmationModal } from '../delete_data_stream_confirmation_modal';
import { humanizeTimeStamp } from '../humanize_time_stamp';
import { DELETE_INDEX_PRIVILEGE } from '../../../../constants';

interface Props {
dataStreams?: DataStream[];
deletePrivileges?: DataStreamPrivileges<typeof DELETE_INDEX_PRIVILEGE>;
reload: UseRequestResponse['resendRequest'];
history: ScopedHistory;
includeStats: boolean;
Expand All @@ -41,7 +39,6 @@ export const DataStreamTable: React.FunctionComponent<Props> = ({
history,
filters,
includeStats,
deletePrivileges,
}) => {
const [selection, setSelection] = useState<DataStream[]>([]);
const [dataStreamsToDelete, setDataStreamsToDelete] = useState<string[]>([]);
Expand Down Expand Up @@ -165,9 +162,7 @@ export const DataStreamTable: React.FunctionComponent<Props> = ({
},
isPrimary: true,
'data-test-subj': 'deleteDataStream',
available: ({ name }: DataStream) => {
return !deletePrivileges || deletePrivileges[name]?.delete_index;
},
available: ({ privileges: { delete_index: deleteIndex } }: DataStream) => deleteIndex,
},
],
});
Expand Down Expand Up @@ -195,10 +190,7 @@ export const DataStreamTable: React.FunctionComponent<Props> = ({
},
toolsLeft:
selection.length > 0 &&
(!deletePrivileges ||
selection.every(
(dataStream: DataStream) => deletePrivileges[dataStream.name]?.delete_index
)) ? (
selection.every((dataStream: DataStream) => dataStream.privileges.delete_index) ? (
<EuiButton
data-test-subj="deleteDataStreamsButton"
onClick={() => setDataStreamsToDelete(selection.map(({ name }: DataStream) => name))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import { RouteDependencies } from '../../../types';

import { registerGetOneRoute, registerGetAllRoute } from './register_get_route';
import { registerDeleteRoute } from './register_delete_route';
import { registerPrivilegesRoute } from './register_privileges_route';

export function registerDataStreamRoutes(dependencies: RouteDependencies) {
registerGetOneRoute(dependencies);
registerGetAllRoute(dependencies);
registerDeleteRoute(dependencies);
registerPrivilegesRoute(dependencies);
}
Loading

0 comments on commit b9d34f7

Please sign in to comment.