Skip to content

Commit

Permalink
Merge branch 'main' into 163690-ml-aiops-log-rate-text-field-support
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Oct 4, 2023
2 parents 8197b1e + cd50e1d commit 0b5f2d0
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export interface EnterpriseSearchApplicationDetails {
template: {
script: {
lang: string;
params: unknown;
options: object;
params: object;
source: string;
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,23 @@ describe('UpdateSearchApplicationApiLogic', () => {
});
describe('updateSearchApplication', () => {
it('calls correct api', async () => {
const searchApplication = { name: 'my-search-application', indices: ['an-index'] };
const searchApplication = {
name: 'my-search-application',
indices: ['an-index'],
template: {
script: {
source: '"query":{"term":{"{{field_name}}":["{{field_value}}"',
lang: 'mustache',
options: {
content_type: 'application/json;charset=utf-8',
},
params: {
field_name: 'hello',
field_value: 'world',
},
},
},
};
const response = { result: 'updated' };
const promise = Promise.resolve(response);
http.put.mockReturnValue(promise);
Expand All @@ -27,7 +43,14 @@ describe('UpdateSearchApplicationApiLogic', () => {
expect(http.put).toHaveBeenCalledWith(
'/internal/enterprise_search/search_applications/my-search-application',
{
body: '{"indices":["an-index"],"name":"my-search-application"}',
body:
'{"indices":["an-index"],' +
'"name":"my-search-application",' +
'"template":{' +
'"script":{"source":"\\"query\\":{\\"term\\":{\\"{{field_name}}\\":[\\"{{field_value}}\\"",' +
'"lang":"mustache",' +
'"options":{"content_type":"application/json;charset=utf-8"},' +
'"params":{"field_name":"hello","field_value":"world"}}}}',
}
);
await expect(result).resolves.toEqual(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import { HttpLogic } from '../../../shared/http';
export interface UpdateSearchApplicationApiParams {
indices: string[];
name: string;
template: {
script: {
lang: string;
options: object;
params: object;
source: string;
};
};
}

export type UpdateSearchApplicationApiResponse = EnterpriseSearchApplication;
Expand All @@ -24,11 +32,12 @@ export type UpdateSearchApplicationApiLogicActions = Actions<
export const updateSearchApplication = async ({
name,
indices,
template,
}: UpdateSearchApplicationApiParams): Promise<UpdateSearchApplicationApiResponse> => {
const route = `/internal/enterprise_search/search_applications/${name}`;

return await HttpLogic.values.http.put<EnterpriseSearchApplication>(route, {
body: JSON.stringify({ indices, name }),
body: JSON.stringify({ indices, name, template }),
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ const mockSearchApplicationData: EnterpriseSearchApplicationDetails = {
name: DEFAULT_VALUES.searchApplicationName,
template: {
script: {
source: '"query":{"term":{"{{field_name}}":["{{field_value}}"',
lang: 'mustache',
params: { query_string: '*' },
source: '',
options: { content_type: 'application/json;charset=utf-8' },
params: {
field_name: 'hello',
field_value: 'world',
},
},
},
updated_at_millis: 1679501369566,
Expand Down Expand Up @@ -114,6 +118,7 @@ describe('SearchApplicationViewLogic', () => {
).toHaveBeenCalledWith({
name: DEFAULT_VALUES.searchApplicationName,
indices: ['search-002'],
template: mockSearchApplicationData.template,
});
});
});
Expand All @@ -131,6 +136,7 @@ describe('SearchApplicationViewLogic', () => {
).toHaveBeenCalledWith({
name: DEFAULT_VALUES.searchApplicationName,
indices: ['search-001', 'search-002', 'search-003'],
template: mockSearchApplicationData.template,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const SearchApplicationIndicesLogic = kea<
actions.updateSearchApplicationRequest({
name: values.searchApplicationName,
indices: updatedIndices,
template: values.searchApplicationData.template,
});
},
searchApplicationUpdated: () => {
Expand All @@ -77,6 +78,7 @@ export const SearchApplicationIndicesLogic = kea<
actions.updateSearchApplicationRequest({
name: values.searchApplicationName,
indices: updatedIndices,
template: values.searchApplicationData.template,
});
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const mockSearchApplicationData: EnterpriseSearchApplicationDetails = {
script: {
lang: 'mustache',
params: { query_string: '*' },
options: { content_type: 'application/json;charset=utf-8' },
source: '',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,65 @@ describe('engines routes', () => {
},
});
});

it('PUT - Upsert API request - create with template', async () => {
mockClient.asCurrentUser.searchApplication.put.mockImplementation(() => ({
acknowledged: true,
}));

await mockRouter.callRoute({
body: {
indices: ['test-indices-1'],
template: {
script: {
source: '"query":{"term":{"{{field_name}}":["{{field_value}}"',
lang: 'mustache',
options: {
content_type: 'application/json;charset=utf-8',
},
params: {
field_name: 'hello',
field_value: 'world',
},
},
},
},
params: {
engine_name: 'engine-name',
},
query: { create: true },
});
expect(mockClient.asCurrentUser.searchApplication.put).toHaveBeenCalledWith({
create: true,
name: 'engine-name',
search_application: {
indices: ['test-indices-1'],
name: 'engine-name',
template: {
script: {
source: '"query":{"term":{"{{field_name}}":["{{field_value}}"',
lang: 'mustache',
options: {
content_type: 'application/json;charset=utf-8',
},
params: {
field_name: 'hello',
field_value: 'world',
},
},
},
updated_at_millis: expect.any(Number),
},
});
const mock = jest.fn();
const mockResponse = mock({ result: 'created' });
expect(mockRouter.response.ok).toHaveReturnedWith(mockResponse);
expect(mockRouter.response.ok).toHaveBeenCalledWith({
body: {
acknowledged: true,
},
});
});
it('returns 400, create search application with invalid characters', async () => {
(mockClient.asCurrentUser.searchApplication.put as jest.Mock).mockRejectedValueOnce({
meta: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { SearchResponse, AcknowledgedResponseBase } from '@elastic/elasticsearch/lib/api/types';
import { AcknowledgedResponseBase, SearchResponse } from '@elastic/elasticsearch/lib/api/types';
import { schema } from '@kbn/config-schema';
import { i18n } from '@kbn/i18n';

Expand Down Expand Up @@ -103,6 +103,16 @@ export function registerSearchApplicationsRoutes({ log, router }: RouteDependenc
body: schema.object({
indices: schema.arrayOf(schema.string()),
name: schema.maybe(schema.string()),
template: schema.maybe(
schema.object({
script: schema.object({
source: schema.oneOf([schema.string(), schema.object({}, { unknowns: 'allow' })]),
lang: schema.string(),
params: schema.maybe(schema.object({}, { unknowns: 'allow' })),
options: schema.maybe(schema.object({}, { unknowns: 'allow' })),
}),
})
),
}),
params: schema.object({
engine_name: schema.string(),
Expand All @@ -115,12 +125,27 @@ export function registerSearchApplicationsRoutes({ log, router }: RouteDependenc
elasticsearchErrorHandler(log, async (context, request, response) => {
const { client } = (await context.core).elasticsearch;
try {
const script = request.body.template?.script;
const engine = (await client.asCurrentUser.searchApplication.put({
...request.query,
name: request.params.engine_name,
search_application: {
indices: request.body.indices,
name: request.params.engine_name,
template:
script == null
? undefined
: {
script: {
source:
typeof script.source === 'string'
? script.source
: JSON.stringify(script.source),
lang: script.lang,
params: script.params,
options: script.options,
},
},
updated_at_millis: Date.now(),
},
})) as EnterpriseSearchApplicationUpsertResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export default function createAlertsAsDataInstallResourcesTest({ getService }: F
'kibana.alert.rule.execution.uuid',
];

describe('alerts as data', () => {
// Failing: See https://github.com/elastic/kibana/issues/167945
describe.skip('alerts as data', () => {
afterEach(() => objectRemover.removeAll());
after(async () => {
await es.deleteByQuery({ index: alertsAsDataIndex, query: { match_all: {} } });
Expand Down

0 comments on commit 0b5f2d0

Please sign in to comment.