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

[Index Management] Add enrich policies fetch api and expose in plugin api #163556

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
*/

export const API_BASE_PATH = '/api/index_management';

export const INTERNAL_API_BASE_PATH = '/internal/index_management';
2 changes: 1 addition & 1 deletion x-pack/plugins/index_management/common/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

export { BASE_PATH } from './base_path';
export { API_BASE_PATH } from './api_base_path';
export { API_BASE_PATH, INTERNAL_API_BASE_PATH } from './api_base_path';
export { INVALID_INDEX_PATTERN_CHARS, INVALID_TEMPLATE_NAME_CHARS } from './invalid_characters';
export * from './index_statuses';

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/index_management/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// TODO: https://github.com/elastic/kibana/issues/110892
/* eslint-disable @kbn/eslint/no_export_all */

export { API_BASE_PATH, BASE_PATH, MAJOR_VERSION } from './constants';
export { API_BASE_PATH, INTERNAL_API_BASE_PATH, BASE_PATH, MAJOR_VERSION } from './constants';

export { getTemplateParameter } from './lib';

Expand Down
16 changes: 16 additions & 0 deletions x-pack/plugins/index_management/common/types/enrich_policies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 type { EnrichPolicyType } from '@elastic/elasticsearch/lib/api/types';

export interface SerializedEnrichPolicy {
type: EnrichPolicyType;
name: string;
sourceIndices: string[];
matchField: string;
enrichFields: string[];
}
2 changes: 2 additions & 0 deletions x-pack/plugins/index_management/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export * from './templates';
export type { DataStreamFromEs, Health, DataStream, DataStreamIndex } from './data_streams';

export * from './component_templates';

export * from './enrich_policies';
3 changes: 3 additions & 0 deletions x-pack/plugins/index_management/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
*/

import { extensionsServiceMock } from './services/extensions_service.mock';
import { publicApiServiceMock } from './services/public_api_service.mock';

export { extensionsServiceMock } from './services/extensions_service.mock';
export { publicApiServiceMock } from './services/public_api_service.mock';

