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 server log action for alerting #37530

Merged
merged 3 commits into from
May 31, 2019
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
6 changes: 3 additions & 3 deletions x-pack/plugins/actions/server/action_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ interface Services {
log: (tags: string | string[], data?: string | object | (() => any), timestamp?: number) => void;
}

interface ExecutorOptions {
export interface ExecutorOptions {
actionTypeConfig: Record<string, any>;
params: Record<string, any>;
services: Services;
}

interface ActionType {
export interface ActionType {
id: string;
name: string;
unencryptedAttributes?: string[];
Expand All @@ -32,7 +32,7 @@ interface ConstructorOptions {
services: Services;
}

interface ExecuteOptions {
export interface ExecuteOptions {
id: string;
actionTypeConfig: Record<string, any>;
params: Record<string, any>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ActionTypeRegistry } from '../../action_type_registry';

import { registerBuiltInActionTypes } from '../index';

const ACTION_TYPE_ID = 'kibana.server-log';
const NO_OP_FN = () => {};

const services = {
log: NO_OP_FN,
};

let actionTypeRegistry: ActionTypeRegistry;

beforeAll(() => {
actionTypeRegistry = new ActionTypeRegistry({ services });
registerBuiltInActionTypes(actionTypeRegistry);
});

beforeEach(() => {
services.log = NO_OP_FN;
});

describe('action is registered', () => {
test('gets registered with builtin actions', () => {
expect(actionTypeRegistry.has(ACTION_TYPE_ID)).toEqual(true);
});
});

describe('get()', () => {
test('returns action type', () => {
const actionType = actionTypeRegistry.get(ACTION_TYPE_ID);
expect(actionType.id).toEqual(ACTION_TYPE_ID);
expect(actionType.name).toEqual('server-log');
});
});

describe('validateParams()', () => {
test('should validate and pass when params is valid', () => {
actionTypeRegistry.validateParams(ACTION_TYPE_ID, { message: 'a message' });
actionTypeRegistry.validateParams(ACTION_TYPE_ID, {
message: 'a message',
tags: ['info', 'blorg'],
});
});

test('should validate and throw error when params is invalid', () => {
expect(() => {
actionTypeRegistry.validateParams(ACTION_TYPE_ID, {});
}).toThrowErrorMatchingInlineSnapshot(
`"child \\"message\\" fails because [\\"message\\" is required]"`
);

expect(() => {
actionTypeRegistry.validateParams(ACTION_TYPE_ID, { message: 1 });
}).toThrowErrorMatchingInlineSnapshot(
`"child \\"message\\" fails because [\\"message\\" must be a string]"`
);

expect(() => {
actionTypeRegistry.validateParams(ACTION_TYPE_ID, { message: 'x', tags: 2 });
}).toThrowErrorMatchingInlineSnapshot(
`"child \\"tags\\" fails because [\\"tags\\" must be an array]"`
);

expect(() => {
actionTypeRegistry.validateParams(ACTION_TYPE_ID, { message: 'x', tags: [2] });
}).toThrowErrorMatchingInlineSnapshot(
`"child \\"tags\\" fails because [\\"tags\\" at position 0 fails because [\\"0\\" must be a string]]"`
);
});
});

describe('execute()', () => {
test('calls the executor with proper params', async () => {
const mockLog = jest.fn().mockResolvedValueOnce({ success: true });

services.log = mockLog;
await actionTypeRegistry.execute({
id: ACTION_TYPE_ID,
actionTypeConfig: {},
params: { message: 'message text here', tags: ['tag1', 'tag2'] },
});
expect(mockLog).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Array [
"tag1",
"tag2",
],
"message text here",
],
],
"results": Array [
Object {
"type": "return",
"value": Promise {},
},
],
}
`);
});
});
13 changes: 13 additions & 0 deletions x-pack/plugins/actions/server/builtin_action_types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ActionTypeRegistry } from '../action_type_registry';

import { actionType as serverLogActionType } from './server_log';

export function registerBuiltInActionTypes(actionTypeRegistry: ActionTypeRegistry) {
actionTypeRegistry.register(serverLogActionType);
}
34 changes: 34 additions & 0 deletions x-pack/plugins/actions/server/builtin_action_types/server_log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import Joi from 'joi';

import { ActionType, ExecutorOptions } from '../action_type_registry';

const DEFAULT_TAGS = ['info', 'alerting'];

const PARAMS_SCHEMA = Joi.object().keys({
message: Joi.string().required(),
tags: Joi.array()
.items(Joi.string())
.optional()
.default(DEFAULT_TAGS),
});

export const actionType: ActionType = {
id: 'kibana.server-log',
name: 'server-log',
validate: {
params: PARAMS_SCHEMA,
},
executor,
};

async function executor({ params, services }: ExecutorOptions): Promise<any> {
const { message, tags } = params;

services.log(tags, message);
}
4 changes: 4 additions & 0 deletions x-pack/plugins/actions/server/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
listActionTypesRoute,
} from './routes';

import { registerBuiltInActionTypes } from './builtin_action_types';

export function init(server: Legacy.Server) {
const actionsEnabled = server.config().get('xpack.actions.enabled');

Expand All @@ -38,6 +40,8 @@ export function init(server: Legacy.Server) {
},
});

registerBuiltInActionTypes(actionTypeRegistry);

// Routes
createRoute(server);
deleteRoute(server);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';

import { KibanaFunctionalTestDefaultProviders } from '../../../../types/providers';

// eslint-disable-next-line import/no-default-export
export default function serverLogTest({ getService }: KibanaFunctionalTestDefaultProviders) {
const supertest = getService('supertest');

describe('create server-log action', () => {
it('should return 200 when creating a builtin server-log action', async () => {
await supertest
.post('/api/action')
.set('kbn-xsrf', 'foo')
.send({
attributes: {
description: 'A server.log action',
actionTypeId: 'kibana.server-log',
actionTypeConfig: {},
},
})
.expect(200)
.then((resp: any) => {
expect(resp.body).to.eql({
type: 'action',
id: resp.body.id,
attributes: {
description: 'A server.log action',
actionTypeId: 'kibana.server-log',
actionTypeConfig: {},
},
references: [],
updated_at: resp.body.updated_at,
version: resp.body.version,
});
expect(typeof resp.body.id).to.be('string');
});
});
});
}
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export default function actionsTests({ loadTestFile }: KibanaFunctionalTestDefau
loadTestFile(require.resolve('./get'));
loadTestFile(require.resolve('./list_action_types'));
loadTestFile(require.resolve('./update'));
loadTestFile(require.resolve('./builtin_action_types/server_log'));
});
}