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

add ilm explain api #5

Merged
merged 2 commits into from
Jan 7, 2023
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
1 change: 1 addition & 0 deletions x-pack/plugins/data_quality/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export const BASE_PATH = '/internal/data_quality';
export const GET_INDEX_STATS = `${BASE_PATH}/stats/{pattern}`;
export const GET_INDEX_MAPPINGS = `${BASE_PATH}/mappings/{pattern}`;
export const GET_UNALLOWED_FIELD_VALUES = `${BASE_PATH}/unallowed_field_values`;
export const GET_ILM_EXPLAIN = `${BASE_PATH}/ilm_explain/{pattern}`;
17 changes: 17 additions & 0 deletions x-pack/plugins/data_quality/server/lib/fetch_ilm_explain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { IlmExplainLifecycleResponse } from '@elastic/elasticsearch/lib/api/types';
import type { IScopedClusterClient } from '@kbn/core/server';

export const fetchILMExplain = async (
client: IScopedClusterClient,
indexPattern: string
): Promise<IlmExplainLifecycleResponse> =>
client.asCurrentUser.ilm.explainLifecycle({
index: indexPattern,
});
4 changes: 2 additions & 2 deletions x-pack/plugins/data_quality/server/lib/fetch_mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import type { IScopedClusterClient } from '@kbn/core/server';

export const fetchMappings = async (
client: IScopedClusterClient,
indexName: string
indexPattern: string
): Promise<Record<string, IndicesGetMappingIndexMappingRecord>> =>
client.asCurrentUser.indices.getMapping({
expand_wildcards: ['open'],
index: indexName,
index: indexPattern,
});
4 changes: 2 additions & 2 deletions x-pack/plugins/data_quality/server/lib/fetch_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import type { IScopedClusterClient } from '@kbn/core/server';

export const fetchStats = async (
client: IScopedClusterClient,
indexName: string
indexPattern: string
): Promise<IndicesStatsResponse> =>
client.asCurrentUser.indices.stats({
expand_wildcards: ['open'],
index: indexName,
index: indexPattern,
});
1 change: 1 addition & 0 deletions x-pack/plugins/data_quality/server/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
export * from './fetch_mappings';
export * from './fetch_stats';
export * from './get_unallowed_field_values';
export * from './fetch_ilm_explain';
8 changes: 7 additions & 1 deletion x-pack/plugins/data_quality/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import { PluginInitializerContext, CoreSetup, CoreStart, Plugin, Logger } from '@kbn/core/server';

import { DataQualityPluginSetup, DataQualityPluginStart } from './types';
import { getIndexMappingsRoute, getIndexStatsRoute, getUnallowedFieldValuesRoute } from './routes';
import {
getILMExplainRoute,
getIndexMappingsRoute,
getIndexStatsRoute,
getUnallowedFieldValuesRoute,
} from './routes';

export class DataQualityPlugin implements Plugin<DataQualityPluginSetup, DataQualityPluginStart> {
private readonly logger: Logger;
Expand All @@ -25,6 +30,7 @@ export class DataQualityPlugin implements Plugin<DataQualityPluginSetup, DataQua
getIndexMappingsRoute(router);
getIndexStatsRoute(router);
getUnallowedFieldValuesRoute(router);
getILMExplainRoute(router);
return {};
}

Expand Down
80 changes: 80 additions & 0 deletions x-pack/plugins/data_quality/server/routes/get_ilm_explain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { GET_ILM_EXPLAIN } from '../../common/constants';

import { fetchMappings } from '../lib';

import { serverMock } from '../__mocks__/server';
import { requestMock } from '../__mocks__/request';
import { requestContextMock } from '../__mocks__/request_context';
import { getILMExplainRoute } from './get_ilm_explain';

jest.mock('../lib', () => ({
fetchMappings: jest.fn(),
}));

describe('getILMExplainRoute route', () => {
let server: ReturnType<typeof serverMock.create>;
let { context } = requestContextMock.createTools();
const req = requestMock.create({
method: 'get',
path: GET_ILM_EXPLAIN,
params: {
pattern: 'auditbeat-*',
},
});

beforeEach(() => {
jest.clearAllMocks();

server = serverMock.create();
({ context } = requestContextMock.createTools());

getILMExplainRoute(server.router);
});

test('Returns index ilm information', async () => {
const mockIndices = { 'auditbeat-7.15.1-2022.12.06-000001': {} };
(fetchMappings as jest.Mock).mockResolvedValue(mockIndices);

const response = await server.inject(req, requestContextMock.convertContext(context));
expect(response.status).toEqual(200);
expect(response.body).toEqual(mockIndices);
});

test('Handles error', async () => {
const errorMessage = 'Error!';
(fetchMappings as jest.Mock).mockRejectedValue({ message: errorMessage });

const response = await server.inject(req, requestContextMock.convertContext(context));
expect(response.status).toEqual(500);
expect(response.body).toEqual({ message: errorMessage, status_code: 500 });
});
});

describe('request validation', () => {
let server: ReturnType<typeof serverMock.create>;

beforeEach(() => {
server = serverMock.create();

getILMExplainRoute(server.router);
});

test('disallows invalid pattern', () => {
const request = requestMock.create({
method: 'get',
path: GET_ILM_EXPLAIN,
params: {
pattern: 123,
},
});
const result = server.validate(request);

expect(result.badRequest).toHaveBeenCalled();
});
});
44 changes: 44 additions & 0 deletions x-pack/plugins/data_quality/server/routes/get_ilm_explain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { IRouter } from '@kbn/core/server';
import { transformError, buildResponse } from '@kbn/securitysolution-es-utils';

import { GET_ILM_EXPLAIN } from '../../common/constants';
import { fetchILMExplain } from '../lib';
import { buildRouteValidation } from '../schemas/common';
import { GetILMExplainParams } from '../schemas/get_ilm_explain';

export const getILMExplainRoute = (router: IRouter) => {
router.get(
{
path: GET_ILM_EXPLAIN,
validate: { params: buildRouteValidation(GetILMExplainParams) },
},
async (context, request, response) => {
const resp = buildResponse(response);

try {
const { client } = (await context.core).elasticsearch;
const decodedIndexName = decodeURIComponent(request.params.pattern);

const ilmExplain = await fetchILMExplain(client, decodedIndexName);

return response.ok({
body: ilmExplain.indices,
});
} catch (err) {
const error = transformError(err);

return resp.error({
body: error.message,
statusCode: error.statusCode,
});
}
}
);
};
1 change: 1 addition & 0 deletions x-pack/plugins/data_quality/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
export { getIndexMappingsRoute } from './get_index_mappings';
export { getIndexStatsRoute } from './get_index_stats';
export { getUnallowedFieldValuesRoute } from './get_unallowed_field_values';
export { getILMExplainRoute } from './get_ilm_explain';
12 changes: 12 additions & 0 deletions x-pack/plugins/data_quality/server/schemas/get_ilm_explain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import * as t from 'io-ts';

export const GetILMExplainParams = t.type({
pattern: t.string,
});