function createIdxManagementSetupMock() {
const mock = {
extensionsService: extensionsServiceMock,
publicApiService: publicApiServiceMock,
};

return mock;
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/index_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SemVer from 'semver/classes/semver';
import { CoreSetup, PluginInitializerContext } from '@kbn/core/public';
import { setExtensionsService } from './application/store/selectors/extension_service';

import { ExtensionsService } from './services';
import { ExtensionsService, PublicApiService } from './services';

import {
IndexManagementPluginSetup,
Expand Down Expand Up @@ -64,6 +64,7 @@ export class IndexMgmtUIPlugin {
}

return {
apiService: new PublicApiService(coreSetup.http),
extensionsService: this.extensionsService.setup(),
};
}
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/index_management/public/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@

export type { ExtensionsSetup } from './extensions_service';
export { ExtensionsService } from './extensions_service';

export type { PublicApiServiceSetup } from './public_api_service';
export { PublicApiService } from './public_api_service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 { PublicApiServiceSetup } from './public_api_service';

export type PublicApiServiceSetupMock = jest.Mocked<PublicApiServiceSetup>;

const createServiceMock = (): PublicApiServiceSetupMock => ({
getAllEnrichPolicies: jest.fn(),
});

export const publicApiServiceMock = {
createSetupContract: createServiceMock,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 { HttpSetup } from '@kbn/core/public';
import { sendRequest, SendRequestResponse } from '../shared_imports';
import { API_BASE_PATH } from '../../common/constants';
import { SerializedEnrichPolicy } from '../../common/types';

export interface PublicApiServiceSetup {
getAllEnrichPolicies(): Promise<SendRequestResponse<SerializedEnrichPolicy[]>>;
}

/**
* Index Management public API service
*/
export class PublicApiService {
private http: HttpSetup;

/**
* constructor
* @param http http dependency
*/
constructor(http: HttpSetup) {
this.http = http;
}

/**
* Gets a list of all the enrich policies
*/
getAllEnrichPolicies() {
return sendRequest(this.http, {
path: `${API_BASE_PATH}/enrich_policies`,
method: 'get',
});
}
}
3 changes: 2 additions & 1 deletion x-pack/plugins/index_management/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/public';
import { ManagementSetup } from '@kbn/management-plugin/public';
import { SharePluginStart } from '@kbn/share-plugin/public';
import { ExtensionsSetup } from './services';
import { ExtensionsSetup, PublicApiServiceSetup } from './services';

export interface IndexManagementPluginSetup {
apiService: PublicApiServiceSetup;
extensionsService: ExtensionsSetup;
}

Expand Down
24 changes: 24 additions & 0 deletions x-pack/plugins/index_management/server/lib/enrich_policies.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { serializeEnrichmentPolicies } from './enrich_policies';
import { createTestESEnrichPolicy } from '../test/helpers';

describe('serializeEnrichmentPolicies', () => {
it('knows how to serialize a list of policies', async () => {
const mockedESPolicy = createTestESEnrichPolicy('my-policy', 'match');
expect(serializeEnrichmentPolicies([mockedESPolicy])).toEqual([
{
name: 'my-policy',
type: 'match',
sourceIndices: ['users'],
matchField: 'email',
enrichFields: ['first_name', 'last_name', 'city'],
},
]);
});
});
52 changes: 52 additions & 0 deletions x-pack/plugins/index_management/server/lib/enrich_policies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 { IScopedClusterClient } from '@kbn/core/server';
import type { EnrichSummary, EnrichPolicyType } from '@elastic/elasticsearch/lib/api/types';
import type { SerializedEnrichPolicy } from '../../common/types';

const getPolicyType = (policy: EnrichSummary): EnrichPolicyType => {
if (policy.config.match) {
return 'match';
}

if (policy.config.geo_match) {
return 'geo_match';
}

if (policy.config.range) {
return 'range';
}

throw new Error('Unknown policy type');
};

export const serializeEnrichmentPolicies = (
policies: EnrichSummary[]
): SerializedEnrichPolicy[] => {
return policies.map((policy: any) => {
const policyType = getPolicyType(policy);

return {
name: policy.config[policyType].name,
type: policyType,
sourceIndices: policy.config[policyType].indices,
matchField: policy.config[policyType].match_field,
enrichFields: policy.config[policyType].enrich_fields,
};
});
};

const fetchAll = async (client: IScopedClusterClient) => {
const res = await client.asCurrentUser.enrich.getPolicy();

return serializeEnrichmentPolicies(res.policies);
};

export const enrichPoliciesActions = {
fetchAll,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 { addInternalBasePath } from '..';
import { RouterMock, routeDependencies, RequestMock } from '../../../test/helpers';
import { serializeEnrichmentPolicies } from '../../../lib/enrich_policies';
import { createTestESEnrichPolicy } from '../../../test/helpers';

import { registerEnrichPoliciesRoute } from './register_enrich_policies_routes';

const mockedPolicy = createTestESEnrichPolicy('my-policy', 'match');

describe('Enrich policies API', () => {
const router = new RouterMock();

beforeEach(() => {
registerEnrichPoliciesRoute({
...routeDependencies,
router,
});
});

afterEach(() => {
jest.resetAllMocks();
});

describe('Get all policies - GET /internal/index_management/enrich_policies', () => {
const getEnrichPolicies = router.getMockESApiFn('enrich.getPolicy');

it('returns all available policies', async () => {
const mockRequest: RequestMock = {
method: 'get',
path: addInternalBasePath('/enrich_policies'),
};

getEnrichPolicies.mockResolvedValue({ policies: [mockedPolicy] });

const res = await router.runRequest(mockRequest);

expect(res).toEqual({
body: serializeEnrichmentPolicies([mockedPolicy]),
});
});

it('should return an error if it fails', async () => {
const mockRequest: RequestMock = {
method: 'get',
path: addInternalBasePath('/enrich_policies'),
};

const error = new Error('Oh no!');
getEnrichPolicies.mockRejectedValue(error);

await expect(router.runRequest(mockRequest)).rejects.toThrowError(error);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export { registerEnrichPoliciesRoute } from './register_enrich_policies_routes';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 { RouteDependencies } from '../../../types';

import { registerListRoute } from './register_list_route';

export function registerEnrichPoliciesRoute(dependencies: RouteDependencies) {
registerListRoute(dependencies);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 { IScopedClusterClient } from '@kbn/core/server';
import { RouteDependencies } from '../../../types';
import { addInternalBasePath } from '..';
import { enrichPoliciesActions } from '../../../lib/enrich_policies';

export function registerListRoute({ router, lib: { handleEsError } }: RouteDependencies) {
router.get(
{ path: addInternalBasePath('/enrich_policies'), validate: false },
async (context, request, response) => {
const client = (await context.core).elasticsearch.client as IScopedClusterClient;
try {
const policies = await enrichPoliciesActions.fetchAll(client);
return response.ok({ body: policies });
} catch (error) {
return handleEsError({ error, response });
}
}
);
}
4 changes: 3 additions & 1 deletion x-pack/plugins/index_management/server/routes/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import { API_BASE_PATH } from '../../../common';
import { API_BASE_PATH, INTERNAL_API_BASE_PATH } from '../../../common';

export const addBasePath = (uri: string): string => API_BASE_PATH + uri;

export const addInternalBasePath = (uri: string): string => INTERNAL_API_BASE_PATH + uri;
2 changes: 2 additions & 0 deletions x-pack/plugins/index_management/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { registerSettingsRoutes } from './api/settings';
import { registerStatsRoute } from './api/stats';
import { registerComponentTemplateRoutes } from './api/component_templates';
import { registerNodesRoute } from './api/nodes';
import { registerEnrichPoliciesRoute } from './api/enrich_policies';

export class ApiRoutes {
setup(dependencies: RouteDependencies) {
Expand All @@ -26,6 +27,7 @@ export class ApiRoutes {
registerMappingRoute(dependencies);
registerComponentTemplateRoutes(dependencies);
registerNodesRoute(dependencies);
registerEnrichPoliciesRoute(dependencies);
}

start() {}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/index_management/server/test/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export type { RequestMock } from './router_mock';
export { RouterMock } from './router_mock';

export { routeDependencies } from './route_dependencies';

export { createTestESEnrichPolicy } from './policies_fixtures';
Loading