Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion src/plugins/es_ui_shared/public/request/send_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface SendRequestConfig {
* HttpFetchOptions#asSystemRequest.
*/
asSystemRequest?: boolean;
version?: string;
}

export interface SendRequestResponse<D = any, E = any> {
Expand All @@ -27,13 +28,14 @@ export interface SendRequestResponse<D = any, E = any> {

export const sendRequest = async <D = any, E = any>(
httpClient: HttpSetup,
{ path, method, body, query, asSystemRequest }: SendRequestConfig
{ path, method, body, query, version, asSystemRequest }: SendRequestConfig
): Promise<SendRequestResponse<D, E>> => {
try {
const stringifiedBody = typeof body === 'string' ? body : JSON.stringify(body);
const response = await httpClient[method]<{ data?: D } & D>(path, {
body: stringifiedBody,
query,
version,
asSystemRequest,
});

Expand Down
14 changes: 12 additions & 2 deletions src/plugins/es_ui_shared/public/request/use_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ export interface UseRequestResponse<D = any, E = Error> {

export const useRequest = <D = any, E = Error>(
httpClient: HttpSetup,
{ path, method, query, body, pollIntervalMs, initialData, deserializer }: UseRequestConfig
{
path,
method,
query,
body,
pollIntervalMs,
initialData,
deserializer,
version,
}: UseRequestConfig
): UseRequestResponse<D, E> => {
const isMounted = useRef(false);

Expand Down Expand Up @@ -60,10 +69,11 @@ export const useRequest = <D = any, E = Error>(
method,
query: queryStringified ? query : undefined,
body: bodyStringified ? body : undefined,
version,
};
// queryStringified and bodyStringified stand in for query and body as dependencies.
/* eslint-disable-next-line react-hooks/exhaustive-deps */
}, [path, method, queryStringified, bodyStringified]);
}, [path, method, queryStringified, bodyStringified, version]);

const resendRequest = useCallback(
async (asSystemRequest?: boolean) => {
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/transform/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export const PLUGIN = {
},
};

export const API_BASE_PATH = '/api/transform/';
const INTERNAL_API_BASE_PATH = '/internal/transform/';
const EXTERNAL_API_BASE_PATH = '/api/transform/';

export const addInternalBasePath = (uri: string): string => `${INTERNAL_API_BASE_PATH}${uri}`;
export const addExternalBasePath = (uri: string): string => `${EXTERNAL_API_BASE_PATH}${uri}`;

// In order to create a transform, the API requires the following privileges:
// - transform_admin (builtin)
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/transform/public/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { FormattedMessage } from '@kbn/i18n-react';

import { KibanaContextProvider, KibanaThemeProvider } from '@kbn/kibana-react-plugin/public';

import { API_BASE_PATH } from '../../common/constants';
import { addInternalBasePath } from '../../common/constants';

import { SectionError } from './components';
import { SECTION_SLUG } from './constants';
Expand Down Expand Up @@ -69,7 +69,9 @@ export const renderApp = (element: HTMLElement, appDependencies: AppDependencies
<EuiErrorBoundary>
<KibanaThemeProvider theme$={appDependencies.theme.theme$}>
<KibanaContextProvider services={appDependencies}>
<AuthorizationProvider privilegesEndpoint={`${API_BASE_PATH}privileges`}>
<AuthorizationProvider
privilegesEndpoint={{ path: addInternalBasePath(`privileges`), version: '1' }}
>
<I18nContext>
<App history={appDependencies.history} />
</I18nContext>
Expand Down
59 changes: 40 additions & 19 deletions x-pack/plugins/transform/public/app/hooks/use_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import type {
} from '../../../common/api_schemas/update_transforms';
import type { GetTransformsStatsResponseSchema } from '../../../common/api_schemas/transforms_stats';
import type { TransformId } from '../../../common/types/transform';
import { API_BASE_PATH } from '../../../common/constants';
import { addInternalBasePath } from '../../../common/constants';
import type { EsIndex } from '../../../common/types/es_index';
import type { EsIngestPipeline } from '../../../common/types/es_ingest_pipeline';

Expand All @@ -81,7 +81,7 @@ export const useApi = () => {
() => ({
async getTransformNodes(): Promise<GetTransformNodesResponseSchema | IHttpFetchError> {
try {
return await http.get(`${API_BASE_PATH}transforms/_nodes`);
return await http.get(addInternalBasePath(`transforms/_nodes`), { version: '1' });
} catch (e) {
return e;
}
Expand All @@ -90,7 +90,9 @@ export const useApi = () => {
transformId: TransformId
): Promise<GetTransformsResponseSchema | IHttpFetchError> {
try {
return await http.get(`${API_BASE_PATH}transforms/${transformId}`);
return await http.get(addInternalBasePath(`transforms/${transformId}`), {
version: '1',
});
} catch (e) {
return e;
}
Expand All @@ -99,7 +101,10 @@ export const useApi = () => {
fetchOptions: FetchOptions = {}
): Promise<GetTransformsResponseSchema | IHttpFetchError> {
try {
return await http.get(`${API_BASE_PATH}transforms`, fetchOptions);
return await http.get(addInternalBasePath(`transforms`), {
...fetchOptions,
version: '1',
});
} catch (e) {
return e;
}
Expand All @@ -108,7 +113,9 @@ export const useApi = () => {
transformId: TransformId
): Promise<GetTransformsStatsResponseSchema | IHttpFetchError> {
try {
return await http.get(`${API_BASE_PATH}transforms/${transformId}/_stats`);
return await http.get(addInternalBasePath(`transforms/${transformId}/_stats`), {
version: '1',
});
} catch (e) {
return e;
}
Expand All @@ -117,7 +124,10 @@ export const useApi = () => {
fetchOptions: FetchOptions = {}
): Promise<GetTransformsStatsResponseSchema | IHttpFetchError> {
try {
return await http.get(`${API_BASE_PATH}transforms/_stats`, fetchOptions);
return await http.get(addInternalBasePath(`transforms/_stats`), {
...fetchOptions,
version: '1',
});
} catch (e) {
return e;
}
Expand All @@ -127,8 +137,9 @@ export const useApi = () => {
transformConfig: PutTransformsRequestSchema
): Promise<PutTransformsResponseSchema | IHttpFetchError> {
try {
return await http.put(`${API_BASE_PATH}transforms/${transformId}`, {
return await http.put(addInternalBasePath(`transforms/${transformId}`), {
body: JSON.stringify(transformConfig),
version: '1',
});
} catch (e) {
return e;
Expand All @@ -139,8 +150,9 @@ export const useApi = () => {
transformConfig: PostTransformsUpdateRequestSchema
): Promise<PostTransformsUpdateResponseSchema | IHttpFetchError> {
try {
return await http.post(`${API_BASE_PATH}transforms/${transformId}/_update`, {
return await http.post(addInternalBasePath(`transforms/${transformId}/_update`), {
body: JSON.stringify(transformConfig),
version: '1',
});
} catch (e) {
return e;
Expand All @@ -150,8 +162,9 @@ export const useApi = () => {
reqBody: DeleteTransformsRequestSchema
): Promise<DeleteTransformsResponseSchema | IHttpFetchError> {
try {
return await http.post(`${API_BASE_PATH}delete_transforms`, {
return await http.post(addInternalBasePath(`delete_transforms`), {
body: JSON.stringify(reqBody),
version: '1',
});
} catch (e) {
return e;
Expand All @@ -161,8 +174,9 @@ export const useApi = () => {
obj: PostTransformsPreviewRequestSchema
): Promise<PostTransformsPreviewResponseSchema | IHttpFetchError> {
try {
return await http.post(`${API_BASE_PATH}transforms/_preview`, {
return await http.post(addInternalBasePath(`transforms/_preview`), {
body: JSON.stringify(obj),
version: '1',
});
} catch (e) {
return e;
Expand All @@ -172,8 +186,9 @@ export const useApi = () => {
reqBody: ReauthorizeTransformsRequestSchema
): Promise<ReauthorizeTransformsResponseSchema | IHttpFetchError> {
try {
return await http.post(`${API_BASE_PATH}reauthorize_transforms`, {
return await http.post(addInternalBasePath(`reauthorize_transforms`), {
body: JSON.stringify(reqBody),
version: '1',
});
} catch (e) {
return e;
Expand All @@ -184,8 +199,9 @@ export const useApi = () => {
reqBody: ResetTransformsRequestSchema
): Promise<ResetTransformsResponseSchema | IHttpFetchError> {
try {
return await http.post(`${API_BASE_PATH}reset_transforms`, {
return await http.post(addInternalBasePath(`reset_transforms`), {
body: JSON.stringify(reqBody),
version: '1',
});
} catch (e) {
return e;
Expand All @@ -195,8 +211,9 @@ export const useApi = () => {
reqBody: StartTransformsRequestSchema
): Promise<StartTransformsResponseSchema | IHttpFetchError> {
try {
return await http.post(`${API_BASE_PATH}start_transforms`, {
return await http.post(addInternalBasePath(`start_transforms`), {
body: JSON.stringify(reqBody),
version: '1',
});
} catch (e) {
return e;
Expand All @@ -206,8 +223,9 @@ export const useApi = () => {
transformsInfo: StopTransformsRequestSchema
): Promise<StopTransformsResponseSchema | IHttpFetchError> {
try {
return await http.post(`${API_BASE_PATH}stop_transforms`, {
return await http.post(addInternalBasePath(`stop_transforms`), {
body: JSON.stringify(transformsInfo),
version: '1',
});
} catch (e) {
return e;
Expand All @@ -217,8 +235,9 @@ export const useApi = () => {
transformsInfo: ScheduleNowTransformsRequestSchema
): Promise<ScheduleNowTransformsResponseSchema | IHttpFetchError> {
try {
return await http.post(`${API_BASE_PATH}schedule_now_transforms`, {
return await http.post(addInternalBasePath(`schedule_now_transforms`), {
body: JSON.stringify(transformsInfo),
version: '1',
});
} catch (e) {
return e;
Expand All @@ -232,26 +251,27 @@ export const useApi = () => {
{ messages: GetTransformsAuditMessagesResponseSchema; total: number } | IHttpFetchError
> {
try {
return await http.get(`${API_BASE_PATH}transforms/${transformId}/messages`, {
return await http.get(addInternalBasePath(`transforms/${transformId}/messages`), {
query: {
sortField,
sortDirection,
},
version: '1',
});
} catch (e) {
return e;
}
},
async getEsIndices(): Promise<EsIndex[] | IHttpFetchError> {
try {
return await http.get(`/api/index_management/indices`);
return await http.get(`/api/index_management/indices`, { version: '1' });
} catch (e) {
return e;
}
},
async getEsIngestPipelines(): Promise<EsIngestPipeline[] | IHttpFetchError> {
try {
return await http.get('/api/ingest_pipelines');
return await http.get('/api/ingest_pipelines', { version: '1' });
} catch (e) {
return e;
}
Expand All @@ -264,13 +284,14 @@ export const useApi = () => {
samplerShardSize = DEFAULT_SAMPLER_SHARD_SIZE
): Promise<FieldHistogramsResponseSchema | IHttpFetchError> {
try {
return await http.post(`${API_BASE_PATH}field_histograms/${dataViewTitle}`, {
return await http.post(addInternalBasePath(`field_histograms/${dataViewTitle}`), {
body: JSON.stringify({
query,
fields,
samplerShardSize,
...(runtimeMappings !== undefined ? { runtimeMappings } : {}),
}),
version: '1',
});
} catch (e) {
return e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ const initialValue: Authorization = {
export const AuthorizationContext = createContext<Authorization>({ ...initialValue });

interface Props {
privilegesEndpoint: string;
privilegesEndpoint: { path: string; version: string };
children: React.ReactNode;
}

export const AuthorizationProvider = ({ privilegesEndpoint, children }: Props) => {
const { path, version } = privilegesEndpoint;
const {
isLoading,
error,
data: privilegesData,
} = useRequest({
path: privilegesEndpoint,
path,
version,
method: 'get',
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

import type { HttpSetup } from '@kbn/core/public';
import type { DataViewsContract } from '@kbn/data-views-plugin/public';
import { API_BASE_PATH } from '../../../common/constants';
import { addInternalBasePath } from '../../../common/constants';

export class IndexService {
async canDeleteIndex(http: HttpSetup) {
const privilege = await http.get<{ hasAllPrivileges: boolean }>(`${API_BASE_PATH}privileges`);
const privilege = await http.get<{ hasAllPrivileges: boolean }>(
addInternalBasePath(`privileges`),
{ version: '1' }
);
if (!privilege) {
return false;
}
Expand Down
Loading