Skip to content

Commit

Permalink
Replace deprecated index patterns API usage with data views API (#124134
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jportner authored Feb 1, 2022
1 parent 6e93e83 commit 0aee982
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ function getProps({
const rolesAPIClient = rolesAPIClientMock.create();
rolesAPIClient.getRole.mockResolvedValue(role);

const indexPatterns = dataPluginMock.createStartContract().indexPatterns;
const dataViews = dataPluginMock.createStartContract().dataViews;
// `undefined` titles can technically happen via import/export or other manual manipulation
indexPatterns.getTitles = jest.fn().mockResolvedValue(['foo*', 'bar*', undefined]);
dataViews.getTitles = jest.fn().mockResolvedValue(['foo*', 'bar*', undefined]);

const indicesAPIClient = indicesAPIClientMock.create();

Expand Down Expand Up @@ -171,7 +171,7 @@ function getProps({
roleName: role?.name,
license,
http,
indexPatterns,
dataViews,
indicesAPIClient,
privilegesAPIClient,
rolesAPIClient,
Expand Down Expand Up @@ -352,11 +352,11 @@ describe('<EditRolePage />', () => {
});

it('can render if index patterns are not available', async () => {
const indexPatterns = dataPluginMock.createStartContract().indexPatterns;
indexPatterns.getTitles = jest.fn().mockRejectedValue({ response: { status: 403 } });
const dataViews = dataPluginMock.createStartContract().dataViews;
dataViews.getTitles = jest.fn().mockRejectedValue({ response: { status: 403 } });

const wrapper = mountWithIntl(
<EditRolePage {...{ ...getProps({ action: 'edit' }), indexPatterns }} />
<EditRolePage {...{ ...getProps({ action: 'edit' }), dataViews }} />
);

await waitForRender(wrapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import type {
NotificationsStart,
ScopedHistory,
} from 'src/core/public';
import type { IndexPatternsContract } from 'src/plugins/data/public';
import type { DataViewsContract } from 'src/plugins/data/public';

import { reactRouterNavigate } from '../../../../../../../src/plugins/kibana_react/public';
import type { KibanaFeature } from '../../../../../features/common';
Expand Down Expand Up @@ -69,7 +69,7 @@ import { RoleValidator } from './validate_role';
interface Props {
action: 'edit' | 'clone';
roleName?: string;
indexPatterns: IndexPatternsContract;
dataViews: DataViewsContract;
userAPIClient: PublicMethodsOf<UserAPIClient>;
indicesAPIClient: PublicMethodsOf<IndicesAPIClient>;
rolesAPIClient: PublicMethodsOf<RolesAPIClient>;
Expand Down Expand Up @@ -101,20 +101,22 @@ function useRunAsUsers(
}

function useIndexPatternsTitles(
indexPatterns: IndexPatternsContract,
dataViews: DataViewsContract,
fatalErrors: FatalErrorsSetup,
notifications: NotificationsStart
) {
const [indexPatternsTitles, setIndexPatternsTitles] = useState<string[] | null>(null);
useEffect(() => {
indexPatterns
dataViews
.getTitles()
.catch((err: IHttpFetchError) => {
// If user doesn't have access to the index patterns they still should be able to create new
// or edit existing role.
if (err.response?.status === 403) {
notifications.toasts.addDanger({
title: i18n.translate('xpack.security.management.roles.noIndexPatternsPermission', {
// Note: we are attempting to fetch data views (a Kibana construct), but we are using those to render a list of usable index
// patterns (an Elasticsearch construct) for the user. This error message reflects what is shown on the UI.
defaultMessage: 'You need permission to access the list of available index patterns.',
}),
});
Expand All @@ -125,7 +127,7 @@ function useIndexPatternsTitles(
throw err;
})
.then((titles) => setIndexPatternsTitles(titles.filter(Boolean)));
}, [fatalErrors, indexPatterns, notifications]);
}, [fatalErrors, dataViews, notifications]);

return indexPatternsTitles;
}
Expand Down Expand Up @@ -271,7 +273,7 @@ function useFeatures(

export const EditRolePage: FunctionComponent<Props> = ({
userAPIClient,
indexPatterns,
dataViews,
rolesAPIClient,
indicesAPIClient,
privilegesAPIClient,
Expand All @@ -294,7 +296,7 @@ export const EditRolePage: FunctionComponent<Props> = ({
const { current: validator } = useRef(new RoleValidator({ shouldValidate: false }));
const [formError, setFormError] = useState<RoleValidationResult | null>(null);
const runAsUsers = useRunAsUsers(userAPIClient, fatalErrors);
const indexPatternsTitles = useIndexPatternsTitles(indexPatterns, fatalErrors, notifications);
const indexPatternsTitles = useIndexPatternsTitles(dataViews, fatalErrors, notifications);
const privileges = usePrivileges(privilegesAPIClient, fatalErrors);
const spaces = useSpaces(http, fatalErrors);
const features = useFeatures(getFeatures, fatalErrors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const rolesManagementApp = Object.freeze({
license={license}
docLinks={docLinks}
uiCapabilities={application.capabilities}
indexPatterns={data.indexPatterns}
dataViews={data.dataViews}
history={history}
spacesApiUi={spacesApiUi}
/>
Expand Down

0 comments on commit 0aee982

Please sign in to comment.